1515 */
1616PIXI . NormalMapFilter = function ( texture )
1717{
18- PIXI . AbstractFilter . call ( this ) ;
19-
20- this . passes = [ this ] ;
21- texture . baseTexture . _powerOf2 = true ;
22-
23- // set the uniforms
24- this . uniforms = {
25- displacementMap : { type : 'sampler2D' , value :texture } ,
26- scale : { type : '2f' , value :{ x :15 , y :15 } } ,
27- offset : { type : '2f' , value :{ x :0 , y :0 } } ,
28- mapDimensions : { type : '2f' , value :{ x :1 , y :1 } } ,
29- dimensions : { type : '4f' , value :[ 0 , 0 , 0 , 0 ] } ,
30- // LightDir: {type: 'f3', value:[0, 1, 0]},
31- LightPos : { type : '3f' , value :[ 0 , 1 , 0 ] }
32- } ;
33-
34-
35- if ( texture . baseTexture . hasLoaded )
36- {
37- this . uniforms . mapDimensions . value . x = texture . width ;
38- this . uniforms . mapDimensions . value . y = texture . height ;
39- }
40- else
41- {
42- this . boundLoadedFunction = this . onTextureLoaded . bind ( this ) ;
43-
44- texture . baseTexture . on ( "loaded" , this . boundLoadedFunction ) ;
45- }
46-
47- this . fragmentSrc = [
48- "precision mediump float;" ,
49- "varying vec2 vTextureCoord;" ,
50- "varying float vColor;" ,
51- "uniform sampler2D displacementMap;" ,
52- "uniform sampler2D uSampler;" ,
53-
54- "uniform vec4 dimensions;" ,
55-
56- "const vec2 Resolution = vec2(1.0,1.0);" , //resolution of screen
57- "uniform vec3 LightPos;" , //light position, normalized
58- "const vec4 LightColor = vec4(1.0, 1.0, 1.0, 1.0);" , //light RGBA -- alpha is intensity
59- "const vec4 AmbientColor = vec4(1.0, 1.0, 1.0, 0.5);" , //ambient RGBA -- alpha is intensity
60- "const vec3 Falloff = vec3(0.0, 1.0, 0.2);" , //attenuation coefficients
61-
62- "uniform vec3 LightDir;" , //" = vec3(1.0, 0.0, 1.0);",
63-
64-
65- "uniform vec2 mapDimensions;" , // = vec2(256.0, 256.0);",
66-
67-
68- "void main(void) {" ,
69- "vec2 mapCords = vTextureCoord.xy;" ,
70-
71- "vec4 color = texture2D(uSampler, vTextureCoord.st);" ,
18+ PIXI . AbstractFilter . call ( this ) ;
19+
20+ this . passes = [ this ] ;
21+ texture . baseTexture . _powerOf2 = true ;
22+
23+ // set the uniforms
24+ this . uniforms = {
25+ displacementMap : { type : 'sampler2D' , value :texture } ,
26+ scale : { type : '2f' , value :{ x :15 , y :15 } } ,
27+ offset : { type : '2f' , value :{ x :0 , y :0 } } ,
28+ mapDimensions : { type : '2f' , value :{ x :1 , y :1 } } ,
29+ dimensions : { type : '4f' , value :[ 0 , 0 , 0 , 0 ] } ,
30+ // LightDir: {type: 'f3', value:[0, 1, 0]},
31+ LightPos : { type : '3f' , value :[ 0 , 1 , 0 ] }
32+ } ;
33+
34+
35+ if ( texture . baseTexture . hasLoaded )
36+ {
37+ this . uniforms . mapDimensions . value . x = texture . width ;
38+ this . uniforms . mapDimensions . value . y = texture . height ;
39+ }
40+ else
41+ {
42+ this . boundLoadedFunction = this . onTextureLoaded . bind ( this ) ;
43+
44+ texture . baseTexture . on ( "loaded" , this . boundLoadedFunction ) ;
45+ }
46+
47+ this . fragmentSrc = [
48+ "precision mediump float;" ,
49+ "varying vec2 vTextureCoord;" ,
50+ "varying float vColor;" ,
51+ "uniform sampler2D displacementMap;" ,
52+ "uniform sampler2D uSampler;" ,
53+
54+ "uniform vec4 dimensions;" ,
55+
56+ "const vec2 Resolution = vec2(1.0,1.0);" , //resolution of screen
57+ "uniform vec3 LightPos;" , //light position, normalized
58+ "const vec4 LightColor = vec4(1.0, 1.0, 1.0, 1.0);" , //light RGBA -- alpha is intensity
59+ "const vec4 AmbientColor = vec4(1.0, 1.0, 1.0, 0.5);" , //ambient RGBA -- alpha is intensity
60+ "const vec3 Falloff = vec3(0.0, 1.0, 0.2);" , //attenuation coefficients
61+
62+ "uniform vec3 LightDir;" , //" = vec3(1.0, 0.0, 1.0);",
63+
64+
65+ "uniform vec2 mapDimensions;" , // = vec2(256.0, 256.0);",
66+
67+
68+ "void main(void) {" ,
69+ "vec2 mapCords = vTextureCoord.xy;" ,
70+
71+ "vec4 color = texture2D(uSampler, vTextureCoord.st);" ,
7272 "vec3 nColor = texture2D(displacementMap, vTextureCoord.st).rgb;" ,
7373
7474
75- "mapCords *= vec2(dimensions.x/512.0, dimensions.y/512.0);" ,
76-
77- "mapCords.y *= -1.0;" ,
78- "mapCords.y += 1.0;" ,
75+ "mapCords *= vec2(dimensions.x/512.0, dimensions.y/512.0);" ,
76+
77+ "mapCords.y *= -1.0;" ,
78+ "mapCords.y += 1.0;" ,
7979
80- //RGBA of our diffuse color
81- "vec4 DiffuseColor = texture2D(uSampler, vTextureCoord);" ,
80+ //RGBA of our diffuse color
81+ "vec4 DiffuseColor = texture2D(uSampler, vTextureCoord);" ,
8282
83- //RGB of our normal map
84- "vec3 NormalMap = texture2D(displacementMap, mapCords).rgb;" ,
83+ //RGB of our normal map
84+ "vec3 NormalMap = texture2D(displacementMap, mapCords).rgb;" ,
8585
86- //The delta position of light
87- //"vec3 LightDir = vec3(LightPos.xy - (gl_FragCoord.xy / Resolution.xy), LightPos.z);",
88- "vec3 LightDir = vec3(LightPos.xy - (mapCords.xy), LightPos.z);" ,
89- //Correct for aspect ratio
90- //"LightDir.x *= Resolution.x / Resolution.y;",
86+ //The delta position of light
87+ //"vec3 LightDir = vec3(LightPos.xy - (gl_FragCoord.xy / Resolution.xy), LightPos.z);",
88+ "vec3 LightDir = vec3(LightPos.xy - (mapCords.xy), LightPos.z);" ,
89+ //Correct for aspect ratio
90+ //"LightDir.x *= Resolution.x / Resolution.y;",
9191
92- //Determine distance (used for attenuation) BEFORE we normalize our LightDir
93- "float D = length(LightDir);" ,
92+ //Determine distance (used for attenuation) BEFORE we normalize our LightDir
93+ "float D = length(LightDir);" ,
9494
95- //normalize our vectors
96- "vec3 N = normalize(NormalMap * 2.0 - 1.0);" ,
97- "vec3 L = normalize(LightDir);" ,
95+ //normalize our vectors
96+ "vec3 N = normalize(NormalMap * 2.0 - 1.0);" ,
97+ "vec3 L = normalize(LightDir);" ,
9898
99- //Pre-multiply light color with intensity
100- //Then perform "N dot L" to determine our diffuse term
101- "vec3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N, L), 0.0);" ,
99+ //Pre-multiply light color with intensity
100+ //Then perform "N dot L" to determine our diffuse term
101+ "vec3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N, L), 0.0);" ,
102102
103- //pre-multiply ambient color with intensity
104- "vec3 Ambient = AmbientColor.rgb * AmbientColor.a;" ,
103+ //pre-multiply ambient color with intensity
104+ "vec3 Ambient = AmbientColor.rgb * AmbientColor.a;" ,
105105
106- //calculate attenuation
107- "float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) );" ,
106+ //calculate attenuation
107+ "float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) );" ,
108108
109- //the calculation which brings it all together
110- "vec3 Intensity = Ambient + Diffuse * Attenuation;" ,
111- "vec3 FinalColor = DiffuseColor.rgb * Intensity;" ,
112- "gl_FragColor = vColor * vec4(FinalColor, DiffuseColor.a);" ,
113- //"gl_FragColor = vec4(1.0, 0.0, 0.0, Attenuation);",//vColor * vec4(FinalColor, DiffuseColor.a);",
114- /*
115- // normalise color
116- "vec3 normal = normalize(nColor * 2.0 - 1.0);",
117-
118- "vec3 deltaPos = vec3( (light.xy - gl_FragCoord.xy) / resolution.xy, light.z );",
109+ //the calculation which brings it all together
110+ "vec3 Intensity = Ambient + Diffuse * Attenuation;" ,
111+ "vec3 FinalColor = DiffuseColor.rgb * Intensity;" ,
112+ "gl_FragColor = vColor * vec4(FinalColor, DiffuseColor.a);" ,
113+ //"gl_FragColor = vec4(1.0, 0.0, 0.0, Attenuation);",//vColor * vec4(FinalColor, DiffuseColor.a);",
114+ /*
115+ // normalise color
116+ "vec3 normal = normalize(nColor * 2.0 - 1.0);",
117+
118+ "vec3 deltaPos = vec3( (light.xy - gl_FragCoord.xy) / resolution.xy, light.z );",
119119
120- "float lambert = clamp(dot(normal, lightDir), 0.0, 1.0);",
120+ "float lambert = clamp(dot(normal, lightDir), 0.0, 1.0);",
121121
122- "float d = sqrt(dot(deltaPos, deltaPos));",
122+ "float d = sqrt(dot(deltaPos, deltaPos));",
123123 "float att = 1.0 / ( attenuation.x + (attenuation.y*d) + (attenuation.z*d*d) );",
124124
125125 "vec3 result = (ambientColor * ambientIntensity) + (lightColor.rgb * lambert) * att;",
126126 "result *= color.rgb;",
127127
128128 "gl_FragColor = vec4(result, 1.0);",*/
129129
130-
130+
131131
132- "}"
133- ] ;
134-
135- }
132+ "}"
133+ ] ;
134+
135+ } ;
136136
137137PIXI . NormalMapFilter . prototype = Object . create ( PIXI . AbstractFilter . prototype ) ;
138138PIXI . NormalMapFilter . prototype . constructor = PIXI . NormalMapFilter ;
@@ -144,10 +144,10 @@ PIXI.NormalMapFilter.prototype.constructor = PIXI.NormalMapFilter;
144144 */
145145PIXI . NormalMapFilter . prototype . onTextureLoaded = function ( )
146146{
147- this . uniforms . mapDimensions . value . x = this . uniforms . displacementMap . value . width ;
148- this . uniforms . mapDimensions . value . y = this . uniforms . displacementMap . value . height ;
147+ this . uniforms . mapDimensions . value . x = this . uniforms . displacementMap . value . width ;
148+ this . uniforms . mapDimensions . value . y = this . uniforms . displacementMap . value . height ;
149149
150- this . uniforms . displacementMap . value . baseTexture . off ( "loaded" , this . boundLoadedFunction )
150+ this . uniforms . displacementMap . value . baseTexture . off ( "loaded" , this . boundLoadedFunction ) ;
151151} ;
152152
153153/**
@@ -161,7 +161,7 @@ Object.defineProperty(PIXI.NormalMapFilter.prototype, 'map', {
161161 return this . uniforms . displacementMap . value ;
162162 } ,
163163 set : function ( value ) {
164- this . uniforms . displacementMap . value = value ;
164+ this . uniforms . displacementMap . value = value ;
165165 }
166166} ) ;
167167
@@ -176,7 +176,7 @@ Object.defineProperty(PIXI.NormalMapFilter.prototype, 'scale', {
176176 return this . uniforms . scale . value ;
177177 } ,
178178 set : function ( value ) {
179- this . uniforms . scale . value = value ;
179+ this . uniforms . scale . value = value ;
180180 }
181181} ) ;
182182
@@ -191,6 +191,6 @@ Object.defineProperty(PIXI.NormalMapFilter.prototype, 'offset', {
191191 return this . uniforms . offset . value ;
192192 } ,
193193 set : function ( value ) {
194- this . uniforms . offset . value = value ;
194+ this . uniforms . offset . value = value ;
195195 }
196196} ) ;
0 commit comments