Three.js png texture - alpha renders as white instead as transparent

前端 未结 1 1847
臣服心动
臣服心动 2020-12-09 15:11

I\'m creating a cube and I apply 6 different textures to each of it\'s faces. Each texture is a .png file and contains transparent parts. I\'m also applying a color to the c

相关标签:
1条回答
  • 2020-12-09 15:52

    the opacity attribute of material does the trick for you. Follows, example code snippet:

    var materialArray = [];
    materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/xpos.png' ), transparent: true, opacity: 0.5, color: 0xFF0000 }));
    materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/xneg.png' ), transparent: true, opacity: 0.5, color: 0xFF0000 }));
    materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/ypos.png' ), transparent: true, opacity: 0.5, color: 0xFF0000 }));
    materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/yneg.png' ), transparent: true, opacity: 0.5, color: 0xFF0000 }));
    materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/zpos.png' ), transparent: true, opacity: 0.5, color: 0xFF0000 }));
    materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/zneg.png' ), transparent: true, opacity: 0.5, color: 0xFF0000 }));
    var MovingCubeMat = new THREE.MeshFaceMaterial(materialArray);
    var MovingCubeGeom = new THREE.CubeGeometry( 50, 50, 50, 1, 1, 1, materialArray );
    MovingCube = new THREE.Mesh( MovingCubeGeom, MovingCubeMat );
    MovingCube.position.set(0, 25.1, 0);
    scene.add( MovingCube );    
    

    http://threejs.org/docs/#Reference/Materials/Material The key is to set transparent attribute true and set opacity to 0.5(for example). Add the second the cube which fits inside exactly with no transparency, idea from @WestLangley ( Three.js canvas render and transparency )

    backCube = new THREE.Mesh( MovingCubeGeom, new THREE.MeshBasicMaterial( { color: 0xFF0000 }) );
    backCube.position.set(0, 25.1, 0);
    backCube.scale.set( 0.99, 0.99, 0.99 );
    scene.add( backCube );
    
    0 讨论(0)
提交回复
热议问题