Skip to content

Commit 12a22f8

Browse files
Update Material type definitions: Add MaterialJSON format (#1072)
* Update Material type definitions: Add MaterialJSON format Introduces the MaterialJSON format in Three.js, which extends the Material interface. The new format includes serializable properties such as color, roughness, metallic, map, normalMap, and many more. This change enables better JSON parsing and handling of material configurations for Three.js applications. Confirmed: Types and interfaces have been updated in Object3D.d.ts and Material.d.ts. The Material class has also been updated to include toJSON methods that return MaterialJSON or MaterialJSONRoot objects based on the provided meta data. Reference(s): #1071 #1070 #426 * Fix: Error: 240:16 error Array type using 'T[]' is forbidden for non-simple types. Use 'Array<T>' instead @typescript-eslint/array-type * ShaderMaterial & Fixes * Add ShaderMaterialJSON to JSONMeta.materials record * Fixes Error: 105:28 error Array type using 'Array<number>' is forbidden for simple types. Use 'number[]' instead @typescript-eslint/array-type Error: 136:19 error Array type using 'Array<number>' is forbidden for simple types. Use 'number[]' instead @typescript-eslint/array-type Error: 157:22 error Array type using 'Array<number>' is forbidden for simple types. Use 'number[]' instead @typescript-eslint/array-type Error: 242:14 error Array type using 'Array<SourceJSON>' is forbidden for simple types. Use 'SourceJSON[]' instead @typescript-eslint/array-type * Error: 240:16 error Array type using 'T[]' is forbidden for non-simple types. Use 'Array<T>' instead @typescript-eslint/array-type * Material * ShaderMaterial * Object3D --------- Co-authored-by: Nathan Bierema <nbierema@gmail.com>
1 parent b72e5c5 commit 12a22f8

File tree

3 files changed

+215
-6
lines changed

3 files changed

+215
-6
lines changed

types/three/src/core/Object3D.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AnimationClip, AnimationClipJSON } from "../animation/AnimationClip.js";
22
import { Camera } from "../cameras/Camera.js";
3-
import { Material } from "../materials/Material.js";
3+
import { Material, MaterialJSON } from "../materials/Material.js";
44
import { Euler } from "../math/Euler.js";
55
import { Matrix3 } from "../math/Matrix3.js";
66
import { Matrix4, Matrix4Tuple } from "../math/Matrix4.js";
@@ -49,7 +49,7 @@ export interface Object3DJSON {
4949

5050
export interface JSONMeta {
5151
geometries: Record<string, BufferGeometryJSON>;
52-
materials: Record<string, unknown>;
52+
materials: Record<string, MaterialJSON>;
5353
textures: Record<string, TextureJSON>;
5454
images: Record<string, SourceJSON>;
5555
shapes: Record<string, unknown>;

types/three/src/materials/Material.d.ts

Lines changed: 164 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ import {
44
BlendingDstFactor,
55
BlendingEquation,
66
BlendingSrcFactor,
7+
Combine,
78
DepthModes,
9+
NormalMapTypes,
810
PixelFormat,
911
Side,
1012
StencilFunc,
1113
StencilOp,
1214
} from "../constants.js";
1315
import { BufferGeometry } from "../core/BufferGeometry.js";
1416
import { EventDispatcher } from "../core/EventDispatcher.js";
15-
import { Object3D } from "../core/Object3D.js";
17+
import { JSONMeta, Object3D } from "../core/Object3D.js";
1618
import { Color, ColorRepresentation } from "../math/Color.js";
1719
import { Plane } from "../math/Plane.js";
1820
import { Group } from "../objects/Group.js";
1921
import { WebGLProgramParametersWithUniforms } from "../renderers/webgl/WebGLPrograms.js";
2022
import { WebGLRenderer } from "../renderers/WebGLRenderer.js";
2123
import { Scene } from "../scenes/Scene.js";
24+
import { EulerTuple, SourceJSON, TextureJSON, Vector2Tuple } from "../Three.js";
2225

2326
export interface MaterialParameters {
2427
alphaHash?: boolean | undefined;
@@ -68,6 +71,165 @@ export interface MaterialParameters {
6871
userData?: Record<string, any> | undefined;
6972
}
7073

74+
export interface MaterialJSON {
75+
metadata: { version: number; type: string; generator: string };
76+
77+
uuid: string;
78+
type: string;
79+
80+
name?: string;
81+
82+
color?: number;
83+
roughness?: number;
84+
metalness?: number;
85+
86+
sheen?: number;
87+
sheenColor?: number;
88+
sheenRoughness?: number;
89+
emissive?: number;
90+
emissiveIntensity?: number;
91+
92+
specular?: number;
93+
specularIntensity?: number;
94+
specularColor?: number;
95+
shininess?: number;
96+
clearcoat?: number;
97+
clearcoatRoughness?: number;
98+
clearcoatMap?: string;
99+
clearcoatRoughnessMap?: string;
100+
clearcoatNormalMap?: string;
101+
clearcoatNormalScale?: Vector2Tuple;
102+
103+
dispersion?: number;
104+
105+
iridescence?: number;
106+
iridescenceIOR?: number;
107+
iridescenceThicknessRange?: number;
108+
iridescenceMap?: string;
109+
iridescenceThicknessMap?: string;
110+
111+
anisotropy?: number;
112+
anisotropyRotation?: number;
113+
anisotropyMap?: string;
114+
115+
map?: string;
116+
matcap?: string;
117+
alphaMap?: string;
118+
119+
lightMap?: string;
120+
lightMapIntensity?: number;
121+
122+
aoMap?: string;
123+
aoMapIntensity?: number;
124+
125+
bumpMap?: string;
126+
bumpScale?: number;
127+
128+
normalMap?: string;
129+
normalMapType?: NormalMapTypes;
130+
normalScale?: Vector2Tuple;
131+
132+
displacementMap?: string;
133+
displacementScale?: number;
134+
displacementBias?: number;
135+
136+
roughnessMap?: string;
137+
metalnessMap?: string;
138+
139+
emissiveMap?: string;
140+
specularMap?: string;
141+
specularIntensityMap?: string;
142+
specularColorMap?: string;
143+
144+
envMap?: string;
145+
combine?: Combine;
146+
147+
envMapRotation?: EulerTuple;
148+
envMapIntensity?: number;
149+
reflectivity?: number;
150+
refractionRatio?: number;
151+
152+
gradientMap?: string;
153+
154+
transmission?: number;
155+
transmissionMap?: string;
156+
thickness?: number;
157+
thicknessMap?: string;
158+
attenuationDistance?: number;
159+
attenuationColor?: number;
160+
161+
size?: number;
162+
shadowSide?: number;
163+
sizeAttenuation?: boolean;
164+
165+
blending?: Blending;
166+
side?: Side;
167+
vertexColors?: boolean;
168+
169+
opacity?: number;
170+
transparent?: boolean;
171+
172+
blendSrc?: BlendingSrcFactor;
173+
blendDst?: BlendingDstFactor;
174+
blendEquation?: BlendingEquation;
175+
blendSrcAlpha?: number | null;
176+
blendDstAlpha?: number | null;
177+
blendEquationAlpha?: number | null;
178+
blendColor?: number;
179+
blendAlpha?: number;
180+
181+
depthFunc?: DepthModes;
182+
depthTest?: boolean;
183+
depthWrite?: boolean;
184+
colorWrite?: boolean;
185+
186+
stencilWriteMask?: number;
187+
stencilFunc?: StencilFunc;
188+
stencilRef?: number;
189+
stencilFuncMask?: number;
190+
stencilFail?: StencilOp;
191+
stencilZFail?: StencilOp;
192+
stencilZPass?: StencilOp;
193+
stencilWrite?: boolean;
194+
195+
rotation?: number;
196+
197+
polygonOffset?: boolean;
198+
polygonOffsetFactor?: number;
199+
polygonOffsetUnits?: number;
200+
201+
linewidth?: number;
202+
dashSize?: number;
203+
gapSize?: number;
204+
scale?: number;
205+
206+
dithering?: boolean;
207+
208+
alphaTest?: number;
209+
alphaHash?: boolean;
210+
alphaToCoverage?: boolean;
211+
premultipliedAlpha?: boolean;
212+
forceSinglePass?: boolean;
213+
214+
wireframe?: boolean;
215+
wireframeLinewidth?: number;
216+
wireframeLinecap?: string;
217+
wireframeLinejoin?: string;
218+
219+
flatShading?: boolean;
220+
221+
visible?: boolean;
222+
223+
toneMapped?: boolean;
224+
225+
fog?: boolean;
226+
227+
userData?: Record<string, unknown>;
228+
229+
textures?: Array<Omit<TextureJSON, "metadata">>;
230+
images?: SourceJSON[];
231+
}
232+
71233
/**
72234
* Materials describe the appearance of objects. They are defined in a (mostly) renderer-independent way, so you don't have to rewrite materials if you decide to use a different renderer.
73235
*/
@@ -418,7 +580,7 @@ export class Material extends EventDispatcher<{ dispose: {} }> {
418580
* Convert the material to three.js JSON format.
419581
* @param meta Object containing metadata such as textures or images for the material.
420582
*/
421-
toJSON(meta?: any): any;
583+
toJSON(meta?: JSONMeta): MaterialJSON;
422584

423585
/**
424586
* Return a new material with the same parameters as this material.

types/three/src/materials/ShaderMaterial.d.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { GLSLVersion } from "../constants.js";
2+
import { JSONMeta } from "../core/Object3D.js";
23
import { UniformsGroup } from "../core/UniformsGroup.js";
4+
import { Matrix3, Matrix3Tuple } from "../math/Matrix3.js";
5+
import { Matrix4, Matrix4Tuple } from "../math/Matrix4.js";
6+
import { Vector2Tuple } from "../math/Vector2.js";
7+
import { Vector3Tuple } from "../math/Vector3.js";
8+
import { Vector4Tuple } from "../math/Vector4.js";
39
import { IUniform } from "../renderers/shaders/UniformsLib.js";
4-
import { Material, MaterialParameters } from "./Material.js";
10+
import { Material, MaterialJSON, MaterialParameters } from "./Material.js";
511

612
export interface ShaderMaterialParameters extends MaterialParameters {
713
uniforms?: { [uniform: string]: IUniform } | undefined;
@@ -23,6 +29,46 @@ export interface ShaderMaterialParameters extends MaterialParameters {
2329
glslVersion?: GLSLVersion | undefined;
2430
}
2531

32+
export type ShaderMaterialUniformJSON = {
33+
type: "t";
34+
value: string;
35+
} | {
36+
type: "c";
37+
value: number;
38+
} | {
39+
type: "v2";
40+
value: Vector2Tuple;
41+
} | {
42+
type: "v3";
43+
value: Vector3Tuple;
44+
} | {
45+
type: "v4";
46+
value: Vector4Tuple;
47+
} | {
48+
type: "m3";
49+
value: Matrix3Tuple;
50+
} | {
51+
type: "m4";
52+
value: Matrix4Tuple;
53+
} | {
54+
value: unknown;
55+
};
56+
57+
export interface ShaderMaterialJSON extends MaterialJSON {
58+
glslVersion: number | null;
59+
uniforms: Record<string, ShaderMaterialUniformJSON>;
60+
61+
defines?: Record<string, unknown>;
62+
63+
vertexShader: string;
64+
ragmentShader: string;
65+
66+
lights: boolean;
67+
clipping: boolean;
68+
69+
extensions?: Record<string, boolean>;
70+
}
71+
2672
export class ShaderMaterial extends Material {
2773
constructor(parameters?: ShaderMaterialParameters);
2874

@@ -116,5 +162,6 @@ export class ShaderMaterial extends Material {
116162
glslVersion: GLSLVersion | null;
117163

118164
setValues(parameters: ShaderMaterialParameters): void;
119-
toJSON(meta: any): any;
165+
166+
toJSON(meta?: JSONMeta): ShaderMaterialJSON;
120167
}

0 commit comments

Comments
 (0)