ソースを参照

adding depth to PBR
validated sss shader until EvaluateSample

Benjamin Guignabert 5 年 前
コミット
44cdc2f4e2

+ 3 - 2
src/PostProcesses/SubSurfaceScatteringPostProcess.ts

@@ -24,7 +24,7 @@ export class SubSurfaceScatteringPostProcess extends PostProcess {
     private _filterRadius: number;
 
     constructor(name: string, scene: Scene, options: number | PostProcessOptions, camera: Nullable<Camera> = null, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT) {
-        super(name, "subSurfaceScattering", ["texelSize", "filterRadius", "viewportSize"], ["irradianceSampler"], options, camera, samplingMode || Texture.BILINEAR_SAMPLINGMODE, engine, reusable, null, textureType, "postprocess", undefined, true);
+        super(name, "subSurfaceScattering", ["texelSize", "filterRadius", "viewportSize"], ["irradianceSampler", "depthSampler"], options, camera, samplingMode || Texture.BILINEAR_SAMPLINGMODE, engine, reusable, null, textureType, "postprocess", undefined, true);
 
         const defines = this._getDefines();
         this.updateEffect(defines);
@@ -32,7 +32,8 @@ export class SubSurfaceScatteringPostProcess extends PostProcess {
         this.onApplyObservable.add((effect: Effect) => {
             var texelSize = this.texelSize;
             effect.setFloat2("texelSize", texelSize.x, texelSize.y);
-            effect.setTexture("irradianceSampler", scene.highDefinitionMRT.textures[2]);
+            effect.setTexture("irradianceSampler", scene.highDefinitionMRT.textures[1]);
+            effect.setTexture("depthSampler", scene.highDefinitionMRT.textures[2]);
             effect.setFloat("filterRadius", this._filterRadius);
             effect.setFloat2("viewportSize", 
                 Math.tan(scene.activeCamera!.fov / 2) * scene.getEngine().getAspectRatio(scene.activeCamera!, true),

+ 5 - 1
src/Shaders/ShadersInclude/pbrFragmentExtraDeclaration.fx

@@ -27,4 +27,8 @@ varying vec3 vPositionW;
 
 #ifdef VERTEXCOLOR
     varying vec4 vColor;
-#endif
+#endif
+
+#ifdef HIGH_DEFINITION_PIPELINE
+	varying vec3 vViewPos;
+#endif

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

@@ -506,7 +506,7 @@ void main(void) {
     // AO and shadows, should they dim the diffuseLight ? (right now they are)
     gl_FragData[0] = finalColor;
     gl_FragData[1] = vec4(irradiance, 1.0);
-    gl_FragData[2] = vec4(0.0, 1.0, 0.0, 1.0);
+    gl_FragData[2] = vec4(vViewPos.z, 0.0, 0.0, 1.0);
     gl_FragData[3] = vec4(1.0, 0.0, 0.0, 1.0);
     gl_FragData[4] = vec4(1.0, 0.0, 0.0, 1.0);
 #else

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

@@ -34,6 +34,10 @@ attribute vec4 color;
 // Uniforms
 #include<instancesDeclaration>
 
+#ifdef HIGH_DEFINITION_PIPELINE
+varying vec3 vViewPos;
+#endif
+
 #if defined(ALBEDO) && ALBEDODIRECTUV == 0
 varying vec2 vAlbedoUV;
 #endif
@@ -169,6 +173,7 @@ void main(void) {
 
     vec4 worldPos = finalWorld * vec4(positionUpdated, 1.0);
     vPositionW = vec3(worldPos);
+    vViewPos = (view * worldPos).rgb;
 
 #ifdef NORMAL
     mat3 normalWorld = mat3(finalWorld);

+ 8 - 1
src/Shaders/subSurfaceScattering.fragment.fx

@@ -5,6 +5,7 @@ varying vec2 vUV;
 uniform vec2 texelSize;
 uniform sampler2D textureSampler;
 uniform sampler2D irradianceSampler;
+uniform sampler2D depthSampler;
 
 uniform float filterRadius;
 uniform vec2 viewportSize;
@@ -132,7 +133,7 @@ void main(void)
 
 	if (passedStencilTest)
 	{
-	    centerDepth = 0.; //texture2D(depthSampler, vUV).r NDC
+	    centerDepth = texture2D(depthSampler, vUV).r;
 	}
 
     if (!passedStencilTest) { return; }
@@ -173,9 +174,15 @@ void main(void)
 
 	// Area of a disk.
 	float filterArea   = PI * Sq(filterRadius * pixelsPerMm);
+
+
 	int  sampleCount  = int(filterArea * rcp(SSS_PIXELS_PER_SAMPLE));
 	int  sampleBudget = _SssSampleBudget;
 
+	// DEBUG
+	gl_FragColor = vec4(vec3(sampleCount) / 1000000., 1.0);
+	return;
+
 	int texturingMode = 0; // GetSubsurfaceScatteringTexturingMode(profileIndex);
 	vec3 albedo  = vec3(0.5); // texture2D(albedoSampler, vUV); //ApplySubsurfaceScatteringTexturingMode(texturingMode, sssData.diffuseColor);
 

+ 9 - 1
src/scene.ts

@@ -1442,8 +1442,16 @@ export class Scene extends AbstractScene implements IAnimatable {
         }
 
         // TODO : TEMPORARY
+        const types = [
+            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,
+        ];
+
         this.highDefinitionMRT = new MultiRenderTarget("sceneHighDefinitionMRT", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, 5, this,
-            { generateMipMaps: false, generateDepthTexture: true, defaultType: Constants.TEXTURETYPE_UNSIGNED_INT });
+            { generateMipMaps: false, generateDepthTexture: true, defaultType: Constants.TEXTURETYPE_UNSIGNED_INT, types: types });
         this.highDefinitionMRT.samples = 1;
         this.sceneCompositorPostProcess = new SceneCompositorPostProcess("sceneCompositor", 1, null, undefined, this._engine);
         this.sceneCompositorPostProcess.inputTexture = this.highDefinitionMRT.getInternalTexture()!;