Skip to content

Commit 9f12b0e

Browse files
authored
Merge pull request #1427 from pixiv/fix/seperate-nodes-entry-for-backward-compatibility
fix: seperate nodes entry for backward compatibility
2 parents 5bece43 + 3dd97e4 commit 9f12b0e

20 files changed

+195
-120
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Code like this:
9292

9393
```javascript
9494
import * as THREE from 'three';
95-
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
95+
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
9696
import { VRMLoaderPlugin } from '@pixiv/three-vrm';
9797

9898
const scene = new THREE.Scene();
@@ -131,25 +131,31 @@ loader.load(
131131
### Use with WebGPURenderer
132132

133133
Starting from v3, we provide [WebGPURenderer](https://github.com/mrdoob/three.js/blob/master/examples/jsm/renderers/webgpu/WebGPURenderer.js) compatibility.
134-
To use three-vrm with WebGPURenderer, specify the WebGPU-compatible `MToonNodeMaterialLoaderPlugin` for the `mtoonMaterialPlugin` option of `VRMLoaderPlugin`.
134+
To use three-vrm with WebGPURenderer, specify the WebGPU-compatible `MToonNodeMaterial` for the `materialType` option of `MToonMaterialLoaderPlugin`.
135135

136136
`MToonNodeMaterial` only supports Three.js r161 or later.
137137
The NodeMaterial system of Three.js is still under development, so we may break compatibility with older versions of Three.js more frequently than other parts of three-vrm.
138138

139139
```js
140-
import { VRMLoaderPlugin, MToonNodeMaterialLoaderPlugin } from '@pixiv/three-vrm';
140+
import { VRMLoaderPlugin } from '@pixiv/three-vrm';
141+
import { MToonNodeMaterial } from '@pixiv/three-vrm/nodes';
141142

142143
// ...
143144

144145
// Register a VRMLoaderPlugin
145146
loader.register((parser) => {
146147

147-
// create a WebGPU compatible MToon loader plugin
148-
const mtoonMaterialPlugin = new MToonNodeMaterialLoaderPlugin(parser);
148+
// create a WebGPU compatible MToonMaterialLoaderPlugin
149+
const mtoonMaterialPlugin = new MToonMaterialLoaderPlugin(parser, {
150+
151+
// set the material type to MToonNodeMaterial
152+
materialType: MToonNodeMaterial,
153+
154+
});
149155

150156
return new VRMLoaderPlugin(parser, {
151157

152-
// Specify the MToon loader plugin to use in the VRMLoaderPlugin instance
158+
// Specify the MToonMaterialLoaderPlugin to use in the VRMLoaderPlugin instance
153159
mtoonMaterialPlugin,
154160

155161
});

bin/build.mjs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,25 @@ async function buildPackage(absWorkingDir) {
5555
* @param {'esm' | 'cjs'} format
5656
*/
5757
const entryPoints = (format) => {
58-
let outFilename = filename + (format === 'esm' ? '.module' : '');
59-
outFilename += DEV ? '' : '.min';
58+
/**
59+
* Add suffix according to the format to the given basename of the output file.
60+
* @param {string} base Basename of the output file
61+
* @returns {string} `<base>[.module][.min]`
62+
*/
63+
const addSuffix = (base) => {
64+
let outFilename = base + (format === 'esm' ? '.module' : '');
65+
return outFilename + (DEV ? '' : '.min');
66+
};
67+
68+
const needsNodesBuild = ['@pixiv/three-vrm', '@pixiv/three-vrm-materials-mtoon'].includes(packageJson.name);
69+
const extraEntryPoints = needsNodesBuild
70+
? { [addSuffix('nodes/index')]: 'src/nodes/index.ts' }
71+
: {};
72+
6073
return {
6174
entryPoints: {
62-
[outFilename]: 'src/index.ts',
75+
[addSuffix(filename)]: 'src/index.ts',
76+
...extraEntryPoints,
6377
},
6478
format,
6579
};

packages/three-vrm-animation/src/VRMAnimationLoaderPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as THREE from 'three';
2-
import { GLTF, GLTFLoaderPlugin, GLTFParser } from 'three/examples/jsm/loaders/GLTFLoader';
2+
import { GLTF, GLTFLoaderPlugin, GLTFParser } from 'three/examples/jsm/loaders/GLTFLoader.js';
33
import { GLTF as GLTFSchema } from '@gltf-transform/core';
44
import { VRMCVRMAnimation } from '@pixiv/types-vrmc-vrm-animation-1.0';
55
import type { VRMHumanBoneName } from '@pixiv/three-vrm-core';

packages/three-vrm-materials-mtoon/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,25 @@ MToon (toon material) module for @pixiv/three-vrm
1111
## WebGPU Support
1212

1313
Starting from v3, we provide [WebGPURenderer](https://github.com/mrdoob/three.js/blob/master/examples/jsm/renderers/webgpu/WebGPURenderer.js) compatibility.
14-
To use three-vrm with WebGPURenderer, specify the WebGPU-compatible `MToonNodeMaterialLoaderPlugin` for the `mtoonMaterialPlugin` option of `VRMLoaderPlugin`.
14+
To use MToon with WebGPURenderer, specify the WebGPU-compatible `MToonNodeMaterial` for the `materialType` option of `MToonMaterialLoaderPlugin`.
1515

1616
`MToonNodeMaterial` only supports Three.js r161 or later.
1717
The NodeMaterial system of Three.js is still under development, so we may break compatibility with older versions of Three.js more frequently than other parts of three-vrm.
1818

1919
```js
20-
import { VRMLoaderPlugin, MToonNodeMaterialLoaderPlugin } from '@pixiv/three-vrm';
20+
import { MToonMaterialLoaderPlugin } from '@pixiv/three-vrm-materials-mtoon';
21+
import { MToonNodeMaterial } from '@pixiv/three-vrm-materials-mtoon/nodes';
2122

2223
// ...
2324

24-
// Register a VRMLoaderPlugin
25+
// Register a MToonMaterialLoaderPlugin with MToonNodeMaterial
2526
loader.register((parser) => {
2627

27-
// create a WebGPU compatible MToon loader plugin
28-
const mtoonMaterialPlugin = new MToonNodeMaterialLoaderPlugin(parser);
28+
// create a WebGPU compatible MToonMaterialLoaderPlugin
29+
return new MToonMaterialLoaderPlugin(parser, {
2930

30-
return new VRMLoaderPlugin(parser, {
31-
32-
// Specify the MToon loader plugin to use in the VRMLoaderPlugin instance
33-
mtoonMaterialPlugin,
31+
// set the material type to MToonNodeMaterial
32+
materialType: MToonNodeMaterial,
3433

3534
});
3635

packages/three-vrm-materials-mtoon/examples/webgpu-feature-test.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"imports": {
2626
"three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
2727
"three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/",
28-
"@pixiv/three-vrm-materials-mtoon": "../lib/three-vrm-materials-mtoon.module.js"
28+
"@pixiv/three-vrm-materials-mtoon": "../lib/three-vrm-materials-mtoon.module.js",
29+
"@pixiv/three-vrm-materials-mtoon/nodes": "../lib/nodes/index.module.js"
2930
}
3031
}
3132
</script>
@@ -34,7 +35,7 @@
3435
import * as THREE from 'three';
3536
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
3637
import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
37-
import { MToonNodeMaterial } from '@pixiv/three-vrm-materials-mtoon';
38+
import { MToonNodeMaterial } from '@pixiv/three-vrm-materials-mtoon/nodes';
3839

3940
// renderer
4041
const renderer = new WebGPURenderer();

packages/three-vrm-materials-mtoon/examples/webgpu-loader-plugin.html

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
<script type="importmap">
2424
{
2525
"imports": {
26-
"three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
27-
"three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/",
28-
"@pixiv/three-vrm-materials-mtoon": "../lib/three-vrm-materials-mtoon.module.js"
26+
"three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
27+
"three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/",
28+
"@pixiv/three-vrm-materials-mtoon": "../lib/three-vrm-materials-mtoon.module.js",
29+
"@pixiv/three-vrm-materials-mtoon/nodes": "../lib/nodes/index.module.js"
2930
}
3031
}
3132
</script>
@@ -35,7 +36,8 @@
3536
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
3637
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
3738
import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
38-
import { MToonNodeMaterialLoaderPlugin } from '@pixiv/three-vrm-materials-mtoon';
39+
import { MToonMaterialLoaderPlugin } from '@pixiv/three-vrm-materials-mtoon';
40+
import { MToonNodeMaterial } from '@pixiv/three-vrm-materials-mtoon/nodes';
3941

4042
// renderer
4143
const renderer = new WebGPURenderer();
@@ -67,8 +69,14 @@
6769

6870
loader.register( ( parser ) => {
6971

70-
const plugin = new MToonNodeMaterialLoaderPlugin( parser );
71-
return plugin;
72+
73+
// create a WebGPU compatible MToonMaterialLoaderPlugin
74+
return new MToonMaterialLoaderPlugin( parser, {
75+
76+
// set the material type to MToonNodeMaterial
77+
materialType: MToonNodeMaterial,
78+
79+
} );
7280

7381
} );
7482

packages/three-vrm-materials-mtoon/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
"import": "./lib/three-vrm-materials-mtoon.module.js",
1919
"require": "./lib/three-vrm-materials-mtoon.cjs"
2020
},
21+
"./nodes": {
22+
"types": "./types/nodes/index.d.ts",
23+
"import": "./lib/nodes/index.module.js",
24+
"require": "./lib/nodes/index.cjs"
25+
},
2126
"./package.json": "./package.json"
2227
},
2328
"repository": {

packages/three-vrm-materials-mtoon/src/MToonMaterialLoaderPlugin.ts

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import * as THREE from 'three';
22
import * as V1MToonSchema from '@pixiv/types-vrmc-materials-mtoon-1.0';
33
import type { GLTF, GLTFLoader, GLTFLoaderPlugin, GLTFParser } from 'three/examples/jsm/loaders/GLTFLoader.js';
44
import type { MToonMaterialParameters } from './MToonMaterialParameters';
5-
import { MToonMaterialOutlineWidthMode } from './MToonMaterialOutlineWidthMode';
5+
import type { MToonMaterialOutlineWidthMode } from './MToonMaterialOutlineWidthMode';
66
import { GLTFMToonMaterialParamsAssignHelper } from './GLTFMToonMaterialParamsAssignHelper';
7-
import { MToonMaterialLoaderPluginOptions } from './MToonMaterialLoaderPluginOptions';
7+
import type { MToonMaterialLoaderPluginOptions } from './MToonMaterialLoaderPluginOptions';
88
import type { MToonMaterialDebugMode } from './MToonMaterialDebugMode';
99
import { GLTF as GLTFSchema } from '@gltf-transform/core';
1010
import { MToonMaterial } from './MToonMaterial';
11-
import type { MToonNodeMaterialLoaderPlugin } from './nodes/MToonNodeMaterialLoaderPlugin';
11+
import type { MToonNodeMaterial } from './nodes/MToonNodeMaterial';
1212

1313
/**
1414
* Possible spec versions it recognizes.
@@ -18,24 +18,56 @@ const POSSIBLE_SPEC_VERSIONS = new Set(['1.0', '1.0-beta']);
1818
/**
1919
* A loader plugin of {@link GLTFLoader} for the extension `VRMC_materials_mtoon`.
2020
*
21-
* This plugin is for uses with WebGLRenderer.
22-
* To use MToon in WebGPURenderer, use {@link MToonNodeMaterialLoaderPlugin} instead.
21+
* This plugin is for uses with WebGLRenderer by default.
22+
* To use MToon in WebGPURenderer, set {@link materialType} to {@link MToonNodeMaterial}.
23+
*
24+
* @example to use with WebGPURenderer
25+
* ```js
26+
* import { MToonMaterialLoaderPlugin } from '@pixiv/three-vrm-materials-mtoon';
27+
* import { MToonNodeMaterial } from '@pixiv/three-vrm-materials-mtoon/nodes';
28+
*
29+
* // ...
30+
*
31+
* // Register a MToonMaterialLoaderPlugin with MToonNodeMaterial
32+
* loader.register((parser) => {
33+
*
34+
* // create a WebGPU compatible MToonMaterialLoaderPlugin
35+
* return new MToonMaterialLoaderPlugin(parser, {
36+
*
37+
* // set the material type to MToonNodeMaterial
38+
* materialType: MToonNodeMaterial,
39+
*
40+
* });
41+
*
42+
* });
43+
* ```
2344
*/
2445
export class MToonMaterialLoaderPlugin implements GLTFLoaderPlugin {
2546
public static EXTENSION_NAME = 'VRMC_materials_mtoon';
2647

48+
/**
49+
* The type of the material that this plugin will generate.
50+
*
51+
* If you are using this plugin with WebGPU, set this to {@link MToonNodeMaterial}.
52+
*
53+
* @default MToonMaterial
54+
*/
55+
public materialType: typeof THREE.Material;
56+
2757
/**
2858
* This value will be added to `renderOrder` of every meshes who have MaterialsMToon.
2959
* The final renderOrder will be sum of this `renderOrderOffset` and `renderQueueOffsetNumber` for each materials.
30-
* `0` by default.
60+
*
61+
* @default 0
3162
*/
3263
public renderOrderOffset: number;
3364

3465
/**
3566
* There is a line of the shader called "comment out if you want to PBR absolutely" in VRM0.0 MToon.
3667
* When this is true, the material enables the line to make it compatible with the legacy rendering of VRM.
3768
* Usually not recommended to turn this on.
38-
* `false` by default.
69+
*
70+
* @default false
3971
*/
4072
public v0CompatShade: boolean;
4173

@@ -44,6 +76,8 @@ export class MToonMaterialLoaderPlugin implements GLTFLoaderPlugin {
4476
* You can visualize several components for diagnosis using debug mode.
4577
*
4678
* See: {@link MToonMaterialDebugMode}
79+
*
80+
* @default 'none'
4781
*/
4882
public debugMode: MToonMaterialDebugMode;
4983

@@ -62,6 +96,7 @@ export class MToonMaterialLoaderPlugin implements GLTFLoaderPlugin {
6296
public constructor(parser: GLTFParser, options: MToonMaterialLoaderPluginOptions = {}) {
6397
this.parser = parser;
6498

99+
this.materialType = options.materialType ?? MToonMaterial;
65100
this.renderOrderOffset = options.renderOrderOffset ?? 0;
66101
this.v0CompatShade = options.v0CompatShade ?? false;
67102
this.debugMode = options.debugMode ?? 'none';
@@ -80,7 +115,7 @@ export class MToonMaterialLoaderPlugin implements GLTFLoaderPlugin {
80115
public getMaterialType(materialIndex: number): typeof THREE.Material | null {
81116
const v1Extension = this._getMToonExtension(materialIndex);
82117
if (v1Extension) {
83-
return MToonMaterial;
118+
return this.materialType;
84119
}
85120

86121
return null;
@@ -248,6 +283,22 @@ export class MToonMaterialLoaderPlugin implements GLTFLoaderPlugin {
248283
}
249284
}
250285

286+
/**
287+
* Check whether the material should generate outline or not.
288+
* @param surfaceMaterial The material to check
289+
* @returns True if the material should generate outline
290+
*/
291+
private _shouldGenerateOutline(surfaceMaterial: THREE.Material): boolean {
292+
// we might receive MToonNodeMaterial as well as MToonMaterial
293+
// so we're gonna duck type to check if it's compatible with MToon type outlines
294+
return (
295+
typeof (surfaceMaterial as any).outlineWidthMode === 'string' &&
296+
(surfaceMaterial as any).outlineWidthMode !== 'none' &&
297+
typeof (surfaceMaterial as any).outlineWidthFactor === 'number' &&
298+
(surfaceMaterial as any).outlineWidthFactor > 0.0
299+
);
300+
}
301+
251302
/**
252303
* Generate outline for the given mesh, if it needs.
253304
*
@@ -265,15 +316,7 @@ export class MToonMaterialLoaderPlugin implements GLTFLoaderPlugin {
265316
return;
266317
}
267318

268-
// check whether we really have to prepare outline or not
269-
// we might receive MToonNodeMaterial as well as MToonMaterial
270-
// so we're gonna duck type to check if it's compatible with MToon type outlines
271-
if (
272-
typeof (surfaceMaterial as any).outlineWidthMode !== 'string' ||
273-
(surfaceMaterial as any).outlineWidthMode === 'none' ||
274-
typeof (surfaceMaterial as any).outlineWidthFactor !== 'number' ||
275-
(surfaceMaterial as any).outlineWidthFactor <= 0.0
276-
) {
319+
if (!this._shouldGenerateOutline(surfaceMaterial)) {
277320
return;
278321
}
279322

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1+
import type * as THREE from 'three';
12
import type { MToonMaterialDebugMode } from './MToonMaterialDebugMode';
3+
import type { MToonNodeMaterial } from './nodes/MToonNodeMaterial';
24

35
export interface MToonMaterialLoaderPluginOptions {
6+
/**
7+
* The type of the material that the loader plugin will generate.
8+
*
9+
* If you are using this plugin with WebGPU, set this to {@link MToonNodeMaterial}.
10+
*
11+
* @default MToonMaterial
12+
*/
13+
materialType?: typeof THREE.Material;
14+
415
/**
516
* This value will be added to `renderOrder` of every meshes who have MToonMaterial.
617
* The final `renderOrder` will be sum of this `renderOrderOffset` and `renderQueueOffsetNumber` for each materials.
7-
* `0` by default.
18+
*
19+
* @default 0
820
*/
921
renderOrderOffset?: number;
1022

1123
/**
1224
* There is a line of the shader called "comment out if you want to PBR absolutely" in VRM0.0 MToon.
1325
* When this is true, the material enables the line to make it compatible with the legacy rendering of VRM.
1426
* Usually not recommended to turn this on.
15-
* `false` by default.
27+
*
28+
* @default false
1629
*/
1730
v0CompatShade?: boolean;
1831

@@ -21,6 +34,8 @@ export interface MToonMaterialLoaderPluginOptions {
2134
* You can visualize several components for diagnosis using debug mode.
2235
*
2336
* See: {@link MToonMaterialDebugMode}
37+
*
38+
* @default 'none'
2439
*/
2540
debugMode?: MToonMaterialDebugMode;
2641
}

packages/three-vrm-materials-mtoon/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@ export { MToonMaterial } from './MToonMaterial';
44
export { MToonMaterialDebugMode } from './MToonMaterialDebugMode';
55
export { MToonMaterialOutlineWidthMode } from './MToonMaterialOutlineWidthMode';
66
export type { MToonMaterialParameters } from './MToonMaterialParameters';
7-
8-
export * from './nodes';

0 commit comments

Comments
 (0)