77
88var Class = require ( '../../../utils/Class' ) ;
99var GetFastValue = require ( '../../../utils/object/GetFastValue' ) ;
10+ var LightShaderSourceFS = require ( '../shaders/Light-frag.js' ) ;
1011var PointLightShaderSourceFS = require ( '../shaders/PointLight-frag.js' ) ;
1112var PointLightShaderSourceVS = require ( '../shaders/PointLight-vert.js' ) ;
1213var TransformMatrix = require ( '../../../gameobjects/components/TransformMatrix' ) ;
@@ -73,11 +74,22 @@ var LightPipeline = new Class({
7374 {
7475 LIGHT_COUNT = config . game . renderer . config . maxLights ;
7576
76- // var fragmentShaderSource = GetFastValue(config, 'fragShader', ShaderSourceFS);
77- // config.fragShader = fragmentShaderSource.replace('%LIGHT_COUNT%', LIGHT_COUNT.toString());
77+ config . shaders = GetFastValue ( config , 'shaders' , [
78+ {
79+ name : 'PointLight' ,
80+ fragShader : PointLightShaderSourceFS ,
81+ vertShader : PointLightShaderSourceVS ,
82+ uniforms : [
83+ 'uProjectionMatrix' ,
84+ 'uResolution'
85+ ]
86+ } ,
87+ {
88+ name : 'Light' ,
89+ fragShader : LightShaderSourceFS . replace ( '%LIGHT_COUNT%' , LIGHT_COUNT . toString ( ) )
90+ }
91+ ] ) ;
7892
79- config . fragShader = GetFastValue ( config , 'fragShader' , PointLightShaderSourceFS ) ;
80- config . vertShader = GetFastValue ( config , 'vertShader' , PointLightShaderSourceVS ) ;
8193 config . attributes = GetFastValue ( config , 'attributes' , [
8294 {
8395 name : 'inPosition' ,
@@ -100,42 +112,35 @@ var LightPipeline = new Class({
100112 type : WEBGL_CONST . FLOAT
101113 }
102114 ] ) ;
103- config . uniforms = GetFastValue ( config , 'uniforms' , [
104- 'uProjectionMatrix' ,
105- 'uResolution'
106- ] ) ;
107115
108116 WebGLPipeline . call ( this , config ) ;
109117
110118 /**
111119 * A temporary Transform Matrix, re-used internally during batching.
112120 *
113- * @name Phaser.Renderer.WebGL.Pipelines.MultiPipeline#_tempMatrix1
114- * @private
121+ * @name Phaser.Renderer.WebGL.Pipelines.MultiPipeline#tempMatrix1
115122 * @type {Phaser.GameObjects.Components.TransformMatrix }
116123 * @since 3.11.0
117124 */
118- this . _tempMatrix1 = new TransformMatrix ( ) ;
125+ this . tempMatrix1 = new TransformMatrix ( ) ;
119126
120127 /**
121128 * A temporary Transform Matrix, re-used internally during batching.
122129 *
123- * @name Phaser.Renderer.WebGL.Pipelines.MultiPipeline#_tempMatrix2
124- * @private
130+ * @name Phaser.Renderer.WebGL.Pipelines.MultiPipeline#tempMatrix2
125131 * @type {Phaser.GameObjects.Components.TransformMatrix }
126132 * @since 3.11.0
127133 */
128- this . _tempMatrix2 = new TransformMatrix ( ) ;
134+ this . tempMatrix2 = new TransformMatrix ( ) ;
129135
130136 /**
131137 * A temporary Transform Matrix, re-used internally during batching.
132138 *
133- * @name Phaser.Renderer.WebGL.Pipelines.MultiPipeline#_tempMatrix3
134- * @private
139+ * @name Phaser.Renderer.WebGL.Pipelines.MultiPipeline#tempMatrix3
135140 * @type {Phaser.GameObjects.Components.TransformMatrix }
136141 * @since 3.11.0
137142 */
138- this . _tempMatrix3 = new TransformMatrix ( ) ;
143+ this . tempMatrix3 = new TransformMatrix ( ) ;
139144
140145 /**
141146 * Inverse rotation matrix for normal map rotations.
@@ -171,6 +176,9 @@ var LightPipeline = new Class({
171176 this . lightCount = 0 ;
172177
173178 this . forceZero = true ;
179+
180+ this . pointLightShader ;
181+ this . lightShader ;
174182 } ,
175183
176184 /**
@@ -179,7 +187,7 @@ var LightPipeline = new Class({
179187 * By this stage all Game level systems are now in place and you can perform any final
180188 * tasks that the pipeline may need that relied on game systems such as the Texture Manager.
181189 *
182- * @method Phaser.Renderer.WebGL.LightPipeline#boot
190+ * @method Phaser.Renderer.WebGL.Pipelines. LightPipeline#boot
183191 * @since 3.11.0
184192 */
185193 boot : function ( )
@@ -198,31 +206,38 @@ var LightPipeline = new Class({
198206
199207 this . defaultNormalMap = { glTexture : tempTexture } ;
200208
201- return this ;
209+ this . pointLightShader = this . shaders [ 0 ] ;
210+ this . lightShader = this . shaders [ 1 ] ;
202211 } ,
203212
204- batchLight : function ( light , camera , x0 , y0 , x1 , y1 , x2 , y2 , x3 , y3 , lightX , lightY )
213+ /**
214+ * Adds a single vertex to the current vertex buffer and increments the
215+ * `vertexCount` property by 1.
216+ *
217+ * This method is called directly by `batchTri` and `batchQuad`.
218+ *
219+ * It does not perform any batch limit checking itself, so if you need to call
220+ * this method directly, do so in the same way that `batchQuad` does, for example.
221+ *
222+ * @method Phaser.Renderer.WebGL.Pipelines.LightPipeline#batchVert
223+ * @since 3.50.0
224+ *
225+ * @param {number } x - The vertex x position.
226+ * @param {number } y - The vertex y position.
227+ * @param {number } u - UV u value.
228+ * @param {number } v - UV v value.
229+ * @param {integer } unit - Texture unit to which the texture needs to be bound.
230+ * @param {(number|boolean) } tintEffect - The tint effect for the shader to use.
231+ * @param {number } tint - The tint color value.
232+ */
233+ batchVert : function ( x , y , lightX , lightY , radius , r , g , b , a )
205234 {
206- var color = light . color ;
207- var intensity = light . intensity ;
208- var radius = light . radius ;
209-
210- var r = color . r * intensity ;
211- var g = color . g * intensity ;
212- var b = color . b * intensity ;
213- var a = camera . alpha * light . alpha ;
214-
215- if ( this . shouldFlush ( 6 ) )
216- {
217- this . flush ( ) ;
218- }
219-
220235 var vertexViewF32 = this . vertexViewF32 ;
221236
222237 var vertexOffset = ( this . vertexCount * this . currentShader . vertexComponentCount ) - 1 ;
223238
224- vertexViewF32 [ ++ vertexOffset ] = x0 ;
225- vertexViewF32 [ ++ vertexOffset ] = y0 ;
239+ vertexViewF32 [ ++ vertexOffset ] = x ;
240+ vertexViewF32 [ ++ vertexOffset ] = y ;
226241 vertexViewF32 [ ++ vertexOffset ] = lightX ;
227242 vertexViewF32 [ ++ vertexOffset ] = lightY ;
228243 vertexViewF32 [ ++ vertexOffset ] = radius ;
@@ -231,57 +246,31 @@ var LightPipeline = new Class({
231246 vertexViewF32 [ ++ vertexOffset ] = b ;
232247 vertexViewF32 [ ++ vertexOffset ] = a ;
233248
234- vertexViewF32 [ ++ vertexOffset ] = x1 ;
235- vertexViewF32 [ ++ vertexOffset ] = y1 ;
236- vertexViewF32 [ ++ vertexOffset ] = lightX ;
237- vertexViewF32 [ ++ vertexOffset ] = lightY ;
238- vertexViewF32 [ ++ vertexOffset ] = radius ;
239- vertexViewF32 [ ++ vertexOffset ] = r ;
240- vertexViewF32 [ ++ vertexOffset ] = g ;
241- vertexViewF32 [ ++ vertexOffset ] = b ;
242- vertexViewF32 [ ++ vertexOffset ] = a ;
243-
244- vertexViewF32 [ ++ vertexOffset ] = x2 ;
245- vertexViewF32 [ ++ vertexOffset ] = y2 ;
246- vertexViewF32 [ ++ vertexOffset ] = lightX ;
247- vertexViewF32 [ ++ vertexOffset ] = lightY ;
248- vertexViewF32 [ ++ vertexOffset ] = radius ;
249- vertexViewF32 [ ++ vertexOffset ] = r ;
250- vertexViewF32 [ ++ vertexOffset ] = g ;
251- vertexViewF32 [ ++ vertexOffset ] = b ;
252- vertexViewF32 [ ++ vertexOffset ] = a ;
249+ this . vertexCount ++ ;
250+ } ,
253251
254- vertexViewF32 [ ++ vertexOffset ] = x0 ;
255- vertexViewF32 [ ++ vertexOffset ] = y0 ;
256- vertexViewF32 [ ++ vertexOffset ] = lightX ;
257- vertexViewF32 [ ++ vertexOffset ] = lightY ;
258- vertexViewF32 [ ++ vertexOffset ] = radius ;
259- vertexViewF32 [ ++ vertexOffset ] = r ;
260- vertexViewF32 [ ++ vertexOffset ] = g ;
261- vertexViewF32 [ ++ vertexOffset ] = b ;
262- vertexViewF32 [ ++ vertexOffset ] = a ;
252+ batchLight : function ( light , camera , x0 , y0 , x1 , y1 , x2 , y2 , x3 , y3 , lightX , lightY )
253+ {
254+ var color = light . color ;
255+ var intensity = light . intensity ;
256+ var radius = light . radius ;
263257
264- vertexViewF32 [ ++ vertexOffset ] = x2 ;
265- vertexViewF32 [ ++ vertexOffset ] = y2 ;
266- vertexViewF32 [ ++ vertexOffset ] = lightX ;
267- vertexViewF32 [ ++ vertexOffset ] = lightY ;
268- vertexViewF32 [ ++ vertexOffset ] = radius ;
269- vertexViewF32 [ ++ vertexOffset ] = r ;
270- vertexViewF32 [ ++ vertexOffset ] = g ;
271- vertexViewF32 [ ++ vertexOffset ] = b ;
272- vertexViewF32 [ ++ vertexOffset ] = a ;
258+ var r = color . r * intensity ;
259+ var g = color . g * intensity ;
260+ var b = color . b * intensity ;
261+ var a = camera . alpha * light . alpha ;
273262
274- vertexViewF32 [ ++ vertexOffset ] = x3 ;
275- vertexViewF32 [ ++ vertexOffset ] = y3 ;
276- vertexViewF32 [ ++ vertexOffset ] = lightX ;
277- vertexViewF32 [ ++ vertexOffset ] = lightY ;
278- vertexViewF32 [ ++ vertexOffset ] = radius ;
279- vertexViewF32 [ ++ vertexOffset ] = r ;
280- vertexViewF32 [ ++ vertexOffset ] = g ;
281- vertexViewF32 [ ++ vertexOffset ] = b ;
282- vertexViewF32 [ ++ vertexOffset ] = a ;
263+ if ( this . shouldFlush ( 6 ) )
264+ {
265+ this . flush ( ) ;
266+ }
283267
284- this . vertexCount += 6 ;
268+ this . batchVert ( x0 , y0 , lightX , lightY , radius , r , g , b , a ) ;
269+ this . batchVert ( x1 , y1 , lightX , lightY , radius , r , g , b , a ) ;
270+ this . batchVert ( x2 , y2 , lightX , lightY , radius , r , g , b , a ) ;
271+ this . batchVert ( x0 , y0 , lightX , lightY , radius , r , g , b , a ) ;
272+ this . batchVert ( x2 , y2 , lightX , lightY , radius , r , g , b , a ) ;
273+ this . batchVert ( x3 , y3 , lightX , lightY , radius , r , g , b , a ) ;
285274 } ,
286275
287276 /**
@@ -301,67 +290,6 @@ var LightPipeline = new Class({
301290 this . set2f ( 'uResolution' , this . width / 2 , this . height / 2 ) ;
302291 } ,
303292
304- /**
305- * Uploads the vertex data and emits a draw call for the current batch of vertices.
306- *
307- * @method Phaser.Renderer.WebGL.Pipelines.MultiPipeline#flush
308- * @since 3.0.0
309- *
310- * @return {this } This WebGLPipeline instance.
311- flush: function ()
312- {
313- var gl = this.gl;
314- var vertexCount = this.vertexCount;
315- var vertexSize = this.vertexSize;
316-
317- if (vertexCount > 0)
318- {
319- if (vertexCount === this.vertexCapacity)
320- {
321- gl.bufferData(gl.ARRAY_BUFFER, this.vertexData, gl.DYNAMIC_DRAW);
322- }
323- else
324- {
325- gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.bytes.subarray(0, vertexCount * vertexSize));
326- }
327-
328- gl.drawArrays(this.topology, 0, vertexCount);
329-
330- this.vertexCount = 0;
331- }
332-
333- return this;
334- },
335- */
336-
337- /**
338- * Called every time the pipeline is bound by the renderer.
339- * Sets the shader program, vertex buffer and other resources.
340- * Should only be called when changing pipeline.
341- *
342- * @method Phaser.Renderer.WebGL.Pipelines.LightPipeline#bind
343- * @since 3.50.0
344- *
345- * @param {boolean } [reset=false] - Should the pipeline be fully re-bound after a renderer pipeline clear?
346- *
347- * @return {this } This WebGLPipeline instance.
348- bind: function (reset)
349- {
350- if (reset === undefined) { reset = false; }
351-
352- WebGLPipeline.prototype.bind.call(this, reset);
353-
354- // var renderer = this.renderer;
355- // var program = this.program;
356-
357- // renderer.setInt1(program, 'uMainSampler', 0);
358- // renderer.setInt1(program, 'uNormSampler', 1);
359- // renderer.setFloat2(program, 'uResolution', this.width / 2, this.height / 2);
360-
361- return this;
362- },
363- */
364-
365293 /**
366294 * This function sets all the needed resources for each camera pass.
367295 *
0 commit comments