77var Class = require ( '../../utils/Class' ) ;
88var Components = require ( '../components' ) ;
99var GameObject = require ( '../GameObject' ) ;
10+ var Merge = require ( '../../utils/object/Merge' ) ;
1011var ShaderRender = require ( './ShaderRender' ) ;
1112var TransformMatrix = require ( '../components/TransformMatrix' ) ;
1213
1314/**
1415 * @classdesc
1516 * A Shader Game Object.
17+ *
18+ * TODO:
19+ *
20+ * Test with sampler2D shader
21+ * Load v/f source from file
1622 *
1723 * @class Shader
1824 * @extends Phaser.GameObjects.GameObject
@@ -31,12 +37,11 @@ var TransformMatrix = require('../components/TransformMatrix');
3137 * @extends Phaser.GameObjects.Components.Visible
3238 *
3339 * @param {Phaser.Scene } scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
40+ * @param {string } key -
3441 * @param {number } [x=0] - The horizontal position of this Game Object in the world.
3542 * @param {number } [y=0] - The vertical position of this Game Object in the world.
3643 * @param {number } [width=128] - The width of the Game Object.
3744 * @param {number } [height=128] - The height of the Game Object.
38- * @param {string } [fragSource] - The source code of the fragment shader.
39- * @param {string } [vertSource] - The source code of the vertex shader.
4045 */
4146var Shader = new Class ( {
4247
@@ -56,48 +61,13 @@ var Shader = new Class({
5661
5762 initialize :
5863
59- function Shader ( scene , x , y , width , height , fragSource , vertSource , uniforms )
64+ function Shader ( scene , key , x , y , width , height )
6065 {
6166 if ( x === undefined ) { x = 0 ; }
6267 if ( y === undefined ) { y = 0 ; }
6368 if ( width === undefined ) { width = 128 ; }
6469 if ( height === undefined ) { height = 128 ; }
6570
66- if ( fragSource === undefined )
67- {
68- fragSource = [
69- 'precision mediump float;' ,
70-
71- 'uniform vec2 resolution;' ,
72-
73- 'varying vec2 fragCoord;' ,
74-
75- 'void main () {' ,
76- ' vec2 uv = fragCoord / resolution.xy;' ,
77- ' gl_FragColor = vec4(uv.xyx, 1.0);' ,
78- '}'
79- ] . join ( '\n' ) ;
80- }
81-
82- if ( vertSource === undefined )
83- {
84- vertSource = [
85- 'precision mediump float;' ,
86-
87- 'uniform mat4 uProjectionMatrix;' ,
88- 'uniform mat4 uViewMatrix;' ,
89-
90- 'attribute vec2 inPosition;' ,
91-
92- 'varying vec2 fragCoord;' ,
93-
94- 'void main () {' ,
95- 'gl_Position = uProjectionMatrix * uViewMatrix * vec4(inPosition, 1.0, 1.0);' ,
96- 'fragCoord = inPosition;' ,
97- '}'
98- ] . join ( '\n' ) ;
99- }
100-
10171 GameObject . call ( this , scene , 'Shader' ) ;
10272
10373 /**
@@ -111,22 +81,13 @@ var Shader = new Class({
11181 this . blendMode = - 1 ;
11282
11383 /**
114- * The source code, as a string, of the vertex shader being used.
115- *
116- * @name Phaser.GameObjects.Shader#vertSource
117- * @type {string }
118- * @since 3.17.0
119- */
120- this . vertSource = vertSource ;
121-
122- /**
123- * The source code, as a string, of the fragment shader being used.
84+ * The underlying shader object being used.
12485 *
125- * @name Phaser.GameObjects.Shader#fragSource
126- * @type {string }
86+ * @name Phaser.GameObjects.Shader#shader
87+ * @type {Phaser.Display.Shader }
12788 * @since 3.17.0
12889 */
129- this . fragSource = fragSource ;
90+ this . shader ;
13091
13192 var renderer = scene . sys . renderer ;
13293
@@ -244,8 +205,6 @@ var Shader = new Class({
244205 */
245206 this . projectionMatrix = new Float32Array ( [ 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 ] ) ;
246207
247- var d = new Date ( ) ;
248-
249208 /**
250209 * The default uniform mappings. These can be added to (or replaced) by specifying your own uniforms when
251210 * creating this shader game object. The uniforms are updated automatically during the render step.
@@ -261,29 +220,9 @@ var Shader = new Class({
261220 *
262221 * @name Phaser.GameObjects.Shader#uniforms
263222 * @type {any }
264- * @readonly
265223 * @since 3.17.0
266224 */
267- this . uniforms = {
268- resolution : { type : '2f' , value : { x : x , y : y } } ,
269- time : { type : '1f' , value : 0 } ,
270- mouse : { type : '2f' , value : { x : 0.0 , y : 0.0 } } ,
271- date : { type : '4fv' , value : [ d . getFullYear ( ) , d . getMonth ( ) , d . getDate ( ) , d . getHours ( ) * 60 * 60 + d . getMinutes ( ) * 60 + d . getSeconds ( ) ] } ,
272- sampleRate : { type : '1f' , value : 44100.0 } ,
273- iChannel0 : { type : 'sampler2D' , value : null , textureData : { repeat : true } } ,
274- iChannel1 : { type : 'sampler2D' , value : null , textureData : { repeat : true } } ,
275- iChannel2 : { type : 'sampler2D' , value : null , textureData : { repeat : true } } ,
276- iChannel3 : { type : 'sampler2D' , value : null , textureData : { repeat : true } }
277- } ;
278-
279- // Copy over / replace any passed in the constructor
280- if ( uniforms )
281- {
282- for ( var key in uniforms )
283- {
284- this . uniforms [ key ] = uniforms [ key ] ;
285- }
286- }
225+ this . uniforms = { } ;
287226
288227 /**
289228 * The pointer bound to this shader, if any.
@@ -357,7 +296,7 @@ var Shader = new Class({
357296 this . setPosition ( x , y ) ;
358297 this . setSize ( width , height ) ;
359298 this . setOrigin ( 0.5 , 0.5 ) ;
360- this . setShader ( fragSource , vertSource ) ;
299+ this . setShader ( key ) ;
361300 this . projOrtho ( 0 , renderer . width , renderer . height , 0 ) ;
362301 } ,
363302
@@ -369,14 +308,13 @@ var Shader = new Class({
369308 * @method Phaser.GameObjects.Shader#setShader
370309 * @since 3.17.0
371310 *
372- * @param {string } fragSource - The fragment shader source code.
373- * @param {string } [vertSource] - The vertex shader source code. If not given it will use the default vertex shader.
311+ * @param {string } key - The key of the shader stored in the shader cache to use.
374312 *
375313 * @return {this } This Shader instance.
376314 */
377- setShader : function ( fragSource , vertSource )
315+ setShader : function ( key )
378316 {
379- if ( vertSource === undefined ) { vertSource = this . vertSource ; }
317+ this . shader = this . scene . sys . cache . shader . get ( key ) ;
380318
381319 var gl = this . gl ;
382320 var renderer = this . renderer ;
@@ -386,14 +324,42 @@ var Shader = new Class({
386324 gl . deleteProgram ( this . program ) ;
387325 }
388326
389- var program = renderer . createProgram ( vertSource , fragSource ) ;
327+ var program = renderer . createProgram ( this . shader . vertexSrc , this . shader . fragmentSrc ) ;
390328
391329 renderer . setMatrix4 ( program , 'uViewMatrix' , false , this . viewMatrix ) ;
392330 renderer . setMatrix4 ( program , 'uProjectionMatrix' , false , this . projectionMatrix ) ;
393331
394332 this . program = program ;
395- this . fragSource = fragSource ;
396- this . vertSource = vertSource ;
333+
334+ var d = new Date ( ) ;
335+
336+ /*
337+ this.uniforms = Merge(this.shader.uniforms, {
338+ resolution: { type: '2f', value: { x: this.width, y: this.height }},
339+ time: { type: '1f', value: 0 },
340+ mouse: { type: '2f', value: { x: 0.0, y: 0.0 } },
341+ date: { type: '4fv', value: [ d.getFullYear(), d.getMonth(), d.getDate(), d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds() ] },
342+ sampleRate: { type: '1f', value: 44100.0 },
343+ iChannel0: { type: 'sampler2D', value: null, textureData: { repeat: true } },
344+ iChannel1: { type: 'sampler2D', value: null, textureData: { repeat: true } },
345+ iChannel2: { type: 'sampler2D', value: null, textureData: { repeat: true } },
346+ iChannel3: { type: 'sampler2D', value: null, textureData: { repeat: true } }
347+ });
348+ */
349+
350+ this . uniforms = {
351+ resolution : { type : '2f' , value : { x : this . width , y : this . height } } ,
352+ time : { type : '1f' , value : 0 } ,
353+ mouse : { type : '2f' , value : { x : 0.0 , y : 0.0 } } ,
354+ date : { type : '4fv' , value : [ d . getFullYear ( ) , d . getMonth ( ) , d . getDate ( ) , d . getHours ( ) * 60 * 60 + d . getMinutes ( ) * 60 + d . getSeconds ( ) ] } ,
355+ sampleRate : { type : '1f' , value : 44100.0 } ,
356+ iChannel0 : { type : 'sampler2D' , value : null , textureData : { repeat : true } } ,
357+ iChannel1 : { type : 'sampler2D' , value : null , textureData : { repeat : true } } ,
358+ iChannel2 : { type : 'sampler2D' , value : null , textureData : { repeat : true } } ,
359+ iChannel3 : { type : 'sampler2D' , value : null , textureData : { repeat : true } }
360+ } ;
361+
362+ console . log ( this . uniforms ) ;
397363
398364 this . initUniforms ( ) ;
399365
0 commit comments