Pārlūkot izejas kodu

now correctly takes into account linear / gamma space

Benjamin Guignabert 5 gadi atpakaļ
vecāks
revīzija
684fb7a19c

+ 1 - 1
src/Materials/PBR/pbrBaseMaterial.ts

@@ -1573,7 +1573,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
         }
 
         if (defines._areImageProcessingDirty && this._imageProcessingConfiguration) {
-            this._imageProcessingConfiguration.prepareDefines(defines);
+            this._imageProcessingConfiguration.prepareDefines(defines, false, this.getScene().highDefinitionPipeline);
         }
 
         defines.FORCENORMALFORWARD = this._forceNormalForward;

+ 3 - 3
src/Materials/imageProcessingConfiguration.ts

@@ -438,8 +438,8 @@ export class ImageProcessingConfiguration {
      * @param defines the list of defines to complete
      * @param forPostProcess Define if we are currently in post process mode or not
      */
-    public prepareDefines(defines: IImageProcessingConfigurationDefines, forPostProcess: boolean = false): void {
-        if (forPostProcess !== this.applyByPostProcess || !this._isEnabled) {
+    public prepareDefines(defines: IImageProcessingConfigurationDefines, forPostProcess: boolean = false, compositionPipeline: boolean = false): void {
+        if (forPostProcess !== this.applyByPostProcess || !this._isEnabled || compositionPipeline) {
             defines.VIGNETTE = false;
             defines.TONEMAPPING = false;
             defines.TONEMAPPING_ACES = false;
@@ -449,7 +449,7 @@ export class ImageProcessingConfiguration {
             defines.COLORGRADING = false;
             defines.COLORGRADING3D = false;
             defines.IMAGEPROCESSING = false;
-            defines.IMAGEPROCESSINGPOSTPROCESS = this.applyByPostProcess && this._isEnabled;
+            defines.IMAGEPROCESSINGPOSTPROCESS = compositionPipeline || (this.applyByPostProcess && this._isEnabled);
             return;
         }
 

+ 1 - 0
src/Materials/materialHelper.ts

@@ -312,6 +312,7 @@ export class MaterialHelper {
 
         if (defines.HIGH_DEFINITION_PIPELINE != previousDeferred) {
             defines.markAsUnprocessed();
+            defines.markAsImageProcessingDirty();
         }
     }
 

+ 9 - 1
src/PostProcesses/SubSurfaceScatteringPostProcess.ts

@@ -25,6 +25,7 @@ export class SubSurfaceScatteringPostProcess extends PostProcess {
 
     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"], ["inputSampler", "irradianceSampler", "depthSampler", "albedoSampler"], options, camera, samplingMode || Texture.BILINEAR_SAMPLINGMODE, engine, reusable, null, textureType, "postprocess", undefined, true);
+        this._scene = scene;
 
         const defines = this._getDefines();
         this.updateEffect(defines);
@@ -51,7 +52,14 @@ export class SubSurfaceScatteringPostProcess extends PostProcess {
             return null;
         }
 
-        return "";
+        let defines = "";
+
+        if (this._scene.imageProcessingConfiguration.applyByPostProcess) {
+            // We must output linear color for post process
+            defines = defines + "#define LINEAR_OUTPUT\n";
+        }
+
+        return defines;
     }
 
     private _getDiffusionProfileParameters()

+ 1 - 1
src/PostProcesses/postProcess.ts

@@ -137,7 +137,7 @@ export class PostProcess {
     public adaptScaleToCurrentViewport = false;
 
     private _camera: Camera;
-    private _scene: Scene;
+    protected _scene: Scene;
     private _engine: Engine;
 
     private _options: number | PostProcessOptions;

+ 2 - 2
src/PostProcesses/sceneCompositorPostProcess.ts

@@ -35,7 +35,7 @@ export class SceneCompositorPostProcess extends PostProcess {
         if (!engine) {
             return null;
         }
-
-        return "";
+        let defines = "";
+        return defines;
     }
 }

+ 0 - 1
src/Shaders/ShadersInclude/pbrBlockImageProcessing.fx

@@ -6,7 +6,6 @@
     // Alway run to ensure we are going back to gamma space.
     finalColor = applyImageProcessing(finalColor);
 #endif
-
     finalColor.a *= visibility;
 
 #ifdef PREMULTIPLYALPHA

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

@@ -494,7 +494,6 @@ void main(void) {
 
     #include<logDepthFragment>
     #include<fogFragment>(color, finalColor)
-
     #include<pbrBlockImageProcessing>
 
     #define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR
@@ -506,14 +505,54 @@ void main(void) {
             irradiance += finalIrradiance;
         #endif
     #endif
+
+    // BJS
+    // vec3 finalDiffuse = diffuseBase; // shadows, lightmap and stuff
+    // finalDiffuse *= surfaceAlbedo.rgb;
+    // finalDiffuse = max(finalDiffuse, 0.0);
+    // finalDiffuse *= vLightingIntensity.x;
+    // finalDiffuse *= ambientOcclusionForDirectDiffuse;
+    
+    // #ifdef REFLECTION
+    //     vec3 finalIrradiance = reflectionOut.environmentIrradiance;
+
+    //     #if defined(CLEARCOAT)
+    //         finalIrradiance *= clearcoatOut.conservationFactor;
+    //         #if defined(CLEARCOAT_TINT)
+    //             finalIrradiance *= clearcoatOut.absorption;
+    //         #endif
+    //     #endif
+
+    //     #if defined(SS_REFRACTION)
+    //         finalIrradiance *= subSurfaceOut.refractionFactorForIrradiance;
+    //     #endif
+
+    //     #if defined(SS_TRANSLUCENCY)
+    //         finalIrradiance += subSurfaceOut.refractionIrradiance;
+    //     #endif
+
+    //     finalIrradiance *= surfaceAlbedo.rgb;
+    //     finalIrradiance *= vLightingIntensity.z;
+    //     finalIrradiance *= aoOut.ambientOcclusionColor;
+    // #endif 
+
+    // UNITY
+    // float3 modifiedDiffuseColor = GetModifiedDiffuseColorForSSS(bsdfData);
+    // diffuseLighting = modifiedDiffuseColor * lighting.direct.diffuse + builtinData.bakeDiffuseLighting + builtinData.emissiveColor;
+    // diffuseLighting = lerp(diffuseLighting, lighting.indirect.specularTransmitted, bsdfData.transmittanceMask * _EnableSSRefraction);
+
     // finalDiffuse and finalIrradiance are already multiplied by surfaceAlbedo
     // What about :
     // Lightmaps ? (can we consider them as pure diffuse ?)
     // AO and shadows, should they dim the diffuseLight ? (right now they are)
+    vec3 sqAlbedo = sqrt(surfaceAlbedo); // for pre and post scatter
+
+    // Irradiance is diffuse * surfaceAlbedo
     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
+    irradiance /= sqAlbedo;
+    gl_FragData[1] = vec4(irradiance, 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
+    gl_FragData[3] = vec4(sqAlbedo, 1.0); // albedo, for pre and post scatter
 #endif
 
     gl_FragColor = finalColor;

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

@@ -5,4 +5,4 @@ uniform sampler2D textureSampler;
 void main(void) 
 {
 	gl_FragColor = texture2D(textureSampler, vUV);
-}
+}

+ 11 - 3
src/Shaders/subSurfaceScattering.fragment.fx

@@ -1,5 +1,6 @@
 // Samplers
 #include<fibonacci>
+#include<helperFunctions>
 
 varying vec2 vUV;
 uniform vec2 texelSize;
@@ -12,7 +13,6 @@ uniform float filterRadius;
 uniform vec2 viewportSize;
 
 const float LOG2_E = 1.4426950408889634;
-const float PI = 3.1415926535897932;
 const float SSS_PIXELS_PER_SAMPLE = 4.;
 const int _SssSampleBudget = 40;
 
@@ -188,7 +188,7 @@ void main(void)
 	float  distScale     = 1.; //sssData.subsurfaceMask;
 	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  metersPerUnit = 0.15; //_WorldScalesAndFilterRadiiAndThicknessRemaps[profileIndex].x;
+	float  metersPerUnit = 0.07; //_WorldScalesAndFilterRadiiAndThicknessRemaps[profileIndex].x;
 
 	// Reconstruct the view-space position corresponding to the central sample.
 	vec2 centerPosNDC = vUV;
@@ -222,7 +222,12 @@ void main(void)
             gl_FragColor = vec4(green, 1.0);
             return;
         #endif
+
 	    gl_FragColor = vec4(inputColor.rgb + albedo * centerIrradiance, 1.0);
+        #ifndef LINEAR_OUTPUT
+            gl_FragColor.rgb = toGammaSpace(gl_FragColor.rgb);
+            gl_FragColor.rgb = saturate(gl_FragColor.rgb);
+        #endif
         return;
 	}
 
@@ -253,6 +258,9 @@ void main(void)
     totalWeight = max(totalWeight, 1e-12);
 
     gl_FragColor = vec4(inputColor.rgb + albedo * (totalIrradiance / totalWeight), 1.);
-
+    #ifndef LINEAR_OUTPUT
+        gl_FragColor.rgb = toGammaSpace(gl_FragColor.rgb);
+        gl_FragColor.rgb = saturate(gl_FragColor.rgb);
+    #endif
 	// gl_FragColor = mix(texture2D(textureSampler, vUV), centerIrradiance, 0.5);
 }

+ 2 - 2
src/scene.ts

@@ -291,7 +291,7 @@ export class Scene extends AbstractScene implements IAnimatable {
     public highDefinitionMRT: MultiRenderTarget;
     private mrtTypes = [
         Constants.TEXTURETYPE_UNSIGNED_INT, // Original color
-        Constants.TEXTURETYPE_UNSIGNED_INT, // Irradiance
+        Constants.TEXTURETYPE_FLOAT, // Irradiance
         Constants.TEXTURETYPE_FLOAT, // Depth (world units)
         Constants.TEXTURETYPE_UNSIGNED_INT
     ];
@@ -1484,7 +1484,7 @@ export class Scene extends AbstractScene implements IAnimatable {
 
         this.highDefinitionMRT = new MultiRenderTarget("sceneHighDefinitionMRT", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, this.mrtCount, this,
             { generateMipMaps: false, generateDepthTexture: true, defaultType: Constants.TEXTURETYPE_UNSIGNED_INT, types: this.mrtTypes });
-        this.highDefinitionMRT.samples = 8;
+        this.highDefinitionMRT.samples = 1;
 
         let gl = this._engine._gl;
         this.multiRenderAttachments = [gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1, gl.COLOR_ATTACHMENT2, gl.COLOR_ATTACHMENT3];