简体   繁体   English

如何在不改变位置的情况下缩放网格?

[英]How to scale a mesh without changing the position?

I have in this scene 27 meshes, each of which has an associated number. 我在这个场景中有27个网格,每个网格都有一个相关的数字。 The one with the highest number will have the scale 1. The rest will have a value less than 1 successively. 数字最大的那个将具有标度1.其余的将具有连续小于1的值。

Next I want it to climb each of the loops accordingly. 接下来我希望它相应地攀爬每个循环。 My problem occurs when the position of each mesh is changed. 当每个网格的位置发生变化时,会出现问题。

Indeed it is scaled to the size that is due, but it changes position. 实际上它按比例缩放到了应有的大小,但它改变了位置。 I'm struggling as several screens are stacking. 几个屏幕正在堆叠,我正在努力。

I just need you to climb and stay in the same position as in this example: 我只需要你爬上并保持在这个例子中的相同位置:

US states are scaled, but still keeping their position. 美国各州都在扩大规模,但仍保持其地位。 I think that they scale to the center of the figure. 我认为它们可以扩展到图形的中心。

I created the meshes (each called "municipios") 我创建了网格(每个网格称为“municipios”)

for(var s=0; s< features.features[x].geometry.coordinates[0].length; s=s+1){                       
   geometria[x].vertices.push(new THREE.Vector3((((features.features[x].geometry.coordinates[0][s][0])+75.5)*10),(((features.features[x].geometry.coordinates[0][s][1])-5.5)*10),0));                                   
}
forma_figura[x]=new THREE.Shape(geometria[x].vertices);
extrude_geometria[x]=new THREE.ExtrudeGeometry(forma_figura[x],datos_extrusion);
municipios[x] = new THREE.Mesh( extrude_geometria[x], materialExtrude[x] );
scene.add(municipios[x]);

// so I change the scale:
//formula is the value of scale. The biggest number has escale 1.
new TWEEN.Tween(municipios[x].scale).to({ x: formula, y: formula  }, 1000).start();
//this ultimate line is the same:    municipios[x].scale.set(formula,formula,formula); 

Because the mesh position is not at its center, or whatever reference you want it to be : if you create a geometry with average vertices x component over 100, its visible position will be its real position plus (100,0,0). 因为网格位置不在其中心,或者您想要的任何参考:如果创建平均顶点x分量超过100的几何体,其可见位置将是其实际位置加上(100,0,0)。 If you did not specify a position for the mesh ((0,0,0) by default), it will look being at (100,0,0) precisely. 如果没有为网格指定位置(默认情况下为(0,0,0)),则它将精确地位于(100,0,0)处。 Scaling it by .5 for instance will result in the average position being at (100,0,0)*.5=(50,0,0), making the mesh apparently change its position. 例如,按.5缩放将导致平均位置为(100,0,0)*。5 =(50,0,0),使得网格明显改变其位置。

What needs to be done is : 需要做的是:

  • to substract the coordinates of the center from all the vertices. 从所有顶点减去中心的坐标。 Vertices are inside the geometry , so once it is done, geometry.verticesNeedUpdate = true needs to be set. 顶点位于几何体内部,因此一旦完成,就需要设置geometry.verticesNeedUpdate = true

  • then to add the coordinates of the center to the mesh , to compensate. 然后将中心的坐标添加到网格中 ,以进行补偿。

So the resulting mesh will be positionned at its center without having moved. 因此,生成的网格将定位在其中心而不会移动。

There are three different possibilities to define the center : 定义中心有三种不同的可能性:

  • Three.js can center your mesh automatically based on the center of the bounding box with geometry.center() and THREE.GeometryUtils.center(geometry) . Three.js可以根据包含geometry.center()THREE.GeometryUtils.center(geometry) 的边界框中心自动居中网格。
  • You can compute the centroid by looping through the vertices a first time to get their average coordinates. 您可以通过首次循环顶点来计算质心 ,以获得它们的平均坐标。
  • You may neither want the center of the bouding box, nor the centroid, but a custom center . 你可能既不想要镂空盒的中心,也不想要质心,而是一个定制的中心 For example if you are scaling 'people' meshes and want their feet touch the ground at any scale : on the horizontal plane the center you want can be one of the two first, but its vertical coordinate must be the lowest vertical coordinate found (the very bottom of the feet). 例如,如果要缩放“人物”网格并希望他们的脚以任何比例触地:在水平面上,您想要的中心可以是前两个中心之一,但其垂直坐标必须是找到的最低垂直坐标(非常的脚底)。

Of course if your meshes are imported from a 3D editor software you can also do that inside it. 当然,如果您的网格是从3D编辑器软件导入的,您也可以在其中进行。

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

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