Three.js png 纹理 - alpha 呈现为白色而不是透明 [英] Three.js png texture - alpha renders as white instead as transparent

查看:115
本文介绍了Three.js png 纹理 - alpha 呈现为白色而不是透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个立方体,并为它的每个面应用了 6 种不同的纹理.每个纹理都是一个 .png 文件并包含透明部分.我还为立方体应用了一种颜色 - 我想通过 png 透明度看到该颜色.

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 cube - I want to see that color trough png transparency.

问题:透明度呈现为白色,所以我看不到立方体的基色(如果我移除 png 纹理,则呈现正常)

Problem: Transparency renders as white color so I cannot see the base color of the cube (which renders ok if I remove the png texture)

如何使 png 透明度起作用?我尝试玩一些材质设置,但没有使它透明.

How can I make the png transparency work? I tried playing with some material settings but none make it transparent.

用于创建立方体和材料的代码:

Code for creating the cube and materials:

var geometry = new THREE.CubeGeometry(150, 200, 150, 2, 2, 2);
var materials = [];

// create textures array for all cube sides
for (var i = 1; i < 7; i++) {
   var img = new Image();
   img.src = 'img/s' + i + '.png';
   var tex = new THREE.Texture(img);
   img.tex = tex;

   img.onload = function () {
      this.tex.needsUpdate = true;
   };

   var mat = new THREE.MeshBasicMaterial({color: 0x00ff00, map: tex, transparent: true, overdraw: true });
   materials.push(mat);
}
cube = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial(materials));
cube.position.y = 150;
scene.add(cube);

<小时>

下图显示了问题-使用senthanal解决方案,左侧纹理现在呈现正常-它是没有透明度的png图像-我使用

Picture below shows the problem - with senthanal solution the left texture now renders ok - it is a png image without transparency - I set the transparency in code with

materialArray.push(new THREE.MeshBasicMaterial({ map: THREE.ImageUtils.loadTexture('img/s2.png'), transparent: true, opacity: 0.9, color: 0xFF0000 }));

正确的纹理也是一个 png 图像 - 只是它有一个透明区域(所有呈现白色的都应该是纯红色,因为它是透明的,并且应该从立方体中获取颜色?).我怎样才能让那个白色部分透明?

The right texture is also a png image - only that it has a transparent area (all that renders white should be pure red since it is transparent and should take the color from the cube?). How can I make that white part transparent?

推荐答案

材质的不透明度属性为您解决问题.如下,示例代码片段:

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 关键是将transparent 属性设置为true,将opacity 设置为0.5(例如).添加第二个立方体,它完全适合内部而没有透明度,来自@WestLangley 的想法(三个.js画布渲染和透明度)

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 );

这篇关于Three.js png 纹理 - alpha 呈现为白色而不是透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆