Fixed shader for clip-space

This commit is contained in:
Richard Davey 2020-10-13 22:34:26 +01:00
parent b1a5ce7f55
commit d3d0c71fe4
4 changed files with 25 additions and 18 deletions

View file

@ -3,19 +3,20 @@ module.exports = [
'', '',
'precision mediump float;', 'precision mediump float;',
'', '',
'varying vec2 lightPosition;', 'varying vec4 lightPosition;',
'varying vec4 lightColor;', 'varying vec4 lightColor;',
'varying float lightRadius;', 'varying float lightRadius;',
'', '',
'void main()', 'void main()',
'{', '{',
' vec2 res = vec2(800.0, 600.0);', ' vec2 res = vec2(800.0, 600.0);',
' vec2 center = vec2(lightPosition.x, res.y - lightPosition.y);', '',
' vec2 center = vec2((lightPosition.x + 1.0) * (res.x / 2.0), (lightPosition.y + 1.0) * (res.y / 2.0));',
'', '',
' float distance = length(center.xy - gl_FragCoord.xy);', ' float distance = length(center.xy - gl_FragCoord.xy);',
'', '',
' // float intensity = 1.0 - min(distance, lightRadius) / lightRadius;', ' float intensity = 1.0 - min(distance, lightRadius) / lightRadius;',
' float intensity = clamp(1.0 - distance * distance / (lightRadius * lightRadius), 0.0, 1.0);', ' // float intensity = clamp(1.0 - distance * distance / (lightRadius * lightRadius), 0.0, 1.0);',
'', '',
' vec4 color = vec4(intensity, intensity, intensity, 0.0) * vec4(lightColor.r, lightColor.g, lightColor.b, 1.0);', ' vec4 color = vec4(intensity, intensity, intensity, 0.0) * vec4(lightColor.r, lightColor.g, lightColor.b, 1.0);',
'', '',

View file

@ -12,17 +12,19 @@ module.exports = [
'attribute vec4 inLightColor;', 'attribute vec4 inLightColor;',
'attribute float inLightRadius;', 'attribute float inLightRadius;',
'', '',
'varying vec2 lightPosition;', 'varying vec4 lightPosition;',
'varying vec4 lightColor;', 'varying vec4 lightColor;',
'varying float lightRadius;', 'varying float lightRadius;',
'', '',
'void main ()', 'void main ()',
'{', '{',
' lightRadius = inLightRadius;', ' mat4 mvp = uProjectionMatrix * uViewMatrix * uModelMatrix;',
' lightColor = inLightColor;',
' lightPosition = inLightPosition;',
'', '',
' gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4(inPosition, 1.0, 1.0);', ' lightColor = inLightColor;',
' lightRadius = inLightRadius;',
' lightPosition = mvp * vec4(inLightPosition, 1.0, 1.0);',
'',
' gl_Position = mvp * vec4(inPosition, 1.0, 1.0);',
'}', '}',
'' ''
].join('\n'); ].join('\n');

View file

@ -2,19 +2,21 @@
precision mediump float; precision mediump float;
varying vec2 lightPosition; varying vec4 lightPosition;
varying vec4 lightColor; varying vec4 lightColor;
varying float lightRadius; varying float lightRadius;
void main() void main()
{ {
// Half these and pass as uniform
vec2 res = vec2(800.0, 600.0); vec2 res = vec2(800.0, 600.0);
vec2 center = vec2(lightPosition.x, res.y - lightPosition.y);
vec2 center = vec2((lightPosition.x + 1.0) * (res.x / 2.0), (lightPosition.y + 1.0) * (res.y / 2.0));
float distance = length(center.xy - gl_FragCoord.xy); float distance = length(center.xy - gl_FragCoord.xy);
// float intensity = 1.0 - min(distance, lightRadius) / lightRadius; float intensity = 1.0 - min(distance, lightRadius) / lightRadius;
float intensity = clamp(1.0 - distance * distance / (lightRadius * lightRadius), 0.0, 1.0); // float intensity = clamp(1.0 - distance * distance / (lightRadius * lightRadius), 0.0, 1.0);
vec4 color = vec4(intensity, intensity, intensity, 0.0) * vec4(lightColor.r, lightColor.g, lightColor.b, 1.0); vec4 color = vec4(intensity, intensity, intensity, 0.0) * vec4(lightColor.r, lightColor.g, lightColor.b, 1.0);

View file

@ -11,15 +11,17 @@ attribute vec2 inLightPosition;
attribute vec4 inLightColor; attribute vec4 inLightColor;
attribute float inLightRadius; attribute float inLightRadius;
varying vec2 lightPosition; varying vec4 lightPosition;
varying vec4 lightColor; varying vec4 lightColor;
varying float lightRadius; varying float lightRadius;
void main () void main ()
{ {
lightRadius = inLightRadius; mat4 mvp = uProjectionMatrix * uViewMatrix * uModelMatrix;
lightColor = inLightColor;
lightPosition = inLightPosition;
gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4(inPosition, 1.0, 1.0); lightColor = inLightColor;
lightRadius = inLightRadius;
lightPosition = mvp * vec4(inLightPosition, 1.0, 1.0);
gl_Position = mvp * vec4(inPosition, 1.0, 1.0);
} }