Skip to content

Commit

Permalink
🦄 refactor: Split parseToThreeMaterial
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Dec 9, 2024
1 parent a3d04f3 commit 3fbd1ac
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
68 changes: 44 additions & 24 deletions packages/chili-three/src/threeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { Material, MathUtils, Matrix4, PhongMaterial, PhysicalMaterial, Texture,
import {
Box3,
Camera,
Matrix4 as ThreeMatrix4,
DoubleSide,
OrthographicCamera,
PerspectiveCamera,
Color as ThreeColor,
Vector3,
DoubleSide,
TextureLoader,
RepeatWrapping,
TextureLoader,
Color as ThreeColor,
MeshLambertMaterial as ThreeLambertMaterial,
Matrix4 as ThreeMatrix4,
MeshPhongMaterial as ThreePhoneMaterial,
MeshPhysicalMaterial as ThreePhysicalMaterial,
MeshLambertMaterial as ThreeLambertMaterial,
Vector3,
} from "three";

export class ThreeHelper {
Expand Down Expand Up @@ -92,36 +92,56 @@ export class ThreeHelper {
return map;
}

static mapMaterial(material: Material) {
const params = {
static parsePhysicalMaterial(material: PhysicalMaterial) {
return new ThreePhysicalMaterial({
color: material.color,
side: DoubleSide,
transparent: true,
name: material.name,
opacity: material.opacity,
map: this.loadTexture(material.map),
};
roughness: material.roughness,
metalness: material.metalness,
roughnessMap: this.loadTexture(material.roughnessMap),
metalnessMap: this.loadTexture(material.metalnessMap),
});
}

static parsePhongMaterial(material: PhongMaterial) {
return new ThreePhoneMaterial({
color: material.color,
side: DoubleSide,
transparent: true,
name: material.name,
opacity: material.opacity,
map: this.loadTexture(material.map),
specularMap: this.loadTexture(material.specularMap),
shininess: material.shininess,
emissive: ThreeHelper.fromColor(material.emissive),
emissiveMap: this.loadTexture(material.emissiveMap),
});
}

static parseBasicMaterial(material: Material) {
return new ThreeLambertMaterial({
color: material.color,
side: DoubleSide,
transparent: true,
name: material.name,
opacity: material.opacity,
map: this.loadTexture(material.map),
});
}

static parseToThreeMaterial(material: Material) {
if (material instanceof PhysicalMaterial) {
return new ThreePhysicalMaterial({
...params,
roughness: material.roughness,
metalness: material.metalness,
roughnessMap: this.loadTexture(material.roughnessMap),
metalnessMap: this.loadTexture(material.metalnessMap),
});
return this.parsePhysicalMaterial(material);
}

if (material instanceof PhongMaterial) {
return new ThreePhoneMaterial({
...params,
specularMap: this.loadTexture(material.specularMap),
shininess: material.shininess,
emissive: ThreeHelper.fromColor(material.emissive),
emissiveMap: this.loadTexture(material.emissiveMap),
});
return this.parsePhongMaterial(material);
}

return new ThreeLambertMaterial(params);
return this.parseBasicMaterial(material);
}
}
2 changes: 1 addition & 1 deletion packages/chili-three/src/threeVisualContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class ThreeVisualContext implements IVisualContext {
};

private createThreeMaterial(material: Material) {
let result = ThreeHelper.mapMaterial(material);
let result = ThreeHelper.parseToThreeMaterial(material);
DeepObserver.addDeepPropertyChangedHandler(material, this.onMaterialPropertyChanged);
this.materialMap.set(material.id, result);
}
Expand Down

0 comments on commit 3fbd1ac

Please sign in to comment.