Add Look panel: edge outlines, shadow mapping, anime cel and soft studio shading, rim light
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
30d8c98891
commit
3154e4fab9
20 files changed
+740
-81
No files matched your search
+202
-35
@@ -1,5 +1,7 @@
|
||||
#include "render/Shaders.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace mr {
|
||||
|
||||
namespace {
|
||||
@@ -7,15 +9,12 @@ 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* kVertexShader = R"(#version 120
|
||||
const char* kSkinning = R"(
|
||||
uniform int u_skinning;
|
||||
uniform sampler2D u_boneTexture;
|
||||
uniform vec2 u_boneTexSize;
|
||||
attribute vec4 a_boneIndices;
|
||||
attribute vec4 a_boneWeights;
|
||||
varying vec3 v_normal;
|
||||
varying vec3 v_viewPos;
|
||||
varying vec2 v_uv;
|
||||
|
||||
vec4 boneColumn(float bone, float k) {
|
||||
vec2 texel = vec2(mod(bone, 1024.0) * 3.0 + k, floor(bone / 1024.0));
|
||||
@@ -30,9 +29,9 @@ void skin(float bone, float weight, vec4 pos, vec3 nrm, inout vec3 outPos, inout
|
||||
outNrm += weight * vec3(dot(nrm, c0.xyz), dot(nrm, c1.xyz), dot(nrm, c2.xyz));
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec4 pos = gl_Vertex;
|
||||
vec3 nrm = gl_Normal;
|
||||
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);
|
||||
@@ -43,6 +42,18 @@ void main() {
|
||||
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;
|
||||
@@ -51,7 +62,39 @@ void main() {
|
||||
}
|
||||
)";
|
||||
|
||||
const char* kFragmentShader = R"(#version 120
|
||||
// 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 lit = 0.0;
|
||||
for (int i = 0; i < 12; ++i) {
|
||||
float depth = texture2D(u_shadowMap, sc.xy + kPoisson[i] * radius).r;
|
||||
lit += sc.z - bias <= depth ? 1.0 : 0.0;
|
||||
}
|
||||
return lit / 12.0;
|
||||
}
|
||||
)";
|
||||
|
||||
const char* kModelFragment = R"(
|
||||
uniform mat4 osg_ViewMatrix;
|
||||
uniform sampler2D u_texture;
|
||||
uniform sampler2D u_sphere;
|
||||
@@ -59,14 +102,17 @@ 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
|
||||
uniform int u_lightFollowsCamera;
|
||||
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;
|
||||
varying vec3 v_normal;
|
||||
varying vec3 v_viewPos;
|
||||
varying vec2 v_uv;
|
||||
@@ -74,51 +120,172 @@ varying vec2 v_uv;
|
||||
void main() {
|
||||
vec3 n = normalize(v_normal);
|
||||
if (!gl_FrontFacing) n = -n;
|
||||
vec3 lightDir = u_lightFollowsCamera == 1 ? normalize(u_lightDirection)
|
||||
: normalize(mat3(osg_ViewMatrix) * u_lightDirection);
|
||||
vec3 toLight = -lightDir;
|
||||
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 base = u_diffuse;
|
||||
if (u_useTexture == 1) base *= texture2D(u_texture, v_uv);
|
||||
float alpha = base.a;
|
||||
if (alpha < 0.004) discard;
|
||||
vec3 tex = u_useTexture == 1 ? texture2D(u_texture, v_uv).rgb : vec3(1.0);
|
||||
|
||||
vec3 color;
|
||||
float alpha = u_diffuse.a;
|
||||
if (u_lightingModel == 0) {
|
||||
color = clamp(u_diffuse.rgb * u_lightColor + u_ambient, 0.0, 1.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.95 + 0.25 * u_lightColor), band);
|
||||
} else {
|
||||
color = u_diffuse.rgb * (0.35 + 0.65 * max(ndl, 0.0));
|
||||
// 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_useTexture == 1) {
|
||||
vec4 t = texture2D(u_texture, v_uv);
|
||||
color *= t.rgb;
|
||||
alpha *= t.a;
|
||||
}
|
||||
if (alpha < 0.004) discard;
|
||||
if (u_sphereMode != 0) {
|
||||
vec3 s = texture2D(u_sphere, n.xy * 0.5 + 0.5).rgb;
|
||||
if (u_sphereMode == 1) color *= s;
|
||||
else color += s;
|
||||
else color += s * (u_lightingModel == 2 ? 0.5 + 0.5 * lit : 1.0);
|
||||
}
|
||||
if (u_lightingModel == 0 && u_useToon == 1) {
|
||||
color *= texture2D(u_toon, vec2(0.5, clamp(ndl + 0.5, 0.0, 1.0))).rgb;
|
||||
if (u_lightingModel == 0) {
|
||||
float t = clamp(ndl + 0.5, 0.0, 1.0);
|
||||
if (u_useToon == 1) color *= texture2D(u_toon, vec2(0.5, min(t, mix(1.0, t * shadow, u_shadowStrength)))).rgb;
|
||||
else color *= mix(1.0, 0.55 + 0.45 * shadow, u_shadowStrength);
|
||||
}
|
||||
if (u_shininess > 0.0) {
|
||||
vec3 h = normalize(toLight + toEye);
|
||||
color += u_specular * u_lightColor * pow(max(dot(n, h), 0.0), u_shininess);
|
||||
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);
|
||||
}
|
||||
)";
|
||||
|
||||
} // namespace
|
||||
// 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 1080p 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);
|
||||
if (len > 1e-6) {
|
||||
dir /= len;
|
||||
float aspect = abs(gl_ProjectionMatrix[1][1] / gl_ProjectionMatrix[0][0]);
|
||||
dir.x /= aspect;
|
||||
clip.xy += dir * (u_edgeSize * a_edgeScale * u_edgeWidth * (2.0 / 1080.0)) * clip.w;
|
||||
}
|
||||
gl_Position = clip;
|
||||
}
|
||||
)";
|
||||
|
||||
osg::ref_ptr<osg::Program> createModelProgram() {
|
||||
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("ModelProgram");
|
||||
program->addShader(new osg::Shader(osg::Shader::VERTEX, kVertexShader));
|
||||
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, kFragmentShader));
|
||||
program->addBindAttribLocation("a_boneIndices", kBoneIndexAttrib);
|
||||
program->addBindAttribLocation("a_boneWeights", kBoneWeightAttrib);
|
||||
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
|
||||
Reference in new issue
Block a user