add point light mode for pre shadow techniques

in-pass-shadows
Kirill Vainer 7 years ago
parent fe158e7b31
commit e4536808ca
  1. 8
      jme3-core/src/main/java/com/jme3/shadow/next/PreShadowArrayRenderer.java
  2. 7
      jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md
  3. 7
      jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md
  4. 7
      jme3-core/src/main/resources/Common/MatDefs/Misc/Particle.j3md
  5. 7
      jme3-core/src/main/resources/Common/MatDefs/Misc/Unshaded.j3md
  6. 13
      jme3-core/src/main/resources/Common/MatDefs/Shadow/PreShadow.j3md
  7. 32
      jme3-core/src/main/resources/Common/MatDefs/Shadow/PreShadow.vert

@ -34,8 +34,10 @@ package com.jme3.shadow.next;
import com.jme3.shadow.next.pssm.DirectionalShadowParameters; import com.jme3.shadow.next.pssm.DirectionalShadowParameters;
import com.jme3.light.DirectionalLight; import com.jme3.light.DirectionalLight;
import com.jme3.light.Light; import com.jme3.light.Light;
import com.jme3.light.Light.Type;
import com.jme3.light.PointLight; import com.jme3.light.PointLight;
import com.jme3.light.SpotLight; import com.jme3.light.SpotLight;
import com.jme3.material.MatParamOverride;
import com.jme3.material.RenderState; import com.jme3.material.RenderState;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
import com.jme3.post.SceneProcessor; import com.jme3.post.SceneProcessor;
@ -46,6 +48,7 @@ import com.jme3.renderer.ViewPort;
import com.jme3.renderer.queue.GeometryList; import com.jme3.renderer.queue.GeometryList;
import com.jme3.renderer.queue.OpaqueComparator; import com.jme3.renderer.queue.OpaqueComparator;
import com.jme3.renderer.queue.RenderQueue; import com.jme3.renderer.queue.RenderQueue;
import com.jme3.shader.VarType;
import com.jme3.shadow.next.array.DirectionalArrayShadowMap; import com.jme3.shadow.next.array.DirectionalArrayShadowMap;
import com.jme3.shadow.next.array.PointArrayShadowMap; import com.jme3.shadow.next.array.PointArrayShadowMap;
import com.jme3.shadow.next.array.SpotArrayShadowMap; import com.jme3.shadow.next.array.SpotArrayShadowMap;
@ -80,6 +83,7 @@ public class PreShadowArrayRenderer implements SceneProcessor {
private final GeometryList shadowCasters = new GeometryList(new OpaqueComparator()); private final GeometryList shadowCasters = new GeometryList(new OpaqueComparator());
private final ListMap<Light, ShadowMap> shadowedLights = new ListMap<>(); private final ListMap<Light, ShadowMap> shadowedLights = new ListMap<>();
private final RenderState prePassRenderState = RenderState.ADDITIONAL.clone(); private final RenderState prePassRenderState = RenderState.ADDITIONAL.clone();
private final MatParamOverride pointLightOverride = new MatParamOverride(VarType.Boolean, "IsPointLight", true);
private final TextureArray array = new TextureArray(); private final TextureArray array = new TextureArray();
private int textureSize = 1024; private int textureSize = 1024;
@ -190,6 +194,7 @@ public class PreShadowArrayRenderer implements SceneProcessor {
private void renderShadowMaps(ViewPort viewPort) { private void renderShadowMaps(ViewPort viewPort) {
renderManager.setForcedRenderState(prePassRenderState); renderManager.setForcedRenderState(prePassRenderState);
renderManager.setForcedTechnique(PRE_SHADOW_TECHNIQUE_NAME); renderManager.setForcedTechnique(PRE_SHADOW_TECHNIQUE_NAME);
renderManager.addForcedMatParam(pointLightOverride);
for (int i = 0; i < shadowedLights.size(); i++) { for (int i = 0; i < shadowedLights.size(); i++) {
Light light = shadowedLights.getKey(i); Light light = shadowedLights.getKey(i);
@ -206,6 +211,8 @@ public class PreShadowArrayRenderer implements SceneProcessor {
vars.release(); vars.release();
} }
pointLightOverride.setEnabled(shadowMap.getLightType() == Type.Point);
switch (shadowMap.getLightType()) { switch (shadowMap.getLightType()) {
case Directional: case Directional:
DirectionalArrayShadowMap directionalShadow = (DirectionalArrayShadowMap) shadowMap; DirectionalArrayShadowMap directionalShadow = (DirectionalArrayShadowMap) shadowMap;
@ -228,6 +235,7 @@ public class PreShadowArrayRenderer implements SceneProcessor {
Renderer renderer = renderManager.getRenderer(); Renderer renderer = renderManager.getRenderer();
renderer.setFrameBuffer(viewPort.getOutputFrameBuffer()); renderer.setFrameBuffer(viewPort.getOutputFrameBuffer());
renderManager.removeForcedMatParam(pointLightOverride);
renderManager.setForcedRenderState(null); renderManager.setForcedRenderState(null);
renderManager.setForcedTechnique(null); renderManager.setForcedTechnique(null);
renderManager.setCamera(viewPort.getCamera(), false); renderManager.setCamera(viewPort.getCamera(), false);

@ -118,6 +118,9 @@ MaterialDef Phong Lighting {
Boolean UseInstancing Boolean UseInstancing
Boolean BackfaceShadows : false Boolean BackfaceShadows : false
// PreShadow: use point light mode for depth
Boolean IsPointLight
} }
Technique { Technique {
@ -228,12 +231,16 @@ MaterialDef Phong Lighting {
WorldViewMatrix WorldViewMatrix
ViewProjectionMatrix ViewProjectionMatrix
ViewMatrix ViewMatrix
WorldMatrix
CameraPosition
FrustumNearFar
} }
Defines { Defines {
DISCARD_ALPHA : AlphaDiscardThreshold DISCARD_ALPHA : AlphaDiscardThreshold
NUM_BONES : NumberOfBones NUM_BONES : NumberOfBones
INSTANCING : UseInstancing INSTANCING : UseInstancing
POINT_LIGHT : IsPointLight
} }
ForcedRenderState { ForcedRenderState {

@ -118,6 +118,9 @@ MaterialDef PBR Lighting {
Boolean UseVertexColor Boolean UseVertexColor
Boolean BackfaceShadows : false Boolean BackfaceShadows : false
// PreShadow: use point light mode for depth
Boolean IsPointLight
} }
Technique { Technique {
@ -171,12 +174,16 @@ MaterialDef PBR Lighting {
WorldViewMatrix WorldViewMatrix
ViewProjectionMatrix ViewProjectionMatrix
ViewMatrix ViewMatrix
WorldMatrix
CameraPosition
FrustumNearFar
} }
Defines { Defines {
DISCARD_ALPHA : AlphaDiscardThreshold DISCARD_ALPHA : AlphaDiscardThreshold
NUM_BONES : NumberOfBones NUM_BONES : NumberOfBones
INSTANCING : UseInstancing INSTANCING : UseInstancing
POINT_LIGHT : IsPointLight
} }
ForcedRenderState { ForcedRenderState {

@ -14,6 +14,9 @@ MaterialDef Point Sprite {
Texture2D GlowMap Texture2D GlowMap
// The glow color of the object // The glow color of the object
Color GlowColor Color GlowColor
// PreShadow: use point light mode for depth
Boolean IsPointLight
} }
Technique { Technique {
@ -56,10 +59,14 @@ MaterialDef Point Sprite {
WorldViewMatrix WorldViewMatrix
ViewProjectionMatrix ViewProjectionMatrix
ViewMatrix ViewMatrix
WorldMatrix
CameraPosition
FrustumNearFar
} }
Defines { Defines {
COLOR_MAP : Texture COLOR_MAP : Texture
POINT_LIGHT : IsPointLight
} }
ForcedRenderState { ForcedRenderState {

@ -54,6 +54,9 @@ MaterialDef Unshaded {
Float ShadowMapSize Float ShadowMapSize
Boolean BackfaceShadows: true Boolean BackfaceShadows: true
// PreShadow: use point light mode for depth
Boolean IsPointLight
} }
Technique { Technique {
@ -108,6 +111,9 @@ MaterialDef Unshaded {
WorldViewMatrix WorldViewMatrix
ViewProjectionMatrix ViewProjectionMatrix
ViewMatrix ViewMatrix
WorldMatrix
CameraPosition
FrustumNearFar
} }
Defines { Defines {
@ -115,6 +121,7 @@ MaterialDef Unshaded {
DISCARD_ALPHA : AlphaDiscardThreshold DISCARD_ALPHA : AlphaDiscardThreshold
NUM_BONES : NumberOfBones NUM_BONES : NumberOfBones
INSTANCING : UseInstancing INSTANCING : UseInstancing
POINT_LIGHT : IsPointLight
} }
ForcedRenderState { ForcedRenderState {

@ -1,11 +1,24 @@
MaterialDef Pre Shadow { MaterialDef Pre Shadow {
MaterialParameters {
// PreShadow: use point light mode for depth
Boolean IsPointLight
}
Technique { Technique {
VertexShader GLSL100 GLSL150 : Common/MatDefs/Shadow/PreShadow.vert VertexShader GLSL100 GLSL150 : Common/MatDefs/Shadow/PreShadow.vert
FragmentShader GLSL100 GLSL150 : Common/MatDefs/Shadow/PreShadow.frag FragmentShader GLSL100 GLSL150 : Common/MatDefs/Shadow/PreShadow.frag
Defines {
POINT_LIGHT : IsPointLight
}
WorldParameters { WorldParameters {
WorldViewProjectionMatrix WorldViewProjectionMatrix
WorldViewMatrix WorldViewMatrix
WorldMatrix
CameraPosition
FrustumNearFar
} }
RenderState { RenderState {

@ -2,16 +2,36 @@
#import "Common/ShaderLib/Instancing.glsllib" #import "Common/ShaderLib/Instancing.glsllib"
#import "Common/ShaderLib/Skinning.glsllib" #import "Common/ShaderLib/Skinning.glsllib"
attribute vec3 inPosition; attribute vec3 inPosition;
#ifdef DISCARD_ALPHA
attribute vec2 inTexCoord; attribute vec2 inTexCoord;
varying vec2 texCoord; varying vec2 texCoord;
#endif
#ifdef POINT_LIGHT
uniform vec3 g_CameraPosition;
uniform vec2 g_FrustumNearFar;
#endif
void main(){ void main() {
vec4 modelSpacePos = vec4(inPosition, 1.0); vec4 modelSpacePos = vec4(inPosition, 1.0);
#ifdef NUM_BONES #ifdef NUM_BONES
Skinning_Compute(modelSpacePos); Skinning_Compute(modelSpacePos);
#endif #endif
#ifdef DISCARD_ALPHA
texCoord = inTexCoord;
#endif
gl_Position = TransformWorldViewProjection(modelSpacePos); gl_Position = TransformWorldViewProjection(modelSpacePos);
texCoord = inTexCoord;
#ifdef POINT_LIGHT
vec3 lightDir = g_CameraPosition - TransformWorld(modelSpacePos).xyz;
// The Z value to write into the depth map, should be [0.0, 1.0]
float z = length(lightDir) / g_FrustumNearFar.y;
// Remap [0.0, 1.0] into [-1.0, 1.0]
gl_Position.z = (clamp(z, 0.0, 1.0) * 2.0 - 1.0) * gl_Position.w;
#endif
} }
Loading…
Cancel
Save