有没有人有一个样本用于在javascript中绘制细分的二十面体,他们能够分享?
答案 0 :(得分:1)
我有一个使用二十面体的球体曲面细分算法。网上有很多地方可以找到它们,但可以随意使用和修改我的实现。虽然我必须说,我的实现并不是最好的,当它来到内存时,一些顶点是重复的。
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;
}
答案 1 :(得分:0)
Worldwind Java SDK有一个二十面体曲面细分器,移植到javascript应该非常简单。它也属于类似BSD的许可证。
gov.nasa.worldwind.util.GeometryBuilder