From d08a4d77dc172314e8e25c28f60e54b3c8d0eb9e Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 23 Nov 2015 16:11:23 -0800 Subject: [PATCH] Shader cleanup --- .../src/textured_particle.slv | 52 ++++++++----------- libraries/gpu/src/gpu/Transform.slh | 18 +++---- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/libraries/entities-renderer/src/textured_particle.slv b/libraries/entities-renderer/src/textured_particle.slv index 72b06e0228..9289c8f08d 100644 --- a/libraries/entities-renderer/src/textured_particle.slv +++ b/libraries/entities-renderer/src/textured_particle.slv @@ -42,6 +42,14 @@ in vec2 inColor; // This is actual Lifetime + Seed out vec4 varColor; out vec2 varTexcoord; +const int NUM_VERTICES_PER_PARTICLE = 4; +const vec4 UNIT_QUAD[NUM_VERTICES_PER_PARTICLE] = vec4[NUM_VERTICES_PER_PARTICLE]( + vec4(-1.0, -1.0, 0.0, 1.0), + vec4(1.0, -1.0, 0.0, 1.0), + vec4(-1.0, 1.0, 0.0, 1.0), + vec4(1.0, 1.0, 0.0, 1.0) +); + float bezierInterpolate(float y1, float y2, float y3, float u) { // https://en.wikipedia.org/wiki/Bezier_curve return (1.0 - u) * (1.0 - u) * y1 + 2.0 * (1.0 - u) * u * y2 + u * u * y3; @@ -100,45 +108,29 @@ vec4 interpolate3Vec4(vec4 y1, vec4 y2, vec4 y3, float u) { void main(void) { - const int NUM_VERTICES_PER_PARTICLE = 4; - const vec4 UNIT_QUAD[NUM_VERTICES_PER_PARTICLE] = vec4[NUM_VERTICES_PER_PARTICLE]( - vec4(-1.0, -1.0, 0.0, 1.0), - vec4(1.0, -1.0, 0.0, 1.0), - vec4(-1.0, 1.0, 0.0, 1.0), - vec4(1.0, 1.0, 0.0, 1.0) - ); - - float age = inColor.x / particle.lifespan; - float seed = inColor.y; - - // anchor point in eye space - vec4 anchorPoint = vec4(inPosition.xyz, 1.0); - float radius = interpolate3Floats(particle.radius.start, particle.radius.middle, particle.radius.finish , age); - TransformCamera cam = getTransformCamera(); TransformObject obj = getTransformObject(); - <$transformModelToEyePos(cam, obj, anchorPoint, anchorPoint)$> // Which icon are we dealing with ? int particleID = gl_VertexID / NUM_VERTICES_PER_PARTICLE; - // Which quad vertex pos? int twoTriID = gl_VertexID - particleID * NUM_VERTICES_PER_PARTICLE; - vec4 quadPos = radius * UNIT_QUAD[twoTriID]; - - vec4 clipPos; - vec4 eyePos = vec4(anchorPoint.xyz + quadPos.xyz, 1.0); - <$transformEyeToClipPos(cam, eyePos, clipPos)$> - gl_Position = clipPos; - - + + // Particle properties + float age = inColor.x / particle.lifespan; + float seed = inColor.y; + // Pass the texcoord and the z texcoord is representing the texture icon varTexcoord = vec2((UNIT_QUAD[twoTriID].xy + 1.0) * 0.5); varColor = interpolate3Vec4(particle.color.start, particle.color.middle, particle.color.finish, age); - // if (inColor.x == 0.0) { - // varColor = vec4(0, 1, 0, 1); - // } else { - // varColor = vec4(1, 0, 0, 1); - // } + // anchor point in eye space + float radius = interpolate3Floats(particle.radius.start, particle.radius.middle, particle.radius.finish , age); + vec4 quadPos = radius * UNIT_QUAD[twoTriID]; + + vec4 anchorPoint; + <$transformModelToEyePos(cam, obj, inPosition, anchorPoint)$> + + vec4 eyePos = anchorPoint + quadPos; + <$transformEyeToClipPos(cam, eyePos, gl_Position)$> } diff --git a/libraries/gpu/src/gpu/Transform.slh b/libraries/gpu/src/gpu/Transform.slh index 5d8be1137d..9a866ca4d0 100644 --- a/libraries/gpu/src/gpu/Transform.slh +++ b/libraries/gpu/src/gpu/Transform.slh @@ -74,15 +74,6 @@ TransformCamera getTransformCamera() { } <@endfunc@> -<@func $transformModelToEyePos(cameraTransform, objectTransform, modelPos, eyePos)@> - - { // transformModelToEyePos - vec4 _worldpos = (<$objectTransform$>._model * vec4(<$modelPos$>.xyz, 1.0)); - <$eyePos$> = (<$cameraTransform$>._view * _worldpos); - } -<@endfunc@> - <@func $transformInstancedModelToEyeAndClipPos(cameraTransform, objectTransform, modelPos, eyePos, clipPos)@> @@ -148,6 +139,15 @@ TransformCamera getTransformCamera() { } <@endfunc@> +<@func $transformModelToEyePos(cameraTransform, objectTransform, modelPos, eyePos)@> + + { // transformModelToEyePos + vec4 _worldpos = (<$objectTransform$>._model * vec4(<$modelPos$>.xyz, 1.0)); + <$eyePos$> = (<$cameraTransform$>._view * _worldpos); + } +<@endfunc@> + <@func transformEyeToClipPos(cameraTransform, eyePos, clipPos)@> { // transformEyeToClipPos <$clipPos$> = <$cameraTransform$>._projection * vec4(<$eyePos$>.xyz, 1.0);