Bläddra i källkod

Add a shadowOnly property (#8384)

Popov72 5 år sedan
förälder
incheckning
a3e075faaf

+ 1 - 0
dist/preview release/what's new.md

@@ -116,6 +116,7 @@
 - Added to `StandardMaterial` RGBD ReflectionTexture, RefractionTexture and LightmapTexture support. ([MackeyK24](https://github.com/MackeyK24))
 - Allow using the single comment syntax `// comment` in a `#if` construct in shader code ([Popov72](https://github.com/Popov72))
 - Added the possibility to update the shader code before being compiled ([Popov72](https://github.com/Popov72))
+- Added the `shadowOnly` property to the `BackgroundMaterial` class ([Popov72](https://github.com/Popov72))
 
 ### WebXR
 

+ 15 - 1
src/Materials/Background/backgroundMaterial.ts

@@ -95,6 +95,11 @@ class BackgroundMaterialDefines extends MaterialDefines implements IImageProcess
     public USEHIGHLIGHTANDSHADOWCOLORS = false;
 
     /**
+     * True if only shadows must be rendered
+     */
+    public BACKMAT_SHADOWONLY = false;
+
+    /**
      * True to add noise in order to reduce the banding effect.
      */
     public NOISE = false;
@@ -412,6 +417,14 @@ export class BackgroundMaterial extends PushMaterial {
     @expandToProperty("_markAllSubMeshesAsTexturesDirty")
     public maxSimultaneousLights: int = 4;
 
+    @serialize()
+    private _shadowOnly: boolean = false;
+    /**
+     * Make the material only render shadows
+     */
+    @expandToProperty("_markAllSubMeshesAsLightsDirty")
+    public shadowOnly: boolean = false;
+
     /**
      * Default configuration related to image processing available in the Background Material.
      */
@@ -642,7 +655,7 @@ export class BackgroundMaterial extends PushMaterial {
      * @returns true if blending is enable
      */
     public needAlphaBlending(): boolean {
-        return ((this.alpha < 0) || (this._diffuseTexture != null && this._diffuseTexture.hasAlpha));
+        return (this.alpha < 1) || (this._diffuseTexture != null && this._diffuseTexture.hasAlpha) || this._shadowOnly;
     }
 
     /**
@@ -799,6 +812,7 @@ export class BackgroundMaterial extends PushMaterial {
 
         if (defines._areLightsDirty) {
             defines.USEHIGHLIGHTANDSHADOWCOLORS = !this._useRGBColor && (this._primaryColorShadowLevel !== 0 || this._primaryColorHighlightLevel !== 0);
+            defines.BACKMAT_SHADOWONLY = this._shadowOnly;
         }
 
         if (defines._areImageProcessingDirty && this._imageProcessingConfiguration) {

+ 120 - 115
src/Shaders/background.fragment.fx

@@ -136,149 +136,154 @@ void main(void) {
     globalShadow = 1.0;
 #endif
 
-// _____________________________ REFLECTION ______________________________________
-vec4 reflectionColor = vec4(1., 1., 1., 1.);
-#ifdef REFLECTION
-    vec3 reflectionVector = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
-    #ifdef REFLECTIONMAP_OPPOSITEZ
-        reflectionVector.z *= -1.0;
-    #endif
-
-    // _____________________________ 2D vs 3D Maps ________________________________
-    #ifdef REFLECTIONMAP_3D
-        vec3 reflectionCoords = reflectionVector;
-    #else
-        vec2 reflectionCoords = reflectionVector.xy;
-        #ifdef REFLECTIONMAP_PROJECTION
-            reflectionCoords /= reflectionVector.z;
+#ifndef BACKMAT_SHADOWONLY
+    // _____________________________ REFLECTION ______________________________________
+    vec4 reflectionColor = vec4(1., 1., 1., 1.);
+    #ifdef REFLECTION
+        vec3 reflectionVector = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
+        #ifdef REFLECTIONMAP_OPPOSITEZ
+            reflectionVector.z *= -1.0;
         #endif
-        reflectionCoords.y = 1.0 - reflectionCoords.y;
-    #endif
 
-    #ifdef REFLECTIONBLUR
-        float reflectionLOD = vReflectionInfos.y;
+        // _____________________________ 2D vs 3D Maps ________________________________
+        #ifdef REFLECTIONMAP_3D
+            vec3 reflectionCoords = reflectionVector;
+        #else
+            vec2 reflectionCoords = reflectionVector.xy;
+            #ifdef REFLECTIONMAP_PROJECTION
+                reflectionCoords /= reflectionVector.z;
+            #endif
+            reflectionCoords.y = 1.0 - reflectionCoords.y;
+        #endif
 
-        #ifdef TEXTURELODSUPPORT
-            // Apply environment convolution scale/offset filter tuning parameters to the mipmap LOD selection
-            reflectionLOD = reflectionLOD * log2(vReflectionMicrosurfaceInfos.x) * vReflectionMicrosurfaceInfos.y + vReflectionMicrosurfaceInfos.z;
-            reflectionColor = sampleReflectionLod(reflectionSampler, reflectionCoords, reflectionLOD);
+        #ifdef REFLECTIONBLUR
+            float reflectionLOD = vReflectionInfos.y;
+
+            #ifdef TEXTURELODSUPPORT
+                // Apply environment convolution scale/offset filter tuning parameters to the mipmap LOD selection
+                reflectionLOD = reflectionLOD * log2(vReflectionMicrosurfaceInfos.x) * vReflectionMicrosurfaceInfos.y + vReflectionMicrosurfaceInfos.z;
+                reflectionColor = sampleReflectionLod(reflectionSampler, reflectionCoords, reflectionLOD);
+            #else
+                float lodReflectionNormalized = saturate(reflectionLOD);
+                float lodReflectionNormalizedDoubled = lodReflectionNormalized * 2.0;
+
+                vec4 reflectionSpecularMid = sampleReflection(reflectionSampler, reflectionCoords);
+                if(lodReflectionNormalizedDoubled < 1.0){
+                    reflectionColor = mix(
+                        sampleReflection(reflectionSamplerHigh, reflectionCoords),
+                        reflectionSpecularMid,
+                        lodReflectionNormalizedDoubled
+                    );
+                } else {
+                    reflectionColor = mix(
+                        reflectionSpecularMid,
+                        sampleReflection(reflectionSamplerLow, reflectionCoords),
+                        lodReflectionNormalizedDoubled - 1.0
+                    );
+                }
+            #endif
         #else
-            float lodReflectionNormalized = saturate(reflectionLOD);
-            float lodReflectionNormalizedDoubled = lodReflectionNormalized * 2.0;
-
-            vec4 reflectionSpecularMid = sampleReflection(reflectionSampler, reflectionCoords);
-            if(lodReflectionNormalizedDoubled < 1.0){
-                reflectionColor = mix(
-                    sampleReflection(reflectionSamplerHigh, reflectionCoords),
-                    reflectionSpecularMid,
-                    lodReflectionNormalizedDoubled
-                );
-            } else {
-                reflectionColor = mix(
-                    reflectionSpecularMid,
-                    sampleReflection(reflectionSamplerLow, reflectionCoords),
-                    lodReflectionNormalizedDoubled - 1.0
-                );
-            }
+            vec4 reflectionSample = sampleReflection(reflectionSampler, reflectionCoords);
+            reflectionColor = reflectionSample;
         #endif
-    #else
-        vec4 reflectionSample = sampleReflection(reflectionSampler, reflectionCoords);
-        reflectionColor = reflectionSample;
-    #endif
 
-    #ifdef RGBDREFLECTION
-        reflectionColor.rgb = fromRGBD(reflectionColor);
-    #endif
+        #ifdef RGBDREFLECTION
+            reflectionColor.rgb = fromRGBD(reflectionColor);
+        #endif
 
-    #ifdef GAMMAREFLECTION
-        reflectionColor.rgb = toLinearSpace(reflectionColor.rgb);
-    #endif
+        #ifdef GAMMAREFLECTION
+            reflectionColor.rgb = toLinearSpace(reflectionColor.rgb);
+        #endif
 
-    #ifdef REFLECTIONBGR
-        reflectionColor.rgb = reflectionColor.bgr;
+        #ifdef REFLECTIONBGR
+            reflectionColor.rgb = reflectionColor.bgr;
+        #endif
+
+        // _____________________________ Levels _____________________________________
+        reflectionColor.rgb *= vReflectionInfos.x;
     #endif
 
-    // _____________________________ Levels _____________________________________
-    reflectionColor.rgb *= vReflectionInfos.x;
-#endif
+    // _____________________________ Diffuse Information _______________________________
+    vec3 diffuseColor = vec3(1., 1., 1.);
+    float finalAlpha = alpha;
+    #ifdef DIFFUSE
+        vec4 diffuseMap = texture2D(diffuseSampler, vDiffuseUV);
+        #ifdef GAMMADIFFUSE
+            diffuseMap.rgb = toLinearSpace(diffuseMap.rgb);
+        #endif
 
-// _____________________________ Diffuse Information _______________________________
-vec3 diffuseColor = vec3(1., 1., 1.);
-float finalAlpha = alpha;
-#ifdef DIFFUSE
-    vec4 diffuseMap = texture2D(diffuseSampler, vDiffuseUV);
-    #ifdef GAMMADIFFUSE
-        diffuseMap.rgb = toLinearSpace(diffuseMap.rgb);
-    #endif
+    // _____________________________ Levels _____________________________________
+        diffuseMap.rgb *= vDiffuseInfos.y;
 
-// _____________________________ Levels _____________________________________
-    diffuseMap.rgb *= vDiffuseInfos.y;
+        #ifdef DIFFUSEHASALPHA
+            finalAlpha *= diffuseMap.a;
+        #endif
 
-    #ifdef DIFFUSEHASALPHA
-        finalAlpha *= diffuseMap.a;
+        diffuseColor = diffuseMap.rgb;
     #endif
 
-    diffuseColor = diffuseMap.rgb;
-#endif
-
-// _____________________________ MIX ________________________________________
-#ifdef REFLECTIONFRESNEL
-    vec3 colorBase = diffuseColor;
-#else
-    vec3 colorBase = reflectionColor.rgb * diffuseColor;
-#endif
-    colorBase = max(colorBase, 0.0);
+    // _____________________________ MIX ________________________________________
+    #ifdef REFLECTIONFRESNEL
+        vec3 colorBase = diffuseColor;
+    #else
+        vec3 colorBase = reflectionColor.rgb * diffuseColor;
+    #endif
+        colorBase = max(colorBase, 0.0);
 
-// ___________________________ COMPOSE _______________________________________
-#ifdef USERGBCOLOR
-    vec3 finalColor = colorBase;
-#else
-    #ifdef USEHIGHLIGHTANDSHADOWCOLORS
-        vec3 mainColor = mix(vPrimaryColorShadow.rgb, vPrimaryColor.rgb, colorBase);
+    // ___________________________ COMPOSE _______________________________________
+    #ifdef USERGBCOLOR
+        vec3 finalColor = colorBase;
     #else
-        vec3 mainColor = vPrimaryColor.rgb;
+        #ifdef USEHIGHLIGHTANDSHADOWCOLORS
+            vec3 mainColor = mix(vPrimaryColorShadow.rgb, vPrimaryColor.rgb, colorBase);
+        #else
+            vec3 mainColor = vPrimaryColor.rgb;
+        #endif
+
+        vec3 finalColor = colorBase * mainColor;
     #endif
 
-    vec3 finalColor = colorBase * mainColor;
-#endif
+    // ___________________________ FRESNELS _______________________________________
+    #ifdef REFLECTIONFRESNEL
+        vec3 reflectionAmount = vReflectionControl.xxx;
+        vec3 reflectionReflectance0 = vReflectionControl.yyy;
+        vec3 reflectionReflectance90 = vReflectionControl.zzz;
+        float VdotN = dot(normalize(vEyePosition), normalW);
 
-// ___________________________ FRESNELS _______________________________________
-#ifdef REFLECTIONFRESNEL
-    vec3 reflectionAmount = vReflectionControl.xxx;
-    vec3 reflectionReflectance0 = vReflectionControl.yyy;
-    vec3 reflectionReflectance90 = vReflectionControl.zzz;
-    float VdotN = dot(normalize(vEyePosition), normalW);
-
-    vec3 planarReflectionFresnel = fresnelSchlickEnvironmentGGX(saturate(VdotN), reflectionReflectance0, reflectionReflectance90, 1.0);
-    reflectionAmount *= planarReflectionFresnel;
-
-    #ifdef REFLECTIONFALLOFF
-        float reflectionDistanceFalloff = 1.0 - saturate(length(vPositionW.xyz - vBackgroundCenter) * vReflectionControl.w);
-        reflectionDistanceFalloff *= reflectionDistanceFalloff;
-        reflectionAmount *= reflectionDistanceFalloff;
+        vec3 planarReflectionFresnel = fresnelSchlickEnvironmentGGX(saturate(VdotN), reflectionReflectance0, reflectionReflectance90, 1.0);
+        reflectionAmount *= planarReflectionFresnel;
+
+        #ifdef REFLECTIONFALLOFF
+            float reflectionDistanceFalloff = 1.0 - saturate(length(vPositionW.xyz - vBackgroundCenter) * vReflectionControl.w);
+            reflectionDistanceFalloff *= reflectionDistanceFalloff;
+            reflectionAmount *= reflectionDistanceFalloff;
+        #endif
+
+        finalColor = mix(finalColor, reflectionColor.rgb, saturate(reflectionAmount));
     #endif
 
-    finalColor = mix(finalColor, reflectionColor.rgb, saturate(reflectionAmount));
-#endif
+    #ifdef OPACITYFRESNEL
+        float viewAngleToFloor = dot(normalW, normalize(vEyePosition - vBackgroundCenter));
+
+        // Fade out the floor plane as the angle between the floor and the camera tends to 0 (starting from startAngle)
+        const float startAngle = 0.1;
+        float fadeFactor = saturate(viewAngleToFloor/startAngle);
 
-#ifdef OPACITYFRESNEL
-    float viewAngleToFloor = dot(normalW, normalize(vEyePosition - vBackgroundCenter));
+        finalAlpha *= fadeFactor * fadeFactor;
+    #endif
 
-    // Fade out the floor plane as the angle between the floor and the camera tends to 0 (starting from startAngle)
-    const float startAngle = 0.1;
-    float fadeFactor = saturate(viewAngleToFloor/startAngle);
+    // ___________________________ SHADOWS _______________________________________
+    #ifdef SHADOWINUSE
+        finalColor = mix(finalColor * shadowLevel, finalColor, globalShadow);
+    #endif
 
-    finalAlpha *= fadeFactor * fadeFactor;
-#endif
+    // ___________________________ FINALIZE _______________________________________
+    vec4 color = vec4(finalColor, finalAlpha);
 
-// ___________________________ SHADOWS _______________________________________
-#ifdef SHADOWINUSE
-    finalColor = mix(finalColor * shadowLevel, finalColor, globalShadow);
+#else
+    vec4 color = vec4(vPrimaryColor.rgb, (1.0 - clamp(globalShadow, 0., 1.)) * alpha);
 #endif
 
-// ___________________________ FINALIZE _______________________________________
-vec4 color = vec4(finalColor, finalAlpha);
-
 #include<fogFragment>
 
 #ifdef IMAGEPROCESSINGPOSTPROCESS