Hi I am trying to change the material of a mesh using mouse onClick event. When clicked on the mesh, it only changes its color to white but no material gets assigned. Can you guys please point me in the right direction? I am using the desktop GUI and referring to materials already created using Material Viewer. The code I have used is below, Thank you.
import { Scene, Color4, Mesh } from 'babylonjs';
export default class MarbleDesk implements IScript {
// Public members
public blackColor = new Color4(0, 0, 0, 1);
public BWTexture
/**
* Constructor
*/
constructor (public mesh: Mesh, public scene: Scene) {
}
/**
* Called once starting the script
*/
public start (): void {
// You can access the scene everywhere
this.scene.clearColor = this.blackColor;
// You can access the attached object everywhere
console.log(this.mesh);
this.BWTexture = new BABYLON.StandardMaterial("floorfloor", scene);
this.BWTexture = this.scene.getMaterialByName("wood");
// this.BWTexture.diffuseTexture = new BABYLON.Texture("white_marble.png", scene);
// this.BWTexture.diffuseTexture.hasAlpha = false;
// this.BWTexture.backFaceCulling = false;
this.BWTexture.diffuseTexture.uScale = 0.01;
this.BWTexture.diffuseTexture.vScale = 0.01;
}
/**
* Called on each frame
*/
public update (deltaTimeMs: number): void {
// Your code...
scene.onPointerPick = function(evt, pickInfo) {
if(pickInfo.hit) {
if (pickInfo.pickedMesh != null){
//alert(pickInfo.pickedMesh.name);
console.log(pickInfo.pickedMesh);
mesh.material = this.BWTexture
}
}
}
}
/**
* Called once the attached object has been disposed
*/
public dispose (): void {
// Called once the attached object has been disposed
}
}
// Export the script as an attached script
// (attached to a node or scene)
exportScript(MarbleDesk);