Add bounded SPT parsing, authored branch frames and foliage generation, independent LOD meshes, billboards, collision metadata, and legacy CPU/shader wind support. Restore seeded sampling and special leaf selection with generation diagnostics and reproducible baseline tools. Include regression coverage and static recovery documentation describing remaining differences from the original runtime. SDK tests pass (4/4); full corpus and client validation are recorded in the recovery notes.
174 lines
6.0 KiB
C++
174 lines
6.0 KiB
C++
#pragma once
|
|
|
|
namespace speedtree {
|
|
|
|
// Reference GLSL / Vulkan / Metal / OpenGL 4.5+ shader sources for high-fidelity foliage rendering.
|
|
// Can be embedded or compiled into SPIR-V.
|
|
|
|
inline const char* GetFoliageVertexShaderGLSL() {
|
|
return R"glsl(
|
|
#version 450 core
|
|
|
|
layout(location = 0) in vec3 in_Position;
|
|
layout(location = 1) in vec3 in_Normal;
|
|
layout(location = 2) in vec2 in_TexCoord;
|
|
layout(location = 3) in vec4 in_WindWeights; // [0]:Trunk, [1]:Branch, [2]:Leaf, [3]:Phase
|
|
layout(location = 4) in vec3 in_LeafPivotOffset; // Leaf center to vertex
|
|
layout(location = 5) in uint in_Diffuse;
|
|
layout(location = 6) in uint in_WindMatrixIndex;
|
|
|
|
layout(std140, binding = 0) uniform FrameBlock {
|
|
mat4 u_ViewProj;
|
|
vec3 u_CameraPos;
|
|
float u_Time;
|
|
vec3 u_WindDir;
|
|
float u_WindStrength;
|
|
};
|
|
|
|
layout(std140, binding = 1) uniform InstanceBlock {
|
|
mat4 u_ModelMatrix;
|
|
vec3 u_TreeOrigin;
|
|
float u_TreeHeight;
|
|
};
|
|
|
|
// Upload settings=(matrixCount, gustFrequency, gustStrength, 0).
|
|
// matrixCount=0 selects generated wind; custom local-space matrices need no strength rescaling.
|
|
layout(std140, binding = 3) uniform WindBlock {
|
|
mat4 u_WindMatrices[4];
|
|
vec4 u_WindSettings;
|
|
// (enabled, phase, authored rocking range, authored rocking frequency).
|
|
vec4 u_LegacyLeafWind;
|
|
};
|
|
|
|
layout(location = 0) out vec3 v_WorldPos;
|
|
layout(location = 1) out vec3 v_WorldNormal;
|
|
layout(location = 2) out vec2 v_TexCoord;
|
|
layout(location = 3) out float v_CanopyAO;
|
|
|
|
vec3 RotateAngleAxis(vec3 v, vec3 axis, float angle) {
|
|
float cosTheta = cos(angle);
|
|
float sinTheta = sin(angle);
|
|
return v * cosTheta + cross(axis, v) * sinTheta + axis * dot(axis, v) * (1.0 - cosTheta);
|
|
}
|
|
|
|
void main() {
|
|
vec3 localPos = in_Position;
|
|
vec3 localNormal = in_Normal;
|
|
|
|
float trunkW = in_WindWeights.r;
|
|
float branchW = in_WindWeights.g;
|
|
float leafW = in_WindWeights.b;
|
|
float phase = in_WindWeights.a;
|
|
|
|
vec3 direction = vec3(u_WindDir.x, 0.0, u_WindDir.z);
|
|
direction = length(direction) > 1e-7 ? normalize(direction) : vec3(1.0, 0.0, 0.0);
|
|
mat3 orientation = mat3(u_ModelMatrix);
|
|
orientation[0] = normalize(orientation[0]);
|
|
orientation[1] = normalize(orientation[1]);
|
|
orientation[2] = normalize(orientation[2]);
|
|
vec3 localDirection = transpose(orientation) * direction;
|
|
vec3 axis = normalize(cross(vec3(0.0, 1.0, 0.0), localDirection));
|
|
float spatial = dot(u_TreeOrigin.xz, direction.xz) * 0.005;
|
|
float gust = 0.5 + 0.5 * sin(u_Time * u_WindSettings.y + spatial);
|
|
float strength = max(0.0, u_WindStrength);
|
|
|
|
// Every corner of a tree leaf card shares this angle and pivot.
|
|
float rock = 0.25 * strength * leafW * sin(u_Time * 8.0 + phase * 15.0);
|
|
if (u_LegacyLeafWind.x > 0.0)
|
|
rock = 3.141592653589793 * u_LegacyLeafWind.z * strength * leafW
|
|
* sin(u_LegacyLeafWind.y * u_LegacyLeafWind.w + phase * 15.0);
|
|
vec3 rotatedOffset = RotateAngleAxis(in_LeafPivotOffset, localDirection, rock);
|
|
localPos += rotatedOffset - in_LeafPivotOffset;
|
|
localNormal = RotateAngleAxis(localNormal, localDirection, rock);
|
|
|
|
uint count = uint(clamp(u_WindSettings.x, 0.0, 4.0));
|
|
mat4 windMatrix = mat4(1.0);
|
|
if (count > 0u) {
|
|
windMatrix = u_WindMatrices[in_WindMatrixIndex % count];
|
|
} else {
|
|
float bend = 0.12 * strength * (1.0 + u_WindSettings.z * gust)
|
|
* sin(u_Time * 1.5 + spatial + float(in_WindMatrixIndex % 4u) * 0.7);
|
|
windMatrix[0].xyz = RotateAngleAxis(vec3(1.0,0.0,0.0), axis, bend);
|
|
windMatrix[1].xyz = RotateAngleAxis(vec3(0.0,1.0,0.0), axis, bend);
|
|
windMatrix[2].xyz = RotateAngleAxis(vec3(0.0,0.0,1.0), axis, bend);
|
|
}
|
|
float weight = clamp(trunkW + 0.4 * branchW, 0.0, 1.0);
|
|
mat3 linear = mat3(1.0) * (1.0 - weight) + mat3(windMatrix) * weight;
|
|
localPos = linear * localPos + windMatrix[3].xyz * weight;
|
|
if (abs(determinant(linear)) > 1e-7)
|
|
localNormal = transpose(inverse(linear)) * localNormal;
|
|
vec3 worldPos = (u_ModelMatrix * vec4(localPos, 1.0)).xyz;
|
|
vec3 worldNormal = normalize(transpose(inverse(mat3(u_ModelMatrix))) * localNormal);
|
|
|
|
// Canopy interior AO
|
|
vec3 crownCenter = u_TreeOrigin + vec3(0.0, u_TreeHeight * 0.65, 0.0);
|
|
float distToCenter = length(worldPos - crownCenter);
|
|
v_CanopyAO = clamp(distToCenter / (u_TreeHeight * 0.45), 0.35, 1.0);
|
|
|
|
v_WorldPos = worldPos;
|
|
v_WorldNormal = worldNormal;
|
|
v_TexCoord = in_TexCoord;
|
|
|
|
gl_Position = u_ViewProj * vec4(worldPos, 1.0);
|
|
}
|
|
)glsl";
|
|
}
|
|
|
|
inline const char* GetFoliageFragmentShaderGLSL() {
|
|
return R"glsl(
|
|
#version 450 core
|
|
|
|
layout(location = 0) in vec3 v_WorldPos;
|
|
layout(location = 1) in vec3 v_WorldNormal;
|
|
layout(location = 2) in vec2 v_TexCoord;
|
|
layout(location = 3) in float v_CanopyAO;
|
|
|
|
layout(binding = 4) uniform sampler2D u_DiffuseAtlas;
|
|
|
|
layout(std140, binding = 2) uniform LightBlock {
|
|
vec3 u_SunDir;
|
|
vec3 u_SunColor;
|
|
vec3 u_AmbientSky;
|
|
vec3 u_CameraPos;
|
|
};
|
|
|
|
layout(location = 0) out vec4 out_Color;
|
|
|
|
void main() {
|
|
vec4 texColor = texture(u_DiffuseAtlas, v_TexCoord);
|
|
|
|
// Alpha Cutoff
|
|
if (texColor.a < 0.45) {
|
|
discard;
|
|
}
|
|
|
|
vec3 N = normalize(v_WorldNormal);
|
|
vec3 L = normalize(u_SunDir);
|
|
vec3 V = normalize(u_CameraPos - v_WorldPos);
|
|
vec3 H = normalize(L + V);
|
|
|
|
// Wrap Lighting (Half-Lambert)
|
|
float NdotL = dot(N, L);
|
|
float wrapNdotL = max(0.0, (NdotL + 0.35) / 1.35);
|
|
vec3 diffuse = texColor.rgb * u_SunColor * wrapNdotL;
|
|
|
|
// Subsurface Transmission (Sunlight penetrating leaf)
|
|
float viewThrough = max(0.0, dot(-L, V));
|
|
float transIntensity = pow(viewThrough, 3.5) * 0.85;
|
|
vec3 transColor = texColor.rgb * vec3(1.1, 1.3, 0.7) * u_SunColor * transIntensity;
|
|
|
|
// Specular Highlight
|
|
float NdotH = max(0.0, dot(N, H));
|
|
float specular = pow(NdotH, 32.0) * 0.25;
|
|
|
|
// Canopy AO & Ambient
|
|
vec3 ambient = u_AmbientSky * texColor.rgb * v_CanopyAO;
|
|
|
|
vec3 finalRGB = (diffuse + transColor + specular) * v_CanopyAO + ambient;
|
|
out_Color = vec4(finalRGB, 1.0);
|
|
}
|
|
)glsl";
|
|
}
|
|
|
|
} // namespace speedtree
|