Skip to content

Commit

Permalink
curved normal
Browse files Browse the repository at this point in the history
  • Loading branch information
BarthPaleologue committed Nov 6, 2023
1 parent 99769e9 commit 02de350
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/shaders/grassFragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ uniform float time;

uniform vec3 lightDirection;

in vec3 vPosition;

in mat4 normalMatrix;
in vec3 vNormal;
in vec3 vNormal1;
in vec3 vNormal2;

void main() {
vec3 finalColor = vec3(0.0, 0.4, 0.0);

vec3 normalW = normalize((normalMatrix * vec4(vNormal, 0.0)).xyz);
float normalBlending = vPosition.x + 0.5;

vec3 normal = mix(vNormal1, vNormal2, normalBlending);
normal = normalize(normal);

vec3 normalW = normalize((normalMatrix * vec4(normal, 0.0)).xyz);

float ndl1 = max(dot(normalW, lightDirection), 0.0);
float ndl2 = max(dot(-normalW, lightDirection), 0.0);
Expand Down
25 changes: 20 additions & 5 deletions src/shaders/grassVertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ attribute vec3 normal;

uniform mat4 viewProjection;

out vec3 vPosition;

out mat4 normalMatrix;
out vec3 vNormal;
out vec3 vNormal1;
out vec3 vNormal2;

// rotation using https://www.wikiwand.com/en/Rodrigues%27_rotation_formula
vec3 rotateAround(vec3 vector, vec3 axis, float theta) {
Expand All @@ -19,14 +22,26 @@ vec3 rotateAround(vec3 vector, vec3 axis, float theta) {
void main() {
#include<instancesVertex>

// curvy normal
float normalCurveAmount = 0.3 * 3.14;
vec3 curvyNormal1 = rotateAround(normal, vec3(0.0, 1.0, 0.0), normalCurveAmount);
vec3 curvyNormal2 = rotateAround(normal, vec3(0.0, 1.0, 0.0), -normalCurveAmount);

// curved grass blade
float leanAmount = 0.3;
float curveAmount = leanAmount * position.y;
vec3 rotatedPosition = rotateAround(position, vec3(1.0, 0.0, 0.0), curveAmount);
vec3 rotatedNormal = rotateAround(normal, vec3(1.0, 0.0, 0.0), curveAmount);
vec3 leaningPosition = rotateAround(position, vec3(1.0, 0.0, 0.0), curveAmount);

vec3 leaningNormal1 = rotateAround(curvyNormal1, vec3(1.0, 0.0, 0.0), curveAmount);
vec3 leaningNormal2 = rotateAround(curvyNormal2, vec3(1.0, 0.0, 0.0), curveAmount);

vec4 outPosition = viewProjection * finalWorld * vec4(rotatedPosition, 1.0);
vec4 outPosition = viewProjection * finalWorld * vec4(leaningPosition, 1.0);
gl_Position = outPosition;

vPosition = position;

normalMatrix = transpose(inverse(finalWorld));
vNormal = rotatedNormal;

vNormal1 = leaningNormal1;
vNormal2 = leaningNormal2;
}

0 comments on commit 02de350

Please sign in to comment.