Преглед изворни кода

add centerPosition to control center of radial intensity, if direction is set to 0 the direction away from the center will be used

Trevor Baron пре 7 година
родитељ
комит
2db1f5235c

+ 2 - 1
src/PostProcess/RenderPipeline/Pipelines/babylon.lensRenderingPipeline.ts

@@ -180,7 +180,7 @@ module BABYLON {
         // colors shifting and distortion
         private _createChromaticAberrationPostProcess(ratio: number): void {
             this._chromaticAberrationPostProcess = new PostProcess("LensChromaticAberration", "chromaticAberration",
-                ["chromatic_aberration", "screen_width", "screen_height", "direction", "radialIntensity"],      // uniforms
+                ["chromatic_aberration", "screen_width", "screen_height", "direction", "radialIntensity", "centerPosition"],      // uniforms
                 [],                                         // samplers
                 ratio, null, Texture.TRILINEAR_SAMPLINGMODE,
                 this._scene.getEngine(), false);
@@ -191,6 +191,7 @@ module BABYLON {
                 effect.setFloat('screen_height', this._scene.getEngine().getRenderHeight());
                 effect.setFloat('radialIntensity', 1);
                 effect.setFloat2('direction', 17, 17);
+                effect.setFloat2('centerPosition', 0.5,0.5);
             };
         }
 

+ 8 - 2
src/PostProcess/babylon.chromaticAberrationPostProcess.ts

@@ -14,9 +14,14 @@ module BABYLON {
         radialIntensity = 0;
 
         /**
-         * The normilized direction in which the rgb channels should be seperated (default: Vector2(0.707,0.707))
+         * The normilized direction in which the rgb channels should be seperated. If set to 0,0 radial direction will be used. (default: Vector2(0.707,0.707))
          */
         direction = new Vector2(0.707,0.707);
+
+        /**
+         * The center position where the radialIntensity should be around. [0.5,0.5 is center of screen, 1,1 is top right corder] (default: Vector2(0.5 ,0.5))
+         */
+        centerPosition = new Vector2(0.5,0.5);
         
         /**
          * Creates a new instance of @see ChromaticAberrationPostProcess
@@ -31,13 +36,14 @@ module BABYLON {
          * @param textureType Type of textures used when performing the post process. (default: 0)
          */
         constructor(name: string, screenWidth:number, screenHeight:number, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Engine.TEXTURETYPE_UNSIGNED_INT) {
-            super(name, "chromaticAberration", ["chromatic_aberration", "screen_width", "screen_height", "direction", "radialIntensity"], [], options, camera, samplingMode, engine, reusable, null, textureType);
+            super(name, "chromaticAberration", ["chromatic_aberration", "screen_width", "screen_height", "direction", "radialIntensity", "centerPosition"], [], options, camera, samplingMode, engine, reusable, null, textureType);
             this.onApplyObservable.add((effect: Effect) => {
                 effect.setFloat('chromatic_aberration', this.aberrationAmount);
                 effect.setFloat('screen_width', screenWidth);
                 effect.setFloat('screen_height', screenHeight);
                 effect.setFloat('radialIntensity', this.radialIntensity);
                 effect.setFloat2('direction', this.direction.x,this.direction.y);
+                effect.setFloat2('centerPosition', this.centerPosition.x,this.centerPosition.y);
             })
         }
     }

+ 9 - 3
src/Shaders/chromaticAberration.fragment.fx

@@ -5,6 +5,7 @@ uniform sampler2D textureSampler;	// original color
 uniform float chromatic_aberration;
 uniform float radialIntensity;
 uniform vec2 direction;
+uniform vec2 centerPosition;
 uniform float screen_width;
 uniform float screen_height;
 
@@ -13,7 +14,12 @@ varying vec2 vUV;
 
 void main(void)
 {
-	vec2 centered_screen_pos = vec2(vUV.x - 0.5, vUV.y - 0.5);
+	vec2 centered_screen_pos = vec2(vUV.x - centerPosition.x, vUV.y - centerPosition.y);
+	vec2 directionOfEffect = direction;
+	if(directionOfEffect.x == 0. && directionOfEffect.y == 0.){
+		directionOfEffect = normalize(centered_screen_pos);
+	}
+
 	float radius2 = centered_screen_pos.x*centered_screen_pos.x
 		+ centered_screen_pos.y*centered_screen_pos.y;
 	float radius = sqrt(radius2);
@@ -22,8 +28,8 @@ void main(void)
 
 	//index of refraction of each color channel, causing chromatic dispersion
 	vec3 ref_indices = vec3(-0.3, 0.0, 0.3);
-	float ref_shiftX = chromatic_aberration * pow(radius, radialIntensity) * direction.x / screen_width;
-	float ref_shiftY = chromatic_aberration * pow(radius, radialIntensity) * direction.y / screen_height;
+	float ref_shiftX = chromatic_aberration * pow(radius, radialIntensity) * directionOfEffect.x / screen_width;
+	float ref_shiftY = chromatic_aberration * pow(radius, radialIntensity) * directionOfEffect.y / screen_height;
 
 	// shifts for red, green & blue
 	vec2 ref_coords_r = vec2(vUV.x + ref_indices.r*ref_shiftX, vUV.y + ref_indices.r*ref_shiftY*0.5);