| |
| |
| |
| |
|
|
| import { Geometry } from '../core/Geometry.js'; |
| import { BufferGeometry } from '../core/BufferGeometry.js'; |
| import { Float32BufferAttribute } from '../core/BufferAttribute.js'; |
| import { Vector3 } from '../math/Vector3.js'; |
|
|
| |
|
|
| function BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) { |
|
|
| Geometry.call( this ); |
|
|
| this.type = 'BoxGeometry'; |
|
|
| this.parameters = { |
| width: width, |
| height: height, |
| depth: depth, |
| widthSegments: widthSegments, |
| heightSegments: heightSegments, |
| depthSegments: depthSegments |
| }; |
|
|
| this.fromBufferGeometry( new BoxBufferGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) ); |
| this.mergeVertices(); |
|
|
| } |
|
|
| BoxGeometry.prototype = Object.create( Geometry.prototype ); |
| BoxGeometry.prototype.constructor = BoxGeometry; |
|
|
| |
|
|
| function BoxBufferGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) { |
|
|
| BufferGeometry.call( this ); |
|
|
| this.type = 'BoxBufferGeometry'; |
|
|
| this.parameters = { |
| width: width, |
| height: height, |
| depth: depth, |
| widthSegments: widthSegments, |
| heightSegments: heightSegments, |
| depthSegments: depthSegments |
| }; |
|
|
| var scope = this; |
|
|
| width = width || 1; |
| height = height || 1; |
| depth = depth || 1; |
|
|
| |
|
|
| widthSegments = Math.floor( widthSegments ) || 1; |
| heightSegments = Math.floor( heightSegments ) || 1; |
| depthSegments = Math.floor( depthSegments ) || 1; |
|
|
| |
|
|
| var indices = []; |
| var vertices = []; |
| var normals = []; |
| var uvs = []; |
|
|
| |
|
|
| var numberOfVertices = 0; |
| var groupStart = 0; |
|
|
| |
|
|
| buildPlane( 'z', 'y', 'x', - 1, - 1, depth, height, width, depthSegments, heightSegments, 0 ); |
| buildPlane( 'z', 'y', 'x', 1, - 1, depth, height, - width, depthSegments, heightSegments, 1 ); |
| buildPlane( 'x', 'z', 'y', 1, 1, width, depth, height, widthSegments, depthSegments, 2 ); |
| buildPlane( 'x', 'z', 'y', 1, - 1, width, depth, - height, widthSegments, depthSegments, 3 ); |
| buildPlane( 'x', 'y', 'z', 1, - 1, width, height, depth, widthSegments, heightSegments, 4 ); |
| buildPlane( 'x', 'y', 'z', - 1, - 1, width, height, - depth, widthSegments, heightSegments, 5 ); |
|
|
| |
|
|
| this.setIndex( indices ); |
| this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); |
| this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); |
| this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); |
|
|
| function buildPlane( u, v, w, udir, vdir, width, height, depth, gridX, gridY, materialIndex ) { |
|
|
| var segmentWidth = width / gridX; |
| var segmentHeight = height / gridY; |
|
|
| var widthHalf = width / 2; |
| var heightHalf = height / 2; |
| var depthHalf = depth / 2; |
|
|
| var gridX1 = gridX + 1; |
| var gridY1 = gridY + 1; |
|
|
| var vertexCounter = 0; |
| var groupCount = 0; |
|
|
| var ix, iy; |
|
|
| var vector = new Vector3(); |
|
|
| |
|
|
| for ( iy = 0; iy < gridY1; iy ++ ) { |
|
|
| var y = iy * segmentHeight - heightHalf; |
|
|
| for ( ix = 0; ix < gridX1; ix ++ ) { |
|
|
| var x = ix * segmentWidth - widthHalf; |
|
|
| |
|
|
| vector[ u ] = x * udir; |
| vector[ v ] = y * vdir; |
| vector[ w ] = depthHalf; |
|
|
| |
|
|
| vertices.push( vector.x, vector.y, vector.z ); |
|
|
| |
|
|
| vector[ u ] = 0; |
| vector[ v ] = 0; |
| vector[ w ] = depth > 0 ? 1 : - 1; |
|
|
| |
|
|
| normals.push( vector.x, vector.y, vector.z ); |
|
|
| |
|
|
| uvs.push( ix / gridX ); |
| uvs.push( 1 - ( iy / gridY ) ); |
|
|
| |
|
|
| vertexCounter += 1; |
|
|
| } |
|
|
| } |
|
|
| |
|
|
| |
| |
| |
|
|
| for ( iy = 0; iy < gridY; iy ++ ) { |
|
|
| for ( ix = 0; ix < gridX; ix ++ ) { |
|
|
| var a = numberOfVertices + ix + gridX1 * iy; |
| var b = numberOfVertices + ix + gridX1 * ( iy + 1 ); |
| var c = numberOfVertices + ( ix + 1 ) + gridX1 * ( iy + 1 ); |
| var d = numberOfVertices + ( ix + 1 ) + gridX1 * iy; |
|
|
| |
|
|
| indices.push( a, b, d ); |
| indices.push( b, c, d ); |
|
|
| |
|
|
| groupCount += 6; |
|
|
| } |
|
|
| } |
|
|
| |
|
|
| scope.addGroup( groupStart, groupCount, materialIndex ); |
|
|
| |
|
|
| groupStart += groupCount; |
|
|
| |
|
|
| numberOfVertices += vertexCounter; |
|
|
| } |
|
|
| } |
|
|
| BoxBufferGeometry.prototype = Object.create( BufferGeometry.prototype ); |
| BoxBufferGeometry.prototype.constructor = BoxBufferGeometry; |
|
|
|
|
| export { BoxGeometry, BoxBufferGeometry }; |
|
|