reze-engine 0.16.3 → 0.17.1
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.
- package/README.md +252 -250
- package/dist/engine.d.ts +3 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +160 -21
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/model.d.ts +1 -0
- package/dist/model.d.ts.map +1 -1
- package/dist/physics/body.d.ts +3 -1
- package/dist/physics/body.d.ts.map +1 -1
- package/dist/physics/body.js +84 -20
- package/dist/physics/constraint.d.ts +14 -1
- package/dist/physics/constraint.d.ts.map +1 -1
- package/dist/physics/constraint.js +43 -1
- package/dist/physics/solver.d.ts.map +1 -1
- package/dist/physics/solver.js +278 -110
- package/dist/pmx-loader.d.ts.map +1 -1
- package/dist/pmx-loader.js +1 -0
- package/dist/shaders/materials/body.d.ts +1 -1
- package/dist/shaders/materials/body.d.ts.map +1 -1
- package/dist/shaders/materials/cloth_rough.d.ts +1 -1
- package/dist/shaders/materials/cloth_rough.d.ts.map +1 -1
- package/dist/shaders/materials/cloth_smooth.d.ts +1 -1
- package/dist/shaders/materials/cloth_smooth.d.ts.map +1 -1
- package/dist/shaders/materials/common.d.ts +1 -1
- package/dist/shaders/materials/common.d.ts.map +1 -1
- package/dist/shaders/materials/common.js +137 -122
- package/dist/shaders/materials/default.d.ts +1 -1
- package/dist/shaders/materials/default.d.ts.map +1 -1
- package/dist/shaders/materials/eye.d.ts +1 -1
- package/dist/shaders/materials/eye.d.ts.map +1 -1
- package/dist/shaders/materials/eye.js +14 -0
- package/dist/shaders/materials/face.d.ts +1 -1
- package/dist/shaders/materials/face.d.ts.map +1 -1
- package/dist/shaders/materials/hair.d.ts +1 -1
- package/dist/shaders/materials/hair.d.ts.map +1 -1
- package/dist/shaders/materials/metal.d.ts +1 -1
- package/dist/shaders/materials/metal.d.ts.map +1 -1
- package/dist/shaders/materials/mmd_classic.d.ts +2 -0
- package/dist/shaders/materials/mmd_classic.d.ts.map +1 -0
- package/dist/shaders/materials/mmd_classic.js +66 -0
- package/dist/shaders/materials/stockings.d.ts +1 -1
- package/dist/shaders/materials/stockings.d.ts.map +1 -1
- package/package.json +42 -42
- package/src/engine.ts +4204 -4045
- package/src/index.ts +29 -28
- package/src/model.ts +3 -0
- package/src/physics/body.ts +82 -24
- package/src/physics/constraint.ts +74 -5
- package/src/physics/solver.ts +915 -752
- package/src/pmx-loader.ts +1 -0
- package/src/shaders/materials/common.ts +204 -189
- package/src/shaders/materials/eye.ts +14 -0
- package/src/shaders/materials/mmd_classic.ts +68 -0
package/src/pmx-loader.ts
CHANGED
|
@@ -1,189 +1,204 @@
|
|
|
1
|
-
// Shared WGSL blocks concatenated by every material shader.
|
|
2
|
-
// Splits the boilerplate (uniform structs, bind group layout, skinning VS, PCF shadow)
|
|
3
|
-
// away from the per-material fragment code so each material file only contains what
|
|
4
|
-
// makes it visually distinct.
|
|
5
|
-
//
|
|
6
|
-
// Concat order in every material:
|
|
7
|
-
// NODES_WGSL (nodes.ts — math/noise/BSDF helpers)
|
|
8
|
-
// COMMON_BINDINGS_WGSL (uniform structs + @group/@binding declarations)
|
|
9
|
-
// SAMPLE_SHADOW_WGSL (3×3 PCF shadow sampler; reads bindings above)
|
|
10
|
-
// COMMON_VS_WGSL (skinning vertex shader; reads bindings above)
|
|
11
|
-
// <material's own constants + @fragment fn fs>
|
|
12
|
-
//
|
|
13
|
-
// WGSL is a whole-module compile — declaration order at module scope doesn't matter,
|
|
14
|
-
// but the readable order is: types → bindings → helpers → entry points.
|
|
15
|
-
|
|
16
|
-
// ─── Uniform structs + bind group layout ────────────────────────────
|
|
17
|
-
// Every material pipeline uses the same bind group layout, so the same bindings are
|
|
18
|
-
// declared here once. Groups:
|
|
19
|
-
// group(0): per-frame scene (camera, lights, shadow map, BRDF LUT via nodes.ts)
|
|
20
|
-
// group(1): per-model skinning
|
|
21
|
-
// group(2): per-material (diffuse texture + material uniforms)
|
|
22
|
-
|
|
23
|
-
export const COMMON_BINDINGS_WGSL = /* wgsl */ `
|
|
24
|
-
|
|
25
|
-
struct CameraUniforms {
|
|
26
|
-
view: mat4x4f,
|
|
27
|
-
projection: mat4x4f,
|
|
28
|
-
viewPos: vec3f,
|
|
29
|
-
_padding: f32,
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
struct Light {
|
|
33
|
-
direction: vec4f,
|
|
34
|
-
color: vec4f,
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
struct LightUniforms {
|
|
38
|
-
ambientColor: vec4f,
|
|
39
|
-
lights: array<Light, 4>,
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// Per-material uniforms. Every material binds this layout even if it ignores fields;
|
|
43
|
-
// the engine keeps one bind group layout across all material pipelines.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
@
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
//
|
|
72
|
-
@
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
//
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
fn
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
output
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
//
|
|
171
|
-
//
|
|
172
|
-
//
|
|
173
|
-
//
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
//
|
|
185
|
-
//
|
|
186
|
-
//
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
1
|
+
// Shared WGSL blocks concatenated by every material shader.
|
|
2
|
+
// Splits the boilerplate (uniform structs, bind group layout, skinning VS, PCF shadow)
|
|
3
|
+
// away from the per-material fragment code so each material file only contains what
|
|
4
|
+
// makes it visually distinct.
|
|
5
|
+
//
|
|
6
|
+
// Concat order in every material:
|
|
7
|
+
// NODES_WGSL (nodes.ts — math/noise/BSDF helpers)
|
|
8
|
+
// COMMON_BINDINGS_WGSL (uniform structs + @group/@binding declarations)
|
|
9
|
+
// SAMPLE_SHADOW_WGSL (3×3 PCF shadow sampler; reads bindings above)
|
|
10
|
+
// COMMON_VS_WGSL (skinning vertex shader; reads bindings above)
|
|
11
|
+
// <material's own constants + @fragment fn fs>
|
|
12
|
+
//
|
|
13
|
+
// WGSL is a whole-module compile — declaration order at module scope doesn't matter,
|
|
14
|
+
// but the readable order is: types → bindings → helpers → entry points.
|
|
15
|
+
|
|
16
|
+
// ─── Uniform structs + bind group layout ────────────────────────────
|
|
17
|
+
// Every material pipeline uses the same bind group layout, so the same bindings are
|
|
18
|
+
// declared here once. Groups:
|
|
19
|
+
// group(0): per-frame scene (camera, lights, shadow map, BRDF LUT via nodes.ts)
|
|
20
|
+
// group(1): per-model skinning
|
|
21
|
+
// group(2): per-material (diffuse texture + material uniforms)
|
|
22
|
+
|
|
23
|
+
export const COMMON_BINDINGS_WGSL = /* wgsl */ `
|
|
24
|
+
|
|
25
|
+
struct CameraUniforms {
|
|
26
|
+
view: mat4x4f,
|
|
27
|
+
projection: mat4x4f,
|
|
28
|
+
viewPos: vec3f,
|
|
29
|
+
_padding: f32,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
struct Light {
|
|
33
|
+
direction: vec4f,
|
|
34
|
+
color: vec4f,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
struct LightUniforms {
|
|
38
|
+
ambientColor: vec4f,
|
|
39
|
+
lights: array<Light, 4>,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Per-material uniforms. Every material binds this layout even if it ignores fields;
|
|
43
|
+
// the engine keeps one bind group layout across all material pipelines. The PMX
|
|
44
|
+
// classic-material fields (ambient/specular/toon/sphere) are consumed by the
|
|
45
|
+
// mmd_classic preset; NPR presets ignore them.
|
|
46
|
+
struct MaterialUniforms {
|
|
47
|
+
diffuseColor: vec3f, // PMX diffuse rgb (mmd_classic); reserved for others
|
|
48
|
+
alpha: f32, // 0 → discard; <1 → transparent draw call
|
|
49
|
+
ambient: vec3f, // PMX ambient rgb
|
|
50
|
+
shininess: f32, // PMX specular power
|
|
51
|
+
specular: vec3f, // PMX specular rgb
|
|
52
|
+
sphereMode: f32, // 0 none · 1 multiply (sph) · 2 add (spa)
|
|
53
|
+
// Skeleton index of the 頭 (head) bone, or -1. Lets the eye shader gate
|
|
54
|
+
// the post-alpha-eye stencil by camera-vs-face hemisphere.
|
|
55
|
+
headBoneIndex: f32,
|
|
56
|
+
_pad0: f32,
|
|
57
|
+
_pad1: f32,
|
|
58
|
+
_pad2: f32,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
struct VertexOutput {
|
|
62
|
+
@builtin(position) position: vec4f,
|
|
63
|
+
@location(0) normal: vec3f,
|
|
64
|
+
@location(1) uv: vec2f,
|
|
65
|
+
@location(2) worldPos: vec3f,
|
|
66
|
+
// Bind-pose object-space position (the raw pre-skin vertex attribute). Procedural
|
|
67
|
+
// textures (noise bump, sparkle, Generated-coord gradients) key off this instead of
|
|
68
|
+
// worldPos so the pattern rides with the surface — otherwise the mesh swims through a
|
|
69
|
+
// world-static noise field under any skinning deformation or root (センター) motion.
|
|
70
|
+
// At rest skinMats are identity so restPos == worldPos, which is why existing noise-
|
|
71
|
+
// scale constants stay valid without retuning.
|
|
72
|
+
@location(3) restPos: vec3f,
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
struct LightVP { viewProj: mat4x4f, };
|
|
76
|
+
|
|
77
|
+
@group(0) @binding(0) var<uniform> camera: CameraUniforms;
|
|
78
|
+
@group(0) @binding(1) var<uniform> light: LightUniforms;
|
|
79
|
+
@group(0) @binding(2) var diffuseSampler: sampler;
|
|
80
|
+
@group(0) @binding(3) var shadowMap: texture_depth_2d;
|
|
81
|
+
@group(0) @binding(4) var shadowSampler: sampler_comparison;
|
|
82
|
+
@group(0) @binding(5) var<uniform> lightVP: LightVP;
|
|
83
|
+
// binding(9) brdfLut is declared inside NODES_WGSL (nodes.ts).
|
|
84
|
+
@group(1) @binding(0) var<storage, read> skinMats: array<mat4x4f>;
|
|
85
|
+
@group(2) @binding(0) var diffuseTexture: texture_2d<f32>;
|
|
86
|
+
@group(2) @binding(1) var<uniform> material: MaterialUniforms;
|
|
87
|
+
// mmd_classic inputs; other presets leave them unread (fallback 1×1 whites).
|
|
88
|
+
@group(2) @binding(2) var toonTexture: texture_2d<f32>;
|
|
89
|
+
@group(2) @binding(3) var sphereTexture: texture_2d<f32>;
|
|
90
|
+
|
|
91
|
+
// Four-bone blended normals can cancel to ~zero on physics-driven parts
|
|
92
|
+
// (opposing bone rotations at 50/50 weights) — normalize(0) is 0/0 = NaN,
|
|
93
|
+
// which poisons the whole shading stack and flashes through bloom. Fall
|
|
94
|
+
// back to up for degenerate normals instead.
|
|
95
|
+
fn safe_normal(nIn: vec3f) -> vec3f {
|
|
96
|
+
let l2 = dot(nIn, nIn);
|
|
97
|
+
if (l2 < 1e-12) { return vec3f(0.0, 1.0, 0.0); }
|
|
98
|
+
return nIn * inverseSqrt(l2);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
`;
|
|
102
|
+
|
|
103
|
+
// ─── Shadow sampler (3×3 PCF) ───────────────────────────────────────
|
|
104
|
+
// 2048-map, normal-bias 0.08, depth-bias 0.001. Unrolled — Safari's Metal backend
|
|
105
|
+
// doesn't unroll nested shadow loops reliably, and the early out on back-facing
|
|
106
|
+
// fragments saves 9 texture taps per skipped pixel.
|
|
107
|
+
|
|
108
|
+
export const SAMPLE_SHADOW_WGSL = /* wgsl */ `
|
|
109
|
+
|
|
110
|
+
fn sampleShadow(worldPos: vec3f, n: vec3f) -> f32 {
|
|
111
|
+
if (dot(n, -light.lights[0].direction.xyz) <= 0.0) { return 0.0; }
|
|
112
|
+
let biasedPos = worldPos + n * 0.08;
|
|
113
|
+
let lclip = lightVP.viewProj * vec4f(biasedPos, 1.0);
|
|
114
|
+
let ndc = lclip.xyz / max(lclip.w, 1e-6);
|
|
115
|
+
let suv = vec2f(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5);
|
|
116
|
+
let cmpZ = ndc.z - 0.001;
|
|
117
|
+
let ts = 1.0 / 2048.0;
|
|
118
|
+
let s00 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(-ts, -ts), cmpZ);
|
|
119
|
+
let s10 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(0.0, -ts), cmpZ);
|
|
120
|
+
let s20 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f( ts, -ts), cmpZ);
|
|
121
|
+
let s01 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(-ts, 0.0), cmpZ);
|
|
122
|
+
let s11 = textureSampleCompareLevel(shadowMap, shadowSampler, suv, cmpZ);
|
|
123
|
+
let s21 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f( ts, 0.0), cmpZ);
|
|
124
|
+
let s02 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(-ts, ts), cmpZ);
|
|
125
|
+
let s12 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f(0.0, ts), cmpZ);
|
|
126
|
+
let s22 = textureSampleCompareLevel(shadowMap, shadowSampler, suv + vec2f( ts, ts), cmpZ);
|
|
127
|
+
return (s00 + s10 + s20 + s01 + s11 + s21 + s02 + s12 + s22) * (1.0 / 9.0);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
`;
|
|
131
|
+
|
|
132
|
+
// ─── Skinning vertex shader ─────────────────────────────────────────
|
|
133
|
+
// Four-bone linear blend skinning. Renormalizes weights when they don't sum to 1
|
|
134
|
+
// (PMX models occasionally ship with unnormalized weights on extras like hair tips).
|
|
135
|
+
// VS normalize on the outgoing normal is skipped — interpolation denormalizes it
|
|
136
|
+
// anyway and every fragment shader does `normalize(input.normal)` as its first line.
|
|
137
|
+
|
|
138
|
+
export const COMMON_VS_WGSL = /* wgsl */ `
|
|
139
|
+
|
|
140
|
+
@vertex fn vs(
|
|
141
|
+
@location(0) position: vec3f,
|
|
142
|
+
@location(1) normal: vec3f,
|
|
143
|
+
@location(2) uv: vec2f,
|
|
144
|
+
@location(3) joints0: vec4<u32>,
|
|
145
|
+
@location(4) weights0: vec4<f32>
|
|
146
|
+
) -> VertexOutput {
|
|
147
|
+
var output: VertexOutput;
|
|
148
|
+
let pos4 = vec4f(position, 1.0);
|
|
149
|
+
let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
|
|
150
|
+
let invWeightSum = select(1.0, 1.0 / weightSum, weightSum > 0.0001);
|
|
151
|
+
let nw = select(vec4f(1.0, 0.0, 0.0, 0.0), weights0 * invWeightSum, weightSum > 0.0001);
|
|
152
|
+
var skinnedPos = vec4f(0.0);
|
|
153
|
+
var skinnedNrm = vec3f(0.0);
|
|
154
|
+
for (var i = 0u; i < 4u; i++) {
|
|
155
|
+
let m = skinMats[joints0[i]];
|
|
156
|
+
let w = nw[i];
|
|
157
|
+
skinnedPos += (m * pos4) * w;
|
|
158
|
+
skinnedNrm += (mat3x3f(m[0].xyz, m[1].xyz, m[2].xyz) * normal) * w;
|
|
159
|
+
}
|
|
160
|
+
output.position = camera.projection * camera.view * vec4f(skinnedPos.xyz, 1.0);
|
|
161
|
+
output.normal = skinnedNrm;
|
|
162
|
+
output.uv = uv;
|
|
163
|
+
output.worldPos = skinnedPos.xyz;
|
|
164
|
+
output.restPos = position;
|
|
165
|
+
return output;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
`;
|
|
169
|
+
|
|
170
|
+
// ─── FS output struct ───────────────────────────────────────────────
|
|
171
|
+
// Location 0: final radiance+alpha (blended into rg11b10ufloat; the HDR target
|
|
172
|
+
// has no alpha channel, but the blend equation still uses the .a you write here
|
|
173
|
+
// as the src-alpha factor that premultiplies rgb into the HDR target).
|
|
174
|
+
// Location 1: auxiliary rg8unorm carrying
|
|
175
|
+
// .r = bloom mask (1 = contributes to bloom, 0 = skip — e.g. ground).
|
|
176
|
+
// .g = accumulated canvas alpha — the channel that used to live in hdr.a
|
|
177
|
+
// before the switch to rg11b10ufloat. Sampled by composite to
|
|
178
|
+
// un-premultiply color for tonemap and to set the final drawable alpha
|
|
179
|
+
// (needed for the `premultiplied` canvas alphaMode that blends the
|
|
180
|
+
// WebGPU surface over the page background).
|
|
181
|
+
// FS output at location 1 must be vec4f — the blend state references src.a, and
|
|
182
|
+
// WebGPU requires the fragment output to provide an alpha component even though
|
|
183
|
+
// the rg8unorm target only stores .r and .g (extra components are discarded).
|
|
184
|
+
// Materials write mask = vec4f(1.0, 1.0, 0.0, color.a); ground writes
|
|
185
|
+
// vec4f(0.0, 1.0, 0.0, edgeFade). With src.a coming from the 4th component and
|
|
186
|
+
// src-alpha blending enabled:
|
|
187
|
+
// out.r = mask_r · src.a + dst.r · (1-src.a) (bloom mask, weighted by alpha)
|
|
188
|
+
// out.g = 1.0 · src.a + dst.g · (1-src.a) (canonical premultiplied alpha-over)
|
|
189
|
+
|
|
190
|
+
export const COMMON_FS_OUT_WGSL = /* wgsl */ `
|
|
191
|
+
|
|
192
|
+
struct FSOut {
|
|
193
|
+
@location(0) color: vec4f,
|
|
194
|
+
@location(1) mask: vec4f,
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
`;
|
|
198
|
+
|
|
199
|
+
// ─── Convenience: full shared prelude ───────────────────────────────
|
|
200
|
+
// Material files compose this as `${NODES_WGSL}${COMMON_MATERIAL_PRELUDE_WGSL}` to
|
|
201
|
+
// pull in everything structural. Each material then adds its own constants + fs().
|
|
202
|
+
|
|
203
|
+
export const COMMON_MATERIAL_PRELUDE_WGSL =
|
|
204
|
+
COMMON_BINDINGS_WGSL + SAMPLE_SHADOW_WGSL + COMMON_VS_WGSL + COMMON_FS_OUT_WGSL
|
|
@@ -25,6 +25,20 @@ const EYE_EMISSION_STRENGTH: f32 = 1.5;
|
|
|
25
25
|
|
|
26
26
|
let n = safe_normal(input.normal);
|
|
27
27
|
let v = normalize(camera.viewPos - input.worldPos);
|
|
28
|
+
|
|
29
|
+
// Rear-view gate for the post-alpha eye. Many PMX heads are open shells
|
|
30
|
+
// (no skull mesh), so from behind nothing occludes the eye — it would draw
|
|
31
|
+
// and stamp the see-through stencil straight through the back hair. Mesh
|
|
32
|
+
// normals/winding are unreliable (double-sided eye geometry), so gate by
|
|
33
|
+
// camera-vs-face hemisphere instead: the 頭 bone's skinning matrix carries
|
|
34
|
+
// the head's world rotation (PMX bind pose is rotationless), and PMX
|
|
35
|
+
// models face −Z locally. Discard drops color, depth, and stencil.
|
|
36
|
+
if (material.headBoneIndex >= 0.0) {
|
|
37
|
+
let hm = skinMats[u32(material.headBoneIndex)];
|
|
38
|
+
let faceDir = -normalize(hm[2].xyz);
|
|
39
|
+
if (dot(faceDir, v) < -0.15) { discard; }
|
|
40
|
+
}
|
|
41
|
+
|
|
28
42
|
let l = -light.lights[0].direction.xyz;
|
|
29
43
|
let sun = light.lights[0].color.xyz * light.lights[0].color.w;
|
|
30
44
|
let amb = light.ambientColor.xyz;
|
|
@@ -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
|
+
`
|