mirror of
https://github.com/photonstorm/phaser
synced 2025-02-16 22:18:29 +00:00
New Mesh Shader
This commit is contained in:
parent
9a1b252a11
commit
307cbe130f
4 changed files with 142 additions and 0 deletions
45
src/renderer/webgl/shaders/Mesh-frag.js
Normal file
45
src/renderer/webgl/shaders/Mesh-frag.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
module.exports = [
|
||||
'#define SHADER_NAME PHASER_MESH_FS',
|
||||
'',
|
||||
'precision mediump float;',
|
||||
'',
|
||||
'uniform vec3 uLightPosition;',
|
||||
'uniform vec3 uLightAmbient;',
|
||||
'uniform vec3 uLightDiffuse;',
|
||||
'uniform vec3 uLightSpecular;',
|
||||
'',
|
||||
'uniform vec3 uMaterialAmbient;',
|
||||
'uniform vec3 uMaterialDiffuse;',
|
||||
'uniform vec3 uMaterialSpecular;',
|
||||
'uniform float uMaterialShine;',
|
||||
'',
|
||||
'uniform vec3 uCameraPosition;',
|
||||
'',
|
||||
'uniform sampler2D uTexture;',
|
||||
'',
|
||||
'varying vec2 vTextureCoord;',
|
||||
'varying vec3 vNormal;',
|
||||
'varying vec3 vPosition;',
|
||||
'',
|
||||
'void main (void)',
|
||||
'{',
|
||||
' vec4 color = texture2D(uTexture, vTextureCoord);',
|
||||
'',
|
||||
' vec3 ambient = uLightAmbient * uMaterialAmbient;',
|
||||
'',
|
||||
' vec3 norm = normalize(vNormal);',
|
||||
' vec3 lightDir = normalize(uLightPosition - vPosition);',
|
||||
' float diff = max(dot(norm, lightDir), 0.0);',
|
||||
' vec3 diffuse = uLightDiffuse * (diff * uMaterialDiffuse);',
|
||||
'',
|
||||
' vec3 viewDir = normalize(uCameraPosition - vPosition);',
|
||||
' vec3 reflectDir = reflect(-lightDir, norm);',
|
||||
' float spec = pow(max(dot(viewDir, reflectDir), 0.0), uMaterialShine);',
|
||||
' vec3 specular = uLightSpecular * (spec * uMaterialSpecular);',
|
||||
'',
|
||||
' vec3 result = (ambient + diffuse + specular) * color.rgb;',
|
||||
'',
|
||||
' gl_FragColor = vec4(result, color.a);',
|
||||
'}',
|
||||
''
|
||||
].join('\n');
|
29
src/renderer/webgl/shaders/Mesh-vert.js
Normal file
29
src/renderer/webgl/shaders/Mesh-vert.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
module.exports = [
|
||||
'#define SHADER_NAME PHASER_MESH_VS',
|
||||
'',
|
||||
'precision mediump float;',
|
||||
'',
|
||||
'attribute vec3 aVertexPosition;',
|
||||
'attribute vec3 aVertexNormal;',
|
||||
'attribute vec2 aTextureCoord;',
|
||||
'',
|
||||
'uniform mat4 uViewProjectionMatrix;',
|
||||
'uniform mat4 uModelMatrix;',
|
||||
'uniform mat4 uNormalMatrix;',
|
||||
'',
|
||||
'varying vec2 vTextureCoord;',
|
||||
'varying vec3 vNormal;',
|
||||
'varying vec3 vPosition;',
|
||||
'',
|
||||
'void main ()',
|
||||
'{',
|
||||
' vTextureCoord = aTextureCoord;',
|
||||
'',
|
||||
' vPosition = vec3(uModelMatrix * vec4(aVertexPosition, 1.0));',
|
||||
'',
|
||||
' vNormal = vec3(uNormalMatrix * vec4(aVertexNormal, 1.0));',
|
||||
'',
|
||||
' gl_Position = uViewProjectionMatrix * uModelMatrix * vec4(aVertexPosition, 1.0);',
|
||||
'}',
|
||||
''
|
||||
].join('\n');
|
42
src/renderer/webgl/shaders/src/Mesh.frag
Normal file
42
src/renderer/webgl/shaders/src/Mesh.frag
Normal file
|
@ -0,0 +1,42 @@
|
|||
#define SHADER_NAME PHASER_MESH_FS
|
||||
|
||||
precision mediump float;
|
||||
|
||||
uniform vec3 uLightPosition;
|
||||
uniform vec3 uLightAmbient;
|
||||
uniform vec3 uLightDiffuse;
|
||||
uniform vec3 uLightSpecular;
|
||||
|
||||
uniform vec3 uMaterialAmbient;
|
||||
uniform vec3 uMaterialDiffuse;
|
||||
uniform vec3 uMaterialSpecular;
|
||||
uniform float uMaterialShine;
|
||||
|
||||
uniform vec3 uCameraPosition;
|
||||
|
||||
uniform sampler2D uTexture;
|
||||
|
||||
varying vec2 vTextureCoord;
|
||||
varying vec3 vNormal;
|
||||
varying vec3 vPosition;
|
||||
|
||||
void main (void)
|
||||
{
|
||||
vec4 color = texture2D(uTexture, vTextureCoord);
|
||||
|
||||
vec3 ambient = uLightAmbient * uMaterialAmbient;
|
||||
|
||||
vec3 norm = normalize(vNormal);
|
||||
vec3 lightDir = normalize(uLightPosition - vPosition);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = uLightDiffuse * (diff * uMaterialDiffuse);
|
||||
|
||||
vec3 viewDir = normalize(uCameraPosition - vPosition);
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), uMaterialShine);
|
||||
vec3 specular = uLightSpecular * (spec * uMaterialSpecular);
|
||||
|
||||
vec3 result = (ambient + diffuse + specular) * color.rgb;
|
||||
|
||||
gl_FragColor = vec4(result, color.a);
|
||||
}
|
26
src/renderer/webgl/shaders/src/Mesh.vert
Normal file
26
src/renderer/webgl/shaders/src/Mesh.vert
Normal file
|
@ -0,0 +1,26 @@
|
|||
#define SHADER_NAME PHASER_MESH_VS
|
||||
|
||||
precision mediump float;
|
||||
|
||||
attribute vec3 aVertexPosition;
|
||||
attribute vec3 aVertexNormal;
|
||||
attribute vec2 aTextureCoord;
|
||||
|
||||
uniform mat4 uViewProjectionMatrix;
|
||||
uniform mat4 uModelMatrix;
|
||||
uniform mat4 uNormalMatrix;
|
||||
|
||||
varying vec2 vTextureCoord;
|
||||
varying vec3 vNormal;
|
||||
varying vec3 vPosition;
|
||||
|
||||
void main ()
|
||||
{
|
||||
vTextureCoord = aTextureCoord;
|
||||
|
||||
vPosition = vec3(uModelMatrix * vec4(aVertexPosition, 1.0));
|
||||
|
||||
vNormal = vec3(uNormalMatrix * vec4(aVertexNormal, 1.0));
|
||||
|
||||
gl_Position = uViewProjectionMatrix * uModelMatrix * vec4(aVertexPosition, 1.0);
|
||||
}
|
Loading…
Add table
Reference in a new issue