reze-engine 0.16.2 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/dist/engine.d.ts +3 -1
  2. package/dist/engine.d.ts.map +1 -1
  3. package/dist/engine.js +165 -26
  4. package/dist/model.d.ts +5 -1
  5. package/dist/model.d.ts.map +1 -1
  6. package/dist/model.js +14 -1
  7. package/dist/physics/body.d.ts +4 -1
  8. package/dist/physics/body.d.ts.map +1 -1
  9. package/dist/physics/body.js +86 -20
  10. package/dist/physics/constraint.d.ts +15 -2
  11. package/dist/physics/constraint.d.ts.map +1 -1
  12. package/dist/physics/constraint.js +49 -2
  13. package/dist/physics/physics.d.ts +3 -0
  14. package/dist/physics/physics.d.ts.map +1 -1
  15. package/dist/physics/physics.js +69 -1
  16. package/dist/physics/solver.d.ts.map +1 -1
  17. package/dist/physics/solver.js +289 -113
  18. package/dist/physics/types.d.ts +2 -1
  19. package/dist/physics/types.d.ts.map +1 -1
  20. package/dist/physics/types.js +5 -0
  21. package/dist/physics/world.d.ts +3 -0
  22. package/dist/physics/world.d.ts.map +1 -1
  23. package/dist/physics/world.js +19 -3
  24. package/dist/pmx-loader.d.ts +2 -0
  25. package/dist/pmx-loader.d.ts.map +1 -1
  26. package/dist/pmx-loader.js +38 -20
  27. package/dist/shaders/materials/body.d.ts +1 -1
  28. package/dist/shaders/materials/body.d.ts.map +1 -1
  29. package/dist/shaders/materials/body.js +90 -85
  30. package/dist/shaders/materials/cloth_rough.d.ts +1 -1
  31. package/dist/shaders/materials/cloth_rough.d.ts.map +1 -1
  32. package/dist/shaders/materials/cloth_rough.js +1 -1
  33. package/dist/shaders/materials/cloth_smooth.d.ts +1 -1
  34. package/dist/shaders/materials/cloth_smooth.d.ts.map +1 -1
  35. package/dist/shaders/materials/cloth_smooth.js +2 -2
  36. package/dist/shaders/materials/common.d.ts +1 -1
  37. package/dist/shaders/materials/common.d.ts.map +1 -1
  38. package/dist/shaders/materials/common.js +137 -112
  39. package/dist/shaders/materials/default.d.ts +1 -1
  40. package/dist/shaders/materials/default.d.ts.map +1 -1
  41. package/dist/shaders/materials/default.js +37 -32
  42. package/dist/shaders/materials/eye.d.ts +1 -1
  43. package/dist/shaders/materials/eye.d.ts.map +1 -1
  44. package/dist/shaders/materials/eye.js +54 -35
  45. package/dist/shaders/materials/face.d.ts +1 -1
  46. package/dist/shaders/materials/face.d.ts.map +1 -1
  47. package/dist/shaders/materials/face.js +90 -85
  48. package/dist/shaders/materials/hair.d.ts +1 -1
  49. package/dist/shaders/materials/hair.d.ts.map +1 -1
  50. package/dist/shaders/materials/hair.js +85 -75
  51. package/dist/shaders/materials/metal.d.ts +1 -1
  52. package/dist/shaders/materials/metal.d.ts.map +1 -1
  53. package/dist/shaders/materials/metal.js +1 -1
  54. package/dist/shaders/materials/mmd_classic.d.ts +2 -0
  55. package/dist/shaders/materials/mmd_classic.d.ts.map +1 -0
  56. package/dist/shaders/materials/mmd_classic.js +66 -0
  57. package/dist/shaders/materials/stockings.d.ts +1 -1
  58. package/dist/shaders/materials/stockings.d.ts.map +1 -1
  59. package/dist/shaders/materials/stockings.js +1 -1
  60. package/package.json +1 -1
  61. package/src/engine.ts +4198 -4030
  62. package/src/model.ts +21 -1
  63. package/src/physics/body.ts +87 -24
  64. package/src/physics/constraint.ts +80 -6
  65. package/src/physics/physics.ts +68 -1
  66. package/src/physics/solver.ts +915 -744
  67. package/src/physics/types.ts +10 -2
  68. package/src/physics/world.ts +20 -3
  69. package/src/pmx-loader.ts +39 -20
  70. package/src/shaders/materials/body.ts +97 -92
  71. package/src/shaders/materials/cloth_rough.ts +1 -1
  72. package/src/shaders/materials/cloth_smooth.ts +2 -2
  73. package/src/shaders/materials/common.ts +204 -179
  74. package/src/shaders/materials/default.ts +45 -40
  75. package/src/shaders/materials/eye.ts +62 -43
  76. package/src/shaders/materials/face.ts +97 -92
  77. package/src/shaders/materials/hair.ts +92 -82
  78. package/src/shaders/materials/metal.ts +1 -1
  79. package/src/shaders/materials/mmd_classic.ts +68 -0
  80. package/src/shaders/materials/stockings.ts +1 -1
@@ -1,92 +1,97 @@
1
- // M_Face — 仿深空之眼渲染预设v1.0_by_小绿毛猫 "M_Face". Toon + warm rim + dual fresnel
2
- // rim + BT.601 bright-tex gate, mixed 50/50 against a Principled BSDF with noise bump.
3
-
4
- import { NODES_WGSL } from "./nodes"
5
- import { COMMON_MATERIAL_PRELUDE_WGSL } from "./common"
6
-
7
- export const FACE_SHADER_WGSL = /* wgsl */ `
8
-
9
- ${NODES_WGSL}
10
- ${COMMON_MATERIAL_PRELUDE_WGSL}
11
-
12
- const FACE_SPECULAR: f32 = 0.5;
13
- const FACE_ROUGHNESS: f32 = 0.3;
14
- const FACE_MIX_NPR: f32 = 0.5;
15
- const FACE_SPEC_CLAMP: f32 = 10.0;
16
- const FACE_RIM2_POW: f32 = 0.6300000548362732;
17
- const FACE_RIM2_BG: vec3f = vec3f(1.0, 0.4684903025627136, 0.3698573112487793);
18
- const FACE_WARM_STR: f32 = 0.30000001192092896;
19
- const FACE_BRIGHT_TEX_THRESH: f32 = 0.9300000071525574;
20
-
21
- @fragment fn fs(input: VertexOutput) -> FSOut {
22
- let alpha = material.alpha;
23
- if (alpha < 0.001) { discard; }
24
-
25
- let n = normalize(input.normal);
26
- let v = normalize(camera.viewPos - input.worldPos);
27
- let l = -light.lights[0].direction.xyz;
28
- let sun = light.lights[0].color.xyz * light.lights[0].color.w;
29
- let amb = light.ambientColor.xyz;
30
- let shadow = sampleShadow(input.worldPos, n);
31
-
32
- let tex_color = textureSample(diffuseTexture, diffuseSampler, input.uv).rgb;
33
-
34
- // ═══ NPR STACK ═══
35
- let ndotl_raw = shader_to_rgb_diffuse(n, l, sun, amb, shadow);
36
- // ramp_constant_edge_aa: avoids binary fac shimmer on terminator (fwidth + smoothstep).
37
- let toon = ramp_constant_edge_aa(ndotl_raw, 0.2966, vec4f(0,0,0,1), vec4f(1,1,1,1)).r;
38
-
39
- let shadow_tint = hue_sat(0.46000000834465027, 2.0, 0.3499999940395355, 1.0, tex_color);
40
- let lit_tint = hue_sat(0.46000000834465027, 1.600000023841858, 1.5, 1.0, tex_color);
41
- let toon_color = mix_blend(toon, shadow_tint, lit_tint);
42
- let bc = bright_contrast(toon_color, 0.1, 0.2);
43
-
44
- let emission3 = bc * 2.5;
45
-
46
- let warm_input = clamp(toon * 0.5 + 0.5, 0.0, 1.0);
47
- let warm_color = ramp_cardinal(warm_input, 0.2409,
48
- vec4f(0.2426, 0.068, 0.0588, 1.0), 0.4663,
49
- vec4f(0.6677, 0.5024, 0.5126, 1.0)).rgb;
50
- let warm_emission = warm_color * FACE_WARM_STR;
51
-
52
- let rim1_str = fresnel(2.0, n, v) * layer_weight_facing(0.24, n, v);
53
- let rim1 = vec3f(0.984157919883728, 0.6110184788703918, 0.5736401677131653) * rim1_str;
54
-
55
- let rim2_raw = fresnel(1.45, n, v) * layer_weight_fresnel(0.61, n, v);
56
- let rim2_fac = math_power(rim2_raw, FACE_RIM2_POW);
57
- let rim2_mixed = mix(emission3, FACE_RIM2_BG, rim2_fac);
58
-
59
- // Blender implicitly converts Color → Float via BT.601 grayscale when a color output
60
- // feeds a Math node's Value input. Using tex_color.r instead fires on R-dominant skin
61
- // and produces firefly speckles on near-white R pixels — color_to_value matches the
62
- // Blender socket semantic and only gates genuinely near-white painted features.
63
- let tex_gate = math_greater_than(color_to_value(tex_color), FACE_BRIGHT_TEX_THRESH);
64
- let bright_emit = vec3f(tex_gate) * 3.0;
65
-
66
- let npr_stack = rim1 + rim2_mixed + bright_emit + warm_emission;
67
-
68
- // ═══ PRINCIPLED BSDF with noise bump ═══
69
- // Height sampled in rest space (restPos) so the micro-bump doesn't swim under motion;
70
- // bump_lh's geometric basis stays in worldPos so the perturbed normal lands in the same
71
- // world frame as n (screen-space height gradient still pairs correctly with it).
72
- let noise_val = tex_noise_d2(input.restPos * vec3f(1.0, 1.0, 1.5), 1.0);
73
- let noise_ramp = ramp_linear(noise_val, 0.0, vec4f(0,0,0,1), 1.0, vec4f(1,1,1,1)).r;
74
- let bumped_n = bump_lh(0.324644535779953, noise_ramp, n, input.worldPos);
75
-
76
- let principled_base = mix_blend(noise_ramp, bc, vec3f(0.6832, 0.1947, 0.1373));
77
- let p_emission = bc * 0.2;
78
-
79
- let principled = eval_principled(
80
- PrincipledIn(principled_base, 0.0, FACE_SPECULAR, FACE_ROUGHNESS, FACE_SPEC_CLAMP, 0.0, 0.0),
81
- bumped_n, l, v, sun, amb, shadow
82
- ) + p_emission;
83
-
84
- let final_color = mix(npr_stack, principled, FACE_MIX_NPR);
85
-
86
- var out: FSOut;
87
- out.color = vec4f(final_color, alpha);
88
- out.mask = vec4f(1.0, 1.0, 0.0, out.color.a);
89
- return out;
90
- }
91
-
92
- `
1
+ // M_Face — 仿深空之眼渲染预设v1.0_by_小绿毛猫 "M_Face". Toon + warm rim + dual fresnel
2
+ // rim + BT.601 bright-tex gate, mixed 50/50 against a Principled BSDF with noise bump.
3
+
4
+ import { NODES_WGSL } from "./nodes"
5
+ import { COMMON_MATERIAL_PRELUDE_WGSL } from "./common"
6
+
7
+ export const FACE_SHADER_WGSL = /* wgsl */ `
8
+
9
+ ${NODES_WGSL}
10
+ ${COMMON_MATERIAL_PRELUDE_WGSL}
11
+
12
+ const FACE_SPECULAR: f32 = 0.5;
13
+ const FACE_ROUGHNESS: f32 = 0.3;
14
+ const FACE_MIX_NPR: f32 = 0.5;
15
+ const FACE_SPEC_CLAMP: f32 = 10.0;
16
+ const FACE_RIM2_POW: f32 = 0.6300000548362732;
17
+ const FACE_RIM2_BG: vec3f = vec3f(1.0, 0.4684903025627136, 0.3698573112487793);
18
+ const FACE_WARM_STR: f32 = 0.30000001192092896;
19
+ const FACE_BRIGHT_TEX_THRESH: f32 = 0.9300000071525574;
20
+
21
+ @fragment fn fs(input: VertexOutput) -> FSOut {
22
+ let tex_s = textureSample(diffuseTexture, diffuseSampler, input.uv);
23
+ // MMD alpha semantics: material alpha × texture alpha. Hair/lace/accessory
24
+ // textures cut their shapes in the alpha channel — ignoring it renders each
25
+ // card's full quad with the texture's padding color (white shimmer on dark
26
+ // hair for models whose textures pad with white).
27
+ let alpha = material.alpha * tex_s.a;
28
+ if (alpha < 0.001) { discard; }
29
+
30
+ let n = safe_normal(input.normal);
31
+ let v = normalize(camera.viewPos - input.worldPos);
32
+ let l = -light.lights[0].direction.xyz;
33
+ let sun = light.lights[0].color.xyz * light.lights[0].color.w;
34
+ let amb = light.ambientColor.xyz;
35
+ let shadow = sampleShadow(input.worldPos, n);
36
+
37
+ let tex_color = tex_s.rgb;
38
+
39
+ // ═══ NPR STACK ═══
40
+ let ndotl_raw = shader_to_rgb_diffuse(n, l, sun, amb, shadow);
41
+ // ramp_constant_edge_aa: avoids binary fac shimmer on terminator (fwidth + smoothstep).
42
+ let toon = ramp_constant_edge_aa(ndotl_raw, 0.2966, vec4f(0,0,0,1), vec4f(1,1,1,1)).r;
43
+
44
+ let shadow_tint = hue_sat(0.46000000834465027, 2.0, 0.3499999940395355, 1.0, tex_color);
45
+ let lit_tint = hue_sat(0.46000000834465027, 1.600000023841858, 1.5, 1.0, tex_color);
46
+ let toon_color = mix_blend(toon, shadow_tint, lit_tint);
47
+ let bc = bright_contrast(toon_color, 0.1, 0.2);
48
+
49
+ let emission3 = bc * 2.5;
50
+
51
+ let warm_input = clamp(toon * 0.5 + 0.5, 0.0, 1.0);
52
+ let warm_color = ramp_cardinal(warm_input, 0.2409,
53
+ vec4f(0.2426, 0.068, 0.0588, 1.0), 0.4663,
54
+ vec4f(0.6677, 0.5024, 0.5126, 1.0)).rgb;
55
+ let warm_emission = warm_color * FACE_WARM_STR;
56
+
57
+ let rim1_str = fresnel(2.0, n, v) * layer_weight_facing(0.24, n, v);
58
+ let rim1 = vec3f(0.984157919883728, 0.6110184788703918, 0.5736401677131653) * rim1_str;
59
+
60
+ let rim2_raw = fresnel(1.45, n, v) * layer_weight_fresnel(0.61, n, v);
61
+ let rim2_fac = math_power(rim2_raw, FACE_RIM2_POW);
62
+ let rim2_mixed = mix(emission3, FACE_RIM2_BG, rim2_fac);
63
+
64
+ // Blender implicitly converts Color → Float via BT.601 grayscale when a color output
65
+ // feeds a Math node's Value input. Using tex_color.r instead fires on R-dominant skin
66
+ // and produces firefly speckles on near-white R pixels — color_to_value matches the
67
+ // Blender socket semantic and only gates genuinely near-white painted features.
68
+ let tex_gate = math_greater_than(color_to_value(tex_color), FACE_BRIGHT_TEX_THRESH);
69
+ let bright_emit = vec3f(tex_gate) * 3.0;
70
+
71
+ let npr_stack = rim1 + rim2_mixed + bright_emit + warm_emission;
72
+
73
+ // ═══ PRINCIPLED BSDF with noise bump ═══
74
+ // Height sampled in rest space (restPos) so the micro-bump doesn't swim under motion;
75
+ // bump_lh's geometric basis stays in worldPos so the perturbed normal lands in the same
76
+ // world frame as n (screen-space height gradient still pairs correctly with it).
77
+ let noise_val = tex_noise_d2(input.restPos * vec3f(1.0, 1.0, 1.5), 1.0);
78
+ let noise_ramp = ramp_linear(noise_val, 0.0, vec4f(0,0,0,1), 1.0, vec4f(1,1,1,1)).r;
79
+ let bumped_n = bump_lh(0.324644535779953, noise_ramp, n, input.worldPos);
80
+
81
+ let principled_base = mix_blend(noise_ramp, bc, vec3f(0.6832, 0.1947, 0.1373));
82
+ let p_emission = bc * 0.2;
83
+
84
+ let principled = eval_principled(
85
+ PrincipledIn(principled_base, 0.0, FACE_SPECULAR, FACE_ROUGHNESS, FACE_SPEC_CLAMP, 0.0, 0.0),
86
+ bumped_n, l, v, sun, amb, shadow
87
+ ) + p_emission;
88
+
89
+ let final_color = mix(npr_stack, principled, FACE_MIX_NPR);
90
+
91
+ var out: FSOut;
92
+ out.color = vec4f(final_color, alpha);
93
+ out.mask = vec4f(1.0, 1.0, 0.0, out.color.a);
94
+ return out;
95
+ }
96
+
97
+ `
@@ -1,82 +1,92 @@
1
- // M_Hair — 仿深空之眼渲染预设v1.0_by_小绿毛猫 "M_Hair". Toon + fresnel rim + bevel +
2
- // bright-tex gate, mixed 80/20 NPR/PBR. MixShader.001 Fac=0.2 keeps Principled subtle.
3
-
4
- import { NODES_WGSL } from "./nodes"
5
- import { COMMON_MATERIAL_PRELUDE_WGSL } from "./common"
6
-
7
- export const HAIR_SHADER_WGSL = /* wgsl */ `
8
-
9
- ${NODES_WGSL}
10
- ${COMMON_MATERIAL_PRELUDE_WGSL}
11
-
12
- // Pipeline-override: the engine compiles two variants — the normal opaque hair pipeline
13
- // (IS_OVER_EYES=false) and a second pipeline that re-draws hair fragments stencil-matched
14
- // against the eye stamp with 25% alpha so eyes read through the hair silhouette. Resolved
15
- // at pipeline-compile time; the dead branch is dropped by the shader compiler.
16
- override IS_OVER_EYES: bool = false;
17
-
18
- const HAIR_SPECULAR: f32 = 1.0;
19
- const HAIR_ROUGHNESS: f32 = 0.3;
20
- const HAIR_TEX_GATE_THRESH: f32 = 0.15000000596046448;
21
- const HAIR_RIM2_POW: f32 = 0.6300000548362732;
22
- const HAIR_MIX_BG: vec3f = vec3f(0.1673291176557541);
23
- const HAIR_MIX_NPR: f32 = 0.2;
24
-
25
- @fragment fn fs(input: VertexOutput) -> FSOut {
26
- let alpha = material.alpha;
27
- if (alpha < 0.001) { discard; }
28
-
29
- let n = normalize(input.normal);
30
- let v = normalize(camera.viewPos - input.worldPos);
31
- let l = -light.lights[0].direction.xyz;
32
- let sun = light.lights[0].color.xyz * light.lights[0].color.w;
33
- let amb = light.ambientColor.xyz;
34
- let shadow = sampleShadow(input.worldPos, n);
35
-
36
- let tex_color = textureSample(diffuseTexture, diffuseSampler, input.uv).rgb;
37
-
38
- // ═══ NPR STACK ═══
39
- let hue_sat_shadow = hue_sat_id(1.2, 0.5, 1.0, tex_color);
40
- let hue_sat_002 = hue_sat(0.48, 1.2, 0.7, 1.0, hue_sat_shadow);
41
- let hue_sat_001 = hue_sat_id(1.5, 1.0, 1.0, tex_color);
42
-
43
- let ndotl_raw = shader_to_rgb_diffuse(n, l, sun, amb, shadow);
44
- let ramp_008 = ramp_constant(ndotl_raw, 0.0, vec4f(0,0,0,1), 0.2966, vec4f(1,1,1,1)).r;
45
-
46
- let mix_004 = mix_blend(ramp_008, hue_sat_002, hue_sat_001);
47
- let bc = bright_contrast(mix_004, 0.1, 0.2);
48
-
49
- let bevel_z = clamp(n.y, 0.0, 1.0);
50
- let mix_003 = mix_blend(bevel_z, bc, hue_sat_002);
51
-
52
- let rim2_raw = fresnel(1.45, n, v) * layer_weight_fresnel(0.61, n, v);
53
- let rim2_fac = math_power(rim2_raw, HAIR_RIM2_POW);
54
- let mix_shader_002 = mix(mix_003, HAIR_MIX_BG, rim2_fac);
55
-
56
- // GREATER_THAN on a color input uses BT.601 luminance — same socket-semantic fix as face.ts.
57
- let tex_gate = math_greater_than(color_to_value(tex_color), HAIR_TEX_GATE_THRESH);
58
- let gate_emit = vec3f(tex_gate) * 0.1;
59
-
60
- let npr_stack = mix_shader_002 + gate_emit;
61
-
62
- // ═══ PRINCIPLED BSDF ═══
63
- // Graph has a noise→normal_map bump (Strength=0.1) on Principled.Normal, but MixShader.001
64
- // weights Principled at only 0.2 — the bumped spec × that weight is imperceptible, so we
65
- // drop the subtree and keep plain n (saves a tex_noise + bump_lh per hair fragment).
66
- let principled = eval_principled(
67
- PrincipledIn(bc, 0.0, HAIR_SPECULAR, HAIR_ROUGHNESS, 1e30, 0.0, 0.0),
68
- n, l, v, sun, amb, shadow
69
- );
70
-
71
- let final_color = mix(npr_stack, principled, HAIR_MIX_NPR);
72
-
73
- var outAlpha = alpha;
74
- if (IS_OVER_EYES) { outAlpha = alpha * 0.25; }
75
-
76
- var out: FSOut;
77
- out.color = vec4f(final_color, outAlpha);
78
- out.mask = vec4f(1.0, 1.0, 0.0, out.color.a);
79
- return out;
80
- }
81
-
82
- `
1
+ // M_Hair — 仿深空之眼渲染预设v1.0_by_小绿毛猫 "M_Hair". Toon + fresnel rim + bevel +
2
+ // bright-tex gate, mixed 80/20 NPR/PBR. MixShader.001 Fac=0.2 keeps Principled subtle.
3
+
4
+ import { NODES_WGSL } from "./nodes"
5
+ import { COMMON_MATERIAL_PRELUDE_WGSL } from "./common"
6
+
7
+ export const HAIR_SHADER_WGSL = /* wgsl */ `
8
+
9
+ ${NODES_WGSL}
10
+ ${COMMON_MATERIAL_PRELUDE_WGSL}
11
+
12
+ // Pipeline-override: the engine compiles two variants — the normal opaque hair pipeline
13
+ // (IS_OVER_EYES=false) and a second pipeline that re-draws hair fragments stencil-matched
14
+ // against the eye stamp with 25% alpha so eyes read through the hair silhouette. Resolved
15
+ // at pipeline-compile time; the dead branch is dropped by the shader compiler.
16
+ override IS_OVER_EYES: bool = false;
17
+
18
+ const HAIR_SPECULAR: f32 = 1.0;
19
+ const HAIR_ROUGHNESS: f32 = 0.3;
20
+ // EEVEE Light Clamp equivalent (same value as body/face/cloth_rough). Hair
21
+ // is the firefly-prone material: physics-animated with the noisiest blended
22
+ // normals, so grazing NV spikes the GGX term and flashes white through
23
+ // bloom without this.
24
+ const HAIR_SPEC_CLAMP: f32 = 10.0;
25
+ const HAIR_TEX_GATE_THRESH: f32 = 0.15000000596046448;
26
+ const HAIR_RIM2_POW: f32 = 0.6300000548362732;
27
+ const HAIR_MIX_BG: vec3f = vec3f(0.1673291176557541);
28
+ const HAIR_MIX_NPR: f32 = 0.2;
29
+
30
+ @fragment fn fs(input: VertexOutput) -> FSOut {
31
+ let tex_s = textureSample(diffuseTexture, diffuseSampler, input.uv);
32
+ // MMD alpha semantics: material alpha × texture alpha. Hair/lace/accessory
33
+ // textures cut their shapes in the alpha channel — ignoring it renders each
34
+ // card's full quad with the texture's padding color (white shimmer on dark
35
+ // hair for models whose textures pad with white).
36
+ let alpha = material.alpha * tex_s.a;
37
+ if (alpha < 0.001) { discard; }
38
+
39
+ let n = safe_normal(input.normal);
40
+ let v = normalize(camera.viewPos - input.worldPos);
41
+ let l = -light.lights[0].direction.xyz;
42
+ let sun = light.lights[0].color.xyz * light.lights[0].color.w;
43
+ let amb = light.ambientColor.xyz;
44
+ let shadow = sampleShadow(input.worldPos, n);
45
+
46
+ let tex_color = tex_s.rgb;
47
+
48
+ // ═══ NPR STACK ═══
49
+ let hue_sat_shadow = hue_sat_id(1.2, 0.5, 1.0, tex_color);
50
+ let hue_sat_002 = hue_sat(0.48, 1.2, 0.7, 1.0, hue_sat_shadow);
51
+ let hue_sat_001 = hue_sat_id(1.5, 1.0, 1.0, tex_color);
52
+
53
+ let ndotl_raw = shader_to_rgb_diffuse(n, l, sun, amb, shadow);
54
+ let ramp_008 = ramp_constant(ndotl_raw, 0.0, vec4f(0,0,0,1), 0.2966, vec4f(1,1,1,1)).r;
55
+
56
+ let mix_004 = mix_blend(ramp_008, hue_sat_002, hue_sat_001);
57
+ let bc = bright_contrast(mix_004, 0.1, 0.2);
58
+
59
+ let bevel_z = clamp(n.y, 0.0, 1.0);
60
+ let mix_003 = mix_blend(bevel_z, bc, hue_sat_002);
61
+
62
+ let rim2_raw = fresnel(1.45, n, v) * layer_weight_fresnel(0.61, n, v);
63
+ let rim2_fac = math_power(rim2_raw, HAIR_RIM2_POW);
64
+ let mix_shader_002 = mix(mix_003, HAIR_MIX_BG, rim2_fac);
65
+
66
+ // GREATER_THAN on a color input uses BT.601 luminance — same socket-semantic fix as face.ts.
67
+ let tex_gate = math_greater_than(color_to_value(tex_color), HAIR_TEX_GATE_THRESH);
68
+ let gate_emit = vec3f(tex_gate) * 0.1;
69
+
70
+ let npr_stack = mix_shader_002 + gate_emit;
71
+
72
+ // ═══ PRINCIPLED BSDF ═══
73
+ // Graph has a noise→normal_map bump (Strength=0.1) on Principled.Normal, but MixShader.001
74
+ // weights Principled at only 0.2 — the bumped spec × that weight is imperceptible, so we
75
+ // drop the subtree and keep plain n (saves a tex_noise + bump_lh per hair fragment).
76
+ let principled = eval_principled(
77
+ PrincipledIn(bc, 0.0, HAIR_SPECULAR, HAIR_ROUGHNESS, HAIR_SPEC_CLAMP, 0.0, 0.0),
78
+ n, l, v, sun, amb, shadow
79
+ );
80
+
81
+ let final_color = mix(npr_stack, principled, HAIR_MIX_NPR);
82
+
83
+ var outAlpha = alpha;
84
+ if (IS_OVER_EYES) { outAlpha = alpha * 0.25; }
85
+
86
+ var out: FSOut;
87
+ out.color = vec4f(final_color, outAlpha);
88
+ out.mask = vec4f(1.0, 1.0, 0.0, out.color.a);
89
+ return out;
90
+ }
91
+
92
+ `
@@ -26,7 +26,7 @@ const METAL_MIX_SHADER_FAC: f32 = 0.6967;
26
26
  const METAL_VORONOI_SCALE: f32 = 4.3;
27
27
 
28
28
  @fragment fn fs(input: VertexOutput) -> FSOut {
29
- let n = normalize(input.normal);
29
+ let n = safe_normal(input.normal);
30
30
  let v = normalize(camera.viewPos - input.worldPos);
31
31
  let l = -light.lights[0].direction.xyz;
32
32
  let sun = light.lights[0].color.xyz * light.lights[0].color.w;
@@ -0,0 +1,68 @@
1
+ // M_MMDClassic — authentic MMD/MikuMikuDance fixed-pipeline material. Uses the
2
+ // PMX material data every model author actually tuned: ambient + diffuse×light
3
+ // base, toon-ramp shading (ramp carries the terminator, NOT an N·L multiply),
4
+ // sphere map (sph multiply / spa add), and Blinn-Phong specular with the PMX
5
+ // shininess. This is the universal fallback for materials no preset map or
6
+ // name heuristic covers — it renders what the author saw in MMD/PMXEditor.
7
+
8
+ import { COMMON_MATERIAL_PRELUDE_WGSL } from "./common"
9
+
10
+ export const MMD_CLASSIC_SHADER_WGSL = /* wgsl */ `
11
+
12
+ ${COMMON_MATERIAL_PRELUDE_WGSL}
13
+
14
+ @fragment fn fs(input: VertexOutput) -> FSOut {
15
+ let tex_s = textureSample(diffuseTexture, diffuseSampler, input.uv);
16
+ // MMD alpha semantics: material alpha × texture alpha.
17
+ let alpha = material.alpha * tex_s.a;
18
+ if (alpha < 0.001) { discard; }
19
+
20
+ let n = safe_normal(input.normal);
21
+ let v = normalize(camera.viewPos - input.worldPos);
22
+ let l = -light.lights[0].direction.xyz;
23
+ let sun = light.lights[0].color.xyz * light.lights[0].color.w;
24
+ let amb = light.ambientColor.xyz;
25
+ let shadow = sampleShadow(input.worldPos, n);
26
+
27
+ // Sphere-map UV from the view-space normal — sampled up front to keep the
28
+ // texture reads in uniform control flow.
29
+ let vn = normalize((camera.view * vec4f(n, 0.0)).xyz);
30
+ let sph_uv = vec2f(vn.x * 0.5 + 0.5, 0.5 - vn.y * 0.5);
31
+ let sphere_rgb = textureSample(sphereTexture, diffuseSampler, sph_uv).rgb;
32
+
33
+ // Toon ramp: v runs light (0) → shadow (1); the ramp texture carries the
34
+ // terminator shape and shadow tint. Self-shadow pushes toward the shadow
35
+ // end by attenuating N·L. Sampled up front for the same uniformity reason.
36
+ let ndl = dot(n, l) * shadow;
37
+ let toon_v = clamp(0.5 - 0.5 * ndl, 0.0, 1.0);
38
+ let toon_rgb = textureSample(toonTexture, diffuseSampler, vec2f(0.5, toon_v)).rgb;
39
+
40
+ // MMD base: saturate(ambient + diffuse × light color), modulated by texture.
41
+ // The engine's ambient light scales the material-ambient floor so scene
42
+ // lighting still has authority over overall exposure.
43
+ let base = clamp(material.ambient * max(amb, vec3f(0.35)) + material.diffuseColor * sun, vec3f(0.0), vec3f(1.0));
44
+ var color = base * tex_s.rgb * toon_rgb;
45
+
46
+ // Sphere map: 1 = sph (multiply), 2 = spa (add). Mode 3 (sub-texture) and
47
+ // 0 fall through unchanged.
48
+ if (material.sphereMode == 1.0) {
49
+ color *= sphere_rgb;
50
+ } else if (material.sphereMode == 2.0) {
51
+ color += sphere_rgb;
52
+ }
53
+
54
+ // Blinn-Phong specular with PMX shininess. shininess 0 disables (many
55
+ // rigs author 0 to mean "no highlight").
56
+ if (material.shininess > 0.0) {
57
+ let h = normalize(l + v);
58
+ let spec = pow(max(dot(n, h), 0.0), max(material.shininess, 1.0));
59
+ color += material.specular * sun * (spec * shadow);
60
+ }
61
+
62
+ var out: FSOut;
63
+ out.color = vec4f(color, alpha);
64
+ out.mask = vec4f(1.0, 1.0, 0.0, out.color.a);
65
+ return out;
66
+ }
67
+
68
+ `
@@ -66,7 +66,7 @@ fn ramp_ease_s(f: f32, p0: f32, p1: f32) -> f32 {
66
66
  }
67
67
 
68
68
  @fragment fn fs(input: VertexOutput) -> FSOut {
69
- let n = normalize(input.normal);
69
+ let n = safe_normal(input.normal);
70
70
  let v = normalize(camera.viewPos - input.worldPos);
71
71
  let l = -light.lights[0].direction.xyz;
72
72
  let sun = light.lights[0].color.xyz * light.lights[0].color.w;