瀏覽代碼

working SSS

Benjamin Guignabert 5 年之前
父節點
當前提交
19344ec31a

+ 2 - 0
src/Engines/Extensions/engine.multiRender.ts

@@ -194,6 +194,8 @@ ThinEngine.prototype.createMultipleRenderTarget = function(size: any, options: I
         texture._generateDepthBuffer = generateDepthBuffer;
         texture._generateDepthBuffer = generateDepthBuffer;
         texture._generateStencilBuffer = generateStencilBuffer;
         texture._generateStencilBuffer = generateStencilBuffer;
         texture._attachments = attachments;
         texture._attachments = attachments;
+        texture._textureArray = textures;
+        texture._textureCount = textureCount;
 
 
         this._internalTexturesCache.push(texture);
         this._internalTexturesCache.push(texture);
     }
     }

+ 10 - 6
src/Engines/thinEngine.ts

@@ -1326,13 +1326,17 @@ export class ThinEngine {
 
 
         // If MSAA, we need to bitblt back to main texture
         // If MSAA, we need to bitblt back to main texture
         var gl = this._gl;
         var gl = this._gl;
-
         if (texture._MSAAFramebuffer) {
         if (texture._MSAAFramebuffer) {
-            gl.bindFramebuffer(gl.READ_FRAMEBUFFER, texture._MSAAFramebuffer);
-            gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, texture._framebuffer);
-            gl.blitFramebuffer(0, 0, texture.width, texture.height,
-                0, 0, texture.width, texture.height,
-                gl.COLOR_BUFFER_BIT, gl.NEAREST);
+            this.unBindMultiColorAttachmentFramebuffer(texture._textureCount!, texture._textureArray!, disableGenerateMipMaps, onBeforeUnbind);
+            return;
+        //     gl.bindFramebuffer(gl.READ_FRAMEBUFFER, texture._MSAAFramebuffer);
+        //     gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, texture._framebuffer);
+        //     gl.readBuffer(gl.COLOR_ATTACHMENT0);
+        //     gl.drawBuffers([gl.COLOR_ATTACHMENT0]);
+        //     gl.blitFramebuffer(0, 0, texture.width, texture.height,
+        //         0, 0, texture.width, texture.height,
+        //         gl.COLOR_BUFFER_BIT, gl.NEAREST);
+        // }
         }
         }
 
 
         if (texture.generateMipMaps && !disableGenerateMipMaps && !texture.isCube) {
         if (texture.generateMipMaps && !disableGenerateMipMaps && !texture.isCube) {

+ 4 - 0
src/Materials/Textures/internalTexture.ts

@@ -195,6 +195,10 @@ export class InternalTexture {
     /** @hidden */
     /** @hidden */
     public _attachments: Nullable<number[]> = null;
     public _attachments: Nullable<number[]> = null;
     /** @hidden */
     /** @hidden */
+    public _textureArray: Nullable<InternalTexture[]> = null;
+    /** @hidden */
+    public _textureCount: Nullable<number> = null;
+    /** @hidden */
     public _cachedCoordinatesMode: Nullable<number> = null;
     public _cachedCoordinatesMode: Nullable<number> = null;
     /** @hidden */
     /** @hidden */
     public _cachedWrapU: Nullable<number> = null;
     public _cachedWrapU: Nullable<number> = null;

+ 3 - 3
src/Shaders/pbr.fragment.fx

@@ -505,10 +505,10 @@ void main(void) {
     // 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[0] = finalColor;
-    gl_FragData[1] = vec4(irradiance, 1.0);
+    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[2] = vec4(vViewPos.z, 0.0, 0.0, 1.0);
-    gl_FragData[3] = vec4(surfaceAlbedo, 1.0);
-    gl_FragData[4] = vec4(finalColor.rgb - irradiance, 1.0);
+    gl_FragData[3] = vec4(sqrt(surfaceAlbedo), 1.0);
+    gl_FragData[4] = vec4(finalColor.rgb - irradiance, 0.0);
 #else
 #else
     // #define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR
     // #define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR
     gl_FragColor = finalColor;
     gl_FragColor = finalColor;

+ 23 - 5
src/Shaders/subSurfaceScattering.fragment.fx

@@ -19,6 +19,7 @@ const int _SssSampleBudget = 40;
 
 
 #define rcp(x) 1. / x
 #define rcp(x) 1. / x
 #define Sq(x) x * x
 #define Sq(x) x * x
+// #define DEBUG_SSS_SAMPLES true
 
 
 vec3 EvalBurleyDiffusionProfile(float r, vec3 S)
 vec3 EvalBurleyDiffusionProfile(float r, vec3 S)
 {
 {
@@ -131,14 +132,18 @@ void main(void)
 {
 {
 	vec3 centerIrradiance  = texture2D(irradianceSampler, vUV).rgb;
 	vec3 centerIrradiance  = texture2D(irradianceSampler, vUV).rgb;
 	float  centerDepth       = 0.;
 	float  centerDepth       = 0.;
-	bool   passedStencilTest = true; //TestLightingForSSS(centerIrradiance);
+    vec4 inputColor = texture2D(inputSampler, vUV);
+	bool passedStencilTest = (inputColor.a == 0.0);
 
 
 	if (passedStencilTest)
 	if (passedStencilTest)
 	{
 	{
 	    centerDepth = texture2D(depthSampler, vUV).r;
 	    centerDepth = texture2D(depthSampler, vUV).r;
 	}
 	}
 
 
-    if (!passedStencilTest) { return; }
+    if (!passedStencilTest) { 
+        gl_FragColor = texture2D(textureSampler, vUV);
+        return;
+    }
 
 
 
 
     // SKIN DIFFUSION PROFILE
     // SKIN DIFFUSION PROFILE
@@ -157,7 +162,7 @@ void main(void)
 	float  distScale     = 1.; //sssData.subsurfaceMask;
 	float  distScale     = 1.; //sssData.subsurfaceMask;
 	vec3 S             = vec3(0.7568628, 0.32156864, 0.20000002); //_ShapeParamsAndMaxScatterDists[profileIndex].rgb diffusion color
 	vec3 S             = vec3(0.7568628, 0.32156864, 0.20000002); //_ShapeParamsAndMaxScatterDists[profileIndex].rgb diffusion color
 	float  d             = 0.7568628; //_ShapeParamsAndMaxScatterDists[profileIndex].a max scatter dist
 	float  d             = 0.7568628; //_ShapeParamsAndMaxScatterDists[profileIndex].a max scatter dist
-	float  metersPerUnit = 1.; //_WorldScalesAndFilterRadiiAndThicknessRemaps[profileIndex].x;
+	float  metersPerUnit = 0.15; //_WorldScalesAndFilterRadiiAndThicknessRemaps[profileIndex].x;
 
 
 	// Reconstruct the view-space position corresponding to the central sample.
 	// Reconstruct the view-space position corresponding to the central sample.
 	vec2 centerPosNDC = vUV;
 	vec2 centerPosNDC = vUV;
@@ -186,9 +191,22 @@ void main(void)
 
 
 	if (distScale == 0. || sampleCount < 1)
 	if (distScale == 0. || sampleCount < 1)
 	{
 	{
-	    gl_FragColor = vec4(albedo * centerIrradiance, 1.0);
+        #ifdef DEBUG_SSS_SAMPLES
+            vec3 green = vec3(0., 1., 0.);
+            gl_FragColor = vec4(green, 1.0);
+            return;
+        #endif
+	    gl_FragColor = vec4(inputColor.rgb + albedo * centerIrradiance, 1.0);
+        return;
 	}
 	}
 
 
+    #ifdef DEBUG_SSS_SAMPLES
+        vec3 red  = vec3(1., 0., 0.);
+        vec3 blue = vec3(0., 0., 1.);
+        gl_FragColor = vec4(mix(blue, red, clamp(float(sampleCount) / float(sampleBudget), 0.0, 1.0)), 1.0);
+        return;
+    #endif
+
 	// TODO : TANGENT PLANE
 	// TODO : TANGENT PLANE
 	vec3 normalVS = vec3(0., 0., 0.);
 	vec3 normalVS = vec3(0., 0., 0.);
     vec3 tangentX = vec3(0., 0., 0.);
     vec3 tangentX = vec3(0., 0., 0.);
@@ -215,7 +233,7 @@ void main(void)
     // Total weight is 0 for color channels without scattering.
     // Total weight is 0 for color channels without scattering.
     totalWeight = max(totalWeight, 1e-12);
     totalWeight = max(totalWeight, 1e-12);
 
 
-    gl_FragColor = vec4(texture2D(inputSampler, vUV).rgb + albedo * (totalIrradiance / totalWeight), 1.);
+    gl_FragColor = vec4(inputColor.rgb + albedo * (totalIrradiance / totalWeight), 1.);
 
 
 	// gl_FragColor = mix(texture2D(textureSampler, vUV), centerIrradiance, 0.5);
 	// gl_FragColor = mix(texture2D(textureSampler, vUV), centerIrradiance, 0.5);
 }
 }

+ 1 - 1
src/scene.ts

@@ -1455,7 +1455,7 @@ export class Scene extends AbstractScene implements IAnimatable {
 
 
         this.highDefinitionMRT = new MultiRenderTarget("sceneHighDefinitionMRT", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, 5, this,
         this.highDefinitionMRT = new MultiRenderTarget("sceneHighDefinitionMRT", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, 5, this,
             { generateMipMaps: false, generateDepthTexture: true, defaultType: Constants.TEXTURETYPE_UNSIGNED_INT, types: types });
             { generateMipMaps: false, generateDepthTexture: true, defaultType: Constants.TEXTURETYPE_UNSIGNED_INT, types: types });
-        this.highDefinitionMRT.samples = 1;
+        this.highDefinitionMRT.samples = 4;
         this.sceneCompositorPostProcess = new SceneCompositorPostProcess("sceneCompositor", 1, null, undefined, this._engine);
         this.sceneCompositorPostProcess = new SceneCompositorPostProcess("sceneCompositor", 1, null, undefined, this._engine);
         this.sceneCompositorPostProcess.inputTexture = this.highDefinitionMRT.getInternalTexture()!;
         this.sceneCompositorPostProcess.inputTexture = this.highDefinitionMRT.getInternalTexture()!;
         this.subSurfaceScatteringPostProcess = new SubSurfaceScatteringPostProcess("subSurfaceScattering", this, 1, null, undefined, this._engine);
         this.subSurfaceScatteringPostProcess = new SubSurfaceScatteringPostProcess("subSurfaceScattering", this, 1, null, undefined, this._engine);