Three.js渲染平面几何的白色部分

时间:2016-02-28 10:56:45

标签: javascript three.js textures

我正在尝试将this plainGeometry转换为this(某种掩码)。

此代码有效:(总结一下,创建两个材质,将平面划分为多个段,并根据MeshFaceMaterial决定每个材质是哪个材料)

   Button.onClick(function () {

            var obj = editor.selected;
            var material = obj.material;
            var tex = material.map;
            var objHeight = obj.geometry.parameters.height;
            var objWidth = obj.geometry.parameters.width;
            var texHeight = tex.image.height;
            var texWidth = tex.image.width;

            var geometry = new THREE.PlaneGeometry(objWidth, objHeight, objWidth, objHeight);

            var facesNum = objHeight * objWidth * 2;
            var facesX = objWidth * 2;
            var facesInX = texWidth * 2;

            var materials = [];
            materials.push(material);
            materials.push(new THREE.MeshBasicMaterial({ }));

            for (var i = 0; i < facesNum; i++) {
                if ((i % facesX >= objWidth - texWidth) &&
                    (i % facesX <= (facesInX + objWidth - texWidth - 1)) &&
                    (i <= (texHeight * objWidth * 2) - 1)) {
                    geometry.faces[i].materialIndex = 0;
                }
                else {
                    geometry.faces[i].materialIndex = 1;
                }
            }

            obj.geometry = geometry;
            obj.material = new THREE.MeshFaceMaterial(materials);

            editor.signals.materialChanged.dispatch(obj);


        });

但我想知道是否有更简单的方法。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

另一种方法是在纹理上使用Alpha通道。您可以使用Gimp或Photoshop执行此操作。

然后,您复制网格,并在材质上使用polygonOffsetFactor将其推出轴。将背景材质应用于第一个网格,将前景材质应用于第二个网格的alpha。 看到这个小提琴alphaTest。 (你可能需要禁用跨域访问安全性,因此纹理可以在这个小提琴中加载,如果使用--disable-web-security标志运行它,chrome将允许这样做) 该方法的优点在于图像可以具有任何形状和位置,并且不需要适合几何面。

另一种方法是,如果您使用的几何体很复杂,则使用Three.DecalGeometry来剪切网格片并使用它在材质上使用polygonOffsetFactor稍微移位。请参阅three.js decals example

示例小提琴的来源如下:

var renderer;
var pointLight;
var scene;
var plane1;
var plane2;

function addPlane()
{
   var material1 = new THREE.MeshPhongMaterial({ color: 0xFFFFFF });
   var material2;
   var loader = new THREE.TextureLoader();
   loader.load('http://i.imgur.com/ETdl4De.png',
     function ( texture ) {     
          var material2 = new THREE.MeshPhongMaterial({
      color: 0xFFFFFF,
      map: texture,
      alphaTest: 0.7,
      polygonOffset: true,
            polygonOffsetFactor: - 4,
     });

   var geometry1 = new THREE.PlaneGeometry(12, 12, 12, 12);
   var geometry2 = geometry1.clone();   
   plane1 = new THREE.Mesh(geometry1,material1);      
   plane2 = new THREE.Mesh(geometry2,material2);      
   scene.add(plane2);
   scene.add(plane1);
    }
);

}
(function() {

    'use strict';

    scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 10000);

    renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);                 
    document.body.appendChild(renderer.domElement);

        addPlane();

    camera.position.z = 52;

    pointLight = new THREE.DirectionalLight(0xffffff,1);
    pointLight.position.x = 11;
    pointLight.position.y = 5;
    pointLight.position.z = 25;
    scene.add(pointLight);

    var reqAnimFrame = window.requestAnimationFrame ||          window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame;

    var render = function() {
        reqAnimFrame(render);        
        renderer.render(scene, camera);
    };
    render();
}());

答案 1 :(得分:0)

这样做了,最终:

ROWNUM