admin 管理员组文章数量: 1087134
I have been playing around with voxeljs, I'm new to 3D programming, and it says in the doc that this code generates the "sphere world":
generate: function(x,y,z) {
return x*x+y*y+z*z <= 20*20 ? 1 : 0 // sphere world
},
How is this actually generating a sphere? From my simple understanding, I think that it's basically "looping" through each "chunk" in the 3D world? Any further explanation or a point to a good tutorial on this would be a huge help!
I have been playing around with voxeljs, I'm new to 3D programming, and it says in the doc that this code generates the "sphere world":
generate: function(x,y,z) {
return x*x+y*y+z*z <= 20*20 ? 1 : 0 // sphere world
},
How is this actually generating a sphere? From my simple understanding, I think that it's basically "looping" through each "chunk" in the 3D world? Any further explanation or a point to a good tutorial on this would be a huge help!
Share Improve this question edited Jan 24, 2013 at 0:48 Charles 51.4k13 gold badges106 silver badges144 bronze badges asked Jan 23, 2013 at 23:58 Doug MolineuxDoug Molineux 12.4k26 gold badges96 silver badges144 bronze badges2 Answers
Reset to default 6Your function says:
If the voxel at
(x, y, z)
is part of the sphere, return1
, else0
.
The author applies the sphere equation. Your sphere is formed by the following set of voxels:
That basically means a voxel is part of the sphere, if the distance to the center (0, 0, 0)
is less than the radius. The distance is calculated using the Pythagorean Theorem. By squaring the radius (in your case 20
) you can pare it to the squared distance without calculating a square root.
This is based on the distance formula in three-dimensional space, since you can define a sphere as every point within a certain distance of the center point.
The distance between any two objects is equal to the square root of (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2.
The above function is flagging each voxel if they are within 20 units of the origin. Since the origin is (0, 0, 0), the distance function simplifies down to square root of x1^2 + y1^2 + z1^2. This also throws in another optimization by getting rid of the square root, and paring the result to 20^2.
本文标签: javascriptGenerating a Sphere with VoxelStack Overflow
版权声明:本文标题:javascript - Generating a Sphere with Voxel - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744051803a2525155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论