2018-05-08 22:04:57 +00:00
|
|
|
module.exports = [
|
2020-08-25 12:23:59 +00:00
|
|
|
'#define SHADER_NAME PHASER_LIGHT_FS',
|
2018-05-08 22:04:57 +00:00
|
|
|
'',
|
|
|
|
'precision mediump float;',
|
|
|
|
'',
|
|
|
|
'struct Light',
|
|
|
|
'{',
|
|
|
|
' vec2 position;',
|
|
|
|
' vec3 color;',
|
|
|
|
' float intensity;',
|
|
|
|
' float radius;',
|
|
|
|
'};',
|
|
|
|
'',
|
|
|
|
'const int kMaxLights = %LIGHT_COUNT%;',
|
|
|
|
'',
|
|
|
|
'uniform vec4 uCamera; /* x, y, rotation, zoom */',
|
|
|
|
'uniform vec2 uResolution;',
|
|
|
|
'uniform sampler2D uMainSampler;',
|
|
|
|
'uniform sampler2D uNormSampler;',
|
|
|
|
'uniform vec3 uAmbientLightColor;',
|
|
|
|
'uniform Light uLights[kMaxLights];',
|
2018-10-31 00:03:34 +00:00
|
|
|
'uniform mat3 uInverseRotationMatrix;',
|
2018-05-08 22:04:57 +00:00
|
|
|
'',
|
|
|
|
'varying vec2 outTexCoord;',
|
|
|
|
'varying vec4 outTint;',
|
|
|
|
'',
|
|
|
|
'void main()',
|
|
|
|
'{',
|
|
|
|
' vec3 finalColor = vec3(0.0, 0.0, 0.0);',
|
2020-09-14 10:02:02 +00:00
|
|
|
' vec4 color = texture2D(uMainSampler, outTexCoord) * vec4(outTint.bgr * outTint.a, outTint.a);',
|
2018-05-08 22:04:57 +00:00
|
|
|
' vec3 normalMap = texture2D(uNormSampler, outTexCoord).rgb;',
|
2018-10-31 00:03:34 +00:00
|
|
|
' vec3 normal = normalize(uInverseRotationMatrix * vec3(normalMap * 2.0 - 1.0));',
|
2018-05-08 22:04:57 +00:00
|
|
|
' vec2 res = vec2(min(uResolution.x, uResolution.y)) * uCamera.w;',
|
|
|
|
'',
|
|
|
|
' for (int index = 0; index < kMaxLights; ++index)',
|
|
|
|
' {',
|
|
|
|
' 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);',
|
|
|
|
'',
|
2020-07-16 14:16:01 +00:00
|
|
|
' gl_FragColor = color * vec4(colorOutput.rgb * colorOutput.a, colorOutput.a);',
|
2018-05-08 22:04:57 +00:00
|
|
|
'}',
|
|
|
|
''
|
|
|
|
].join('\n');
|