|
| 1 | +/** |
| 2 | + * @author Mugen87 / https://github.com/Mugen87 |
| 3 | + */ |
| 4 | + |
| 5 | + // points - to create a closed torus, one must use a set of points |
| 6 | + // like so: [ a, b, c, d, a ], see first is the same as last. |
| 7 | + // segments - the number of circumference segments to create |
| 8 | + // phiStart - the starting radian |
| 9 | + // phiLength - the radian (0 to 2PI) range of the lathed section |
| 10 | + // 2PI is a closed lathe, less than 2PI is a portion. |
| 11 | + |
| 12 | +THREE.LatheBufferGeometry = function ( points, segments, phiStart, phiLength ) { |
| 13 | + |
| 14 | + THREE.BufferGeometry.call( this ); |
| 15 | + |
| 16 | + this.type = 'LatheBufferGeometry'; |
| 17 | + |
| 18 | + this.parameters = { |
| 19 | + points: points, |
| 20 | + segments: segments, |
| 21 | + phiStart: phiStart, |
| 22 | + phiLength: phiLength |
| 23 | + }; |
| 24 | + |
| 25 | + segments = Math.floor( segments ) || 12; |
| 26 | + phiStart = phiStart || 0; |
| 27 | + phiLength = phiLength || Math.PI * 2; |
| 28 | + |
| 29 | + // clamp phiLength so it's in range of [ 0, 2PI ] |
| 30 | + phiLength = THREE.Math.clamp( phiLength, 0, Math.PI * 2 ); |
| 31 | + |
| 32 | + // these are used to calculate buffer length |
| 33 | + var vertexCount = ( segments + 1 ) * points.length; |
| 34 | + var indexCount = segments * points.length * 2 * 3; |
| 35 | + |
| 36 | + // buffers |
| 37 | + var indices = new THREE.BufferAttribute( new ( indexCount > 65535 ? Uint32Array : Uint16Array )( indexCount ) , 1 ); |
| 38 | + var vertices = new THREE.BufferAttribute( new Float32Array( vertexCount * 3 ), 3 ); |
| 39 | + var uvs = new THREE.BufferAttribute( new Float32Array( vertexCount * 2 ), 2 ); |
| 40 | + |
| 41 | + // helper variables |
| 42 | + var index = 0, indexOffset = 0, base; |
| 43 | + var inversePointLength = 1.0 / ( points.length - 1 ); |
| 44 | + var inverseSegments = 1.0 / segments; |
| 45 | + var vertex = new THREE.Vector3(); |
| 46 | + var uv = new THREE.Vector2(); |
| 47 | + var i, j; |
| 48 | + |
| 49 | + // generate vertices and uvs |
| 50 | + |
| 51 | + for ( i = 0; i <= segments; i ++ ) { |
| 52 | + |
| 53 | + var phi = phiStart + i * inverseSegments * phiLength; |
| 54 | + |
| 55 | + var sin = Math.sin( phi ); |
| 56 | + var cos = Math.cos( phi ); |
| 57 | + |
| 58 | + for ( j = 0; j <= ( points.length - 1 ); j ++ ) { |
| 59 | + |
| 60 | + // vertex |
| 61 | + vertex.x = points[ j ].x * sin; |
| 62 | + vertex.y = points[ j ].y; |
| 63 | + vertex.z = points[ j ].x * cos; |
| 64 | + vertices.setXYZ( index, vertex.x, vertex.y, vertex.z ); |
| 65 | + |
| 66 | + // uv |
| 67 | + uv.x = i / segments; |
| 68 | + uv.y = j / ( points.length - 1 ); |
| 69 | + uvs.setXY( index, uv.x, uv.y ); |
| 70 | + |
| 71 | + // increase index |
| 72 | + index ++; |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + // generate indices |
| 79 | + |
| 80 | + for ( i = 0; i < segments; i ++ ) { |
| 81 | + |
| 82 | + for ( j = 0; j < ( points.length - 1 ); j ++ ) { |
| 83 | + |
| 84 | + base = j + i * points.length; |
| 85 | + |
| 86 | + // indices |
| 87 | + var a = base; |
| 88 | + var b = base + points.length; |
| 89 | + var c = base + points.length + 1; |
| 90 | + var d = base + 1; |
| 91 | + |
| 92 | + // face one |
| 93 | + indices.setX( indexOffset, a ); indexOffset++; |
| 94 | + indices.setX( indexOffset, b ); indexOffset++; |
| 95 | + indices.setX( indexOffset, d ); indexOffset++; |
| 96 | + |
| 97 | + // face two |
| 98 | + indices.setX( indexOffset, b ); indexOffset++; |
| 99 | + indices.setX( indexOffset, c ); indexOffset++; |
| 100 | + indices.setX( indexOffset, d ); indexOffset++; |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + // build geometry |
| 107 | + |
| 108 | + this.setIndex( indices ); |
| 109 | + this.addAttribute( 'position', vertices ); |
| 110 | + this.addAttribute( 'uv', uvs ); |
| 111 | + |
| 112 | + // generate normals |
| 113 | + |
| 114 | + this.computeVertexNormals(); |
| 115 | + |
| 116 | + // if the geometry is closed, we need to average the normals along the seam. |
| 117 | + // because the corresponding vertices are identical (but still have different UVs). |
| 118 | + |
| 119 | + if( phiLength === Math.PI * 2 ) { |
| 120 | + |
| 121 | + var normals = this.attributes.normal.array; |
| 122 | + var n1 = new THREE.Vector3(); |
| 123 | + var n2 = new THREE.Vector3(); |
| 124 | + var n = new THREE.Vector3(); |
| 125 | + |
| 126 | + // this is the buffer offset for the last line of vertices |
| 127 | + base = segments * points.length * 3; |
| 128 | + |
| 129 | + for( i = 0, j = 0; i < points.length; i ++, j += 3 ) { |
| 130 | + |
| 131 | + // select the normal of the vertex in the first line |
| 132 | + n1.x = normals[ j + 0 ]; |
| 133 | + n1.y = normals[ j + 1 ]; |
| 134 | + n1.z = normals[ j + 2 ]; |
| 135 | + |
| 136 | + // select the normal of the vertex in the last line |
| 137 | + n2.x = normals[ base + j + 0 ]; |
| 138 | + n2.y = normals[ base + j + 1 ]; |
| 139 | + n2.z = normals[ base + j + 2 ]; |
| 140 | + |
| 141 | + // average normals |
| 142 | + n.addVectors( n1, n2 ).normalize(); |
| 143 | + |
| 144 | + // assign the new values to both normals |
| 145 | + normals[ j + 0 ] = normals[ base + j + 0 ] = n.x; |
| 146 | + normals[ j + 1 ] = normals[ base + j + 1 ] = n.y; |
| 147 | + normals[ j + 2 ] = normals[ base + j + 2 ] = n.z; |
| 148 | + |
| 149 | + } // next row |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | +}; |
| 154 | + |
| 155 | +THREE.LatheBufferGeometry.prototype = Object.create( THREE.BufferGeometry.prototype ); |
| 156 | +THREE.LatheBufferGeometry.prototype.constructor = THREE.LatheBufferGeometry; |
0 commit comments