@webxr-jp/mtoon-atlas 0.3.2 → 0.5.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.
- package/README.md +39 -0
- package/dist/index.cjs +309 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +218 -156
- package/dist/index.d.ts +218 -156
- package/dist/index.js +296 -49
- package/dist/index.js.map +1 -1
- package/dist/nodes/index.cjs +568 -0
- package/dist/nodes/index.cjs.map +1 -0
- package/dist/nodes/index.d.cts +200 -0
- package/dist/nodes/index.d.ts +200 -0
- package/dist/nodes/index.js +569 -0
- package/dist/nodes/index.js.map +1 -0
- package/dist/types-C3AJTssR.d.cts +175 -0
- package/dist/types-C3AJTssR.d.ts +175 -0
- package/package.json +8 -3
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/nodes/index.ts
|
|
21
|
+
var nodes_exports = {};
|
|
22
|
+
__export(nodes_exports, {
|
|
23
|
+
MToonAtlasLightingModel: () => MToonAtlasLightingModel,
|
|
24
|
+
MToonAtlasNodeMaterial: () => MToonAtlasNodeMaterial
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(nodes_exports);
|
|
27
|
+
|
|
28
|
+
// src/nodes/MToonAtlasNodeMaterial.ts
|
|
29
|
+
var import_tsl2 = require("three/tsl");
|
|
30
|
+
var import_webgpu2 = require("three/webgpu");
|
|
31
|
+
|
|
32
|
+
// src/parameter-buffer.ts
|
|
33
|
+
var MAX_PARAMETER_UBO_BYTES = 16384;
|
|
34
|
+
var DEFAULT_TEXELS_PER_SLOT = 9;
|
|
35
|
+
var BYTES_PER_VEC4 = 16;
|
|
36
|
+
var PARAMETER_UBO_BUCKET_SLOTS = [8, 16, 32, 64, 112];
|
|
37
|
+
function getMaxParameterUBOSlots(texelsPerSlot = DEFAULT_TEXELS_PER_SLOT) {
|
|
38
|
+
if (!Number.isInteger(texelsPerSlot) || texelsPerSlot <= 0) {
|
|
39
|
+
throw new RangeError(`texelsPerSlot \u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059\uFF08\u53D7\u3051\u53D6\u3063\u305F\u5024: ${texelsPerSlot}\uFF09`);
|
|
40
|
+
}
|
|
41
|
+
return Math.floor(MAX_PARAMETER_UBO_BYTES / (texelsPerSlot * BYTES_PER_VEC4));
|
|
42
|
+
}
|
|
43
|
+
function resolveParameterBucketSlots(slotCount, texelsPerSlot = DEFAULT_TEXELS_PER_SLOT) {
|
|
44
|
+
if (!Number.isInteger(slotCount) || slotCount < 0) {
|
|
45
|
+
throw new RangeError(`slotCount \u306F 0 \u4EE5\u4E0A\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059\uFF08\u53D7\u3051\u53D6\u3063\u305F\u5024: ${slotCount}\uFF09`);
|
|
46
|
+
}
|
|
47
|
+
const maxSlots = getMaxParameterUBOSlots(texelsPerSlot);
|
|
48
|
+
if (slotCount > maxSlots) {
|
|
49
|
+
throw new RangeError(
|
|
50
|
+
`\u30DE\u30C6\u30EA\u30A2\u30EB\u30B9\u30ED\u30C3\u30C8\u6570 (${slotCount}) \u304C UBO \u4E0A\u9650 (${maxSlots} \u30B9\u30ED\u30C3\u30C8 / ${MAX_PARAMETER_UBO_BYTES} bytes, texelsPerSlot=${texelsPerSlot}) \u3092\u8D85\u3048\u3066\u3044\u307E\u3059`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
const effectiveSlots = Math.max(1, slotCount);
|
|
54
|
+
for (const bucket of PARAMETER_UBO_BUCKET_SLOTS) {
|
|
55
|
+
if (effectiveSlots <= bucket && bucket <= maxSlots) {
|
|
56
|
+
return bucket;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return maxSlots;
|
|
60
|
+
}
|
|
61
|
+
function createPaddedParameterBuffer(data, slotCount, texelsPerSlot = DEFAULT_TEXELS_PER_SLOT) {
|
|
62
|
+
const bucketSlots = resolveParameterBucketSlots(slotCount, texelsPerSlot);
|
|
63
|
+
const paddedLength = bucketSlots * texelsPerSlot * 4;
|
|
64
|
+
const buffer = new Float32Array(paddedLength);
|
|
65
|
+
const copyLength = Math.min(data.length, slotCount * texelsPerSlot * 4, paddedLength);
|
|
66
|
+
if (copyLength > 0) {
|
|
67
|
+
buffer.set(data.subarray(0, copyLength), 0);
|
|
68
|
+
}
|
|
69
|
+
return { buffer, bucketSlots };
|
|
70
|
+
}
|
|
71
|
+
function extractParameterBufferFromTexture(descriptor) {
|
|
72
|
+
const { texture, slotCount, texelsPerSlot, semantics, atlasedTextures } = descriptor;
|
|
73
|
+
const expectedLength = Math.max(0, slotCount * texelsPerSlot * 4);
|
|
74
|
+
const image = texture.image;
|
|
75
|
+
const raw = image?.data;
|
|
76
|
+
let data;
|
|
77
|
+
if (raw instanceof Float32Array) {
|
|
78
|
+
data = raw;
|
|
79
|
+
} else if (raw instanceof Uint16Array) {
|
|
80
|
+
data = new Float32Array(raw.length);
|
|
81
|
+
for (let i = 0; i < raw.length; i++) {
|
|
82
|
+
data[i] = raw[i] / 65535;
|
|
83
|
+
}
|
|
84
|
+
} else if (raw instanceof Uint8Array || raw instanceof Uint8ClampedArray) {
|
|
85
|
+
data = new Float32Array(raw.length);
|
|
86
|
+
for (let i = 0; i < raw.length; i++) {
|
|
87
|
+
data[i] = raw[i] / 255;
|
|
88
|
+
}
|
|
89
|
+
} else if (raw) {
|
|
90
|
+
data = Float32Array.from(raw);
|
|
91
|
+
} else {
|
|
92
|
+
data = new Float32Array(expectedLength);
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
data,
|
|
96
|
+
slotCount,
|
|
97
|
+
texelsPerSlot,
|
|
98
|
+
semantics,
|
|
99
|
+
atlasedTextures
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/nodes/MToonAtlasLightingModel.ts
|
|
104
|
+
var import_tsl = require("three/tsl");
|
|
105
|
+
var import_webgpu = require("three/webgpu");
|
|
106
|
+
var shadeColor = (0, import_tsl.property)("vec3").toVar("MToonAtlasShadeColor");
|
|
107
|
+
var shadingShift = (0, import_tsl.property)("float").toVar("MToonAtlasShadingShift");
|
|
108
|
+
var shadingToony = (0, import_tsl.property)("float").toVar("MToonAtlasShadingToony");
|
|
109
|
+
var rim = (0, import_tsl.property)("vec3").toVar("MToonAtlasRim");
|
|
110
|
+
function splitShadow(lightColor, lightNode) {
|
|
111
|
+
const node = lightNode;
|
|
112
|
+
if (node.light?.isDirectionalLight && node.shadowNode && node.baseColorNode) {
|
|
113
|
+
return { color: node.baseColorNode, shadow: node.shadowNode };
|
|
114
|
+
}
|
|
115
|
+
return { color: lightColor, shadow: (0, import_tsl.float)(1) };
|
|
116
|
+
}
|
|
117
|
+
function lambert(diffuse) {
|
|
118
|
+
return (0, import_tsl.BRDF_Lambert)({ diffuseColor: diffuse });
|
|
119
|
+
}
|
|
120
|
+
var MToonAtlasLightingModel = class extends import_webgpu.LightingModel {
|
|
121
|
+
direct({ lightDirection, lightColor, lightNode, reflectedLight }) {
|
|
122
|
+
const { color, shadow } = splitShadow(lightColor, lightNode);
|
|
123
|
+
const out = reflectedLight;
|
|
124
|
+
const dotNL = import_tsl.normalView.dot(lightDirection).clamp(-1, 1);
|
|
125
|
+
const a = (0, import_tsl.float)(-1).add(shadingToony);
|
|
126
|
+
const b = (0, import_tsl.float)(1).sub(shadingToony);
|
|
127
|
+
const shading = dotNL.add(shadingShift).sub(a).div(b.sub(a)).clamp().mul(shadow);
|
|
128
|
+
const feathered = (0, import_tsl.mix)(shadeColor, import_tsl.diffuseColor.rgb, shading);
|
|
129
|
+
out.directDiffuse.addAssign(color.mul(lambert(feathered)));
|
|
130
|
+
out.directSpecular.addAssign(color.mul(lambert(rim)));
|
|
131
|
+
}
|
|
132
|
+
indirect(builder) {
|
|
133
|
+
const context = builder.context;
|
|
134
|
+
const { irradiance, reflectedLight } = context;
|
|
135
|
+
reflectedLight.indirectDiffuse.addAssign(irradiance.mul(lambert(import_tsl.diffuseColor.rgb)));
|
|
136
|
+
reflectedLight.indirectSpecular.addAssign(irradiance.mul(lambert(rim)));
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// src/nodes/MToonAtlasNodeMaterial.ts
|
|
141
|
+
var DEFAULT_SLOT_ATTRIBUTE_NAME = "mtoonMaterialSlot";
|
|
142
|
+
var MIN_BUCKET_SLOTS = PARAMETER_UBO_BUCKET_SLOTS[0];
|
|
143
|
+
var _atlasTextureRefs = {
|
|
144
|
+
map: (0, import_tsl2.materialReference)("map", "texture"),
|
|
145
|
+
normalMap: (0, import_tsl2.materialReference)("normalMap", "texture"),
|
|
146
|
+
emissiveMap: (0, import_tsl2.materialReference)("emissiveMap", "texture"),
|
|
147
|
+
shadeMultiplyTexture: (0, import_tsl2.materialReference)("shadeMultiplyTexture", "texture"),
|
|
148
|
+
shadingShiftTexture: (0, import_tsl2.materialReference)("shadingShiftTexture", "texture"),
|
|
149
|
+
matcapTexture: (0, import_tsl2.materialReference)("matcapTexture", "texture"),
|
|
150
|
+
rimMultiplyTexture: (0, import_tsl2.materialReference)("rimMultiplyTexture", "texture"),
|
|
151
|
+
uvAnimationMaskTexture: (0, import_tsl2.materialReference)("uvAnimationMaskTexture", "texture")
|
|
152
|
+
};
|
|
153
|
+
var _parameterBufferRefs = /* @__PURE__ */ new Map();
|
|
154
|
+
function getParameterBufferRef(vec4Count) {
|
|
155
|
+
let ref = _parameterBufferRefs.get(vec4Count);
|
|
156
|
+
if (!ref) {
|
|
157
|
+
const node = (0, import_tsl2.materialReference)("parameterBufferData", "vec4");
|
|
158
|
+
node.count = vec4Count;
|
|
159
|
+
ref = node;
|
|
160
|
+
_parameterBufferRefs.set(vec4Count, ref);
|
|
161
|
+
}
|
|
162
|
+
return ref;
|
|
163
|
+
}
|
|
164
|
+
var MToonAtlasNodeMaterial = class extends import_webgpu2.NodeMaterial {
|
|
165
|
+
/**
|
|
166
|
+
* コンストラクタ
|
|
167
|
+
*
|
|
168
|
+
* @param parameters - オプション
|
|
169
|
+
*/
|
|
170
|
+
constructor(parameters = {}) {
|
|
171
|
+
super();
|
|
172
|
+
/** マテリアル識別フラグ(GLSL 版と共通。エクスポーターが参照する) */
|
|
173
|
+
this.isMToonAtlasMaterial = true;
|
|
174
|
+
/** TSL 版であることを示すフラグ */
|
|
175
|
+
this.isMToonAtlasNodeMaterial = true;
|
|
176
|
+
// ---- シェーダーから materialReference 経由で参照されるプロパティ ----
|
|
177
|
+
// 自前のプロパティは three がキャッシュキーに含めるため、
|
|
178
|
+
// テクスチャの有無・サンプラーが変わると自動でシェーダーが切り替わる
|
|
179
|
+
/** UBO 転送用のパディング済みパラメータ配列(bucketSlots × texelsPerSlot × 4) */
|
|
180
|
+
this.parameterBufferData = new Float32Array(
|
|
181
|
+
MIN_BUCKET_SLOTS * DEFAULT_TEXELS_PER_SLOT * 4
|
|
182
|
+
);
|
|
183
|
+
/** ベースカラーアトラス */
|
|
184
|
+
this.map = null;
|
|
185
|
+
/** ノーマルマップアトラス */
|
|
186
|
+
this.normalMap = null;
|
|
187
|
+
/** エミッシブアトラス */
|
|
188
|
+
this.emissiveMap = null;
|
|
189
|
+
/** 影色乗算アトラス */
|
|
190
|
+
this.shadeMultiplyTexture = null;
|
|
191
|
+
/** シェーディングシフトアトラス */
|
|
192
|
+
this.shadingShiftTexture = null;
|
|
193
|
+
/** matcap テクスチャ */
|
|
194
|
+
this.matcapTexture = null;
|
|
195
|
+
/** リム乗算アトラス */
|
|
196
|
+
this.rimMultiplyTexture = null;
|
|
197
|
+
/** UV アニメーションマスクアトラス */
|
|
198
|
+
this.uvAnimationMaskTexture = null;
|
|
199
|
+
/**
|
|
200
|
+
* エミッシブを差し替えるノード(NodeMaterial.setupLighting が参照する)。
|
|
201
|
+
* 通常は null のままで、パラメータバッファから求めた値が使われる
|
|
202
|
+
*/
|
|
203
|
+
this.emissiveNode = null;
|
|
204
|
+
this._parameterBucketSlots = MIN_BUCKET_SLOTS;
|
|
205
|
+
this._texelsPerSlot = DEFAULT_TEXELS_PER_SLOT;
|
|
206
|
+
this._slotAttribute = { name: DEFAULT_SLOT_ATTRIBUTE_NAME };
|
|
207
|
+
this._isOutline = false;
|
|
208
|
+
this._outlineWidthMode = "none";
|
|
209
|
+
// シェーダー構築中に共有するアニメーション適用済み UV ノード(setup ごとに作り直す)
|
|
210
|
+
this._animatedUVNode = null;
|
|
211
|
+
this.lights = true;
|
|
212
|
+
const { parameterBuffer, parameterTexture, slotAttribute, ...rest } = parameters;
|
|
213
|
+
this.setValues(rest);
|
|
214
|
+
if (parameterBuffer) {
|
|
215
|
+
this.setParameterBuffer(parameterBuffer);
|
|
216
|
+
} else if (parameterTexture) {
|
|
217
|
+
this.setParameterTexture(parameterTexture);
|
|
218
|
+
}
|
|
219
|
+
if (slotAttribute) {
|
|
220
|
+
this.setSlotAttribute(slotAttribute);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* パラメータバッファを設定する(推奨 API)
|
|
225
|
+
*
|
|
226
|
+
* スロット数が UBO 上限(WebGL2 最小 16KB 基準で既定 112 スロット)を
|
|
227
|
+
* 超える場合は RangeError を投げる。
|
|
228
|
+
* atlasedTextures を含む場合は各アトラステクスチャも設定する。
|
|
229
|
+
*
|
|
230
|
+
* @param descriptor - パラメータバッファディスクリプタ。null で解除
|
|
231
|
+
* @returns this
|
|
232
|
+
*/
|
|
233
|
+
setParameterBuffer(descriptor) {
|
|
234
|
+
if (descriptor) {
|
|
235
|
+
const { buffer, bucketSlots } = createPaddedParameterBuffer(
|
|
236
|
+
descriptor.data,
|
|
237
|
+
descriptor.slotCount,
|
|
238
|
+
descriptor.texelsPerSlot
|
|
239
|
+
);
|
|
240
|
+
const prevBucketSlots = this._parameterBucketSlots;
|
|
241
|
+
const prevTexelsPerSlot = this._texelsPerSlot;
|
|
242
|
+
this._parameterBuffer = descriptor;
|
|
243
|
+
this._parameterTexture = void 0;
|
|
244
|
+
this._parameterBucketSlots = bucketSlots;
|
|
245
|
+
this._texelsPerSlot = descriptor.texelsPerSlot;
|
|
246
|
+
this.parameterBufferData = buffer;
|
|
247
|
+
if (descriptor.atlasedTextures) {
|
|
248
|
+
const prevMask = this._atlasTexturePresenceMask();
|
|
249
|
+
this._setAtlasedTextures(descriptor.atlasedTextures);
|
|
250
|
+
if (this._atlasTexturePresenceMask() !== prevMask) {
|
|
251
|
+
this.needsUpdate = true;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (prevBucketSlots !== bucketSlots || prevTexelsPerSlot !== descriptor.texelsPerSlot) {
|
|
255
|
+
this.needsUpdate = true;
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
this._parameterBuffer = void 0;
|
|
259
|
+
this._parameterTexture = void 0;
|
|
260
|
+
this._parameterBucketSlots = MIN_BUCKET_SLOTS;
|
|
261
|
+
this._texelsPerSlot = DEFAULT_TEXELS_PER_SLOT;
|
|
262
|
+
this.parameterBufferData = new Float32Array(
|
|
263
|
+
MIN_BUCKET_SLOTS * DEFAULT_TEXELS_PER_SLOT * 4
|
|
264
|
+
);
|
|
265
|
+
this.needsUpdate = true;
|
|
266
|
+
}
|
|
267
|
+
return this;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* パラメータテクスチャを設定する(後方互換 API)
|
|
271
|
+
*
|
|
272
|
+
* 受け取った DataTexture から生 Float32Array を抽出して UBO に載せる。
|
|
273
|
+
* 描画時にパラメータテクスチャはサンプリングしない。
|
|
274
|
+
*
|
|
275
|
+
* @param descriptor - パラメータテクスチャディスクリプタ。null で解除
|
|
276
|
+
* @returns this
|
|
277
|
+
*/
|
|
278
|
+
setParameterTexture(descriptor) {
|
|
279
|
+
if (!descriptor) {
|
|
280
|
+
return this.setParameterBuffer(null);
|
|
281
|
+
}
|
|
282
|
+
const bufferDescriptor = extractParameterBufferFromTexture(descriptor);
|
|
283
|
+
this.setParameterBuffer(bufferDescriptor);
|
|
284
|
+
this._parameterTexture = descriptor;
|
|
285
|
+
return this;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* スロット属性を設定する
|
|
289
|
+
*
|
|
290
|
+
* @param config - スロット属性設定
|
|
291
|
+
* @returns this
|
|
292
|
+
*/
|
|
293
|
+
setSlotAttribute(config) {
|
|
294
|
+
if (config.name !== this._slotAttribute.name) {
|
|
295
|
+
this.needsUpdate = true;
|
|
296
|
+
}
|
|
297
|
+
this._slotAttribute = { ...config };
|
|
298
|
+
return this;
|
|
299
|
+
}
|
|
300
|
+
/** パラメータバッファディスクリプタ */
|
|
301
|
+
get parameterBuffer() {
|
|
302
|
+
return this._parameterBuffer;
|
|
303
|
+
}
|
|
304
|
+
/** パラメータテクスチャディスクリプタ(後方互換。setParameterTexture 経由時のみ設定される) */
|
|
305
|
+
get parameterTexture() {
|
|
306
|
+
return this._parameterTexture;
|
|
307
|
+
}
|
|
308
|
+
/** 現在の UBO バケットのスロット数(8 / 16 / 32 / 64 / 112) */
|
|
309
|
+
get parameterBucketSlots() {
|
|
310
|
+
return this._parameterBucketSlots;
|
|
311
|
+
}
|
|
312
|
+
/** スロット属性設定 */
|
|
313
|
+
get slotAttribute() {
|
|
314
|
+
return this._slotAttribute;
|
|
315
|
+
}
|
|
316
|
+
/** アウトライン用マテリアルかどうか。true にすると裏面を描画する */
|
|
317
|
+
get isOutline() {
|
|
318
|
+
return this._isOutline;
|
|
319
|
+
}
|
|
320
|
+
set isOutline(value) {
|
|
321
|
+
if (this._isOutline === value) return;
|
|
322
|
+
this._isOutline = value;
|
|
323
|
+
this.side = value ? import_webgpu2.BackSide : import_webgpu2.FrontSide;
|
|
324
|
+
this.needsUpdate = true;
|
|
325
|
+
}
|
|
326
|
+
/** アウトライン幅の計算方式 */
|
|
327
|
+
get outlineWidthMode() {
|
|
328
|
+
return this._outlineWidthMode;
|
|
329
|
+
}
|
|
330
|
+
set outlineWidthMode(value) {
|
|
331
|
+
if (this._outlineWidthMode === value) return;
|
|
332
|
+
this._outlineWidthMode = value;
|
|
333
|
+
this.needsUpdate = true;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* このマテリアルを元にアウトライン用マテリアルを作る
|
|
337
|
+
*
|
|
338
|
+
* @param widthMode - アウトライン幅の計算方式
|
|
339
|
+
* @returns アウトライン用マテリアル
|
|
340
|
+
*/
|
|
341
|
+
createOutlineMaterial(widthMode = "worldCoordinates") {
|
|
342
|
+
const outline = this.clone();
|
|
343
|
+
outline.outlineWidthMode = widthMode;
|
|
344
|
+
outline.isOutline = true;
|
|
345
|
+
outline.polygonOffset = true;
|
|
346
|
+
outline.polygonOffsetFactor = 1;
|
|
347
|
+
outline.polygonOffsetUnits = 1;
|
|
348
|
+
return outline;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* UV アニメーションの更新(GLSL 版と同じく現状は何もしない)
|
|
352
|
+
*
|
|
353
|
+
* パラメータバッファのオフセット値をそのまま使うため、時間経過による
|
|
354
|
+
* 更新は行わない。three-vrm の VRM.update() から呼ばれる。
|
|
355
|
+
*/
|
|
356
|
+
update(_deltaTime) {
|
|
357
|
+
}
|
|
358
|
+
customProgramCacheKey() {
|
|
359
|
+
return super.customProgramCacheKey() + `mtoonAtlas:${this._parameterBucketSlots},${this._texelsPerSlot},${this._isOutline},${this._outlineWidthMode},${this._slotAttribute.name},`;
|
|
360
|
+
}
|
|
361
|
+
copy(source) {
|
|
362
|
+
super.copy(source);
|
|
363
|
+
this.parameterBufferData = source.parameterBufferData.slice();
|
|
364
|
+
this.map = source.map;
|
|
365
|
+
this.normalMap = source.normalMap;
|
|
366
|
+
this.emissiveMap = source.emissiveMap;
|
|
367
|
+
this.shadeMultiplyTexture = source.shadeMultiplyTexture;
|
|
368
|
+
this.shadingShiftTexture = source.shadingShiftTexture;
|
|
369
|
+
this.matcapTexture = source.matcapTexture;
|
|
370
|
+
this.rimMultiplyTexture = source.rimMultiplyTexture;
|
|
371
|
+
this.uvAnimationMaskTexture = source.uvAnimationMaskTexture;
|
|
372
|
+
this._parameterBuffer = source._parameterBuffer ? { ...source._parameterBuffer } : void 0;
|
|
373
|
+
this._parameterTexture = source._parameterTexture ? { ...source._parameterTexture } : void 0;
|
|
374
|
+
this._parameterBucketSlots = source._parameterBucketSlots;
|
|
375
|
+
this._texelsPerSlot = source._texelsPerSlot;
|
|
376
|
+
this._slotAttribute = { ...source._slotAttribute };
|
|
377
|
+
this._isOutline = source._isOutline;
|
|
378
|
+
this._outlineWidthMode = source._outlineWidthMode;
|
|
379
|
+
return this;
|
|
380
|
+
}
|
|
381
|
+
// ---- ノード構築 ----
|
|
382
|
+
setup(builder) {
|
|
383
|
+
this._animatedUVNode = null;
|
|
384
|
+
super.setup(builder);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* アトラステクスチャを指定 UV でサンプリングする
|
|
388
|
+
*
|
|
389
|
+
* モジュール共有の materialReference を使うことで、マテリアルごとに
|
|
390
|
+
* シェーダーを再コンパイルせずバインディングだけを切り替えられる。
|
|
391
|
+
*
|
|
392
|
+
* @param property - 読むテクスチャのプロパティ名(null でないこと)
|
|
393
|
+
* @param uvNode - サンプリングする UV
|
|
394
|
+
*/
|
|
395
|
+
_sampleAtlas(property2, uvNode) {
|
|
396
|
+
return _atlasTextureRefs[property2].context({ getUV: () => uvNode });
|
|
397
|
+
}
|
|
398
|
+
/** 現在のフラグメント / 頂点のスロット番号(補間誤差を避けるため四捨五入し範囲内に丸める) */
|
|
399
|
+
_slotIndex() {
|
|
400
|
+
const slotAttr = (0, import_tsl2.attribute)(this._slotAttribute.name, "float");
|
|
401
|
+
return (0, import_tsl2.int)(slotAttr.add(0.5).clamp(0, this._parameterBucketSlots - 1));
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* パラメータ UBO の 1 vec4 を読む
|
|
405
|
+
*
|
|
406
|
+
* @param texel - スロット内の vec4 インデックス(0〜8)
|
|
407
|
+
*/
|
|
408
|
+
_param(texel) {
|
|
409
|
+
const vec4Count = this._parameterBucketSlots * this._texelsPerSlot;
|
|
410
|
+
const index = this._slotIndex().mul((0, import_tsl2.int)(this._texelsPerSlot)).add((0, import_tsl2.int)(texel));
|
|
411
|
+
return getParameterBufferRef(vec4Count).element(index);
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* UV アニメーション(回転 → スクロール)を適用した UV(フラグメント内で 1 回計算して共有)
|
|
415
|
+
*
|
|
416
|
+
* GLSL 版と同じく、マスクは B チャンネル、回転は (0.5, 0.5) 中心。
|
|
417
|
+
*/
|
|
418
|
+
_getAnimatedUV() {
|
|
419
|
+
if (!this._animatedUVNode) {
|
|
420
|
+
const p6 = this._param(6);
|
|
421
|
+
const p7 = this._param(7);
|
|
422
|
+
const mask = this.uvAnimationMaskTexture ? this._sampleAtlas("uvAnimationMaskTexture", (0, import_tsl2.uv)()).b : (0, import_tsl2.float)(1);
|
|
423
|
+
const phase = p6.a.mul(mask);
|
|
424
|
+
const c = (0, import_tsl2.cos)(phase);
|
|
425
|
+
const s = (0, import_tsl2.sin)(phase);
|
|
426
|
+
const centered = (0, import_tsl2.uv)().sub(0.5);
|
|
427
|
+
const rotated = (0, import_tsl2.vec2)(
|
|
428
|
+
c.mul(centered.x).add(s.mul(centered.y)),
|
|
429
|
+
s.negate().mul(centered.x).add(c.mul(centered.y))
|
|
430
|
+
).add(0.5);
|
|
431
|
+
this._animatedUVNode = rotated.add((0, import_tsl2.vec2)(p7.b, p7.a).mul(mask)).toVar("MToonAtlasAnimatedUV");
|
|
432
|
+
}
|
|
433
|
+
return this._animatedUVNode;
|
|
434
|
+
}
|
|
435
|
+
setupLightingModel() {
|
|
436
|
+
return new MToonAtlasLightingModel();
|
|
437
|
+
}
|
|
438
|
+
setupDiffuseColor(builder) {
|
|
439
|
+
const p0 = this._param(0);
|
|
440
|
+
let color = (0, import_tsl2.vec4)(p0.rgb, 1);
|
|
441
|
+
if (this.map) {
|
|
442
|
+
color = color.mul(this._sampleAtlas("map", this._getAnimatedUV()));
|
|
443
|
+
}
|
|
444
|
+
const prevColor = this.colorNode;
|
|
445
|
+
const prevOpacity = this.opacityNode;
|
|
446
|
+
this.colorNode = color;
|
|
447
|
+
this.opacityNode = p0.a;
|
|
448
|
+
super.setupDiffuseColor(builder);
|
|
449
|
+
this.colorNode = prevColor;
|
|
450
|
+
this.opacityNode = prevOpacity;
|
|
451
|
+
}
|
|
452
|
+
setupVariants() {
|
|
453
|
+
let shade = this._param(1).rgb;
|
|
454
|
+
if (this.shadeMultiplyTexture) {
|
|
455
|
+
shade = shade.mul(this._sampleAtlas("shadeMultiplyTexture", this._getAnimatedUV()).rgb);
|
|
456
|
+
}
|
|
457
|
+
shadeColor.assign(shade);
|
|
458
|
+
let shift = this._param(8).r.mul(2).sub(1);
|
|
459
|
+
if (this.shadingShiftTexture) {
|
|
460
|
+
shift = shift.add(this._sampleAtlas("shadingShiftTexture", this._getAnimatedUV()).r.mul(this._param(1).a));
|
|
461
|
+
}
|
|
462
|
+
shadingShift.assign(shift);
|
|
463
|
+
shadingToony.assign(this._param(6).g);
|
|
464
|
+
rim.assign(this._rimNode());
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* パラメトリックリム + matcap に rimMultiplyTexture を掛けたもの
|
|
468
|
+
*/
|
|
469
|
+
_rimNode() {
|
|
470
|
+
const p3 = this._param(3);
|
|
471
|
+
const p5 = this._param(5);
|
|
472
|
+
const p6 = this._param(6);
|
|
473
|
+
const viewDir = import_tsl2.positionViewDirection;
|
|
474
|
+
let rimColor = p5.rgb.mul(
|
|
475
|
+
(0, import_tsl2.float)(1).sub(viewDir.dot(import_tsl2.normalView)).add(p5.a).clamp().pow(p6.r)
|
|
476
|
+
);
|
|
477
|
+
if (this.matcapTexture) {
|
|
478
|
+
const x = (0, import_tsl2.vec3)(viewDir.z, 0, viewDir.x.negate()).normalize();
|
|
479
|
+
const y = viewDir.cross(x);
|
|
480
|
+
const sphereUV = (0, import_tsl2.vec2)(x.dot(import_tsl2.normalView), y.dot(import_tsl2.normalView).negate()).mul(0.5).add(0.5);
|
|
481
|
+
rimColor = rimColor.add(p3.rgb.mul(this._sampleAtlas("matcapTexture", sphereUV).rgb));
|
|
482
|
+
}
|
|
483
|
+
if (this.rimMultiplyTexture) {
|
|
484
|
+
rimColor = rimColor.mul(this._sampleAtlas("rimMultiplyTexture", this._getAnimatedUV()).rgb);
|
|
485
|
+
}
|
|
486
|
+
return rimColor;
|
|
487
|
+
}
|
|
488
|
+
setupNormal() {
|
|
489
|
+
let normal = import_tsl2.materialNormal;
|
|
490
|
+
if (this.normalMap) {
|
|
491
|
+
const normalScale = this._param(7).xy;
|
|
492
|
+
normal = (0, import_tsl2.normalMap)(this._sampleAtlas("normalMap", this._getAnimatedUV()), normalScale);
|
|
493
|
+
}
|
|
494
|
+
if (this._isOutline) {
|
|
495
|
+
normal = normal.negate();
|
|
496
|
+
}
|
|
497
|
+
return normal;
|
|
498
|
+
}
|
|
499
|
+
setupLighting(builder) {
|
|
500
|
+
const p2 = this._param(2);
|
|
501
|
+
let emissive = p2.rgb.mul(p2.a);
|
|
502
|
+
if (this.emissiveMap) {
|
|
503
|
+
emissive = emissive.mul(this._sampleAtlas("emissiveMap", this._getAnimatedUV()).rgb);
|
|
504
|
+
}
|
|
505
|
+
const prevEmissive = this.emissiveNode;
|
|
506
|
+
this.emissiveNode = emissive;
|
|
507
|
+
const result = super.setupLighting(builder);
|
|
508
|
+
this.emissiveNode = prevEmissive;
|
|
509
|
+
return result;
|
|
510
|
+
}
|
|
511
|
+
setupPosition(builder) {
|
|
512
|
+
if (!this._hasOutlineWidth()) {
|
|
513
|
+
return super.setupPosition(builder);
|
|
514
|
+
}
|
|
515
|
+
const normalLocalNormalized = import_tsl2.normalLocal.normalize();
|
|
516
|
+
const width = this._param(3).a;
|
|
517
|
+
const worldNormalLength = (0, import_tsl2.length)(import_tsl2.modelNormalMatrix.mul(normalLocalNormalized));
|
|
518
|
+
let offset = width.mul(worldNormalLength).mul(normalLocalNormalized);
|
|
519
|
+
if (this._outlineWidthMode === "screenCoordinates") {
|
|
520
|
+
const projection = import_tsl2.cameraProjectionMatrix;
|
|
521
|
+
const clipScale = projection.element((0, import_tsl2.int)(1)).y;
|
|
522
|
+
const viewZ = import_tsl2.modelViewMatrix.mul((0, import_tsl2.vec4)(import_tsl2.positionLocal, 1)).z;
|
|
523
|
+
offset = offset.div(clipScale).mul(viewZ.negate());
|
|
524
|
+
}
|
|
525
|
+
const prevPosition = this.positionNode;
|
|
526
|
+
this.positionNode = (prevPosition ?? import_tsl2.positionLocal).add(offset);
|
|
527
|
+
const result = super.setupPosition(builder);
|
|
528
|
+
this.positionNode = prevPosition;
|
|
529
|
+
return result;
|
|
530
|
+
}
|
|
531
|
+
setupOutput(builder, outputNode) {
|
|
532
|
+
let output = outputNode;
|
|
533
|
+
if (this._hasOutlineWidth()) {
|
|
534
|
+
const p4 = this._param(4);
|
|
535
|
+
const color = outputNode;
|
|
536
|
+
output = (0, import_tsl2.vec4)(p4.rgb.mul((0, import_tsl2.mix)((0, import_tsl2.vec3)(1), color.xyz, p4.a)), color.w);
|
|
537
|
+
}
|
|
538
|
+
return super.setupOutput(builder, output);
|
|
539
|
+
}
|
|
540
|
+
/** アウトラインとして押し出し・着色するかどうか */
|
|
541
|
+
_hasOutlineWidth() {
|
|
542
|
+
return this._isOutline && this._outlineWidthMode !== "none";
|
|
543
|
+
}
|
|
544
|
+
_atlasTexturePresenceMask() {
|
|
545
|
+
return (this.map ? 1 : 0) | (this.shadeMultiplyTexture ? 2 : 0) | (this.shadingShiftTexture ? 4 : 0) | (this.normalMap ? 8 : 0) | (this.emissiveMap ? 16 : 0) | (this.matcapTexture ? 32 : 0) | (this.rimMultiplyTexture ? 64 : 0) | (this.uvAnimationMaskTexture ? 128 : 0);
|
|
546
|
+
}
|
|
547
|
+
_setAtlasedTextures(atlases) {
|
|
548
|
+
if (atlases.baseColor) this.map = atlases.baseColor;
|
|
549
|
+
if (atlases.shade) this.shadeMultiplyTexture = atlases.shade;
|
|
550
|
+
if (atlases.shadingShift) this.shadingShiftTexture = atlases.shadingShift;
|
|
551
|
+
if (atlases.normal) this.normalMap = atlases.normal;
|
|
552
|
+
if (atlases.emissive) this.emissiveMap = atlases.emissive;
|
|
553
|
+
if (atlases.matcap) this.matcapTexture = atlases.matcap;
|
|
554
|
+
if (atlases.rim) this.rimMultiplyTexture = atlases.rim;
|
|
555
|
+
if (atlases.uvAnimationMask) this.uvAnimationMaskTexture = atlases.uvAnimationMask;
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
/**
|
|
559
|
+
* avatar-optimizer や MToonAtlasLoaderPlugin に対し、
|
|
560
|
+
* DataTexture ではなく生 Float32Array(parameterBuffer)の直接受け渡しを要求するフラグ
|
|
561
|
+
*/
|
|
562
|
+
MToonAtlasNodeMaterial.prefersParameterBuffer = true;
|
|
563
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
564
|
+
0 && (module.exports = {
|
|
565
|
+
MToonAtlasLightingModel,
|
|
566
|
+
MToonAtlasNodeMaterial
|
|
567
|
+
});
|
|
568
|
+
//# sourceMappingURL=index.cjs.map
|