Browse Source

fixed deferred mode on/off switch bug

Benjamin Guignabert 5 năm trước cách đây
mục cha
commit
b2334cb755

+ 6 - 0
src/Materials/materialHelper.ts

@@ -301,12 +301,18 @@ export class MaterialHelper {
      * @param defines The defines to update
      */
     public static PrepareDefinesForDeferred(scene: Scene, defines: any) {
+        var previousDeferred = defines.HIGH_DEFINITION_PIPELINE;
+
         if (scene.highDefinitionPipeline) {
             defines.HIGH_DEFINITION_PIPELINE = true;
             defines.SCENE_MRT_COUNT = scene.mrtCount;
         } else {
             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
 	// putting that in a loop won't compile
 	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

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

@@ -502,11 +502,10 @@ void main(void) {
     // What about :
     // Lightmaps ? (can we consider them as pure diffuse ?)
     // 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
 
     gl_FragColor = finalColor;

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

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

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

@@ -4,7 +4,6 @@
 varying vec2 vUV;
 uniform vec2 texelSize;
 uniform sampler2D textureSampler;
-uniform sampler2D inputSampler;
 uniform sampler2D irradianceSampler;
 uniform sampler2D depthSampler;
 uniform sampler2D albedoSampler;
@@ -83,6 +82,10 @@ vec3 ComputeBilateralWeight(float xy2, float z, float mmPerUnit, vec3 S, float r
     #endif
 }
 
+bool IsSSSMaterial(vec3 irradiance) {
+    return irradiance.b > 0.;
+}
+
 // TODO : inout vec3 totalIrradiance, inout vec3 totalWeight
 void EvaluateSample(int i, int n, vec3 S, float d, vec3 centerPosVS, float mmPerUnit, float pixelsPerMm,
                     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.
     // TODO
-    if (true)
+    if (IsSSSMaterial(irradiance))
     {
         // Apply bilateral weighting.
         float relZ = viewZ - centerPosVS.z;
@@ -155,8 +158,8 @@ void main(void)
 {
 	vec3 centerIrradiance  = texture2D(irradianceSampler, vUV).rgb;
 	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)
 	{
@@ -209,8 +212,8 @@ void main(void)
 	int  sampleCount  = int(filterArea * rcp(SSS_PIXELS_PER_SAMPLE));
 	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)
 	{

+ 2 - 3
src/scene.ts

@@ -258,7 +258,7 @@ export class Scene extends AbstractScene implements IAnimatable {
         this._highDefinitionPipeline = b;
     }
 
-    public mrtCount: number = 5;
+    public mrtCount: number = 4;
     public highDefinitionMRT: MultiRenderTarget;
     public sceneCompositorPostProcess: SceneCompositorPostProcess;
     public subSurfaceScatteringPostProcess: SubSurfaceScatteringPostProcess;
@@ -1450,8 +1450,7 @@ export class Scene extends AbstractScene implements IAnimatable {
             Constants.TEXTURETYPE_UNSIGNED_INT, // Original color
             Constants.TEXTURETYPE_UNSIGNED_INT, // Irradiance
             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,