代码之家  ›  专栏  ›  技术社区  ›  Chris

有没有人有一个用javascript绘制细分二十面体的样本,他们可以分享吗?

  •  2
  • Chris  · 技术社区  · 14 年前

    有没有人有一个用javascript绘制细分二十面体的样本,他们可以分享吗?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Rickard    14 年前

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

        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;
    
    }
    
        2
  •  0
  •   Jon Bringhurst    14 年前

    worldwindjavasdk有一个二十面体镶嵌器,应该很容易移植到javascript。它也有类似BSD的许可证。

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