Files
3d-model-renderer/src/render/Shaders.cpp
T
gs5579andDevin AI 302e7c4f20 UV and material morphs; FXAA-only path skips composite, 8-bit post targets
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-12-01 07:03:56 +00:00

310 lines
11 KiB
C++

#include "render/Shaders.h"
#include <string>
namespace mr {
namespace {
// GLSL 1.20 so the same shaders run on macOS legacy contexts, Windows and Linux.
// Hardware skinning reads bone matrices from a float texture: bone b occupies texels
// (b % 1024) * 3 + k, row b / 1024, where texel k holds column k of its 4x3 matrix.
const char* kSkinning = R"(
uniform int u_skinning;
uniform sampler2D u_boneTexture;
uniform vec2 u_boneTexSize;
attribute vec4 a_boneIndices;
attribute vec4 a_boneWeights;
vec4 boneColumn(float bone, float k) {
vec2 texel = vec2(mod(bone, 1024.0) * 3.0 + k, floor(bone / 1024.0));
return texture2DLod(u_boneTexture, (texel + 0.5) / u_boneTexSize, 0.0);
}
void skin(float bone, float weight, vec4 pos, vec3 nrm, inout vec3 outPos, inout vec3 outNrm) {
vec4 c0 = boneColumn(bone, 0.0);
vec4 c1 = boneColumn(bone, 1.0);
vec4 c2 = boneColumn(bone, 2.0);
outPos += weight * vec3(dot(pos, c0), dot(pos, c1), dot(pos, c2));
outNrm += weight * vec3(dot(nrm, c0.xyz), dot(nrm, c1.xyz), dot(nrm, c2.xyz));
}
void skinnedVertex(out vec4 pos, out vec3 nrm) {
pos = gl_Vertex;
nrm = gl_Normal;
if (u_skinning == 1 && a_boneWeights.x > 0.0) {
vec3 p = vec3(0.0);
vec3 n = vec3(0.0);
skin(a_boneIndices.x, a_boneWeights.x, pos, nrm, p, n);
if (a_boneWeights.y > 0.0) skin(a_boneIndices.y, a_boneWeights.y, pos, nrm, p, n);
if (a_boneWeights.z > 0.0) skin(a_boneIndices.z, a_boneWeights.z, pos, nrm, p, n);
if (a_boneWeights.w > 0.0) skin(a_boneIndices.w, a_boneWeights.w, pos, nrm, p, n);
pos = vec4(p, 1.0);
nrm = n;
}
}
)";
const char* kModelVertex = R"(
varying vec3 v_normal;
varying vec3 v_viewPos;
varying vec2 v_uv;
void main() {
vec4 pos;
vec3 nrm;
skinnedVertex(pos, nrm);
vec4 viewPos = gl_ModelViewMatrix * pos;
v_viewPos = viewPos.xyz;
v_normal = gl_NormalMatrix * nrm;
v_uv = gl_MultiTexCoord0.xy;
gl_Position = gl_ProjectionMatrix * viewPos;
}
)";
// Shadow map lookup with percentage-closer filtering over a Poisson disc.
// Returns 1 when lit, 0 when fully in shadow.
const char* kShadowLookup = R"(
uniform mat4 osg_ViewMatrixInverse;
uniform sampler2D u_shadowMap;
uniform int u_shadowEnabled;
uniform mat4 u_shadowMatrix; // world -> shadow map texture coordinates
uniform float u_shadowTexel; // 1 / shadow map size
uniform float u_shadowSoftness;
uniform float u_shadowBias;
const vec2 kPoisson[12] = vec2[12](
vec2(-0.326, -0.406), vec2(-0.840, -0.074), vec2(-0.696, 0.457), vec2(-0.203, 0.621),
vec2(0.962, -0.195), vec2(0.473, -0.480), vec2(0.519, 0.767), vec2(0.185, -0.893),
vec2(0.507, 0.064), vec2(0.896, 0.412), vec2(-0.322, -0.933), vec2(-0.792, -0.598));
float shadowFactor(vec3 viewPos, float ndl) {
if (u_shadowEnabled == 0) return 1.0;
vec4 sc = u_shadowMatrix * (osg_ViewMatrixInverse * vec4(viewPos, 1.0));
sc.xyz /= sc.w;
if (sc.x <= 0.0 || sc.x >= 1.0 || sc.y <= 0.0 || sc.y >= 1.0 || sc.z >= 1.0) return 1.0;
float bias = u_shadowBias * (1.0 + 3.0 * (1.0 - clamp(abs(ndl), 0.0, 1.0)));
float radius = u_shadowTexel * u_shadowSoftness;
float ref = sc.z - bias;
// Four corner taps first: fully lit or fully shadowed areas (most of the screen) stop here.
float lit = 0.0;
lit += ref <= texture2D(u_shadowMap, sc.xy + vec2(-0.7, -0.7) * radius).r ? 1.0 : 0.0;
lit += ref <= texture2D(u_shadowMap, sc.xy + vec2(0.7, -0.7) * radius).r ? 1.0 : 0.0;
lit += ref <= texture2D(u_shadowMap, sc.xy + vec2(-0.7, 0.7) * radius).r ? 1.0 : 0.0;
lit += ref <= texture2D(u_shadowMap, sc.xy + vec2(0.7, 0.7) * radius).r ? 1.0 : 0.0;
if (lit == 0.0 || lit == 4.0) return lit * 0.25;
for (int i = 0; i < 12; ++i)
lit += ref <= texture2D(u_shadowMap, sc.xy + kPoisson[i] * radius).r ? 1.0 : 0.0;
return lit / 16.0;
}
)";
const char* kModelFragment = R"(
uniform mat4 osg_ViewMatrix;
uniform sampler2D u_texture;
uniform sampler2D u_sphere;
uniform sampler2D u_toon;
uniform int u_useTexture;
uniform int u_sphereMode; // 0 none, 1 multiply, 2 add
uniform int u_useToon;
uniform int u_lightingModel; // 0 MMD toon, 1 Lambert, 2 anime cel, 3 soft studio
uniform int u_receiveShadow;
uniform vec4 u_diffuse;
uniform vec3 u_ambient;
uniform vec3 u_specular;
uniform float u_shininess;
uniform vec3 u_lightDirection; // direction the light travels (world space)
uniform vec3 u_lightColor;
uniform float u_shadowStrength;
uniform float u_rimStrength;
uniform vec3 u_rimColor;
uniform vec4 u_texMul; // material-morph tints: texel * mul + add
uniform vec4 u_texAdd;
uniform vec4 u_sphereMul;
uniform vec4 u_sphereAdd;
uniform vec4 u_toonMul;
uniform vec4 u_toonAdd;
varying vec3 v_normal;
varying vec3 v_viewPos;
varying vec2 v_uv;
void main() {
vec3 n = normalize(v_normal);
if (!gl_FrontFacing) n = -n;
vec3 toLight = -normalize(mat3(osg_ViewMatrix) * u_lightDirection);
vec3 toEye = normalize(-v_viewPos);
float ndl = dot(n, toLight);
float ndv = max(dot(n, toEye), 0.0);
float shadow = u_receiveShadow == 1 ? shadowFactor(v_viewPos, ndl) : 1.0;
float lit = mix(1.0, shadow, u_shadowStrength);
vec4 texel = vec4(1.0);
if (u_useTexture == 1) texel = clamp(texture2D(u_texture, v_uv) * u_texMul + u_texAdd, 0.0, 1.0);
vec4 base = u_diffuse * texel;
float alpha = base.a;
if (alpha < 0.004) discard;
vec3 tex = texel.rgb;
vec3 color;
if (u_lightingModel == 0) {
color = clamp(u_diffuse.rgb * u_lightColor + u_ambient, 0.0, 1.0) * tex;
} else if (u_lightingModel == 1) {
color = base.rgb * (0.35 + 0.65 * max(ndl, 0.0) * lit);
} else if (u_lightingModel == 2) {
// Two-tone cel: a hard terminator with a cool, slightly saturated shade colour.
float band = smoothstep(-0.02, 0.06, ndl) * lit;
vec3 shade = base.rgb * vec3(0.66, 0.62, 0.78);
color = mix(shade, base.rgb * (0.85 + 0.25 * u_lightColor), band);
} else {
// Soft studio: wrapped diffuse, sky/ground ambient and Fresnel-weighted highlights.
vec3 up = normalize(mat3(osg_ViewMatrix) * vec3(0.0, 0.0, 1.0));
float sky = 0.5 + 0.5 * dot(n, up);
vec3 ambient = mix(vec3(0.26, 0.23, 0.22), vec3(0.50, 0.54, 0.62), sky);
float wrap = max((ndl + 0.35) / 1.35, 0.0) * lit;
color = base.rgb * (ambient + wrap * u_lightColor * 1.25);
}
if (u_sphereMode != 0) {
vec3 s = clamp(texture2D(u_sphere, n.xy * 0.5 + 0.5).rgb * u_sphereMul.rgb + u_sphereAdd.rgb, 0.0, 1.0);
if (u_sphereMode == 1) color *= s;
else color += s * (u_lightingModel == 2 ? 0.5 + 0.5 * lit : 1.0);
}
if (u_lightingModel == 0) {
float t = clamp(ndl + 0.5, 0.0, 1.0);
if (u_useToon == 1) {
vec3 toon = texture2D(u_toon, vec2(0.5, min(t, mix(1.0, t * shadow, u_shadowStrength)))).rgb;
color *= clamp(toon * u_toonMul.rgb + u_toonAdd.rgb, 0.0, 1.0);
}
else color *= mix(1.0, 0.55 + 0.45 * shadow, u_shadowStrength);
}
vec3 h = normalize(toLight + toEye);
float ndh = max(dot(n, h), 0.0);
if (u_lightingModel == 2) {
if (u_shininess > 0.0) color += u_specular * smoothstep(0.90, 0.93, pow(ndh, u_shininess * 0.25)) * 0.6 * lit;
} else if (u_lightingModel == 3) {
float fresnel = 0.04 + 0.96 * pow(1.0 - max(dot(h, toEye), 0.0), 5.0);
float gloss = max(u_shininess, 12.0);
color += (u_specular + 0.08) * u_lightColor * pow(ndh, gloss) * (gloss + 8.0) / 25.0 * fresnel * lit;
} else if (u_shininess > 0.0) {
color += u_specular * u_lightColor * pow(ndh, u_shininess) * (u_lightingModel == 1 ? lit : 1.0);
}
if (u_rimStrength > 0.0) {
float rim = u_lightingModel == 2 ? smoothstep(0.62, 0.70, 1.0 - ndv) : pow(1.0 - ndv, 3.0);
float facing = 0.35 + 0.65 * clamp(dot(n, normalize(toLight + vec3(0.0, 0.0, -0.5))) + 0.5, 0.0, 1.0);
color += u_rimColor * rim * facing * u_rimStrength * (0.4 + 0.6 * lit);
}
gl_FragColor = vec4(clamp(color, 0.0, 1.0), alpha);
}
)";
// MMD-style ink lines: back faces pushed outwards along the screen-space normal, so the
// width stays constant in pixels regardless of distance.
const char* kEdgeVertex = R"(
attribute float a_edgeScale;
uniform float u_edgeSize;
uniform float u_edgeWidth; // global multiplier: pixels per edge-size unit at 720p height
void main() {
vec4 pos;
vec3 nrm;
skinnedVertex(pos, nrm);
vec4 clip = gl_ProjectionMatrix * (gl_ModelViewMatrix * pos);
vec2 dir = (gl_ProjectionMatrix * vec4(gl_NormalMatrix * nrm, 0.0)).xy;
float len = length(dir);
// Thin edges are kept near a pixel wide; sub-pixel lines break up into dots.
float width = u_edgeSize * a_edgeScale;
width = width > 0.0 ? max(width * u_edgeWidth, 0.8) : 0.0;
if (len > 1e-6) {
dir /= len;
float aspect = abs(gl_ProjectionMatrix[1][1] / gl_ProjectionMatrix[0][0]);
dir.x /= aspect;
clip.xy += dir * (width * (2.0 / 720.0)) * clip.w;
}
gl_Position = clip;
}
)";
const char* kEdgeFragment = R"(
uniform vec4 u_edgeColor;
void main() { gl_FragColor = u_edgeColor; }
)";
const char* kDepthVertex = R"(
varying vec2 v_uv;
void main() {
vec4 pos;
vec3 nrm;
skinnedVertex(pos, nrm);
v_uv = gl_MultiTexCoord0.xy;
gl_Position = gl_ModelViewProjectionMatrix * pos;
}
)";
const char* kDepthFragment = R"(
uniform sampler2D u_texture;
uniform int u_useTexture;
uniform int u_castShadow;
uniform vec4 u_diffuse;
varying vec2 v_uv;
void main() {
if (u_castShadow == 0) discard;
float alpha = u_diffuse.a;
if (u_useTexture == 1) alpha *= texture2D(u_texture, v_uv).a;
if (alpha < 0.5) discard;
gl_FragColor = vec4(1.0);
}
)";
const char* kGroundVertex = R"(
varying vec3 v_viewPos;
void main() {
vec4 viewPos = gl_ModelViewMatrix * gl_Vertex;
v_viewPos = viewPos.xyz;
gl_Position = gl_ProjectionMatrix * viewPos;
}
)";
const char* kGroundFragment = R"(
uniform float u_groundShadowOpacity;
varying vec3 v_viewPos;
void main() {
float shade = 1.0 - shadowFactor(v_viewPos, 1.0);
if (shade <= 0.0) discard;
gl_FragColor = vec4(0.0, 0.0, 0.0, shade * u_groundShadowOpacity);
}
)";
osg::ref_ptr<osg::Program> makeProgram(const char* name, const std::string& vertex, const std::string& fragment,
bool skinned) {
osg::ref_ptr<osg::Program> program = new osg::Program;
program->setName(name);
const std::string header = "#version 120\n";
program->addShader(new osg::Shader(osg::Shader::VERTEX, header + (skinned ? kSkinning : "") + vertex));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, header + fragment));
if (skinned) {
program->addBindAttribLocation("a_boneIndices", kBoneIndexAttrib);
program->addBindAttribLocation("a_boneWeights", kBoneWeightAttrib);
}
return program;
}
} // namespace
osg::ref_ptr<osg::Program> createModelProgram() {
return makeProgram("ModelProgram", kModelVertex, std::string(kShadowLookup) + kModelFragment, true);
}
osg::ref_ptr<osg::Program> createEdgeProgram() {
osg::ref_ptr<osg::Program> program = makeProgram("EdgeProgram", kEdgeVertex, kEdgeFragment, true);
program->addBindAttribLocation("a_edgeScale", kEdgeScaleAttrib);
return program;
}
osg::ref_ptr<osg::Program> createShadowDepthProgram() {
return makeProgram("ShadowDepthProgram", kDepthVertex, kDepthFragment, true);
}
osg::ref_ptr<osg::Program> createGroundShadowProgram() {
return makeProgram("GroundShadowProgram", kGroundVertex, std::string(kShadowLookup) + kGroundFragment, false);
}
} // namespace mr