简体   繁体   English

WebGL更改形状动画

[英]WebGL Change Shape Animation

I'm creating a 3D globe with a map on it which is supposed to unravel and fill the screen after a few seconds. 我正在创建一个带有地图的3D地球仪,它应该在几秒钟后解开并填满屏幕。

I've managed to create the globe using three.js and webGL, but I'm having trouble finding any information on being able to animate a shape change. 我已经设法使用three.js和webGL创建地球仪,但是我无法找到有关能够为形状变化设置动画的任何信息。 Can anyone provide any help? 谁能提供任何帮助? Is it even possible? 它甚至可能吗?

(Abstract Algorithm's and Kevin Reid's answers are good, and only one thing is missing: some actual Three.js code.) (Abstract Algorithm和Kevin Reid的答案很好,只缺少一件事:一些实际的Three.js代码。)

You basically need to calculate where each point of the original sphere will be mapped to after it flattens out into a plane. 基本上,您需要计算原始球体的每个点在展平到一个平面后将被映射到的位置。 This data is an attribute of the shader: a piece of data attached to each vertex that differs from vertex to vertex of the geometry. 此数据是着色器的属性 :附加到每个顶点的一条数据,该数据在几何图形的各个顶点之间是不同的。 Then, to animate the transition from the original position to the end position, in your animation loop you will need to update the amount of time that has passed. 然后,为动画从原始位置到结束位置的过渡,在动画循环中,您将需要更新已过去的时间。 This data is a uniform of the shader: a piece of data that remains constant for all vertices during each frame of the animation, but may change from one frame to the next. 此数据是着色器的统一形式:在动画的每一帧期间,所有顶点的所有数据均保持不变,但可能会从一帧变化到下一帧。 Finally, there exists a convenient function called "mix" that will linearly interpolate between the original position and the end/goal position of each vertex. 最后,存在一个方便的函数,称为“ mix”,它将在每个顶点的原始位置和终点/目标位置之间线性插值。

I've written two examples for you: the first just "flattens" a sphere, sending the point (x,y,z) to the point (x,0,z). 我为您编写了两个示例:第一个示例只是“展平”一个球体,将点(x,y,z)发送到点(x,0,z)。

http://stemkoski.github.io/Three.js/Shader-Attributes.html http://stemkoski.github.io/Three.js/Shader-Attributes.html

The second example follows Abstract Algorithm's suggestion in the comments: "unwrapping the sphere's vertices back on plane surface, like inverse sphere UV mapping." 第二个示例遵循Abstract Algorithm在注释中的建议:“将球体的顶点重新展开到平面上,就像逆球体UV映射一样。” In this example, we can easily calculate the ending position from the UV coordinates, and so we actually don't need attributes in this case. 在此示例中,我们可以轻松地从UV坐标计算结束位置,因此在这种情况下,我们实际上不需要属性。

http://stemkoski.github.io/Three.js/Sphere-Unwrapping.html http://stemkoski.github.io/Three.js/Sphere-Unwrapping.html

Hope this helps! 希望这可以帮助!

In 3D, anything and everything is possible. 在3D模式下,一切皆有可能。 ;) ;)

Your sphere geometry has it's own vertices, and basically you just need to animate their position, so after animation they are all sitting on one planar surface. 你的球体几何体有它自己的顶点,基本上你只需要为它们的位置设置动画,所以在动画之后它们都位于一个平面上。

Try creating sphere and plane geometry, with same number of vertices, and animating sphere's vertices with interpolated values of sphere's and plane's original values. 尝试创建具有相同数量顶点的球体和平面几何体,并使用球体和平面原始值的内插值对球体的顶点进行动画处理。 That way, on the start you would have sphere shape and in the end, plane shape. 这样,在开始时你会有球形,最后是平面形状。

Hope this helps, tell me if you need more directives how to do it. 希望这有帮助,告诉我你是否需要更多的指令如何做到这一点。

myGlobe.geometry.vertices[index].position = something_calculated;
// myGlobe is instance of THREE.Mesh and something_calculated would be THREE.Vector3 instance that you can calculate in some manner (sphere-plane interpolation over time)

(Abstract Algorithm's answer is good, but I think one thing needs improvement: namely using vertex shaders.) (抽象算法的答案很好,但我认为有一点需要改进:即使用顶点着色器。)

You make a set of vertices textured with the map image. 您制作了一组带有地图图像纹理的顶点。 Then, design a calculation for interpolating between the sphere shape and the flat shape. 然后,设计用于在球形和平面形状之间进行插值的计算。 It doesn't have to be linear interpolation — for example, one way that might be good is to put the map on a small portion of an sphere of increasing radius until it looks flat (getting it all the way will be tricky). 它不必是线性插值的-例如,一种可能很好的方法是将地图放在半径不断增大的球体的一小部分上,直到看起来看起来平坦为止(将其完全获取会很棘手)。

Then, write that calculation in your vertex shader . 然后, 在顶点着色器中写入该计算。 The position of each vertex can be computed entirely from the texture coordinates (since that determines where-on-the-map the vertex goes and implies its position) and a uniform variable containing a time value. 每个顶点的位置可以完全从纹理坐标(因为确定顶点在地图上的位置并隐含其位置)和包含时间值的统一变量中计算出来。

Using the vertex shader will be much more efficient than recomputing and re-uploading the coordinates using JavaScript, allowing perfectly smooth animation with plenty of spare resources to do other things as well. 使用顶点着色器将比使用JavaScript重新计算和重新上传坐标更有效,允许使用大量备用资源完美平滑的动画来执行其他操作。

Unfortunately, I'm not familiar enough with Three.js to describe how to do this in detail, but all of the above is straightforward in basic WebGL and should be possible in any decent framework. 不幸的是,我对Three.js不太熟悉,不能详细描述如何做到这一点,但上述所有内容在基本的WebGL中都是直截了当的,应该可以在任何体面的框架中实现。

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

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