简体   繁体   English

在three.js中合并几何

[英]Merging geometries in three.js

I have a scene which contains multiple meshes, each of varying shapes and sizes.我有一个场景,其中包含多个网格,每个网格的形状和大小各不相同。

I have looped through each Mesh and using geometry.merge() I have been able to create a new mesh from the geometries in the scene.我已经遍历了每个网格并使用geometry.merge()我已经能够从场景中的几何图形创建一个新网格。

I want to mask the entire mesh with an alphaMask, however, each geometry has the material applied to it separately.我想用 alphaMask 遮罩整个网格,但是,每个几何体都分别应用了材质。

An example of this can be seen here - https://codepen.io/danlong/pen/KXOObr一个例子可以在这里看到 - https://codepen.io/danlong/pen/KXOObr

    function addObjects(scene) {

    // merged geomoetry & material
    var mergedGeometry = new THREE.Geometry();
    var mergedMaterial = new THREE.MeshStandardMaterial({ color: "#444", transparent: true, side: THREE.DoubleSide, alphaTest: 0.5, opacity: 1, roughness: 1 });


    // multiple meshes
    var geometry = new THREE.IcosahedronGeometry(30, 5);
    var material = new THREE.MeshStandardMaterial({ color: "#444" });

    var geo1 = new THREE.IcosahedronGeometry(30, 5);
    var mesh1 = new THREE.Mesh( geo1, material );
    mesh1.position.x = 10;
    mesh1.position.y = 10;
    mesh1.position.z = 0;

    var geo2 = new THREE.IcosahedronGeometry(30, 5);
    var mesh2 = new THREE.Mesh( geo2, material );
    mesh2.position.x = 20;
    mesh2.position.y = 20;
    mesh2.position.z = 0;

    var geo3 = new THREE.IcosahedronGeometry(30, 5);
    var mesh3 = new THREE.Mesh( geo3, material );
    mesh3.position.x = 30;
    mesh3.position.y = 30;
    mesh3.position.z = 0;

    // scene.add(mesh1, mesh2, mesh3);
    mesh1.updateMatrix();
    mergedGeometry.merge(mesh1.geometry, mesh1.matrix);

    mesh2.updateMatrix();
    mergedGeometry.merge(mesh2.geometry, mesh2.matrix);

    mesh3.updateMatrix();
    mergedGeometry.merge(mesh3.geometry, mesh3.matrix);

    // alpha texture 
    var image = document.createElement('img');
    var alphaMap = new THREE.Texture(image);
    image.onload = function()  {
        alphaMap.needsUpdate = true;
    };
    image.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAGUlEQVQoU2NkYGD4z4AHMP7//x+/gmFhAgCXphP14bko/wAAAABJRU5ErkJggg==';
    mergedMaterial.alphaMap = alphaMap;
    mergedMaterial.alphaMap.magFilter = THREE.NearestFilter;
    mergedMaterial.alphaMap.wrapT = THREE.RepeatWrapping;
    mergedMaterial.alphaMap.repeat.y = 1;

    // merged geometry with alpha mask
    merge1 = new THREE.Mesh(mergedGeometry, mergedMaterial);
        merge1.rotation.z = -Math.PI/4;

    // merge geometry without alpha mask
    var merge2 = new THREE.Mesh(mergedGeometry, material);
    merge2.position.x = -100;
        merge2.rotation.z = -Math.PI/4;

    scene.add(merge1, merge2);
    return mesh;
}

The mesh on the left is the merged geometries which I want to apply the alphaMask to.左侧的网格是我想要应用 alphaMask 的合并几何图形。 The mesh on the right is the outcome of this and instead of the map being applied to the mesh as a whole, each of the geometries has the map applied.右边的网格是这个结果,而不是将贴图作为一个整体应用于网格,每个几何体都应用了贴图。

Is there a way to mask the entire mesh and not each geometry?有没有办法屏蔽整个网格而不是每个几何体?

-- three.js r86 -- 三.js r86

EDIT: I've tried to apply a clipping plane to my mesh but it's not the effect I'm looking for.编辑:我尝试将剪切平面应用于我的网格,但这不是我想要的效果。 I want to be able to apply an alphaMask across the whole mesh and reveal it however I make my mask image.我希望能够在整个网格上应用 alphaMask 并显示它,但是我制作了我的蒙版图像。 Something like this effect - https://codepen.io/supah/pen/zwJxdb像这样的效果 - https://codepen.io/supah/pen/zwJxdb

Is it something to do with the UV's being preserved from the original geometries?这与从原始几何形状中保留 UV 的内容有关吗? Do I need to change these in some way?我需要以某种方式改变这些吗?

I think what you really want is an overlaid mask.我想你真正想要的是一个覆盖的面具。 This can be accomplished by rendering a single plane that has the alpha map applied, on top of the scene rendering.这可以通过在场景渲染之上渲染应用了 alpha 贴图的单个平面来实现。 Using an orthographic camera, and controlling certain renderer settings, such as disabling automatic clearing of color.使用正交相机,并控制某些渲染器设置,例如禁用自动清除颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM