Trevor Baron преди 7 години
родител
ревизия
4a8656e7d0

+ 2 - 7
dist/preview release/typedocValidationBaseline.json

@@ -1,7 +1,7 @@
 {
-  "errors": 7219,
+  "errors": 7220,
   "babylon.typedoc.json": {
-    "errors": 7219,
+    "errors": 7220,
     "AnimationKeyInterpolation": {
       "Enumeration": {
         "Comments": {
@@ -38928,11 +38928,6 @@
           "Comments": {
             "MissingText": true
           }
-        },
-        "GRAIN": {
-          "Naming": {
-            "NotCamelCase": true
-          }
         }
       }
     },

+ 0 - 2
src/Materials/Background/babylon.backgroundMaterial.ts

@@ -64,8 +64,6 @@
          */
         public NOISE = false;
 
-
-
         /**
          * is the reflection texture in BGR color scheme? 
          * Mainly used to solve a bug in ios10 video tag

+ 26 - 10
src/Materials/babylon.imageProcessingConfiguration.ts

@@ -220,23 +220,39 @@ module BABYLON {
         public vignetteCameraFov = 0.5;
 
         @serialize()
-        private _grainVarianceAmount = 0;
+        private _grainEnabled = false;
+
         /**
-         * Amount of grain to be applied by the grain effect.
+         * If the grain effect should be enabled.
          */
-        public get grainVarianceAmount(): number {
-            return this._grainVarianceAmount;
+        public get grainEnabled(): boolean {
+            return this._grainEnabled;
         }
-        public set grainVarianceAmount(value: number) {
-            if (this._grainVarianceAmount === value) {
+        public set grainEnabled(value: boolean) {
+            if (this._grainEnabled === value) {
                 return;
             }
 
-            this._grainVarianceAmount = value;
+            this._grainEnabled = value;
             this._updateParameters();
         }
 
         @serialize()
+        private _grainIntensity = 30;
+        /**
+         * Amount of grain to be applied by the grain effect.
+         */
+        public get grainIntensity(): number {
+            return this._grainIntensity;
+        }
+        public set grainIntensity(value: number) {
+            if (this._grainIntensity === value) {
+                return;
+            }
+            this._grainIntensity = value;
+        }
+
+        @serialize()
         private _grainAnimated = false;
 
         /**
@@ -426,7 +442,7 @@ module BABYLON {
             defines.SAMPLER3DBGRMAP = this.colorGradingBGR;
             defines.IMAGEPROCESSINGPOSTPROCESS = this.applyByPostProcess;
             defines.IMAGEPROCESSING = defines.VIGNETTE || defines.TONEMAPPING || defines.CONTRAST || defines.EXPOSURE || defines.COLORCURVES || defines.COLORGRADING;
-            defines.GRAIN = (this.grainVarianceAmount != 0.0);
+            defines.GRAIN = this.grainEnabled;
         }
 
         /**
@@ -485,8 +501,8 @@ module BABYLON {
                 );
             }
 
-            effect.setFloat("grainVarianceAmount", this.grainVarianceAmount);
-            effect.setFloat("grainAnimatedSeed", this.grainAnimated ? Math.random() : 1);
+            effect.setFloat("grainVarianceAmount", this.grainIntensity);
+            effect.setFloat("grainAnimatedSeed", this.grainAnimated ? Math.random() + 1 : 1);
         }
 
         /**

+ 1 - 0
src/PostProcess/babylon.imageProcessingPostProcess.ts

@@ -294,6 +294,7 @@
 
         /**
          * Defines cache preventing GC.
+         * @ignore
          */
         private _defines: IImageProcessingConfigurationDefines & { FROMLINEARSPACE: boolean } = {
             IMAGEPROCESSING: false,

+ 3 - 12
src/Shaders/ShadersInclude/helperFunctions.fx

@@ -70,18 +70,9 @@ float getRand(vec2 seed) {
 	return fract(sin(dot(seed.xy ,vec2(12.9898,78.233))) * 43758.5453);
 }
 
-vec3 dither(vec2 seed, vec3 color, float varianceAmount) {
+float dither(vec2 seed, vec3 color, float varianceAmount) {
 	float rand = getRand(seed);
-	float grain = mix(-varianceAmount/255.0, varianceAmount/255.0, rand);
+	float dither = mix(-varianceAmount/255.0, varianceAmount/255.0, rand);
 	
-	// Add less grain when luminance is high or low
-	float lum = getLuminance(color);	
-	if(lum > 0.5){
-		lum = 0.5-(lum-0.5);
-	}
-	float grainAmount = smoothstep(0., 1., lum/0.5);
-	color += grain * grainAmount;
-
-	color = max(color, 0.0);
-	return color;
+	return dither;
 }

+ 9 - 1
src/Shaders/ShadersInclude/imageProcessingFunctions.fx

@@ -82,7 +82,15 @@ vec4 applyImageProcessing(vec4 result) {
 #endif
 
 #ifdef GRAIN
-	result.rgb = dither(vUV*(grainAnimatedSeed+1.), result.rgb, grainVarianceAmount);
+	vec2 seed = vUV*(grainAnimatedSeed);
+	float grain = dither(seed, result.rgb, grainVarianceAmount);
+
+	// Add less grain when luminance is high or low
+	float lum = getLuminance(result.rgb);
+	float grainAmount = (cos(-PI + (lum*PI*2.))+1.)/2.;
+	result.rgb += grain * grainAmount;
+
+	result.rgb = max(result.rgb, 0.0);
 #endif
 
 	// Going back to gamma space

+ 2 - 1
src/Shaders/background.fragment.fx

@@ -289,7 +289,8 @@ vec4 color = vec4(finalColor, finalAlpha);
 
 #ifndef GRAIN // Do not apply noise multiple times
     #ifdef NOISE
-        color.rgb = dither(vPositionW.xy, color.rgb, 0.5);
+        color.rgb += dither(vPositionW.xy, color.rgb, 0.5);
+        color = max(color, 0.0);
     #endif
 #endif