mirror of
https://github.com/photonstorm/phaser
synced 2024-11-10 07:04:31 +00:00
Fix missing Graphics light shader.
This commit is contained in:
parent
6ec1bed898
commit
f6d71c32cc
2 changed files with 95 additions and 0 deletions
42
src/renderer/webgl/shaders/FlatLight-frag.js
Normal file
42
src/renderer/webgl/shaders/FlatLight-frag.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
module.exports = [
|
||||
'#define SHADER_NAME PHASER_LIGHT_FS',
|
||||
'precision mediump float;',
|
||||
'struct Light',
|
||||
'{',
|
||||
' vec2 position;',
|
||||
' vec3 color;',
|
||||
' float intensity;',
|
||||
' float radius;',
|
||||
'};',
|
||||
'const int kMaxLights = %LIGHT_COUNT%;',
|
||||
'const vec3 normal = vec3(0.0, 0.0, 1.0);',
|
||||
'uniform vec4 uCamera; /* x, y, rotation, zoom */',
|
||||
'uniform vec2 uResolution;',
|
||||
'uniform vec3 uAmbientLightColor;',
|
||||
'uniform Light uLights[kMaxLights];',
|
||||
'uniform int uLightCount;',
|
||||
'varying vec4 outTint;',
|
||||
'void main ()',
|
||||
'{',
|
||||
' vec3 finalColor = vec3(0.0, 0.0, 0.0);',
|
||||
' vec4 color = outTint;',
|
||||
' vec2 res = vec2(min(uResolution.x, uResolution.y)) * uCamera.w;',
|
||||
' for (int index = 0; index < kMaxLights; ++index)',
|
||||
' {',
|
||||
' if (index < uLightCount)',
|
||||
' {',
|
||||
' Light light = uLights[index];',
|
||||
' vec3 lightDir = vec3((light.position.xy / res) - (gl_FragCoord.xy / res), 0.1);',
|
||||
' vec3 lightNormal = normalize(lightDir);',
|
||||
' float distToSurf = length(lightDir) * uCamera.w;',
|
||||
' float diffuseFactor = max(dot(normal, lightNormal), 0.0);',
|
||||
' float radius = (light.radius / res.x * uCamera.w) * uCamera.w;',
|
||||
' float attenuation = clamp(1.0 - distToSurf * distToSurf / (radius * radius), 0.0, 1.0);',
|
||||
' vec3 diffuse = light.color * diffuseFactor;',
|
||||
' finalColor += (attenuation * diffuse) * light.intensity;',
|
||||
' }',
|
||||
' }',
|
||||
' vec4 colorOutput = vec4(uAmbientLightColor + finalColor, 1.0);',
|
||||
' gl_FragColor = color * vec4(colorOutput.rgb * colorOutput.a, colorOutput.a);',
|
||||
'}',
|
||||
].join('\n');
|
53
src/renderer/webgl/shaders/src/FlatLight.frag
Normal file
53
src/renderer/webgl/shaders/src/FlatLight.frag
Normal file
|
@ -0,0 +1,53 @@
|
|||
#define SHADER_NAME PHASER_LIGHT_FS
|
||||
|
||||
precision mediump float;
|
||||
|
||||
struct Light
|
||||
{
|
||||
vec2 position;
|
||||
vec3 color;
|
||||
float intensity;
|
||||
float radius;
|
||||
};
|
||||
|
||||
const int kMaxLights = %LIGHT_COUNT%;
|
||||
|
||||
// Constant normal for flat lighting
|
||||
const vec3 normal = vec3(0.0, 0.0, 1.0);
|
||||
|
||||
uniform vec4 uCamera; /* x, y, rotation, zoom */
|
||||
uniform vec2 uResolution;
|
||||
uniform vec3 uAmbientLightColor;
|
||||
uniform Light uLights[kMaxLights];
|
||||
uniform int uLightCount;
|
||||
|
||||
varying vec4 outTint;
|
||||
|
||||
void main ()
|
||||
{
|
||||
vec3 finalColor = vec3(0.0, 0.0, 0.0);
|
||||
|
||||
vec4 color = outTint;
|
||||
|
||||
vec2 res = vec2(min(uResolution.x, uResolution.y)) * uCamera.w;
|
||||
|
||||
for (int index = 0; index < kMaxLights; ++index)
|
||||
{
|
||||
if (index < uLightCount)
|
||||
{
|
||||
Light light = uLights[index];
|
||||
vec3 lightDir = vec3((light.position.xy / res) - (gl_FragCoord.xy / res), 0.1);
|
||||
vec3 lightNormal = normalize(lightDir);
|
||||
float distToSurf = length(lightDir) * uCamera.w;
|
||||
float diffuseFactor = max(dot(normal, lightNormal), 0.0);
|
||||
float radius = (light.radius / res.x * uCamera.w) * uCamera.w;
|
||||
float attenuation = clamp(1.0 - distToSurf * distToSurf / (radius * radius), 0.0, 1.0);
|
||||
vec3 diffuse = light.color * diffuseFactor;
|
||||
finalColor += (attenuation * diffuse) * light.intensity;
|
||||
}
|
||||
}
|
||||
|
||||
vec4 colorOutput = vec4(uAmbientLightColor + finalColor, 1.0);
|
||||
|
||||
gl_FragColor = color * vec4(colorOutput.rgb * colorOutput.a, colorOutput.a);
|
||||
}
|
Loading…
Reference in a new issue