javascriptthree.jscsgthreecsg

How add texture to a three.js after it has gone throught a CSG.js process?


This my code:

var materialNormal = new THREE.MeshNormalMaterial();

var cubeGeometry = new THREE.CubeGeometry( 20, 500, 1000, 1, 1, 1 );
var cubeMesh = new THREE.Mesh( cubeGeometry );
cubeMesh.position.set(-50, 60, 0);
//scene.add(cubeMesh); //Meu
var cubeBSP = new ThreeBSP( cubeMesh );

var cubeGeometry = new THREE.CubeGeometry( 90, 90, 90, 1, 1, 1 );
var mat = new THREE.MeshBasicMaterial({color:0xffffff, shading: THREE.FlatShading, overdraw:0.5});
var cube3Mesh = new THREE.Mesh( cubeGeometry, mat );
cube3Mesh.position.set(-50, 60, 0);
//scene.add(cubeMesh); //Meu
var cubeOutroBSP = new ThreeBSP( cube3Mesh );

// Example #1 - Cube subtract
var newBSP = cubeBSP.subtract(cubeOutroBSP);
var newMesh = newBSP.toMesh(materialNormal);
newMesh.position.set(-180, 60, 0);
scene.add( newMesh );

How do I add texture to the object that was generated by subtraction?


Solution

  • The mesh returned by three.CSG is like a default three.js mesh where you can apply a material with a texture to.

    In your example this would be newMesh, so do:

    var texture = THREE.ImageUtils.loadTexture('yourtexture.jpg');
    var material = new THREE.MeshPhongMaterial( { map: texture } );
    //...
    
    var newMesh = newBSP.toMesh( material );
    newMesh.position.set(-180, 60, 0);
    scene.add( newMesh );