mirror of
https://github.com/photonstorm/phaser
synced 2024-11-23 21:24:09 +00:00
Added default shader
This commit is contained in:
parent
27d6bd544f
commit
a0ef6e9d1b
2 changed files with 68 additions and 25 deletions
|
@ -31,8 +31,12 @@ var TransformMatrix = require('../components/TransformMatrix');
|
|||
* @extends Phaser.GameObjects.Components.Visible
|
||||
*
|
||||
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
|
||||
* @param {number} x - The horizontal position of this Game Object in the world.
|
||||
* @param {number} y - The vertical position of this Game Object in the world.
|
||||
* @param {number} [x=0] - The horizontal position of this Game Object in the world.
|
||||
* @param {number} [y=0] - The vertical position of this Game Object in the world.
|
||||
* @param {number} [width=128] - The width of the Game Object.
|
||||
* @param {number} [height=128] - The height of the Game Object.
|
||||
* @param {string} [fragSource] - The source code of the fragment shader.
|
||||
* @param {string} [vertSource] - The source code of the vertex shader.
|
||||
*/
|
||||
var Shader = new Class({
|
||||
|
||||
|
@ -52,13 +56,56 @@ var Shader = new Class({
|
|||
|
||||
initialize:
|
||||
|
||||
function Shader (scene, x, y, width, height, vert, frag)
|
||||
function Shader (scene, x, y, width, height, fragSource, vertSource)
|
||||
{
|
||||
if (x === undefined) { x = 0; }
|
||||
if (y === undefined) { y = 0; }
|
||||
if (width === undefined) { width = 128; }
|
||||
if (height === undefined) { height = 128; }
|
||||
|
||||
if (fragSource === undefined)
|
||||
{
|
||||
fragSource = [
|
||||
'precision mediump float;',
|
||||
|
||||
'uniform vec2 resolution;',
|
||||
|
||||
'varying vec2 fragCoord;',
|
||||
|
||||
'void main () {',
|
||||
' vec2 uv = fragCoord / resolution.xy;',
|
||||
' gl_FragColor = vec4(uv.xyx, 1.0);',
|
||||
'}'
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
if (vertSource === undefined)
|
||||
{
|
||||
vertSource = [
|
||||
'precision mediump float;',
|
||||
|
||||
'uniform mat4 uProjectionMatrix;',
|
||||
'uniform mat4 uViewMatrix;',
|
||||
|
||||
'attribute vec2 inPosition;',
|
||||
|
||||
'varying vec2 fragCoord;',
|
||||
|
||||
'void main () {',
|
||||
'gl_Position = uProjectionMatrix * uViewMatrix * vec4(inPosition, 1.0, 1.0);',
|
||||
'fragCoord = inPosition;',
|
||||
'}'
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
GameObject.call(this, scene, 'Shader');
|
||||
|
||||
// This Game Object cannot have a blend mode, so skip all checks
|
||||
this.blendMode = -1;
|
||||
|
||||
this.vertSource = vertSource;
|
||||
this.fragSource = fragSource;
|
||||
|
||||
this.vertexCount = 0;
|
||||
this.vertexCapacity = 6;
|
||||
|
||||
|
@ -214,13 +261,15 @@ var Shader = new Class({
|
|||
this.setSize(width, height);
|
||||
this.setOrigin(0.5, 0.5);
|
||||
|
||||
this.setShader(vert, frag);
|
||||
this.setShader(fragSource, vertSource);
|
||||
|
||||
this.projOrtho(0, renderer.width, renderer.height, 0);
|
||||
},
|
||||
|
||||
setShader: function (vertSource, fragSource)
|
||||
setShader: function (fragSource, vertSource)
|
||||
{
|
||||
if (vertSource === undefined) { vertSource = this.vertSource; }
|
||||
|
||||
var gl = this.gl;
|
||||
var renderer = this.renderer;
|
||||
|
||||
|
@ -235,6 +284,10 @@ var Shader = new Class({
|
|||
renderer.setMatrix4(program, 'uProjectionMatrix', false, this.projectionMatrix);
|
||||
|
||||
this.program = program;
|
||||
this.fragSource = fragSource;
|
||||
this.vertSource = vertSource;
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -12,33 +12,23 @@ var GameObjectFactory = require('../GameObjectFactory');
|
|||
*
|
||||
* Note: This method will only be available if the Shader Game Object and WebGL support have been built into Phaser.
|
||||
*
|
||||
* @method Phaser.GameObjects.GameObjectFactory#mesh
|
||||
* @method Phaser.GameObjects.GameObjectFactory#shader
|
||||
* @webglOnly
|
||||
* @since 3.0.0
|
||||
* @since 3.17.0
|
||||
*
|
||||
* @param {number} x - The horizontal position of this Game Object in the world.
|
||||
* @param {number} y - The vertical position of this Game Object in the world.
|
||||
* @param {number[]} vertices - An array containing the vertices data for this Shader.
|
||||
* @param {number[]} uv - An array containing the uv data for this Shader.
|
||||
* @param {number[]} colors - An array containing the color data for this Shader.
|
||||
* @param {number[]} alphas - An array containing the alpha data for this Shader.
|
||||
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
|
||||
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
|
||||
* @param {number} [x=0] - The horizontal position of this Game Object in the world.
|
||||
* @param {number} [y=0] - The vertical position of this Game Object in the world.
|
||||
* @param {number} [width=128] - The width of the Game Object.
|
||||
* @param {number} [height=128] - The height of the Game Object.
|
||||
* @param {string} [fragSource] - The source code of the fragment shader.
|
||||
* @param {string} [vertSource] - The source code of the vertex shader.
|
||||
*
|
||||
* @return {Phaser.GameObjects.Shader} The Game Object that was created.
|
||||
*/
|
||||
if (typeof WEBGL_RENDERER)
|
||||
{
|
||||
GameObjectFactory.register('shader', function (x, y, width, height, vert, frag)
|
||||
GameObjectFactory.register('shader', function (x, y, width, height, fragSource, vertSource)
|
||||
{
|
||||
return this.displayList.add(new Shader(this.scene, x, y, width, height, vert, frag));
|
||||
return this.displayList.add(new Shader(this.scene, x, y, width, height, fragSource, vertSource));
|
||||
});
|
||||
}
|
||||
|
||||
// When registering a factory function 'this' refers to the GameObjectFactory context.
|
||||
//
|
||||
// There are several properties available to use:
|
||||
//
|
||||
// this.scene - a reference to the Scene that owns the GameObjectFactory
|
||||
// this.displayList - a reference to the Display List the Scene owns
|
||||
// this.updateList - a reference to the Update List the Scene owns
|
||||
|
|
Loading…
Reference in a new issue