Forráskód Böngészése

Combining baked and dynamic shadows nicely (lightmap)

https://www.bitofgold.com/lightmaptest/

A little demonstration to show what is wrong with the previous ligthmap
calculation - no ambient lighting, no other light, no specular
highlights.

Does not break the previous lightmap calculation.

material.lightmapTexture needs to be set to activate light map.

light.lightmapExcluded
if true, gets de diffuse lighting from the light map. adds specular
highlights mixed widh baked and dynamic shadows.

if false, lightmap calculation is not affected. ("Old" lightmap,
material.useLightmapAsShadowmap works as before (simply multiplying
color.rgb after composition). Does not look right.)

light.lightmapNoSpecular
if true, does not even calculate the light functions, only the baked
lightmap + dynamic shadow. (no specular then from this light, only the
baked diffuse)
László Matuska 9 éve
szülő
commit
8eea1e484f

+ 6 - 0
src/Lights/babylon.light.ts

@@ -42,6 +42,12 @@
         @serialize()
         public excludeWithLayerMask = 0;
 
+        @serialize()
+        public lightmapExcluded = false;
+
+        @serialize()
+        public lightmapNoSpecular = false;
+
         // PBR Properties.
         @serialize()
         public radius = 0.00001;

+ 24 - 0
src/Materials/babylon.materialHelper.ts

@@ -5,6 +5,7 @@
             var needNormals = false;
             var needRebuild = false;
             var needShadows = false;
+            var lightmapExcluded = false;
 
             for (var index = 0; index < scene.lights.length; index++) {
                 var light = scene.lights[index];
@@ -103,6 +104,22 @@
                     }
                 }
 
+                //lightmapExcluded
+                if (defines["LIGHTMAPEXCLUDED" + lightIndex] === undefined) {
+                    needRebuild = true;
+                }
+                defines["LIGHTMAPEXCLUDED" + lightIndex] = light.lightmapExcluded;
+                if (light.lightmapExcluded) {
+                    lightmapExcluded = true;
+                }
+
+                //lightmapNoSpecular
+                if (defines["LIGHTMAPNOSPECULAR" + lightIndex] === undefined) {
+                    needRebuild = true;
+                }
+                defines["LIGHTMAPNOSPECULAR" + lightIndex] = light.lightmapNoSpecular;
+
+
                 lightIndex++;
                 if (lightIndex === maxSimultaneousLights)
                     break;
@@ -117,6 +134,13 @@
                 defines["SHADOWFULLFLOAT"] = true;
             }
 
+            if (defines["LIGHTMAPEXCLUDED"] === undefined) {
+                needRebuild = true;
+            }
+            if (lightmapExcluded) {
+                defines["LIGHTMAPEXCLUDED"] = true;
+            }
+
             if (needRebuild) {
                 defines.rebuild();
             }

+ 28 - 15
src/Shaders/ShadersInclude/lightFragment.fx

@@ -1,16 +1,20 @@
 #ifdef LIGHT{X}
-	#ifndef SPECULARTERM
-		vec3 vLightSpecular{X} = vec3(0.);
-	#endif
-	#ifdef SPOTLIGHT{X}
-		info = computeSpotLighting(viewDirectionW, normalW, vLightData{X}, vLightDirection{X}, vLightDiffuse{X}.rgb, vLightSpecular{X}, vLightDiffuse{X}.a, glossiness);
-	#endif
-	#ifdef HEMILIGHT{X}
-		info = computeHemisphericLighting(viewDirectionW, normalW, vLightData{X}, vLightDiffuse{X}.rgb, vLightSpecular{X}, vLightGround{X}, glossiness);
-	#endif
-	#if defined(POINTLIGHT{X}) || defined(DIRLIGHT{X})
-		info = computeLighting(viewDirectionW, normalW, vLightData{X}, vLightDiffuse{X}.rgb, vLightSpecular{X}, vLightDiffuse{X}.a, glossiness);
-	#endif
+    #if defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED{X}) && defined(LIGHTMAPNOSPECULAR{X})
+        //No light calculation
+    #else
+        #ifndef SPECULARTERM
+            vec3 vLightSpecular{X} = vec3(0.);
+        #endif
+        #ifdef SPOTLIGHT{X}
+            info = computeSpotLighting(viewDirectionW, normalW, vLightData{X}, vLightDirection{X}, vLightDiffuse{X}.rgb, vLightSpecular{X}, vLightDiffuse{X}.a, glossiness);
+        #endif
+        #ifdef HEMILIGHT{X}
+            info = computeHemisphericLighting(viewDirectionW, normalW, vLightData{X}, vLightDiffuse{X}.rgb, vLightSpecular{X}, vLightGround{X}, glossiness);
+        #endif
+        #if defined(POINTLIGHT{X}) || defined(DIRLIGHT{X})
+            info = computeLighting(viewDirectionW, normalW, vLightData{X}, vLightDiffuse{X}.rgb, vLightSpecular{X}, vLightDiffuse{X}.a, glossiness);
+        #endif
+    #endif
 	#ifdef SHADOW{X}
 		#ifdef SHADOWVSM{X}
 			shadow = computeShadowWithVSM(vPositionFromLight{X}, shadowSampler{X}, shadowsInfo{X}.z, shadowsInfo{X}.x);
@@ -32,8 +36,17 @@
 	#else
 		shadow = 1.;
 	#endif
-		diffuseBase += info.diffuse * shadow;
-	#ifdef SPECULARTERM
-		specularBase += info.specular * shadow;
+    #if defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED{X})
+	    diffuseBase += lightmapColor * shadow;
+	    #ifdef SPECULARTERM
+            #ifndef LIGHTMAPNOSPECULAR{X}
+                specularBase += info.specular * shadow * lightmapColor;
+            #endif
+        #endif
+    #else
+	    diffuseBase += info.diffuse * shadow;
+	    #ifdef SPECULARTERM
+		    specularBase += info.specular * shadow;
+	    #endif
 	#endif
 #endif

+ 12 - 7
src/Shaders/default.fragment.fx

@@ -232,6 +232,10 @@ void main(void) {
 #endif
 	float shadow = 1.;
 
+#ifdef LIGHTMAP
+	vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
+#endif
+
 #include<lightFragment>[0..maxSimultaneousLights]
 
 	// Refraction
@@ -387,14 +391,15 @@ void main(void) {
 	vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + refractionColor, alpha);
 #endif
 
+//Old lightmap calculation method
 #ifdef LIGHTMAP
-	vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
-
-#ifdef USELIGHTMAPASSHADOWMAP
-	color.rgb *= lightmapColor;
-#else
-	color.rgb += lightmapColor;
-#endif
+    #ifndef LIGHTMAPEXCLUDED
+        #ifdef USELIGHTMAPASSHADOWMAP
+            color.rgb *= lightmapColor;
+        #else
+            color.rgb += lightmapColor;
+        #endif
+    #endif
 #endif
 
 #include<logDepthFragment>