Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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. </p> <pre><code> 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&lt;subs;i++){ var newIndices = new Array(); for(var j = 0;j&lt;_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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload