简体   繁体   English

在three.js中动态添加和旋转几何

[英]Dynamically add and rotate a geometry in three.js

Refer https://jsfiddle.net/pmankar/svt0nhuv/ 请参阅https://jsfiddle.net/pmankar/svt0nhuv/

Main large red icosahedron geometry keeps rotating about the y axis. 主要的大红色二十面体主要几何形状始终围绕y轴旋转。 I added a small red sphere geometry and merged it to the main large red icosahedron geometry. 我添加了一个小的红色球形几何体,并将其合并到主要的大红色二十面体几何体中。 Until here it works fine. 到这里为止一切正常。 For this I used, THREE.GeometryUtils.merge(point_sphere_iso_geom, sphere); 为此,我使用了THREE.GeometryUtils.merge(point_sphere_iso_geom, sphere);

However, when I try to add spheres dynamically with a mouse click, they are added (yellow spheres), but they do not rotate with the main large red icosahedron geometry. 但是,当我尝试通过单击鼠标动态添加球体时,会添加它们(黄色球体),但它们不会随主要的大红色二十面体几何体一起旋转。

Can anyone explain why does it works in the initial case, but not when added dynamically and how to achieve it dynamically as well. 任何人都可以解释为什么它在最初的情况下有效,但在动态添加时却无效,以及如何动态实现它。

I hope I understood you correctly. 我希望我能正确理解你。 Every mouse click you have to create a new geometry based on the previous one (mesh geometry and mesh matrix), merging it with the geometry of a new sphere, and apply it to a new mesh, then remove the old mesh and add the new one. 每次单击鼠标都必须在前一个(网格几何和网格矩阵)的基础上创建一个新几何,将其与新球体的几何合并,并将其应用于新网格,然后删除旧网格并添加新网格一。

some changes in vars vars的一些变化

var geometry, material, point_sphere_iso_geom, mesh;

in creation of the start merged mesh 在创建起始合并网格时

point_sphere_iso_geom = new THREE.IcosahedronGeometry(100, 4);
cygeo = new THREE.SphereGeometry(5, 10, 10);
cygeo.translate(0,0,120);
point_sphere_iso_geom.merge( cygeo );
mesh = new THREE.Mesh(point_sphere_iso_geom, material);

and in the function of addYellowPoint 并在addYellowPoint函数中

function addYellowPoint(valX, valY) {
    var sgeometry = new THREE.SphereGeometry(2.5, 10, 10);

    var range = 150;
    var x = Math.random() * range - range / 2;
    var y = Math.random() * range - range / 2;
    var z = Math.random() * range - range / 2;
    sgeometry.translate(x,y,z);
    point_sphere_iso_geom = mesh.geometry.clone();
    point_sphere_iso_geom.applyMatrix(mesh.matrix);
    point_sphere_iso_geom.merge(sgeometry);

    scene.remove(mesh);
    mesh.geometry.dispose();
    mesh.material.dispose();
    mesh = new THREE.Mesh(point_sphere_iso_geom, material);
    scene.add(mesh);
}

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

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