有谁有一个在javascript中绘制细分二十面体的示例,他们可以分享吗?

发布于 2024-10-04 06:42:25 字数 44 浏览 5 评论 0原文

有谁有一个在javascript中绘制细分二十面体的示例,他们可以分享吗?

Does anyone have a sample for drawing a subdivided icosahedron in javascript, that they would be able to share?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个ˇ局外人 2024-10-11 06:42:25

我有一个使用二十面体的球体镶嵌算法。网上有很多地方可以找到它们,但请随意使用和修改我的实现。尽管我必须说,就内存而言,我的实现并不是最好的,有些顶点是重复的。

    function initSphere(subs){
    if(typeof(subs) == 'undefined'){
        subs = 1;
    }
    var t = (1+Math.sqrt(5))/2;
    var tau = t/Math.sqrt(1+t*t);
    var one = 1/Math.sqrt(1+t*t);
    var pos = [tau, one, 0.0,
              -tau, one, 0.0,
              -tau, -one, 0.0,
              tau, -one, 0.0,
              one, 0.0 ,  tau,
              one, 0.0 , -tau,
              -one, 0.0 , -tau,
              -one, 0.0 , tau,
              0.0 , tau, one,
              0.0 , -tau, one,
              0.0 , -tau, -one,
              0.0 , tau, -one];
    var _indices =  [4, 8, 7,
                    4, 7, 9,
                    5, 6, 11,
                    5, 10, 6,
                    0, 4, 3,
                    0, 3, 5,
                    2, 7, 1,
                    2, 1, 6,
                    8, 0, 11,
                    8, 11, 1,
                    9, 10, 3,
                    9, 2, 10,
                    8, 4, 0,
                    11, 0, 5,
                    4, 9, 3,
                    5, 3, 10,
                    7, 8, 1,
                    6, 1, 11,
                    7, 2, 9,
                    6, 10, 2];

    for(var i = 0;i<subs;i++){
        var newIndices = new Array();
        for(var j = 0;j<_indices.length;j+=3){
            var p1 = [ 
                       (pos[_indices[j+0]*3+0] + pos[_indices[j+1]*3+0])*0.5 ,
                       (pos[_indices[j+0]*3+1] + pos[_indices[j+1]*3+1])*0.5 ,
                       (pos[_indices[j+0]*3+2] + pos[_indices[j+1]*3+2])*0.5
                   ];
           var p2 = [ 
                       (pos[_indices[j+1]*3+0] + pos[_indices[j+2]*3+0])*0.5 ,
                       (pos[_indices[j+1]*3+1] + pos[_indices[j+2]*3+1])*0.5 ,
                       (pos[_indices[j+1]*3+2] + pos[_indices[j+2]*3+2])*0.5
                   ];
           var p3 = [ 
                       (pos[_indices[j+2]*3+0] + pos[_indices[j+0]*3+0])*0.5 ,
                       (pos[_indices[j+2]*3+1] + pos[_indices[j+0]*3+1])*0.5 ,
                       (pos[_indices[j+2]*3+2] + pos[_indices[j+0]*3+2])*0.5
                   ];
          p1 = normalize(p1);
          p2 = normalize(p2);
          p3 = normalize(p3);
          var i0,i1,i2,a,b,c;
          i0 = pos.length/3;
          i1 = pos.length/3+1;
          i2 = pos.length/3+2;
          a = _indices[j+0];
          b = _indices[j+1];
          c = _indices[j+2];
          newIndices.push(a);newIndices.push(i2);newIndices.push(i0);
          newIndices.push(b);newIndices.push(i0);newIndices.push(i1);
          newIndices.push(c);newIndices.push(i1);newIndices.push(i2);
          newIndices.push(i0);newIndices.push(i2);newIndices.push(i1);

          pos.push(p1[0]);pos.push(p1[1]);pos.push(p1[2]);
          pos.push(p2[0]);pos.push(p2[1]);pos.push(p2[2]);
          pos.push(p3[0]);pos.push(p3[1]);pos.push(p3[2]);

        }
        _indices = newIndices;

    }



    gl.bindBuffer(gl.ARRAY_BUFFER,sphereBuffer);
    gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(pos),gl.STATIC_DRAW);
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,sphereIndexBuffer);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(_indices),gl.STATIC_DRAW);

    sphereIndexBuffer.num = _indices.length;

}

I have a sphere tessellation algorithm using a icosahedron. There are plenty of places on the net to find them but feel free to use and modify my implementation. Though i must say, my implementation is not the best when it come to memory, some vertices are duplicates.

    function initSphere(subs){
    if(typeof(subs) == 'undefined'){
        subs = 1;
    }
    var t = (1+Math.sqrt(5))/2;
    var tau = t/Math.sqrt(1+t*t);
    var one = 1/Math.sqrt(1+t*t);
    var pos = [tau, one, 0.0,
              -tau, one, 0.0,
              -tau, -one, 0.0,
              tau, -one, 0.0,
              one, 0.0 ,  tau,
              one, 0.0 , -tau,
              -one, 0.0 , -tau,
              -one, 0.0 , tau,
              0.0 , tau, one,
              0.0 , -tau, one,
              0.0 , -tau, -one,
              0.0 , tau, -one];
    var _indices =  [4, 8, 7,
                    4, 7, 9,
                    5, 6, 11,
                    5, 10, 6,
                    0, 4, 3,
                    0, 3, 5,
                    2, 7, 1,
                    2, 1, 6,
                    8, 0, 11,
                    8, 11, 1,
                    9, 10, 3,
                    9, 2, 10,
                    8, 4, 0,
                    11, 0, 5,
                    4, 9, 3,
                    5, 3, 10,
                    7, 8, 1,
                    6, 1, 11,
                    7, 2, 9,
                    6, 10, 2];

    for(var i = 0;i<subs;i++){
        var newIndices = new Array();
        for(var j = 0;j<_indices.length;j+=3){
            var p1 = [ 
                       (pos[_indices[j+0]*3+0] + pos[_indices[j+1]*3+0])*0.5 ,
                       (pos[_indices[j+0]*3+1] + pos[_indices[j+1]*3+1])*0.5 ,
                       (pos[_indices[j+0]*3+2] + pos[_indices[j+1]*3+2])*0.5
                   ];
           var p2 = [ 
                       (pos[_indices[j+1]*3+0] + pos[_indices[j+2]*3+0])*0.5 ,
                       (pos[_indices[j+1]*3+1] + pos[_indices[j+2]*3+1])*0.5 ,
                       (pos[_indices[j+1]*3+2] + pos[_indices[j+2]*3+2])*0.5
                   ];
           var p3 = [ 
                       (pos[_indices[j+2]*3+0] + pos[_indices[j+0]*3+0])*0.5 ,
                       (pos[_indices[j+2]*3+1] + pos[_indices[j+0]*3+1])*0.5 ,
                       (pos[_indices[j+2]*3+2] + pos[_indices[j+0]*3+2])*0.5
                   ];
          p1 = normalize(p1);
          p2 = normalize(p2);
          p3 = normalize(p3);
          var i0,i1,i2,a,b,c;
          i0 = pos.length/3;
          i1 = pos.length/3+1;
          i2 = pos.length/3+2;
          a = _indices[j+0];
          b = _indices[j+1];
          c = _indices[j+2];
          newIndices.push(a);newIndices.push(i2);newIndices.push(i0);
          newIndices.push(b);newIndices.push(i0);newIndices.push(i1);
          newIndices.push(c);newIndices.push(i1);newIndices.push(i2);
          newIndices.push(i0);newIndices.push(i2);newIndices.push(i1);

          pos.push(p1[0]);pos.push(p1[1]);pos.push(p1[2]);
          pos.push(p2[0]);pos.push(p2[1]);pos.push(p2[2]);
          pos.push(p3[0]);pos.push(p3[1]);pos.push(p3[2]);

        }
        _indices = newIndices;

    }



    gl.bindBuffer(gl.ARRAY_BUFFER,sphereBuffer);
    gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(pos),gl.STATIC_DRAW);
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,sphereIndexBuffer);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(_indices),gl.STATIC_DRAW);

    sphereIndexBuffer.num = _indices.length;

}
伏妖词 2024-10-11 06:42:25

Worldwind Java SDK 有一个二十面体镶嵌器,可以非常简单地移植到 JavaScript。它也遵循类似 BSD 的许可证。

gov.nasa.worldwind.util.GeometryBuilder

http://builds.worldwind.arc.nasa。政府/download.asp

Worldwind Java SDK has a icosahedron tessellator that should be very simple to port to javascript. It's under a BSD-like license as well.

gov.nasa.worldwind.util.GeometryBuilder

http://builds.worldwind.arc.nasa.gov/download.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文