@@ -9,6 +9,7 @@ var Components = require('../components');
99var GameObject = require ( '../GameObject' ) ;
1010var GetFastValue = require ( '../../utils/object/GetFastValue' ) ;
1111var Merge = require ( '../../utils/object/Merge' ) ;
12+ var SetValue = require ( '../../utils/object/SetValue' ) ;
1213var ShaderRender = require ( './ShaderRender' ) ;
1314var TransformMatrix = require ( '../components/TransformMatrix' ) ;
1415
@@ -251,46 +252,15 @@ var Shader = new Class({
251252 */
252253 this . _rendererHeight = renderer . height ;
253254
254- var gl = this . gl ;
255-
256255 /**
257- * Internal gl function mapping for uniform look-up.
258- * https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/uniform
256+ * Internal texture count tracker.
259257 *
260- * @name Phaser.GameObjects.Shader#_glFuncMap
261- * @type {any }
258+ * @name Phaser.GameObjects.Shader#_textureCount
259+ * @type {number }
262260 * @private
263261 * @since 3.17.0
264262 */
265- this . _glFuncMap = {
266-
267- mat2 : { func : gl . uniformMatrix2fv , length : 1 , matrix : true } ,
268- mat3 : { func : gl . uniformMatrix3fv , length : 1 , matrix : true } ,
269- mat4 : { func : gl . uniformMatrix4fv , length : 1 , matrix : true } ,
270-
271- '1f' : { func : gl . uniform1f , length : 1 } ,
272- '1fv' : { func : gl . uniform1fv , length : 1 } ,
273- '1i' : { func : gl . uniform1i , length : 1 } ,
274- '1iv' : { func : gl . uniform1iv , length : 1 } ,
275-
276- '2f' : { func : gl . uniform2f , length : 2 } ,
277- '2fv' : { func : gl . uniform2fv , length : 1 } ,
278- '2i' : { func : gl . uniform2i , length : 2 } ,
279- '2iv' : { func : gl . uniform2iv , length : 1 } ,
280-
281- '3f' : { func : gl . uniform3f , length : 3 } ,
282- '3fv' : { func : gl . uniform3fv , length : 1 } ,
283- '3i' : { func : gl . uniform3i , length : 3 } ,
284- '3iv' : { func : gl . uniform3iv , length : 1 } ,
285-
286- '4f' : { func : gl . uniform4f , length : 4 } ,
287- '4fv' : { func : gl . uniform4fv , length : 1 } ,
288- '4i' : { func : gl . uniform4i , length : 4 } ,
289- '4iv' : { func : gl . uniform4iv , length : 1 }
290-
291- } ;
292-
293- this . textureCount = 0 ;
263+ this . _textureCount = 0 ;
294264
295265 this . setPosition ( x , y ) ;
296266 this . setSize ( width , height ) ;
@@ -449,10 +419,10 @@ var Shader = new Class({
449419 initUniforms : function ( )
450420 {
451421 var gl = this . gl ;
452- var map = this . _glFuncMap ;
422+ var map = this . renderer . glFuncMap ;
453423 var program = this . program ;
454424
455- this . textureCount = 0 ;
425+ this . _textureCount = 0 ;
456426
457427 for ( var key in this . uniforms )
458428 {
@@ -472,18 +442,34 @@ var Shader = new Class({
472442 }
473443 } ,
474444
475- setSampler2D : function ( uniformKey , key , textureIndex , textureData )
445+ /**
446+ * Sets a sampler2D uniform on this shader.
447+ *
448+ * The textureKey given is the key from the Texture Manager cache. You cannot use a single frame
449+ * from a texture, only the full image. Also, lots of shaders expect textures to be power-of-two sized.
450+ *
451+ * @method Phaser.GameObjects.Shader#setSampler2D
452+ * @since 3.17.0
453+ *
454+ * @param {string } uniformKey - The key of the sampler2D uniform to be updated, i.e. `iChannel0`.
455+ * @param {string } textureKey - The key of the texture, as stored in the Texture Manager. Must already be loaded.
456+ * @param {integer } [textureIndex=0] - The texture index.
457+ * @param {any } [textureData] - Additional texture data.
458+ *
459+ * @return {this } This Shader instance.
460+ */
461+ setSampler2D : function ( uniformKey , textureKey , textureIndex , textureData )
476462 {
477463 if ( textureIndex === undefined ) { textureIndex = 0 ; }
478464
479465 var textureManager = this . scene . sys . textures ;
480466
481- if ( textureManager . exists ( key ) )
467+ if ( textureManager . exists ( textureKey ) )
482468 {
483- var frame = textureManager . getFrame ( key ) ;
469+ var frame = textureManager . getFrame ( textureKey ) ;
484470 var uniform = this . uniforms [ uniformKey ] ;
485471
486- uniform . textureKey = key ;
472+ uniform . textureKey = textureKey ;
487473 uniform . source = frame . source . image ;
488474 uniform . value = frame . glTexture ;
489475
@@ -492,34 +478,132 @@ var Shader = new Class({
492478 uniform . textureData = textureData ;
493479 }
494480
495- this . textureCount = textureIndex ;
481+ this . _textureCount = textureIndex ;
496482
497483 this . initSampler2D ( uniform ) ;
498484 }
499485
500486 return this ;
501487 } ,
502488
503- setChannel0 : function ( key , textureData )
489+ /**
490+ * Sets a property of a uniform already present on this shader.
491+ *
492+ * To modify the value of a uniform such as a 1f or 1i use the `value` property directly:
493+ *
494+ * ```javascript
495+ * shader.setUniform('size.value', 16);
496+ * ```
497+ *
498+ * You can use dot notation to access deeper values, for example:
499+ *
500+ * ```javascript
501+ * shader.setUniform('resolution.value.x', 512);
502+ * ```
503+ *
504+ * The change to the uniform will take effect the next time the shader is rendered.
505+ *
506+ * @method Phaser.GameObjects.Shader#setUniform
507+ * @since 3.17.0
508+ *
509+ * @param {string } key - The key of the uniform to modify. Use dots for deep properties, i.e. `resolution.value.x`.
510+ * @param {any } value - The value to set into the uniform.
511+ *
512+ * @return {this } This Shader instance.
513+ */
514+ setUniform : function ( key , value )
504515 {
505- return this . setSampler2D ( 'iChannel0' , key , 0 , textureData ) ;
516+ SetValue ( this . uniforms , key , value ) ;
517+
518+ return this ;
506519 } ,
507520
508- setChannel1 : function ( key , textureData )
521+ /**
522+ * A short-cut method that will directly set the texture being used by the `iChannel0` sampler2D uniform.
523+ *
524+ * The textureKey given is the key from the Texture Manager cache. You cannot use a single frame
525+ * from a texture, only the full image. Also, lots of shaders expect textures to be power-of-two sized.
526+ *
527+ * @method Phaser.GameObjects.Shader#setChannel0
528+ * @since 3.17.0
529+ *
530+ * @param {string } textureKey - The key of the texture, as stored in the Texture Manager. Must already be loaded.
531+ * @param {any } [textureData] - Additional texture data.
532+ *
533+ * @return {this } This Shader instance.
534+ */
535+ setChannel0 : function ( textureKey , textureData )
509536 {
510- return this . setSampler2D ( 'iChannel1 ' , key , 1 , textureData ) ;
537+ return this . setSampler2D ( 'iChannel0 ' , textureKey , 0 , textureData ) ;
511538 } ,
512539
513- setChannel2 : function ( key , textureData )
540+ /**
541+ * A short-cut method that will directly set the texture being used by the `iChannel1` sampler2D uniform.
542+ *
543+ * The textureKey given is the key from the Texture Manager cache. You cannot use a single frame
544+ * from a texture, only the full image. Also, lots of shaders expect textures to be power-of-two sized.
545+ *
546+ * @method Phaser.GameObjects.Shader#setChannel1
547+ * @since 3.17.0
548+ *
549+ * @param {string } textureKey - The key of the texture, as stored in the Texture Manager. Must already be loaded.
550+ * @param {any } [textureData] - Additional texture data.
551+ *
552+ * @return {this } This Shader instance.
553+ */
554+ setChannel1 : function ( textureKey , textureData )
555+ {
556+ return this . setSampler2D ( 'iChannel1' , textureKey , 1 , textureData ) ;
557+ } ,
558+
559+ /**
560+ * A short-cut method that will directly set the texture being used by the `iChannel2` sampler2D uniform.
561+ *
562+ * The textureKey given is the key from the Texture Manager cache. You cannot use a single frame
563+ * from a texture, only the full image. Also, lots of shaders expect textures to be power-of-two sized.
564+ *
565+ * @method Phaser.GameObjects.Shader#setChannel2
566+ * @since 3.17.0
567+ *
568+ * @param {string } textureKey - The key of the texture, as stored in the Texture Manager. Must already be loaded.
569+ * @param {any } [textureData] - Additional texture data.
570+ *
571+ * @return {this } This Shader instance.
572+ */
573+ setChannel2 : function ( textureKey , textureData )
514574 {
515- return this . setSampler2D ( 'iChannel2' , key , 2 , textureData ) ;
575+ return this . setSampler2D ( 'iChannel2' , textureKey , 2 , textureData ) ;
516576 } ,
517577
518- setChannel3 : function ( key , textureData )
578+ /**
579+ * A short-cut method that will directly set the texture being used by the `iChannel3` sampler2D uniform.
580+ *
581+ * The textureKey given is the key from the Texture Manager cache. You cannot use a single frame
582+ * from a texture, only the full image. Also, lots of shaders expect textures to be power-of-two sized.
583+ *
584+ * @method Phaser.GameObjects.Shader#setChannel3
585+ * @since 3.17.0
586+ *
587+ * @param {string } textureKey - The key of the texture, as stored in the Texture Manager. Must already be loaded.
588+ * @param {any } [textureData] - Additional texture data.
589+ *
590+ * @return {this } This Shader instance.
591+ */
592+ setChannel3 : function ( textureKey , textureData )
519593 {
520- return this . setSampler2D ( 'iChannel3' , key , 3 , textureData ) ;
594+ return this . setSampler2D ( 'iChannel3' , textureKey , 3 , textureData ) ;
521595 } ,
522596
597+ /**
598+ * Internal method that takes a sampler2D uniform and prepares it for use by setting the
599+ * gl texture parameters.
600+ *
601+ * @method Phaser.GameObjects.Shader#initSampler2D
602+ * @private
603+ * @since 3.17.0
604+ *
605+ * @param {any } uniform - The sampler2D uniform to process.
606+ */
523607 initSampler2D : function ( uniform )
524608 {
525609 if ( ! uniform . value )
@@ -529,7 +613,7 @@ var Shader = new Class({
529613
530614 var gl = this . gl ;
531615
532- gl . activeTexture ( gl . TEXTURE0 + this . textureCount ) ;
616+ gl . activeTexture ( gl . TEXTURE0 + this . _textureCount ) ;
533617 gl . bindTexture ( gl . TEXTURE_2D , uniform . value ) ;
534618
535619 // Extended texture data
@@ -539,13 +623,6 @@ var Shader = new Class({
539623 if ( data )
540624 {
541625 // https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texImage2D
542-
543- // GLTexture = mag linear, min linear_mipmap_linear, wrap repeat + gl.generateMipmap(gl.TEXTURE_2D);
544- // GLTextureLinear = mag/min linear, wrap clamp
545- // GLTextureNearestRepeat = mag/min NEAREST, wrap repeat
546- // GLTextureNearest = mag/min nearest, wrap clamp
547- // AudioTexture = whatever + luminance + width 512, height 2, border 0
548- // KeyTexture = whatever + luminance + width 256, height 2, border 0
549626
550627 // mag / minFilter can be: gl.LINEAR, gl.LINEAR_MIPMAP_LINEAR or gl.NEAREST
551628 // wrapS/T can be: gl.CLAMP_TO_EDGE or gl.REPEAT
@@ -588,9 +665,9 @@ var Shader = new Class({
588665
589666 this . renderer . setProgram ( this . program ) ;
590667
591- gl . uniform1i ( uniform . uniformLocation , this . textureCount ) ;
668+ gl . uniform1i ( uniform . uniformLocation , this . _textureCount ) ;
592669
593- this . textureCount ++ ;
670+ this . _textureCount ++ ;
594671 } ,
595672
596673 /**
@@ -803,7 +880,7 @@ var Shader = new Class({
803880 *
804881 * @method Phaser.GameObjects.RenderTexture#preDestroy
805882 * @protected
806- * @since 3.9 .0
883+ * @since 3.17 .0
807884 */
808885 preDestroy : function ( )
809886 {
0 commit comments