|
63 | 63 |
|
64 | 64 | import Stats from './jsm/libs/stats.module.js'; |
65 | 65 |
|
| 66 | + import { BufferGeometryUtils } from './jsm/utils/BufferGeometryUtils.js'; |
| 67 | + |
66 | 68 | let renderer, scene, camera, stats; |
67 | 69 |
|
68 | 70 | let object; |
|
84 | 86 |
|
85 | 87 | let radius = 100; |
86 | 88 | const inner = 0.6 * radius; |
| 89 | + const vertex = new THREE.Vector3(); |
87 | 90 | const vertices = []; |
88 | 91 |
|
89 | 92 | for ( let i = 0; i < 100000; i ++ ) { |
90 | 93 |
|
91 | | - const vertex = new THREE.Vector3(); |
92 | 94 | vertex.x = Math.random() * 2 - 1; |
93 | 95 | vertex.y = Math.random() * 2 - 1; |
94 | 96 | vertex.z = Math.random() * 2 - 1; |
95 | 97 | vertex.multiplyScalar( radius ); |
96 | 98 |
|
97 | 99 | if ( ( vertex.x > inner || vertex.x < - inner ) || |
98 | | - ( vertex.y > inner || vertex.y < - inner ) || |
99 | | - ( vertex.z > inner || vertex.z < - inner ) ) |
| 100 | + ( vertex.y > inner || vertex.y < - inner ) || |
| 101 | + ( vertex.z > inner || vertex.z < - inner ) ) |
100 | 102 |
|
101 | | - vertices.push( vertex ); |
| 103 | + vertices.push( vertex.x, vertex.y, vertex.z ); |
102 | 104 |
|
103 | 105 | } |
104 | 106 |
|
105 | | - vertices1 = vertices.length; |
| 107 | + vertices1 = vertices.length / 3; |
106 | 108 |
|
107 | 109 | radius = 200; |
108 | | - const geometry2 = new THREE.BoxGeometry( radius, 0.1 * radius, 0.1 * radius, 50, 5, 5 ); |
| 110 | + |
| 111 | + let boxGeometry1 = new THREE.BoxBufferGeometry( radius, 0.1 * radius, 0.1 * radius, 50, 5, 5 ); |
| 112 | + |
| 113 | + // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data |
| 114 | + |
| 115 | + boxGeometry1.deleteAttribute( 'normal' ); |
| 116 | + boxGeometry1.deleteAttribute( 'uv' ); |
| 117 | + |
| 118 | + boxGeometry1 = BufferGeometryUtils.mergeVertices( boxGeometry1 ); |
109 | 119 |
|
110 | 120 | const matrix = new THREE.Matrix4(); |
111 | 121 | const position = new THREE.Vector3(); |
|
120 | 130 |
|
121 | 131 | matrix.compose( position, quaternion.setFromEuler( rotation ), scale ); |
122 | 132 |
|
123 | | - for ( var i = 0, l = geo.vertices.length; i < l; i ++ ) { |
| 133 | + const positionAttribute = geo.getAttribute( 'position' ); |
124 | 134 |
|
125 | | - const vertex = geo.vertices[ i ]; |
126 | | - vertices.push( vertex.clone().applyMatrix4( matrix ) ); |
| 135 | + for ( var i = 0, l = positionAttribute.count; i < l; i ++ ) { |
| 136 | + |
| 137 | + vertex.fromBufferAttribute( positionAttribute, i ); |
| 138 | + vertex.applyMatrix4( matrix ); |
| 139 | + vertices.push( vertex.x, vertex.y, vertex.z ); |
127 | 140 |
|
128 | 141 | } |
129 | 142 |
|
130 | 143 | } |
131 | 144 |
|
132 | 145 | // side 1 |
133 | 146 |
|
134 | | - addGeo( geometry2, 0, 110, 110, 0 ); |
135 | | - addGeo( geometry2, 0, 110, - 110, 0 ); |
136 | | - addGeo( geometry2, 0, - 110, 110, 0 ); |
137 | | - addGeo( geometry2, 0, - 110, - 110, 0 ); |
| 147 | + addGeo( boxGeometry1, 0, 110, 110, 0 ); |
| 148 | + addGeo( boxGeometry1, 0, 110, - 110, 0 ); |
| 149 | + addGeo( boxGeometry1, 0, - 110, 110, 0 ); |
| 150 | + addGeo( boxGeometry1, 0, - 110, - 110, 0 ); |
138 | 151 |
|
139 | 152 | // side 2 |
140 | 153 |
|
141 | | - addGeo( geometry2, 110, 110, 0, Math.PI / 2 ); |
142 | | - addGeo( geometry2, 110, - 110, 0, Math.PI / 2 ); |
143 | | - addGeo( geometry2, - 110, 110, 0, Math.PI / 2 ); |
144 | | - addGeo( geometry2, - 110, - 110, 0, Math.PI / 2 ); |
| 154 | + addGeo( boxGeometry1, 110, 110, 0, Math.PI / 2 ); |
| 155 | + addGeo( boxGeometry1, 110, - 110, 0, Math.PI / 2 ); |
| 156 | + addGeo( boxGeometry1, - 110, 110, 0, Math.PI / 2 ); |
| 157 | + addGeo( boxGeometry1, - 110, - 110, 0, Math.PI / 2 ); |
145 | 158 |
|
146 | 159 | // corner edges |
147 | 160 |
|
148 | | - const geometry3 = new THREE.BoxGeometry( 0.1 * radius, radius * 1.2, 0.1 * radius, 5, 60, 5 ); |
| 161 | + let boxGeometry2 = new THREE.BoxBufferGeometry( 0.1 * radius, radius * 1.2, 0.1 * radius, 5, 60, 5 ); |
149 | 162 |
|
150 | | - addGeo( geometry3, 110, 0, 110, 0 ); |
151 | | - addGeo( geometry3, 110, 0, - 110, 0 ); |
152 | | - addGeo( geometry3, - 110, 0, 110, 0 ); |
153 | | - addGeo( geometry3, - 110, 0, - 110, 0 ); |
| 163 | + boxGeometry2.deleteAttribute( 'normal' ); |
| 164 | + boxGeometry2.deleteAttribute( 'uv' ); |
154 | 165 |
|
155 | | - const positions = new Float32Array( vertices.length * 3 ); |
156 | | - const colors = new Float32Array( vertices.length * 3 ); |
157 | | - const sizes = new Float32Array( vertices.length ); |
| 166 | + boxGeometry2 = BufferGeometryUtils.mergeVertices( boxGeometry2 ); |
158 | 167 |
|
159 | | - const color = new THREE.Color(); |
| 168 | + addGeo( boxGeometry2, 110, 0, 110, 0 ); |
| 169 | + addGeo( boxGeometry2, 110, 0, - 110, 0 ); |
| 170 | + addGeo( boxGeometry2, - 110, 0, 110, 0 ); |
| 171 | + addGeo( boxGeometry2, - 110, 0, - 110, 0 ); |
| 172 | + |
| 173 | + const positionAttribute = new THREE.Float32BufferAttribute( vertices, 3 ); |
160 | 174 |
|
161 | | - for ( let i = 0; i < vertices.length; i ++ ) { |
| 175 | + const colors = []; |
| 176 | + const sizes = []; |
| 177 | + |
| 178 | + const color = new THREE.Color(); |
162 | 179 |
|
163 | | - const vertex = vertices[ i ]; |
164 | | - vertex.toArray( positions, i * 3 ); |
| 180 | + for ( let i = 0; i < positionAttribute.count; i ++ ) { |
165 | 181 |
|
166 | 182 | if ( i < vertices1 ) { |
167 | 183 |
|
|
180 | 196 | } |
181 | 197 |
|
182 | 198 | const geometry = new THREE.BufferGeometry(); |
183 | | - geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) ); |
184 | | - geometry.setAttribute( 'ca', new THREE.BufferAttribute( colors, 3 ) ); |
185 | | - geometry.setAttribute( 'size', new THREE.BufferAttribute( sizes, 1 ) ); |
| 199 | + geometry.setAttribute( 'position', positionAttribute ); |
| 200 | + geometry.setAttribute( 'ca', new THREE.Float32BufferAttribute( colors, 3 ) ); |
| 201 | + geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ) ); |
186 | 202 |
|
187 | 203 | // |
188 | 204 |
|
189 | | - const texture = new THREE.TextureLoader().load( "textures/sprites/ball.png" ); |
| 205 | + const texture = new THREE.TextureLoader().load( 'textures/sprites/ball.png' ); |
190 | 206 | texture.wrapS = THREE.RepeatWrapping; |
191 | 207 | texture.wrapT = THREE.RepeatWrapping; |
192 | 208 |
|
|
0 commit comments