|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>three.js webgl - buffergeometry - uint</title> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> |
| 7 | + <style> |
| 8 | + body { |
| 9 | + color: #cccccc; |
| 10 | + font-family:Monospace; |
| 11 | + font-size:13px; |
| 12 | + text-align:center; |
| 13 | + |
| 14 | + background-color: #050505; |
| 15 | + margin: 0px; |
| 16 | + overflow: hidden; |
| 17 | + } |
| 18 | + |
| 19 | + #info { |
| 20 | + position: absolute; |
| 21 | + top: 0px; width: 100%; |
| 22 | + padding: 5px; |
| 23 | + } |
| 24 | + |
| 25 | + a { |
| 26 | + color: #0080ff; |
| 27 | + } |
| 28 | + |
| 29 | + </style> |
| 30 | + </head> |
| 31 | + <body> |
| 32 | + |
| 33 | + <div id="container"></div> |
| 34 | + <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> webgl - buffergeometry - BufferAttributeTypes</div> |
| 35 | + |
| 36 | + <script src="../build/three.min.js"></script> |
| 37 | + |
| 38 | + <script src="js/Detector.js"></script> |
| 39 | + <script src="js/libs/stats.min.js"></script> |
| 40 | + |
| 41 | + <script> |
| 42 | + |
| 43 | + if ( ! Detector.webgl ) Detector.addGetWebGLMessage(); |
| 44 | + |
| 45 | + var container, stats; |
| 46 | + |
| 47 | + var camera, scene, renderer; |
| 48 | + |
| 49 | + var mesh; |
| 50 | + |
| 51 | + init(); |
| 52 | + animate(); |
| 53 | + |
| 54 | + function init() { |
| 55 | + |
| 56 | + container = document.getElementById( 'container' ); |
| 57 | + |
| 58 | + // |
| 59 | + |
| 60 | + camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 3500 ); |
| 61 | + camera.position.z = 2750; |
| 62 | + |
| 63 | + scene = new THREE.Scene(); |
| 64 | + scene.fog = new THREE.Fog( 0x050505, 2000, 3500 ); |
| 65 | + |
| 66 | + // |
| 67 | + |
| 68 | + scene.add( new THREE.AmbientLight( 0x444444 ) ); |
| 69 | + |
| 70 | + var light1 = new THREE.DirectionalLight( 0xffffff, 0.5 ); |
| 71 | + light1.position.set( 1, 1, 1 ); |
| 72 | + scene.add( light1 ); |
| 73 | + |
| 74 | + var light2 = new THREE.DirectionalLight( 0xffffff, 1.5 ); |
| 75 | + light2.position.set( 0, -1, 0 ); |
| 76 | + scene.add( light2 ); |
| 77 | + |
| 78 | + // |
| 79 | + |
| 80 | + var triangles = 500000; |
| 81 | + |
| 82 | + var geometry = new THREE.BufferGeometry(); |
| 83 | + |
| 84 | + var indices = new Uint32Array( triangles * 3 ); |
| 85 | + |
| 86 | + for ( var i = 0; i < indices.length; i ++ ) { |
| 87 | + |
| 88 | + indices[ i ] = i; |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + var positions = new Float32Array( triangles * 3 * 3 ); |
| 93 | + var normals = new Int16Array( triangles * 3 * 3 ); |
| 94 | + var colors = new Uint8Array( triangles * 3 * 3 ); |
| 95 | + |
| 96 | + var color = new THREE.Color(); |
| 97 | + |
| 98 | + var n = 800, n2 = n/2; // triangles spread in the cube |
| 99 | + var d = 12, d2 = d/2; // individual triangle size |
| 100 | + |
| 101 | + var pA = new THREE.Vector3(); |
| 102 | + var pB = new THREE.Vector3(); |
| 103 | + var pC = new THREE.Vector3(); |
| 104 | + |
| 105 | + var cb = new THREE.Vector3(); |
| 106 | + var ab = new THREE.Vector3(); |
| 107 | + |
| 108 | + for ( var i = 0; i < positions.length; i += 9 ) { |
| 109 | + |
| 110 | + // positions |
| 111 | + |
| 112 | + var x = Math.random() * n - n2; |
| 113 | + var y = Math.random() * n - n2; |
| 114 | + var z = Math.random() * n - n2; |
| 115 | + |
| 116 | + var ax = x + Math.random() * d - d2; |
| 117 | + var ay = y + Math.random() * d - d2; |
| 118 | + var az = z + Math.random() * d - d2; |
| 119 | + |
| 120 | + var bx = x + Math.random() * d - d2; |
| 121 | + var by = y + Math.random() * d - d2; |
| 122 | + var bz = z + Math.random() * d - d2; |
| 123 | + |
| 124 | + var cx = x + Math.random() * d - d2; |
| 125 | + var cy = y + Math.random() * d - d2; |
| 126 | + var cz = z + Math.random() * d - d2; |
| 127 | + |
| 128 | + positions[ i ] = ax; |
| 129 | + positions[ i + 1 ] = ay; |
| 130 | + positions[ i + 2 ] = az; |
| 131 | + |
| 132 | + positions[ i + 3 ] = bx; |
| 133 | + positions[ i + 4 ] = by; |
| 134 | + positions[ i + 5 ] = bz; |
| 135 | + |
| 136 | + positions[ i + 6 ] = cx; |
| 137 | + positions[ i + 7 ] = cy; |
| 138 | + positions[ i + 8 ] = cz; |
| 139 | + |
| 140 | + // flat face normals |
| 141 | + |
| 142 | + pA.set( ax, ay, az ); |
| 143 | + pB.set( bx, by, bz ); |
| 144 | + pC.set( cx, cy, cz ); |
| 145 | + |
| 146 | + cb.subVectors( pC, pB ); |
| 147 | + ab.subVectors( pA, pB ); |
| 148 | + cb.cross( ab ); |
| 149 | + |
| 150 | + cb.normalize(); |
| 151 | + |
| 152 | + var nx = cb.x; |
| 153 | + var ny = cb.y; |
| 154 | + var nz = cb.z; |
| 155 | + |
| 156 | + normals[ i ] = nx * 32767; |
| 157 | + normals[ i + 1 ] = ny * 32767; |
| 158 | + normals[ i + 2 ] = nz * 32767; |
| 159 | + |
| 160 | + normals[ i + 3 ] = nx * 32767; |
| 161 | + normals[ i + 4 ] = ny * 32767; |
| 162 | + normals[ i + 5 ] = nz * 32767; |
| 163 | + |
| 164 | + normals[ i + 6 ] = nx * 32767; |
| 165 | + normals[ i + 7 ] = ny * 32767; |
| 166 | + normals[ i + 8 ] = nz * 32767; |
| 167 | + |
| 168 | + // colors |
| 169 | + |
| 170 | + var vx = ( x / n ) + 0.5; |
| 171 | + var vy = ( y / n ) + 0.5; |
| 172 | + var vz = ( z / n ) + 0.5; |
| 173 | + |
| 174 | + color.setRGB( vx, vy, vz ); |
| 175 | + |
| 176 | + colors[ i ] = color.r * 255; |
| 177 | + colors[ i + 1 ] = color.g * 255; |
| 178 | + colors[ i + 2 ] = color.b * 255; |
| 179 | + |
| 180 | + colors[ i + 3 ] = color.r * 255; |
| 181 | + colors[ i + 4 ] = color.g * 255; |
| 182 | + colors[ i + 5 ] = color.b * 255; |
| 183 | + |
| 184 | + colors[ i + 6 ] = color.r * 255; |
| 185 | + colors[ i + 7 ] = color.g * 255; |
| 186 | + colors[ i + 8 ] = color.b * 255; |
| 187 | + |
| 188 | + } |
| 189 | + |
| 190 | + geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) ); |
| 191 | + geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) ); |
| 192 | + geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3, true ) ); |
| 193 | + geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3, true ) ); |
| 194 | + |
| 195 | + geometry.computeBoundingSphere(); |
| 196 | + |
| 197 | + var material = new THREE.MeshPhongMaterial( { |
| 198 | + color: 0xaaaaaa, specular: 0xffffff, shininess: 250, |
| 199 | + side: THREE.DoubleSide, vertexColors: THREE.VertexColors |
| 200 | + } ); |
| 201 | + |
| 202 | + mesh = new THREE.Mesh( geometry, material ); |
| 203 | + scene.add( mesh ); |
| 204 | + |
| 205 | + // |
| 206 | + |
| 207 | + renderer = new THREE.WebGLRenderer( { antialias: false } ); |
| 208 | + renderer.setClearColor( scene.fog.color ); |
| 209 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 210 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 211 | + |
| 212 | + renderer.gammaInput = true; |
| 213 | + renderer.gammaOutput = true; |
| 214 | + |
| 215 | + container.appendChild( renderer.domElement ); |
| 216 | + |
| 217 | + // |
| 218 | + |
| 219 | + stats = new Stats(); |
| 220 | + stats.domElement.style.position = 'absolute'; |
| 221 | + stats.domElement.style.top = '0px'; |
| 222 | + container.appendChild( stats.domElement ); |
| 223 | + |
| 224 | + // |
| 225 | + |
| 226 | + window.addEventListener( 'resize', onWindowResize, false ); |
| 227 | + |
| 228 | + } |
| 229 | + |
| 230 | + function onWindowResize() { |
| 231 | + |
| 232 | + camera.aspect = window.innerWidth / window.innerHeight; |
| 233 | + camera.updateProjectionMatrix(); |
| 234 | + |
| 235 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 236 | + |
| 237 | + } |
| 238 | + |
| 239 | + // |
| 240 | + |
| 241 | + function animate() { |
| 242 | + |
| 243 | + requestAnimationFrame( animate ); |
| 244 | + |
| 245 | + render(); |
| 246 | + stats.update(); |
| 247 | + |
| 248 | + } |
| 249 | + |
| 250 | + function render() { |
| 251 | + |
| 252 | + var time = Date.now() * 0.001; |
| 253 | + |
| 254 | + mesh.rotation.x = time * 0.25; |
| 255 | + mesh.rotation.y = time * 0.5; |
| 256 | + |
| 257 | + renderer.render( scene, camera ); |
| 258 | + |
| 259 | + } |
| 260 | + |
| 261 | + </script> |
| 262 | + |
| 263 | + </body> |
| 264 | +</html> |
0 commit comments