Browse Source

fixed deferred mode on/off switch bug

Benjamin Guignabert 5 years ago
parent
commit
b2334cb755

+ 6 - 0
src/Materials/materialHelper.ts

@@ -301,12 +301,18 @@ export class MaterialHelper {
      * @param defines The defines to update
      * @param defines The defines to update
      */
      */
     public static PrepareDefinesForDeferred(scene: Scene, defines: any) {
     public static PrepareDefinesForDeferred(scene: Scene, defines: any) {
+        var previousDeferred = defines.HIGH_DEFINITION_PIPELINE;
+
         if (scene.highDefinitionPipeline) {
         if (scene.highDefinitionPipeline) {
             defines.HIGH_DEFINITION_PIPELINE = true;
             defines.HIGH_DEFINITION_PIPELINE = true;
             defines.SCENE_MRT_COUNT = scene.mrtCount;
             defines.SCENE_MRT_COUNT = scene.mrtCount;
         } else {
         } else {
             defines.HIGH_DEFINITION_PIPELINE = false;
             defines.HIGH_DEFINITION_PIPELINE = false;
         }
         }
+
+        if (defines.HIGH_DEFINITION_PIPELINE != previousDeferred) {
+            defines.markAsUnprocessed();
+        }
     }
     }
 
 
     /**
     /**

+ 3 - 4
src/Shaders/ShadersInclude/deferredDefaultOutput.fx

@@ -1,8 +1,7 @@
 #ifdef HIGH_DEFINITION_PIPELINE
 #ifdef HIGH_DEFINITION_PIPELINE
 	// putting that in a loop won't compile
 	// putting that in a loop won't compile
 	gl_FragData[0] = gl_FragColor;
 	gl_FragData[0] = gl_FragColor;
-	gl_FragData[1] = gl_FragColor;
-	gl_FragData[2] = gl_FragColor;
-	gl_FragData[3] = gl_FragColor;
-	gl_FragData[4] = gl_FragColor;
+	gl_FragData[1] = vec4(0.0, 0.0, 0.0, 0.0);
+	gl_FragData[2] = vec4(0.0, 0.0, 0.0, 0.0);
+	gl_FragData[3] = vec4(0.0, 0.0, 0.0, 0.0);
 #endif
 #endif

+ 4 - 5
src/Shaders/pbr.fragment.fx

@@ -502,11 +502,10 @@ void main(void) {
     // What about :
     // What about :
     // Lightmaps ? (can we consider them as pure diffuse ?)
     // Lightmaps ? (can we consider them as pure diffuse ?)
     // AO and shadows, should they dim the diffuseLight ? (right now they are)
     // AO and shadows, should they dim the diffuseLight ? (right now they are)
-    gl_FragData[0] = finalColor;
-    gl_FragData[1] = vec4(irradiance / sqrt(surfaceAlbedo), 1.0); // pre and post scatter
-    gl_FragData[2] = vec4(vViewPos.z, 0.0, 0.0, 1.0);
-    gl_FragData[3] = vec4(sqrt(surfaceAlbedo), 1.0);
-    gl_FragData[4] = vec4(finalColor.rgb - irradiance, 0.0);
+    gl_FragData[0] = vec4(finalColor.rgb - irradiance, 1.0); // Lit without irradiance
+    gl_FragData[1] = vec4(irradiance / sqrt(surfaceAlbedo), 1.0); // irradiance, for pre and post scatter
+    gl_FragData[2] = vec4(vViewPos.z, 0.0, 0.0, 1.0); // Linear depth
+    gl_FragData[3] = vec4(sqrt(surfaceAlbedo), 1.0); // albedo, for pre and post scatter
 #endif
 #endif
 
 
     gl_FragColor = finalColor;
     gl_FragColor = finalColor;

+ 2 - 0
src/Shaders/pbr.vertex.fx

@@ -173,7 +173,9 @@ void main(void) {
 
 
     vec4 worldPos = finalWorld * vec4(positionUpdated, 1.0);
     vec4 worldPos = finalWorld * vec4(positionUpdated, 1.0);
     vPositionW = vec3(worldPos);
     vPositionW = vec3(worldPos);
+#ifdef HIGH_DEFINITION_PIPELINE
     vViewPos = (view * worldPos).rgb;
     vViewPos = (view * worldPos).rgb;
+#endif
 
 
 #ifdef NORMAL
 #ifdef NORMAL
     mat3 normalWorld = mat3(finalWorld);
     mat3 normalWorld = mat3(finalWorld);

+ 9 - 6
src/Shaders/subSurfaceScattering.fragment.fx

@@ -4,7 +4,6 @@
 varying vec2 vUV;
 varying vec2 vUV;
 uniform vec2 texelSize;
 uniform vec2 texelSize;
 uniform sampler2D textureSampler;
 uniform sampler2D textureSampler;
-uniform sampler2D inputSampler;
 uniform sampler2D irradianceSampler;
 uniform sampler2D irradianceSampler;
 uniform sampler2D depthSampler;
 uniform sampler2D depthSampler;
 uniform sampler2D albedoSampler;
 uniform sampler2D albedoSampler;
@@ -83,6 +82,10 @@ vec3 ComputeBilateralWeight(float xy2, float z, float mmPerUnit, vec3 S, float r
     #endif
     #endif
 }
 }
 
 
+bool IsSSSMaterial(vec3 irradiance) {
+    return irradiance.b > 0.;
+}
+
 // TODO : inout vec3 totalIrradiance, inout vec3 totalWeight
 // TODO : inout vec3 totalIrradiance, inout vec3 totalWeight
 void EvaluateSample(int i, int n, vec3 S, float d, vec3 centerPosVS, float mmPerUnit, float pixelsPerMm,
 void EvaluateSample(int i, int n, vec3 S, float d, vec3 centerPosVS, float mmPerUnit, float pixelsPerMm,
                     float phase, inout vec3 totalIrradiance, inout vec3 totalWeight)
                     float phase, inout vec3 totalIrradiance, inout vec3 totalWeight)
@@ -129,7 +132,7 @@ void EvaluateSample(int i, int n, vec3 S, float d, vec3 centerPosVS, float mmPer
 
 
     // Check the results of the stencil test.
     // Check the results of the stencil test.
     // TODO
     // TODO
-    if (true)
+    if (IsSSSMaterial(irradiance))
     {
     {
         // Apply bilateral weighting.
         // Apply bilateral weighting.
         float relZ = viewZ - centerPosVS.z;
         float relZ = viewZ - centerPosVS.z;
@@ -155,8 +158,8 @@ void main(void)
 {
 {
 	vec3 centerIrradiance  = texture2D(irradianceSampler, vUV).rgb;
 	vec3 centerIrradiance  = texture2D(irradianceSampler, vUV).rgb;
 	float  centerDepth       = 0.;
 	float  centerDepth       = 0.;
-    vec4 inputColor = texture2D(inputSampler, vUV);
-	bool passedStencilTest = (inputColor.a == 0.0);
+    vec4 inputColor = texture2D(textureSampler, vUV);
+	bool passedStencilTest = IsSSSMaterial(centerIrradiance);
 
 
 	if (passedStencilTest)
 	if (passedStencilTest)
 	{
 	{
@@ -209,8 +212,8 @@ void main(void)
 	int  sampleCount  = int(filterArea * rcp(SSS_PIXELS_PER_SAMPLE));
 	int  sampleCount  = int(filterArea * rcp(SSS_PIXELS_PER_SAMPLE));
 	int  sampleBudget = _SssSampleBudget;
 	int  sampleBudget = _SssSampleBudget;
 
 
-	int texturingMode = 0; // GetSubsurfaceScatteringTexturingMode(profileIndex);
-	vec3 albedo  = texture2D(albedoSampler, vUV).rgb; //ApplySubsurfaceScatteringTexturingMode(texturingMode, sssData.diffuseColor);
+	int texturingMode = 0;
+	vec3 albedo  = texture2D(albedoSampler, vUV).rgb;
 
 
 	if (distScale == 0. || sampleCount < 1)
 	if (distScale == 0. || sampleCount < 1)
 	{
 	{

+ 2 - 3
src/scene.ts

@@ -258,7 +258,7 @@ export class Scene extends AbstractScene implements IAnimatable {
         this._highDefinitionPipeline = b;
         this._highDefinitionPipeline = b;
     }
     }
 
 
-    public mrtCount: number = 5;
+    public mrtCount: number = 4;
     public highDefinitionMRT: MultiRenderTarget;
     public highDefinitionMRT: MultiRenderTarget;
     public sceneCompositorPostProcess: SceneCompositorPostProcess;
     public sceneCompositorPostProcess: SceneCompositorPostProcess;
     public subSurfaceScatteringPostProcess: SubSurfaceScatteringPostProcess;
     public subSurfaceScatteringPostProcess: SubSurfaceScatteringPostProcess;
@@ -1450,8 +1450,7 @@ export class Scene extends AbstractScene implements IAnimatable {
             Constants.TEXTURETYPE_UNSIGNED_INT, // Original color
             Constants.TEXTURETYPE_UNSIGNED_INT, // Original color
             Constants.TEXTURETYPE_UNSIGNED_INT, // Irradiance
             Constants.TEXTURETYPE_UNSIGNED_INT, // Irradiance
             Constants.TEXTURETYPE_FLOAT, // Depth (world units)
             Constants.TEXTURETYPE_FLOAT, // Depth (world units)
-            Constants.TEXTURETYPE_UNSIGNED_INT,
-            Constants.TEXTURETYPE_UNSIGNED_INT,
+            Constants.TEXTURETYPE_UNSIGNED_INT
         ];
         ];
 
 
         this.highDefinitionMRT = new MultiRenderTarget("sceneHighDefinitionMRT", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, this.mrtCount, this,
         this.highDefinitionMRT = new MultiRenderTarget("sceneHighDefinitionMRT", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, this.mrtCount, this,