Refactor
This commit is contained in:
parent
0362632222
commit
706b17b19a
|
@ -5,7 +5,8 @@ layout(quads, fractional_even_spacing, cw) in;
|
|||
layout(binding = 0) uniform sampler2D heightTexture;
|
||||
layout(location = 0) uniform mat4 M;
|
||||
|
||||
uniform float displacementScale = 0.05;
|
||||
uniform float displacementScale = 0.1f;
|
||||
uniform float tessellationFactor;
|
||||
|
||||
layout(std140, binding = 0) uniform SceneUniforms
|
||||
{
|
||||
|
@ -25,33 +26,24 @@ out TE_OUT {
|
|||
} te_out;
|
||||
|
||||
float getHeight(vec2 uv) {
|
||||
return texture(heightTexture, uv).r;
|
||||
return texture(heightTexture, uv).r * displacementScale;
|
||||
}
|
||||
|
||||
vec3 calculateFaceNormal(vec3 p1, vec3 p2, vec3 p3) {
|
||||
return normalize(cross(p2 - p1, p3 - p1));
|
||||
vec3 calculateSmoothNormal(vec3 pos, vec2 coord) {
|
||||
float step = 1.0f / (tessellationFactor + 1.0f);
|
||||
|
||||
float hLeft = getHeight(coord + vec2(-step, 0.0f));
|
||||
float hRight = getHeight(coord + vec2(step, 0.0f));
|
||||
float hUp = getHeight(coord + vec2(0.0f, step));
|
||||
float hDown = getHeight(coord + vec2(0.0f, -step));
|
||||
|
||||
vec3 tangentX = normalize(vec3(step, hRight - hLeft, 0.0));
|
||||
vec3 tangentY = normalize(vec3(0.0, hUp - hDown, step));
|
||||
|
||||
return normalize(cross(tangentY, tangentX));
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec3 barycentricCoords = gl_TessCoord;
|
||||
|
||||
vec3 p0 = te_in[0].position;
|
||||
vec3 p1 = te_in[1].position;
|
||||
vec3 p2 = te_in[2].position;
|
||||
vec3 displacedPosition = barycentricCoords.x * p0 + barycentricCoords.y * p1 + barycentricCoords.z * p2;
|
||||
|
||||
float displacement = texture(heightTexture, vec2(displacedPosition.x, displacedPosition.z)).r * displacementScale;
|
||||
|
||||
//te_out.position = vec4(displacedPosition, 1.0f).xyz;
|
||||
|
||||
vec3 dp1 = p1 - p0;
|
||||
vec3 dp2 = p2 - p0;
|
||||
|
||||
dp1.z += texture(heightTexture, vec2(p1.x, p1.y)).r * displacementScale - texture(heightTexture, vec2(p0.x, p0.y)).r * displacementScale;
|
||||
dp2.z += texture(heightTexture, vec2(p2.x, p2.y)).r * displacementScale - texture(heightTexture, vec2(p0.x, p0.y)).r * displacementScale;
|
||||
|
||||
te_out.normal = normalize(cross(dp1, dp2));
|
||||
|
||||
// Interpolate texture coordinates
|
||||
vec2 texCoord = mix(
|
||||
mix(te_in[0].texCoord, te_in[1].texCoord, gl_TessCoord[0]),
|
||||
|
@ -68,24 +60,8 @@ void main() {
|
|||
|
||||
position.z += texture(heightTexture, texCoord).r * displacementScale;
|
||||
|
||||
// // Calculate normals based on the tessellated triangle's vertices
|
||||
// vec3 p0 = gl_in[0].gl_Position.xyz;
|
||||
// vec3 p1 = gl_in[1].gl_Position.xyz;
|
||||
// vec3 p2 = gl_in[2].gl_Position.xyz;
|
||||
|
||||
// vec3 n_u = p1 - p0;
|
||||
// vec3 v_u = p2 - p0;
|
||||
// vec3 v_normal = normalize(cross(n_u, v_u));
|
||||
|
||||
// // Calculate normals for the specific triangle being rendered
|
||||
// vec3 normal = calculateFaceNormal(p0, p1, position);
|
||||
// normal += calculateFaceNormal(p1, p2, position);
|
||||
// normal += calculateFaceNormal(p2, p0, position);
|
||||
|
||||
// normal = (PV*M*vec4(normalize(normal), 1.0f)).xyz;
|
||||
|
||||
te_out.position = (PV*M*vec4(position, 1.0f)).xyz;
|
||||
te_out.position = (M*vec4(position, 1.0f)).xyz; // Position in world-space
|
||||
te_out.texCoord = texCoord;
|
||||
//te_out.normal = (PV*M*vec4(v_normal, 1.0f)).xyz;
|
||||
gl_Position = PV*M*vec4(position, 1.0f);
|
||||
te_out.normal = calculateSmoothNormal(position, texCoord);
|
||||
gl_Position = PV*M*vec4(position, 1.0f); // Position in screen-space
|
||||
}
|
Loading…
Reference in New Issue