troisjs / trois

✨ ThreeJS + VueJS 3 + ViteJS ⚡
https://troisjs.github.io
MIT License
4.24k stars 304 forks source link

Allow setting arrays of materials in a mesh #189

Open feritarou opened 1 year ago

feritarou commented 1 year ago

Made in reaction to #89

klevron commented 1 year ago

Thanks, can you revert chore: build package so the PR is just about Mesh.ts

feritarou commented 1 year ago

@klevron Sure, just did so (wasn't sure how long it would take you to note/merge this PR but I needed that change urgently so I just built my fork without thinking - sorry for that)

klevron commented 1 year ago

Thanks

setMaterial won't be called with an array of materials : https://github.com/troisjs/trois/blob/master/src/materials/Material.ts#L50

I think it should be :

setMaterial(material: Material) {
  if (Array.isArray(this.material)) {
    this.material.push(material)
  } else {
    if (this.material) {
      this.material = [this.material, material]
    } else {
      this.material = material
    }
  }
  if (this.mesh) this.mesh.material = material
}

But there should be some HMR problem, I don't have time to test for the moment.