@webxr-jp/mtoon-atlas 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 xrift
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,300 @@
1
+ # @webxr-jp/mtoon-atlas
2
+
3
+ MToon shader atlas optimization utilities for three-vrm WebGL applications.
4
+
5
+ ## Overview
6
+
7
+ `@webxr-jp/mtoon-atlas` provides `MToonAtlasMaterial` - a WebGL-based custom MToon material that consumes the atlas + packed parameter textures produced by `@webxr-jp/avatar-optimizer`.
8
+
9
+ 本パッケージは `SkinnedMesh` で使用中の複数 `MToonMaterial` を 1 つにまとめ、頂点属性経由で元マテリアルを参照する形に統一することを目的としています。
10
+
11
+ **架構戦略**: `MToonAtlasMaterial` は `THREE.ShaderMaterial` を継承した WebGL ベースの実装です。WebXR 環境での互換性を重視しており、WebGPU 対応は将来の検討事項となっています。
12
+
13
+ ## Features
14
+
15
+ - **ShaderMaterial ベースのカスタムマテリアル**: WebGL 標準対応、WebXR 環境で動作確実
16
+ - **パラメータテクスチャサポート**: 全20種類の数値パラメータをテクスチャからサンプリング
17
+ - litFactor(baseColor), opacity, shadeColor, emissiveColor, emissiveIntensity
18
+ - shadingShift, shadingToony, shadingShiftTextureScale
19
+ - rimLightingMix, parametricRimColor, parametricRimLift, parametricRimFresnelPower
20
+ - matcapFactor, outlineWidth, outlineColor, outlineLightingMix
21
+ - uvAnimationScrollX, uvAnimationScrollY, uvAnimationRotation, normalScale
22
+ - **アトラステクスチャ自動設定**: 8種類のテクスチャマップを自動バインディング
23
+ - baseColor, shade, shadingShift, normal, emissive, matcap, rim, uvAnimationMask
24
+ - **頂点属性ベースのスロット管理**: 元マテリアルのスロット index を頂点属性で受け取る
25
+ - **SkinnedMesh 対応**: InstancedMesh 不要でスキニングアニメーションと互換
26
+
27
+ ## Installation
28
+
29
+ ```bash
30
+ pnpm add @webxr-jp/mtoon-atlas
31
+ ```
32
+
33
+ ## Requirements
34
+
35
+ - Three.js r181+
36
+ - @pixiv/three-vrm 3.4.4+
37
+ - @pixiv/three-vrm-materials-mtoon 3.4.4+
38
+
39
+ ## Usage
40
+
41
+ ### 基本的な使い方
42
+
43
+ ```typescript
44
+ import { Float32BufferAttribute, Mesh } from 'three'
45
+ import { MToonAtlasMaterial } from '@webxr-jp/mtoon-atlas'
46
+
47
+ // 1. SkinnedMesh のジオメトリに slot index 属性を追加する
48
+ const slotIndices = new Float32Array(vertexCount) // avatar-optimizer から得たインデックス
49
+ geometry.setAttribute('mtoonMaterialSlot', new Float32BufferAttribute(slotIndices, 1))
50
+
51
+ // 2. パラメータテクスチャとアトラステクスチャを設定
52
+ const material = new MToonAtlasMaterial()
53
+ material.setParameterTexture({
54
+ texture: packedParameterTexture,
55
+ slotCount: packedSlotCount,
56
+ texelsPerSlot: 9, // デフォルト値: 9テクセル/スロット
57
+
58
+ // アトラス化されたテクスチャを指定すると自動的にマテリアルに設定されます
59
+ atlasedTextures: {
60
+ baseColor: atlasMainTexture,
61
+ normal: atlasNormalTexture,
62
+ emissive: atlasEmissiveTexture,
63
+ shade: atlasShadeTexture,
64
+ matcap: atlasMatcapTexture,
65
+ rim: atlasRimTexture,
66
+ shadingShift: atlasShadingShiftTexture,
67
+ uvAnimationMask: atlasUvAnimationMaskTexture,
68
+ }
69
+ })
70
+
71
+ const mesh = new Mesh(geometry, material)
72
+ ```
73
+
74
+ ### コンストラクタで一括設定
75
+
76
+ ```typescript
77
+ const material = new MToonAtlasMaterial({
78
+ // THREE.ShaderMaterial のパラメータ
79
+ transparent: true,
80
+ depthWrite: true,
81
+ fog: true,
82
+ lights: true,
83
+
84
+ // パラメータテクスチャ設定
85
+ parameterTexture: {
86
+ texture: packedParameterTexture,
87
+ slotCount: packedSlotCount,
88
+ texelsPerSlot: 9, // デフォルト値
89
+ atlasedTextures: {
90
+ baseColor: atlasMainTexture,
91
+ normal: atlasNormalTexture,
92
+ shade: atlasShadeTexture,
93
+ emissive: atlasEmissiveTexture,
94
+ // ... その他のテクスチャ
95
+ }
96
+ },
97
+
98
+ // スロット属性設定(オプション)
99
+ slotAttribute: {
100
+ name: 'mtoonMaterialSlot' // デフォルト値
101
+ }
102
+ })
103
+ ```
104
+
105
+ ### 手動でテクスチャを設定する場合(従来の方法)
106
+
107
+ ```typescript
108
+ const material = new MToonAtlasMaterial({
109
+ // MToonNodeMaterial のテクスチャプロパティを直接設定
110
+ map: atlasMainTexture,
111
+ normalMap: atlasNormalTexture,
112
+ emissiveMap: atlasEmissiveTexture,
113
+
114
+ parameterTexture: {
115
+ texture: packedParameterTexture,
116
+ slotCount: packedSlotCount,
117
+ texelsPerSlot: 9, // デフォルト値
118
+ // atlasedTextures を省略した場合は手動設定が必要
119
+ },
120
+ })
121
+ ```
122
+
123
+ ## Material slot attribute specification
124
+
125
+ SkinnedMesh で複数マテリアルを結合するため、各頂点には「元マテリアルに対応する slot index」を記録した `BufferAttribute` を追加します。
126
+
127
+ ```ts
128
+ // root geometry に直接書き込む
129
+ geometry.setAttribute(
130
+ 'mtoonMaterialSlot', // デフォルト名。MaterialOptions で変更可能
131
+ new Float32BufferAttribute(slotIndices, 1),
132
+ )
133
+ ```
134
+
135
+ 頂点属性を使用しているため `InstancedMesh` を組む必要はなく、スキニング後もそのまま varying としてシェーダに渡せます。
136
+
137
+ **Vertex Shader での処理**:
138
+ ```glsl
139
+ attribute float mtoonMaterialSlot; // 頂点属性として受け取る
140
+ varying float vMaterialSlot; // Fragment Shader へ渡す
141
+
142
+ void main() {
143
+ vMaterialSlot = mtoonMaterialSlot;
144
+ // ... 通常の頂点変換処理
145
+ }
146
+ ```
147
+
148
+ **Fragment Shader での処理**:
149
+ ```glsl
150
+ varying float vMaterialSlot; // Vertex Shader から受け取る
151
+
152
+ void main() {
153
+ // vMaterialSlot を使用してパラメータテクスチャからパラメータをサンプリング
154
+ vec4 param0 = sampleParameter(vMaterialSlot, 0.0);
155
+ // ...
156
+ }
157
+ ```
158
+
159
+ ## API Reference
160
+
161
+ ### MToonAtlasMaterial
162
+
163
+ #### Constructor
164
+
165
+ ```typescript
166
+ new MToonAtlasMaterial(options?: MToonAtlasOptions)
167
+ ```
168
+
169
+ `THREE.ShaderMaterialParameters` を継承し、以下のインスタンシング固有オプションを追加サポート:
170
+
171
+ - `parameterTexture?: ParameterTextureDescriptor` - パラメータテクスチャディスクリプタ
172
+ - `slotAttribute?: MaterialSlotAttributeConfig` - スロット属性設定
173
+
174
+ #### Methods
175
+
176
+ **`setParameterTexture(descriptor: ParameterTextureDescriptor | null): this`**
177
+
178
+ パラメータテクスチャをバインドします。`atlasedTextures` が指定されている場合、テクスチャマップが自動設定されます。
179
+
180
+ **`setSlotAttribute(attribute: MaterialSlotAttributeConfig): this`**
181
+
182
+ スロット属性メタデータを更新します。
183
+
184
+ #### Properties
185
+
186
+ **`slotAttribute: MaterialSlotAttributeConfig` (読み取り専用)**
187
+
188
+ 現在のスロット属性設定を取得します。
189
+
190
+ **`parameterTexture: ParameterTextureDescriptor | undefined` (読み取り専用)**
191
+
192
+ 現在のパラメータテクスチャディスクリプタを取得します。
193
+
194
+ **`isMToonAtlasMaterial: true` (読み取り専用)**
195
+
196
+ マテリアル識別用フラグ。
197
+
198
+ ### Types
199
+
200
+ #### `ParameterTextureDescriptor`
201
+
202
+ ```typescript
203
+ interface ParameterTextureDescriptor {
204
+ texture: Texture // パック済みパラメータテクスチャ
205
+ slotCount: number // スロット数
206
+ texelsPerSlot: number // スロットあたりのテクセル数(デフォルト: 9)
207
+ semantics?: ParameterSemantic[] // カスタムレイアウト(省略時はデフォルト)
208
+ atlasedTextures?: AtlasedTextureSet // アトラステクスチャセット
209
+ }
210
+ ```
211
+
212
+ #### `AtlasedTextureSet`
213
+
214
+ ```typescript
215
+ interface AtlasedTextureSet {
216
+ baseColor?: Texture // MToonNodeMaterial.map
217
+ shade?: Texture // shadeMultiplyTexture
218
+ shadingShift?: Texture // shadingShiftTexture
219
+ normal?: Texture // normalMap
220
+ emissive?: Texture // emissiveMap
221
+ matcap?: Texture // matcapTexture
222
+ rim?: Texture // rimMultiplyTexture
223
+ uvAnimationMask?: Texture // uvAnimationMaskTexture
224
+ }
225
+ ```
226
+
227
+ #### `MaterialSlotAttributeConfig`
228
+
229
+ ```typescript
230
+ interface MaterialSlotAttributeConfig {
231
+ name: string // 属性名(デフォルト: 'mtoonMaterialSlot')
232
+ description?: string // オプション説明
233
+ }
234
+ ```
235
+
236
+ ## Parameter texture layout
237
+
238
+ avatar-optimizer で生成されたパラメータテクスチャは、各スロット(元マテリアル)あたり 9 texel(`texelsPerSlot = 9`)の RGBA チャンネルに、以下の順番で 20 個のパラメータを詰め込んだ構造になっています。
239
+
240
+ `MToonAtlasMaterial` の Fragment Shader はこのレイアウトを前提にパラメータをサンプリングします:
241
+
242
+ ```glsl
243
+ // Fragment Shader でのサンプリング例
244
+ vec4 param0 = sampleParameter(vMaterialSlot, 0.0); // Texel 0: litFactor + opacity
245
+ vec3 litFactor = param0.rgb;
246
+ float opacity = param0.a;
247
+
248
+ vec4 param1 = sampleParameter(vMaterialSlot, 1.0); // Texel 1: shadeColorFactor + shadingShiftTextureScale
249
+ vec3 shadeColorFactor = param1.rgb;
250
+ float shadingShiftTextureScale = param1.a;
251
+
252
+ // ... 以下同様に各テクセルをサンプリング
253
+ ```
254
+
255
+ | Texel index | Channel(s) | Semantic | 内容 |
256
+ | ----------- | ---------- | -------- | ---- |
257
+ | 0 | RGB | `litFactor` | ベースディフューズカラー (linear RGB) |
258
+ | 0 | A | `opacity` | 不透明度 |
259
+ | 1 | RGB | `shadeColorFactor` | Shade pass 用カラー |
260
+ | 1 | A | `shadingShiftTextureScale` | Shading shift texture スケール |
261
+ | 2 | RGB | `emissive` | エミッシブカラー |
262
+ | 2 | A | `emissiveIntensity` | エミッシブ強度 |
263
+ | 3 | RGB | `matcapFactor` | Matcap カラー係数 |
264
+ | 3 | A | `outlineWidthFactor` | アウトライン幅 |
265
+ | 4 | RGB | `outlineColorFactor` | アウトラインカラー |
266
+ | 4 | A | `outlineLightingMixFactor` | アウトラインのライティングブレンド |
267
+ | 5 | RGB | `parametricRimColorFactor` | Parametric rim カラー |
268
+ | 5 | A | `parametricRimLiftFactor` | Parametric rim lift |
269
+ | 6 | R | `parametricRimFresnelPowerFactor` | Parametric rim フレネル係数 |
270
+ | 6 | G | `shadingToonyFactor` | Toon 化係数 |
271
+ | 6 | B | `rimLightingMixFactor` | ランバート/リムブレンド比 |
272
+ | 6 | A | `uvAnimationRotationPhase` | UV アニメーション回転速度 |
273
+ | 7 | RG | `normalScale` | ノーマルマップスケール (x, y) |
274
+ | 7 | B | `uvAnimationScrollXOffset` | UV アニメーション X 方向スクロール |
275
+ | 7 | A | `uvAnimationScrollYOffset` | UV アニメーション Y 方向スクロール |
276
+ | 8 | R | `shadingShiftFactor` | シェーディングシフト係数 |
277
+
278
+ ## Development
279
+
280
+ ### Build
281
+
282
+ ```bash
283
+ pnpm -F mtoon-atlas run build
284
+ ```
285
+
286
+ ### Test
287
+
288
+ ```bash
289
+ pnpm -F mtoon-atlas run test
290
+ ```
291
+
292
+ ### Watch Mode
293
+
294
+ ```bash
295
+ pnpm -F mtoon-atlas run dev
296
+ ```
297
+
298
+ ## License
299
+
300
+ MIT