Added ambient light support in PBR shader (#1077)

* Added ambient light support in PBR shader

* Updated to auto inject the define value if ambient light found
accellbaker
Ali-RS 6 years ago committed by GitHub
parent 40be1b42b8
commit 36ddb5b0ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      jme3-core/src/main/java/com/jme3/material/logic/SinglePassAndImageBasedLightingLogic.java
  2. 17
      jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.frag
  3. 6
      jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.j3md

@ -48,8 +48,10 @@ public final class SinglePassAndImageBasedLightingLogic extends DefaultTechnique
private static final String DEFINE_SINGLE_PASS_LIGHTING = "SINGLE_PASS_LIGHTING"; private static final String DEFINE_SINGLE_PASS_LIGHTING = "SINGLE_PASS_LIGHTING";
private static final String DEFINE_NB_LIGHTS = "NB_LIGHTS"; private static final String DEFINE_NB_LIGHTS = "NB_LIGHTS";
private static final String DEFINE_NB_PROBES = "NB_PROBES"; private static final String DEFINE_NB_PROBES = "NB_PROBES";
private static final String DEFINE_USE_AMBIENT_LIGHT = "USE_AMBIENT_LIGHT";
private static final RenderState ADDITIVE_LIGHT = new RenderState(); private static final RenderState ADDITIVE_LIGHT = new RenderState();
private boolean useAmbientLight;
private final ColorRGBA ambientLightColor = new ColorRGBA(0, 0, 0, 1); private final ColorRGBA ambientLightColor = new ColorRGBA(0, 0, 0, 1);
private List<LightProbe> lightProbes = new ArrayList<>(3); private List<LightProbe> lightProbes = new ArrayList<>(3);
@ -61,12 +63,14 @@ public final class SinglePassAndImageBasedLightingLogic extends DefaultTechnique
private final int singlePassLightingDefineId; private final int singlePassLightingDefineId;
private final int nbLightsDefineId; private final int nbLightsDefineId;
private final int nbProbesDefineId; private final int nbProbesDefineId;
private final int useAmbientLightDefineId;
public SinglePassAndImageBasedLightingLogic(TechniqueDef techniqueDef) { public SinglePassAndImageBasedLightingLogic(TechniqueDef techniqueDef) {
super(techniqueDef); super(techniqueDef);
singlePassLightingDefineId = techniqueDef.addShaderUnmappedDefine(DEFINE_SINGLE_PASS_LIGHTING, VarType.Boolean); singlePassLightingDefineId = techniqueDef.addShaderUnmappedDefine(DEFINE_SINGLE_PASS_LIGHTING, VarType.Boolean);
nbLightsDefineId = techniqueDef.addShaderUnmappedDefine(DEFINE_NB_LIGHTS, VarType.Int); nbLightsDefineId = techniqueDef.addShaderUnmappedDefine(DEFINE_NB_LIGHTS, VarType.Int);
nbProbesDefineId = techniqueDef.addShaderUnmappedDefine(DEFINE_NB_PROBES, VarType.Int); nbProbesDefineId = techniqueDef.addShaderUnmappedDefine(DEFINE_NB_PROBES, VarType.Int);
useAmbientLightDefineId = techniqueDef.addShaderUnmappedDefine(DEFINE_USE_AMBIENT_LIGHT, VarType.Boolean);
} }
@Override @Override
@ -83,6 +87,7 @@ public final class SinglePassAndImageBasedLightingLogic extends DefaultTechnique
lightProbes.clear(); lightProbes.clear();
extractIndirectLights(lights, false); extractIndirectLights(lights, false);
defines.set(nbProbesDefineId, lightProbes.size()); defines.set(nbProbesDefineId, lightProbes.size());
defines.set(useAmbientLightDefineId, useAmbientLight);
} }
return super.makeCurrent(assetManager, renderManager, rendererCaps, lights, defines); return super.makeCurrent(assetManager, renderManager, rendererCaps, lights, defines);
@ -127,7 +132,7 @@ public final class SinglePassAndImageBasedLightingLogic extends DefaultTechnique
// apply additive blending for 2nd and future passes // apply additive blending for 2nd and future passes
rm.getRenderer().applyRenderState(ADDITIVE_LIGHT); rm.getRenderer().applyRenderState(ADDITIVE_LIGHT);
ambientColor.setValue(VarType.Vector4, ColorRGBA.Black); ambientColor.setValue(VarType.Vector4, ColorRGBA.Black);
}else{ } else{
extractIndirectLights(lightList,true); extractIndirectLights(lightList,true);
ambientColor.setValue(VarType.Vector4, ambientLightColor); ambientColor.setValue(VarType.Vector4, ambientLightColor);
} }
@ -260,9 +265,11 @@ public final class SinglePassAndImageBasedLightingLogic extends DefaultTechnique
protected void extractIndirectLights(LightList lightList, boolean removeLights) { protected void extractIndirectLights(LightList lightList, boolean removeLights) {
ambientLightColor.set(0, 0, 0, 1); ambientLightColor.set(0, 0, 0, 1);
useAmbientLight = false;
for (int j = 0; j < lightList.size(); j++) { for (int j = 0; j < lightList.size(); j++) {
Light l = lightList.get(j); Light l = lightList.get(j);
if (l instanceof AmbientLight) { if (l instanceof AmbientLight) {
useAmbientLight = true;
ambientLightColor.addLocal(l.getColor()); ambientLightColor.addLocal(l.getColor());
if(removeLights){ if(removeLights){
lightList.remove(l); lightList.remove(l);

@ -12,6 +12,7 @@ varying vec4 Color;
uniform vec4 g_LightData[NB_LIGHTS]; uniform vec4 g_LightData[NB_LIGHTS];
uniform vec3 g_CameraPosition; uniform vec3 g_CameraPosition;
uniform vec4 g_AmbientLightColor;
uniform float m_Roughness; uniform float m_Roughness;
uniform float m_Metallic; uniform float m_Metallic;
@ -40,7 +41,7 @@ varying vec3 wPosition;
#endif #endif
#ifdef USE_PACKED_MR #ifdef USE_PACKED_MR
uniform sampler2D m_MetallicRoughnessMap; uniform sampler2D m_MetallicRoughnessMap;
#else #else
#ifdef METALLICMAP #ifdef METALLICMAP
uniform sampler2D m_MetallicMap; uniform sampler2D m_MetallicMap;
@ -51,10 +52,10 @@ varying vec3 wPosition;
#endif #endif
#ifdef EMISSIVE #ifdef EMISSIVE
uniform vec4 m_Emissive; uniform vec4 m_Emissive;
#endif #endif
#ifdef EMISSIVEMAP #ifdef EMISSIVEMAP
uniform sampler2D m_EmissiveMap; uniform sampler2D m_EmissiveMap;
#endif #endif
#if defined(EMISSIVE) || defined(EMISSIVEMAP) #if defined(EMISSIVE) || defined(EMISSIVEMAP)
uniform float m_EmissivePower; uniform float m_EmissivePower;
@ -91,7 +92,7 @@ varying vec3 wPosition;
varying vec3 wNormal; varying vec3 wNormal;
#ifdef DISCARD_ALPHA #ifdef DISCARD_ALPHA
uniform float m_AlphaDiscardThreshold; uniform float m_AlphaDiscardThreshold;
#endif #endif
void main(){ void main(){
@ -273,7 +274,7 @@ void main(){
float ndf3 = renderProbe(viewDir, wPosition, normal, norm, Roughness, diffuseColor, specularColor, ndotv, ao, g_LightProbeData3, g_ShCoeffs3, g_PrefEnvMap3, color3); float ndf3 = renderProbe(viewDir, wPosition, normal, norm, Roughness, diffuseColor, specularColor, ndotv, ao, g_LightProbeData3, g_ShCoeffs3, g_PrefEnvMap3, color3);
#endif #endif
#if NB_PROBES >= 2 #if NB_PROBES >= 2
float invNdf = max(1.0 - ndf,0.0); float invNdf = max(1.0 - ndf,0.0);
float invNdf2 = max(1.0 - ndf2,0.0); float invNdf2 = max(1.0 - ndf2,0.0);
float sumNdf = ndf + ndf2; float sumNdf = ndf + ndf2;
@ -294,6 +295,12 @@ void main(){
weight2 /= weightSum; weight2 /= weightSum;
weight3 /= weightSum; weight3 /= weightSum;
#endif #endif
#if USE_AMBIENT_LIGHT
color1.rgb *= g_AmbientLightColor.rgb;
color2.rgb *= g_AmbientLightColor.rgb;
color3.rgb *= g_AmbientLightColor.rgb;
#endif
gl_FragColor.rgb += color1 * clamp(weight1,0.0,1.0) + color2 * clamp(weight2,0.0,1.0) + color3 * clamp(weight3,0.0,1.0); gl_FragColor.rgb += color1 * clamp(weight1,0.0,1.0) + color2 * clamp(weight2,0.0,1.0) + color3 * clamp(weight3,0.0,1.0);
#endif #endif

@ -53,7 +53,7 @@ MaterialDef PBR Lighting {
// Parallax/height map // Parallax/height map
Texture2D ParallaxMap -LINEAR Texture2D ParallaxMap -LINEAR
//Set to true is parallax map is stored in the alpha channel of the normal map //Set to true if parallax map is stored in the alpha channel of the normal map
Boolean PackedNormalParallax Boolean PackedNormalParallax
//Sets the relief height for parallax mapping //Sets the relief height for parallax mapping
@ -111,10 +111,10 @@ MaterialDef PBR Lighting {
Int NumberOfMorphTargets Int NumberOfMorphTargets
Int NumberOfTargetsBuffers Int NumberOfTargetsBuffers
//For instancing // For instancing
Boolean UseInstancing Boolean UseInstancing
//For Vertex Color // For Vertex Color
Boolean UseVertexColor Boolean UseVertexColor
Boolean BackfaceShadows : false Boolean BackfaceShadows : false

Loading…
Cancel
Save