Skip to content

Commit

Permalink
✨ feat: the material can be an array.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Dec 14, 2024
1 parent 075de46 commit 113f900
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 3 additions & 3 deletions packages/chili-core/src/model/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ export class MeshNode extends VisualNode {

@Serializer.serialze()
@Property.define("common.material", { type: "materialId" })
get materialId(): string {
get materialId(): string | string[] {
return this.getPrivateValue("materialId");
}
set materialId(value: string) {
set materialId(value: string | string[]) {
this.setProperty("materialId", value);
}

Expand All @@ -281,7 +281,7 @@ export class MeshNode extends VisualNode {
document: IDocument,
mesh: Mesh,
name: string,
materialId?: string,
materialId?: string | string[],
id: string = Id.generate(),
) {
super(document, name, id);
Expand Down
3 changes: 3 additions & 0 deletions packages/chili-three/src/threeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ export class ThreeHelper {
map: this.loadTexture(material.map),
roughness: material.roughness,
metalness: material.metalness,
bumpMap: this.loadTexture(material.bumpMap),
normalMap: this.loadTexture(material.normalMap),
emissiveMap: this.loadTexture(material.emissiveMap),
roughnessMap: this.loadTexture(material.roughnessMap),
metalnessMap: this.loadTexture(material.metalnessMap),
});
Expand Down
24 changes: 17 additions & 7 deletions packages/chili-three/src/threeVisualObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BufferGeometry,
DoubleSide,
Float32BufferAttribute,
Material,
Mesh,
MeshLambertMaterial,
Object3D
Expand Down Expand Up @@ -63,15 +64,15 @@ export class ThreeMeshObject extends ThreeVisualObject {
get mesh() {
return this._mesh;
}
private material: LineMaterial | MeshLambertMaterial;
private material: Material | Material[];

constructor(
readonly context: ThreeVisualContext,
readonly meshNode: MeshNode,
) {
super(meshNode);
this._mesh = this.createMesh();
this.material = this._mesh.material as MeshLambertMaterial;
this.material = this._mesh.material;
this.add(this._mesh);
meshNode.onPropertyChanged(this.handleGeometryPropertyChanged);
}
Expand Down Expand Up @@ -113,7 +114,7 @@ export class ThreeMeshObject extends ThreeVisualObject {
this.add(this._mesh);
} else if (property === "materialId") {
if (this._mesh instanceof Mesh) {
this.material = this.context.getMaterial(this.meshNode.materialId) as MeshLambertMaterial;
this.material = this.getMaterial();
this._mesh.material = this.material;
}
}
Expand All @@ -132,10 +133,19 @@ export class ThreeMeshObject extends ThreeVisualObject {
buff.setIndex(this.meshNode.mesh.index);
}
buff.computeBoundingBox();
let material =this.meshNode.materialId
? this.context.getMaterial(this.meshNode.materialId)
: this.context.materialMap.values().next().value;
return new Mesh(buff, material);
return new Mesh(buff, this.getMaterial());
}

private getMaterial() {
let material: Material | Material[];
if (Array.isArray(this.meshNode.materialId)) {
material = this.meshNode.materialId.map(id => this.context.getMaterial(id));
} else if (typeof this.meshNode.materialId === "string") {
material = this.context.getMaterial(this.meshNode.materialId);
} else {
material = this.context.materialMap.values().next().value!;
}
return material;
}

private newLineSegments() {
Expand Down

0 comments on commit 113f900

Please sign in to comment.