Skip to content

Commit 94e871d

Browse files
committed
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): three-types#1071 three-types#1070 three-types#426
1 parent b72e5c5 commit 94e871d

File tree

2 files changed

+179
-4
lines changed

2 files changed

+179
-4
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: 177 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import {
1212
} from "../constants.js";
1313
import { BufferGeometry } from "../core/BufferGeometry.js";
1414
import { EventDispatcher } from "../core/EventDispatcher.js";
15-
import { Object3D } from "../core/Object3D.js";
15+
import { JSONMeta, Object3D } from "../core/Object3D.js";
1616
import { Color, ColorRepresentation } from "../math/Color.js";
1717
import { Plane } from "../math/Plane.js";
1818
import { Group } from "../objects/Group.js";
1919
import { WebGLProgramParametersWithUniforms } from "../renderers/webgl/WebGLPrograms.js";
2020
import { WebGLRenderer } from "../renderers/WebGLRenderer.js";
2121
import { Scene } from "../scenes/Scene.js";
22+
import { SourceJSON, TextureJSON } from "../Three.js";
2223

2324
export interface MaterialParameters {
2425
alphaHash?: boolean | undefined;
@@ -67,6 +68,179 @@ export interface MaterialParameters {
6768
stencilZPass?: StencilOp | undefined;
6869
userData?: Record<string, any> | undefined;
6970
}
71+
export class MaterialJSON {
72+
metadata: {
73+
version: number;
74+
type: "Material";
75+
generator: "Material.toJSON";
76+
};
77+
78+
// standard Material serialization
79+
uuid: string;
80+
type: string;
81+
name: string;
82+
83+
color?: number;
84+
roughness?: number;
85+
metalness?: number;
86+
87+
sheen?: number;
88+
sheenColor?: number;
89+
sheenRoughness?: number;
90+
91+
emissive?: number;
92+
emissiveIntensity?: number;
93+
94+
specular?: number;
95+
specularIntensity?: number;
96+
specularColor?: number;
97+
98+
shininess?: number;
99+
100+
clearcoat?: number;
101+
clearcoatRoughness?: number;
102+
clearcoatMap?: string;
103+
clearcoatRoughnessMap?: string;
104+
clearcoatNormalMap?: string;
105+
clearcoatNormalScale?: number[];
106+
107+
dispersion?: number;
108+
109+
iridescence?: number;
110+
iridescenceIOR?: number;
111+
iridescenceThicknessRange?: number;
112+
iridescenceMap?: string;
113+
iridescenceThicknessMap?: string;
114+
115+
anisotropy?: number;
116+
anisotropyRotation?: number;
117+
anisotropyMap?: string;
118+
119+
map?: string;
120+
121+
matcap?: string;
122+
123+
alphaMap?: string;
124+
125+
lightMap?: string;
126+
lightMapIntensity?: number;
127+
128+
aoMap?: string;
129+
aoMapIntensity?: number;
130+
131+
bumpMap?: string;
132+
bumpScale?: number;
133+
134+
normalMap?: string;
135+
normalMapType?: number;
136+
normalScale?: number[];
137+
138+
displacementMap?: string;
139+
displacementScale?: number;
140+
displacementBias?: number;
141+
142+
roughnessMap?: string;
143+
144+
metalnessMap?: string;
145+
146+
emissiveMap?: string;
147+
148+
specularMap?: string;
149+
150+
specularIntensityMap?: string;
151+
specularColorMap?: string;
152+
153+
envMap?: string;
154+
155+
combine?: number;
156+
157+
envMapRotation?: number[];
158+
envMapIntensity?: number;
159+
160+
reflectivity?: number;
161+
refractionRatio?: number;
162+
163+
gradientMap?: string;
164+
165+
transmission?: number;
166+
transmissionMap?: string;
167+
168+
thickness?: number;
169+
thicknessMap?: string;
170+
171+
attenuationDistance?: number;
172+
attenuationColor?: number;
173+
174+
size?: number;
175+
shadowSide?: number;
176+
sizeAttenuation?: boolean;
177+
178+
blending?: number;
179+
side?: number;
180+
vertexColors?: boolean;
181+
opacity?: number;
182+
transparent?: boolean;
183+
184+
blendSrc?: BlendingSrcFactor;
185+
blendDst?: number;
186+
blendEquation?: number;
187+
blendSrcAlpha?: number | null;
188+
blendDstAlpha?: number | null;
189+
blendEquationAlpha?: number | null;
190+
blendColor?: number;
191+
blendAlpha?: number;
192+
193+
depthFunc?: number;
194+
depthTest?: boolean;
195+
depthWrite?: boolean;
196+
colorWrite?: boolean;
197+
198+
stencilWriteMask?: number;
199+
stencilFunc?: StencilFunc;
200+
stencilRef?: number;
201+
stencilFuncMask?: number;
202+
stencilFail?: number;
203+
stencilZFail?: number;
204+
stencilZPass?: number;
205+
stencilWrite?: boolean;
206+
207+
rotation?: number;
208+
209+
polygonOffset?: boolean;
210+
polygonOffsetFactor?: number;
211+
polygonOffsetUnits?: number;
212+
213+
linewidth?: number;
214+
dashSize?: number;
215+
gapSize?: number;
216+
scale?: number;
217+
dithering?: boolean;
218+
219+
alphaTest?: number;
220+
alphaHash?: boolean;
221+
alphaToCoverage?: boolean;
222+
premultipliedAlpha?: boolean;
223+
224+
forceSinglePass?: boolean;
225+
226+
wireframe?: boolean;
227+
wireframeLinewidth?: number;
228+
wireframeLinecap?: string;
229+
wireframeLinejoin?: string;
230+
231+
flatShading?: boolean;
232+
visible?: boolean;
233+
toneMapped?: boolean;
234+
fog?: boolean;
235+
236+
userData: Record<string, unknown>;
237+
}
238+
239+
export interface MaterialJSONRoot extends MaterialJSON {
240+
textures?: Omit<TextureJSON, "metadata">[];
241+
242+
images?: SourceJSON[];
243+
}
70244

71245
/**
72246
* 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.
@@ -418,7 +592,8 @@ export class Material extends EventDispatcher<{ dispose: {} }> {
418592
* Convert the material to three.js JSON format.
419593
* @param meta Object containing metadata such as textures or images for the material.
420594
*/
421-
toJSON(meta?: any): any;
595+
toJSON(): MaterialJSON;
596+
toJSON(meta: JSONMeta): MaterialJSONRoot;
422597

423598
/**
424599
* Return a new material with the same parameters as this material.

0 commit comments

Comments
 (0)