From 7356b727c33ebf2f6923b6c790eb7faa176ced31 Mon Sep 17 00:00:00 2001 From: Nehon Date: Sun, 29 Jun 2014 18:41:32 +0200 Subject: [PATCH] Instancing now works with Lighting material, and shadows are supported with instanced geometries --- .../Common/MatDefs/Light/Lighting.j3md | 25 ++++++++++++++++++- .../Common/MatDefs/Light/Lighting.vert | 22 +++++++++------- .../Common/MatDefs/Misc/Unshaded.j3md | 15 +++++++++++ .../Common/MatDefs/Shadow/PostShadow.j3md | 1 + .../Common/MatDefs/Shadow/PostShadow.vert | 6 ++--- .../Common/MatDefs/Shadow/PostShadow15.vert | 7 +++--- .../Common/MatDefs/Shadow/PreShadow.vert | 6 ++--- .../Common/ShaderLib/Instancing.glsllib | 5 ++++ .../resources/Common/MatDefs/SSAO/normal.vert | 1 + 9 files changed, 66 insertions(+), 22 deletions(-) diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md b/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md index d27adb9f4..bf28391eb 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.j3md @@ -132,6 +132,8 @@ MaterialDef Phong Lighting { // For hardware skinning Int NumberOfBones Matrix4Array BoneMatrices + + Boolean UseInstancing } Technique { @@ -148,6 +150,7 @@ MaterialDef Phong Lighting { ViewMatrix CameraPosition WorldMatrix + ViewProjectionMatrix } Defines { @@ -177,6 +180,8 @@ MaterialDef Phong Lighting { SPHERE_MAP : SphereMap NUM_BONES : NumberOfBones + + INSTANCING : UseInstancing } } @@ -188,12 +193,15 @@ MaterialDef Phong Lighting { WorldParameters { WorldViewProjectionMatrix WorldViewMatrix + ViewProjectionMatrix + ViewMatrix } Defines { COLOR_MAP : ColorMap DISCARD_ALPHA : AlphaDiscardThreshold NUM_BONES : NumberOfBones + INSTANCING : UseInstancing } ForcedRenderState { @@ -214,6 +222,8 @@ MaterialDef Phong Lighting { WorldParameters { WorldViewProjectionMatrix WorldMatrix + ViewProjectionMatrix + ViewMatrix } Defines { @@ -227,6 +237,7 @@ MaterialDef Phong Lighting { PSSM : Splits POINTLIGHT : LightViewProjectionMatrix5 NUM_BONES : NumberOfBones + INSTANCING : UseInstancing } ForcedRenderState { @@ -243,6 +254,8 @@ MaterialDef Phong Lighting { WorldParameters { WorldViewProjectionMatrix WorldMatrix + ViewProjectionMatrix + ViewMatrix } Defines { @@ -256,6 +269,7 @@ MaterialDef Phong Lighting { PSSM : Splits POINTLIGHT : LightViewProjectionMatrix5 NUM_BONES : NumberOfBones + INSTANCING : UseInstancing } ForcedRenderState { @@ -274,11 +288,14 @@ MaterialDef Phong Lighting { WorldViewProjectionMatrix WorldViewMatrix NormalMatrix + ViewProjectionMatrix + ViewMatrix } Defines { DIFFUSEMAP_ALPHA : DiffuseMap NUM_BONES : NumberOfBones + INSTANCING : UseInstancing } } @@ -292,12 +309,15 @@ MaterialDef Phong Lighting { WorldParameters { WorldViewProjectionMatrix WorldViewMatrix - NormalMatrix + NormalMatrix + ViewProjectionMatrix + ViewMatrix } Defines { DIFFUSEMAP_ALPHA : DiffuseMap NUM_BONES : NumberOfBones + INSTANCING : UseInstancing } } @@ -339,6 +359,8 @@ MaterialDef Phong Lighting { WorldParameters { WorldViewProjectionMatrix + ViewProjectionMatrix + ViewMatrix } Defines { @@ -347,6 +369,7 @@ MaterialDef Phong Lighting { HAS_GLOWCOLOR : GlowColor NUM_BONES : NumberOfBones + INSTANCING : UseInstancing } } diff --git a/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.vert b/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.vert index c7cb3e5db..1bd192c94 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.vert +++ b/jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.vert @@ -1,12 +1,16 @@ +#import "Common/ShaderLib/Instancing.glsllib" #define ATTENUATION //#define HQ_ATTENUATION #import "Common/ShaderLib/Skinning.glsllib" -uniform mat4 g_WorldViewProjectionMatrix; -uniform mat4 g_WorldViewMatrix; -uniform mat3 g_NormalMatrix; -uniform mat4 g_ViewMatrix; +/* + uniform mat4 g_WorldViewProjectionMatrix; + uniform mat4 g_WorldViewMatrix; + uniform mat3 g_NormalMatrix; + uniform mat4 g_ViewMatrix; +*/ + uniform vec4 m_Ambient; uniform vec4 m_Diffuse; @@ -148,14 +152,14 @@ void main(){ #endif #endif - gl_Position = g_WorldViewProjectionMatrix * modelSpacePos; + gl_Position = TransformWorldViewProjection(modelSpacePos);// g_WorldViewProjectionMatrix * modelSpacePos; texCoord = inTexCoord; #ifdef SEPARATE_TEXCOORD texCoord2 = inTexCoord2; #endif - vec3 wvPosition = (g_WorldViewMatrix * modelSpacePos).xyz; - vec3 wvNormal = normalize(g_NormalMatrix * modelSpaceNorm); + vec3 wvPosition = TransformWorldView(modelSpacePos).xyz;// (g_WorldViewMatrix * modelSpacePos).xyz; + vec3 wvNormal = normalize(TransformNormal(modelSpaceNorm));//normalize(g_NormalMatrix * modelSpaceNorm); vec3 viewDir = normalize(-wvPosition); //vec4 lightColor = g_LightColor[gl_InstanceID]; @@ -168,7 +172,7 @@ void main(){ vec4 lightColor = g_LightColor; #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING) - vec3 wvTangent = normalize(g_NormalMatrix * modelSpaceTan); + vec3 wvTangent = normalize(TransformNormal(modelSpaceTan)); vec3 wvBinormal = cross(wvNormal, wvTangent); mat3 tbnMat = mat3(wvTangent, wvBinormal * inTangent.w,wvNormal); @@ -187,7 +191,7 @@ void main(){ lightComputeDir(wvPosition, lightColor, wvLightPos, vLightDir); #ifdef V_TANGENT - vNormal = normalize(g_NormalMatrix * inTangent.xyz); + vNormal = normalize(TransformNormal(inTangent.xyz)); vNormal = -cross(cross(vLightDir.xyz, vNormal), vNormal); #endif #endif diff --git a/jme3-core/src/main/resources/Common/MatDefs/Misc/Unshaded.j3md b/jme3-core/src/main/resources/Common/MatDefs/Misc/Unshaded.j3md index 40d64cb7e..aee284705 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Misc/Unshaded.j3md +++ b/jme3-core/src/main/resources/Common/MatDefs/Misc/Unshaded.j3md @@ -87,10 +87,13 @@ MaterialDef Unshaded { WorldViewProjectionMatrix WorldViewMatrix NormalMatrix + ViewProjectionMatrix + ViewMatrix } Defines { NUM_BONES : NumberOfBones + INSTANCING : UseInstancing } } @@ -102,12 +105,15 @@ MaterialDef Unshaded { WorldParameters { WorldViewProjectionMatrix WorldViewMatrix + ViewProjectionMatrix + ViewMatrix } Defines { COLOR_MAP : ColorMap DISCARD_ALPHA : AlphaDiscardThreshold NUM_BONES : NumberOfBones + INSTANCING : UseInstancing } ForcedRenderState { @@ -128,6 +134,8 @@ MaterialDef Unshaded { WorldParameters { WorldViewProjectionMatrix WorldMatrix + ViewProjectionMatrix + ViewMatrix } Defines { @@ -141,6 +149,7 @@ MaterialDef Unshaded { PSSM : Splits POINTLIGHT : LightViewProjectionMatrix5 NUM_BONES : NumberOfBones + INSTANCING : UseInstancing } ForcedRenderState { @@ -157,6 +166,8 @@ MaterialDef Unshaded { WorldParameters { WorldViewProjectionMatrix WorldMatrix + ViewProjectionMatrix + ViewMatrix } Defines { @@ -170,6 +181,7 @@ MaterialDef Unshaded { PSSM : Splits POINTLIGHT : LightViewProjectionMatrix5 NUM_BONES : NumberOfBones + INSTANCING : UseInstancing } ForcedRenderState { @@ -186,6 +198,8 @@ MaterialDef Unshaded { WorldParameters { WorldViewProjectionMatrix + ViewProjectionMatrix + ViewMatrix } Defines { @@ -193,6 +207,7 @@ MaterialDef Unshaded { HAS_GLOWMAP : GlowMap HAS_GLOWCOLOR : GlowColor NUM_BONES : NumberOfBones + INSTANCING : UseInstancing } } } \ No newline at end of file diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.j3md b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.j3md index 1ac99f079..af80bdae3 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.j3md +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.j3md @@ -29,6 +29,7 @@ MaterialDef Post Shadow { Float PCFEdge Float ShadowMapSize + } Technique { diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.vert b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.vert index 262f14fe9..9c2318a9a 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.vert +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.vert @@ -1,12 +1,10 @@ +#import "Common/ShaderLib/Instancing.glsllib" #import "Common/ShaderLib/Skinning.glsllib" uniform mat4 m_LightViewProjectionMatrix0; uniform mat4 m_LightViewProjectionMatrix1; uniform mat4 m_LightViewProjectionMatrix2; uniform mat4 m_LightViewProjectionMatrix3; -uniform mat4 g_WorldViewProjectionMatrix; -uniform mat4 g_WorldMatrix; -uniform mat4 g_ViewMatrix; uniform vec3 m_LightPos; varying vec4 projCoord0; @@ -52,7 +50,7 @@ void main(){ #ifdef NUM_BONES Skinning_Compute(modelSpacePos); #endif - gl_Position = g_WorldViewProjectionMatrix * modelSpacePos; + gl_Position = TransformWorldViewProjection(modelSpacePos); #ifndef POINTLIGHT #ifdef PSSM diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow15.vert b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow15.vert index c473cafaa..034e5bef6 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow15.vert +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow15.vert @@ -1,11 +1,10 @@ +#import "Common/ShaderLib/Instancing.glsllib" #import "Common/ShaderLib/Skinning.glsllib" uniform mat4 m_LightViewProjectionMatrix0; uniform mat4 m_LightViewProjectionMatrix1; uniform mat4 m_LightViewProjectionMatrix2; uniform mat4 m_LightViewProjectionMatrix3; -uniform mat4 g_WorldViewProjectionMatrix; -uniform mat4 g_WorldMatrix; out vec4 projCoord0; out vec4 projCoord1; @@ -51,7 +50,7 @@ void main(){ #ifdef NUM_BONES Skinning_Compute(modelSpacePos); #endif - gl_Position = g_WorldViewProjectionMatrix * modelSpacePos; + gl_Position = TransformWorldViewProjection(modelSpacePos); #ifndef POINTLIGHT #ifdef PSSM @@ -60,7 +59,7 @@ void main(){ vec4 worldPos=vec4(0.0); #endif // get the vertex in world space - worldPos = g_WorldMatrix * modelSpacePos; + worldPos = TransformWorld(modelSpacePos); #ifdef DISCARD_ALPHA texCoord = inTexCoord; diff --git a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PreShadow.vert b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PreShadow.vert index e57d45141..9bb5d3a87 100644 --- a/jme3-core/src/main/resources/Common/MatDefs/Shadow/PreShadow.vert +++ b/jme3-core/src/main/resources/Common/MatDefs/Shadow/PreShadow.vert @@ -1,10 +1,8 @@ +#import "Common/ShaderLib/Instancing.glsllib" #import "Common/ShaderLib/Skinning.glsllib" attribute vec3 inPosition; attribute vec2 inTexCoord; -uniform mat4 g_WorldViewProjectionMatrix; -uniform mat4 g_WorldViewMatrix; - varying vec2 texCoord; void main(){ @@ -13,6 +11,6 @@ void main(){ #ifdef NUM_BONES Skinning_Compute(modelSpacePos); #endif - gl_Position = g_WorldViewProjectionMatrix * modelSpacePos; + gl_Position = TransformWorldViewProjection(modelSpacePos); texCoord = inTexCoord; } \ No newline at end of file diff --git a/jme3-core/src/main/resources/Common/ShaderLib/Instancing.glsllib b/jme3-core/src/main/resources/Common/ShaderLib/Instancing.glsllib index ae34935c5..7ed91181d 100644 --- a/jme3-core/src/main/resources/Common/ShaderLib/Instancing.glsllib +++ b/jme3-core/src/main/resources/Common/ShaderLib/Instancing.glsllib @@ -78,6 +78,11 @@ vec3 TransformNormal(vec3 vec) #else +vec4 TransformWorld(vec4 position) +{ + return g_WorldMatrix * position; +} + vec4 TransformWorldView(vec4 position) { return g_WorldViewMatrix * position; diff --git a/jme3-effects/src/main/resources/Common/MatDefs/SSAO/normal.vert b/jme3-effects/src/main/resources/Common/MatDefs/SSAO/normal.vert index 7eb36c40c..0d812a8cb 100644 --- a/jme3-effects/src/main/resources/Common/MatDefs/SSAO/normal.vert +++ b/jme3-effects/src/main/resources/Common/MatDefs/SSAO/normal.vert @@ -1,3 +1,4 @@ +#import "Common/ShaderLib/Instancing.glsllib" #import "Common/ShaderLib/Skinning.glsllib" uniform mat4 g_WorldViewProjectionMatrix; uniform mat3 g_NormalMatrix;