javascript - 更新 DecalGeometry 顶点、UV、

标签 javascript three.js geometry decal

我目前正在玩ThreeJS decals 。我已经能够在我的球体上留下美丽的污点。

这是我用来在球体上“应用”贴花的代码片段。 (我有一些自定义类,但不用担心这个。

// Create sphere
var mainMesh = new THREE.Mesh(
    new THREE.SphereGeometry(7, 16, 16),
    new THREE.MeshBasicMaterial({ color: 0x00a1fd })
);

// Declare decal material
var decalMaterial = new THREE.MeshPhongMaterial({
    color               : 0xff0000,    
    specular            : 0x444444,
    map                 : TextureLoader.instance.getTexture('http://threejs.org/examples/textures/decal/decal-diffuse.png'),
    normalMap           : TextureLoader.instance.getTexture('http://threejs.org/examples/textures/decal/decal-normal.jpg'),
    normalScale         : new THREE.Vector2( 1, 1 ),
    shininess           : 30,
    transparent         : true,
    depthTest           : true,
    depthWrite          : false,
    polygonOffset       : true,
    polygonOffsetFactor : -4,
    wireframe           : false
});

// Create decal itself
var decal = new THREE.Mesh(
    new THREE.DecalGeometry(
        mainMesh,
        new THREE.Vector3(0, 2.5, 3),
        new THREE.Vector3(0, 0, 0),
        new THREE.Vector3(8, 8, 8),
        new THREE.Vector3(1, 1, 1)
    ),
    decalMaterial.clone()
);

// Add mesh + decal + helpers
scene.add(
    mainMesh,
    new THREE.HemisphereLight(0xffffff, 0, 1),
    decal,
    new THREE.WireframeHelper(decal, 0xffff00)
);

decal.add(new THREE.BoxHelper(decal, 0xffff00));

现在,我想移动球体上的污点,从而更新贴花的几何形状。

不幸的是,当我调用decal.geometry.computeDecal()时,贴花的网格不会更新。我找不到任何解决方案。

    function moveDecal()
    {
        decal.translateX(1);
        decal.geometry.computeDecal();
    };

根据 DecalGeometry 类,函数 computeDecal 已设置为 true 更新顶点、颜色、UV 等所需的各种成员。

    this.computeDecal = function() {
        // [...]
        this.verticesNeedUpdate     = true;
        this.elementsNeedUpdate     = true;
        this.morphTargetsNeedUpdate = true;
        this.uvsNeedUpdate          = true;
        this.normalsNeedUpdate      = true;
        this.colorsNeedUpdate       = true;
     };

感谢您的帮助! :D

PS:ThreeJS r80

最佳答案

您正在尝试更新几何体的顶点。

您可以更改顶点组件的值,

geometry.vertices[ 0 ].x += 1;

但是你不能添加新的Verites

geometry.vertices.push( new THREE.Vector3( x, y, z ) ); // not allowed

或者分配一个新的顶点数组

geometry.vertices = new_array; // not allowed

在几何图形至少渲染一次之后。

对于其他属性(例如 UV)也是如此。

有关更多信息,请参阅此答案:verticesNeedUpdate in Three.js .

三.js r.80

关于javascript - 更新 DecalGeometry 顶点、UV、,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39619870/

相关文章:

javascript - 用户名和密码的Javascript登录页面数组不起作用

javascript - Three.js 敌人向玩家移动

python - 计算n维圆弧路径

java - 如何从一个地方的纬度和经度得到一个地方到另一个地方的方向?

javascript - fancybox div 中的本地链接不起作用

javascript - 在不知道其键的情况下操纵对象值

javascript - img 目录结构 react.js

javascript - 如何使用 Three.js 检测移动设备上缓慢的 GPU?

matrix - 将对象的旋转定向到THREE.JS中的样条点切线

javascript - 如何计算两点之间的 3D Angular ?