├── LICENSE ├── README.md ├── css └── webgl2examples.css ├── cube.html ├── deferred.html ├── dof.html ├── img └── khronos_webgl.png ├── occlusion.html ├── oit-dual-depth-peeling.html ├── oit.html ├── particles.html ├── ssao.html ├── tests ├── oit-near-opaque.html └── sorted-over-near-opaque.html ├── triangle.html └── utils ├── gl-matrix.js └── utils.js /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Tarek Sherif 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | WebGL 2 Examples 2 | ================ 3 | 4 | Rendering algorithms implemented in raw WebGL 2. 5 | 6 | Currently include: 7 | - [Just a Triangle](https://tsherif.github.io/webgl2examples/triangle.html) (vertex arrays) 8 | - [Phong-shaded Cube](https://tsherif.github.io/webgl2examples/cube.html) (vertex arrays, uniform buffers, immutable textures) 9 | - [Particles](https://tsherif.github.io/webgl2examples/particles.html) (vertex arrays, uniform buffers, transform feedback) 10 | - [Deferred Rendering](https://tsherif.github.io/webgl2examples/deferred.html) (vertex arrays, uniform buffers, immutable textures, multiple render targets, float textures, texelFetch, EXT_color_buffer_float) 11 | - [Depth of Field](https://tsherif.github.io/webgl2examples/dof.html) (vertex arrays, uniform buffers, immutable textures, depth textures, texelFetch, hardware instancing) 12 | - [Order-independent Transparency](https://tsherif.github.io/webgl2examples/oit.html) (vertex arrays, uniform buffers, immutable textures, multiple render targets, float textures, texelFetch, hardware instancing, EXT_color_buffer_float) 13 | - [Order-independent Transparency using Dual Depth Peeling](https://tsherif.github.io/webgl2examples/oit-dual-depth-peeling.html) (vertex arrays, uniform buffers, immutable textures, multiple render targets, float textures, texelFetch, hardware instancing, EXT_color_buffer_float) 14 | - [Screen Space Ambient Occlusion](https://tsherif.github.io/webgl2examples/ssao.html) (vertex arrays, uniform buffers, immutable textures, multiple render targets, float textures, texelFetch, hardware instancing, EXT_color_buffer_float) 15 | - [Occlusion Culling](https://tsherif.github.io/webgl2examples/occlusion.html) (vertex arrays, uniform buffers, immutable textures, occlusion query) 16 | 17 | All examples are implemented in a single HTML file with minimal use of functions, modules, classes or other abstractions. The goal is to allow the reader to easily see, in sequential order, all GL calls that are made. 18 | 19 | These examples can be thought of as companion to Shrek Shao and Trung Le's excellent [WebGL 2 Samples Pack](http://webglsamples.org/WebGL2Samples/). While their samples demonstrate individual features of WebGL 2, this project aims to demonstrate how those features can be used to implement commonly-used algorithms. 20 | 21 | Contributing 22 | ------------ 23 | 24 | Contributions are welcome! Especially new examples that show features that aren't included in the current set. The only requirement is that new examples use a similar basic structure: 25 | - All GL calls are made at the top level (without being split up into "helper functions"). A reader should be able to read through the source HTML and see all GL calls that are made, in the order that they made. 26 | - Shader code should be included at the top of the HTML file in script elements. 27 | - Small utility functions are fine (but shouldn't contain any GL calls!) and should go in [utils/utils.js](https://github.com/tsherif/webgl2examples/blob/master/utils/utils.js). 28 | - Use [glMatrix](https://github.com/tsherif/webgl2examples/blob/master/utils/gl-matrix.js) for the math. 29 | -------------------------------------------------------------------------------- /css/webgl2examples.css: -------------------------------------------------------------------------------- 1 | html { 2 | overflow: hidden; 3 | } 4 | 5 | body { 6 | margin: 0; 7 | } 8 | 9 | #example-title { 10 | position: absolute; 11 | top: 10px; 12 | left: 10px; 13 | color: white; 14 | background-color: gray; 15 | padding: 0.5em; 16 | max-width: 24%; 17 | } 18 | 19 | #example-title header { 20 | font-size: 1.2em; 21 | font-weight: bold; 22 | } 23 | -------------------------------------------------------------------------------- /cube.html: -------------------------------------------------------------------------------- 1 | 2 | 24 | 25 | 26 | WebGL 2 Example: Phong-shaded Cube 27 | 28 | 29 | 30 | 31 | 32 | 33 |
34 |
WebGL 2 Example: Phong-shaded Cube
35 |
36 | Features: Vertex Arrays, Uniform Buffers, Immutable Textures 37 |
38 |
39 | Source code 40 |
41 |
42 | 43 | 71 | 103 | 265 | Fork me on GitHub 266 | 267 | 268 | -------------------------------------------------------------------------------- /deferred.html: -------------------------------------------------------------------------------- 1 | 2 | 24 | 25 | 26 | WebGL 2 Example: Deferred Rendering 27 | 28 | 29 | 30 | 31 | 32 | 33 |
34 |
WebGL 2 Example: Deferred Rendering
35 |
36 | Features: Vertex Arrays, Uniform Buffers, Immutable Textures, Multiple Render Targets, Float Textures, texelFetch, EXT_color_buffer_float 37 |
38 |
39 | Source code 40 |
41 |
42 | 43 | 68 | 86 | 103 | 143 | 535 | 536 | 537 | -------------------------------------------------------------------------------- /dof.html: -------------------------------------------------------------------------------- 1 | 2 | 24 | 27 | 28 | 29 | WebGL 2 Example: Depth of Field 30 | 31 | 32 | 33 | 34 | 35 | 36 |
37 |
WebGL 2 Example: Depth of Field
38 |
39 | Features: Vertex Arrays, Uniform Buffers, Immutable Textures, Depth Textures, texelFetch, Hardware Instancing 40 |
41 |
42 | Source code 43 |
44 |
45 | 46 | 73 | 105 | 114 | 176 | 560 | Fork me on GitHub 561 | 562 | 563 | -------------------------------------------------------------------------------- /img/khronos_webgl.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shrekshao/webgl2examples/50c1a542dd3d2b2fb8da15988a4e62a9414c6f72/img/khronos_webgl.png -------------------------------------------------------------------------------- /occlusion.html: -------------------------------------------------------------------------------- 1 | 2 | 24 | 29 | 30 | 31 | WebGL 2 Example: Occlusion Culling 32 | 33 | 34 | 35 | 36 | 44 | 45 | 46 |
47 |
WebGL 2 Example: Occlusion Culling
48 |
49 | Features: Vertex Arrays, Uniform Buffers, Immutable Textures, Occlusion Query 50 |
51 |
52 | Source code 53 |
54 |
55 | 56 |
57 | Spheres:
58 | Culled spheres:
59 | Enable occlusion culling:
60 | Top-down HUD: 61 |
62 | 90 | 122 | 141 | 153 | 167 | 179 | 622 | Fork me on GitHub 623 | 624 | 625 | -------------------------------------------------------------------------------- /oit.html: -------------------------------------------------------------------------------- 1 | 2 | 24 | 29 | 30 | 31 | WebGL 2 Example: Order-independent Transparency 32 | 33 | 34 | 35 | 36 | 37 | 38 |
39 |
WebGL 2 Example: Order-independent Transparency
40 |
41 | Features: Vertex Arrays, Uniform Buffers, Immutable Textures, Multiple Render Targets, Float Textures, texelFetch, Hardware Instancing, EXT_color_buffer_float 42 |
43 |
44 | Source code 45 |
46 |
47 | 48 | 79 | 127 | 136 | 137 | 152 | 515 | Fork me on GitHub 516 | 517 | 518 | -------------------------------------------------------------------------------- /particles.html: -------------------------------------------------------------------------------- 1 | 2 | 24 | 25 | 26 | WebGL 2 Example: Particles 27 | 28 | 29 | 30 | 31 | 32 |
33 |
WebGL 2 Example: Particles
34 |
35 | Features: Vertex Arrays, Uniform Buffers, Transform Feedback 36 |
37 |
38 | Source code 39 |
40 |
41 | 42 | 90 | 102 | 327 | Fork me on GitHub 328 | 329 | 330 | -------------------------------------------------------------------------------- /ssao.html: -------------------------------------------------------------------------------- 1 | 2 | 24 | 27 | 28 | 29 | WebGL 2 Example: Screen Space Ambient Occlusion 30 | 31 | 32 | 33 | 34 | 42 | 43 | 44 |
45 |
WebGL 2 Example: Screen Space Ambient Occlusion
46 |
47 | Features: Vertex Arrays, Uniform Buffers, Immutable Textures, Multiple Render Targets, Float Textures, texelFetch, Hardware Instancing, EXT_color_buffer_float 48 |
49 |
50 | Source code 51 |
52 |
53 | 54 |
55 | Enable SSAO: 56 |
57 | 90 | 135 | 144 | 145 | 208 | 221 | 233 | 722 | Fork me on GitHub 723 | 724 | 725 | -------------------------------------------------------------------------------- /tests/oit-near-opaque.html: -------------------------------------------------------------------------------- 1 | 2 | 24 | 29 | 30 | 31 | 40 | 41 | 42 | 43 | 65 | 140 | 149 | 150 | 165 | 570 | Fork me on GitHub 571 | 572 | 573 | -------------------------------------------------------------------------------- /tests/sorted-over-near-opaque.html: -------------------------------------------------------------------------------- 1 | 2 | 24 | 29 | 30 | 31 | 40 | 41 | 42 | 43 | 65 | 83 | 92 | 93 | 108 | 505 | Fork me on GitHub 506 | 507 | 508 | -------------------------------------------------------------------------------- /triangle.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | WebGL 2 Example: Just a Triangle 5 | 6 | 7 | 8 | 9 |
10 |
WebGL 2 Example: Just a Triangle
11 |
12 | Features: Vertex Arrays 13 |
14 |
15 | Source code 16 |
17 |
18 | 32 | 43 | 44 | 128 | Fork me on GitHub 129 | 130 | 131 | -------------------------------------------------------------------------------- /utils/utils.js: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////// 2 | // The MIT License (MIT) 3 | // 4 | // Copyright (c) 2017 Tarek Sherif 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | // this software and associated documentation files (the "Software"), to deal in 8 | // the Software without restriction, including without limitation the rights to 9 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | // the Software, and to permit persons to whom the Software is furnished to do so, 11 | // subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | /////////////////////////////////////////////////////////////////////////////////// 23 | 24 | (function() { 25 | var translateMat = mat4.create(); 26 | var rotateXMat = mat4.create(); 27 | var rotateYMat = mat4.create(); 28 | var rotateZMat = mat4.create(); 29 | var scaleMat = mat4.create(); 30 | 31 | var zeros = [0, 0, 0]; 32 | var ones = [1, 1, 1]; 33 | 34 | window.utils = { 35 | xformMatrix: function xformMatrix(xform, translate, rotate, scale) { 36 | translate = translate || zeros; 37 | rotate = rotate || zeros; 38 | scale = scale || ones; 39 | 40 | mat4.fromTranslation(translateMat, translate); 41 | mat4.fromXRotation(rotateXMat, rotate[0]); 42 | mat4.fromYRotation(rotateYMat, rotate[1]); 43 | mat4.fromZRotation(rotateZMat, rotate[2]); 44 | mat4.fromScaling(scaleMat, scale); 45 | 46 | mat4.multiply(xform, rotateXMat, scaleMat); 47 | mat4.multiply(xform, rotateYMat, xform); 48 | mat4.multiply(xform, rotateZMat, xform); 49 | mat4.multiply(xform, translateMat, xform); 50 | }, 51 | 52 | loadCubemapImages: function loadCubeMapImages(urls, ok) { 53 | var numImages = 6; 54 | 55 | var negX = new Image(); 56 | var posX = new Image(); 57 | var negY = new Image(); 58 | var posY = new Image(); 59 | var negZ = new Image(); 60 | var posZ = new Image(); 61 | 62 | function onload() { 63 | if (--numImages === 0) { 64 | ok(negX, posX, negY, posY, negZ, posZ); 65 | } 66 | } 67 | 68 | negX.onload = onload; 69 | posX.onload = onload; 70 | negY.onload = onload; 71 | posY.onload = onload; 72 | negZ.onload = onload; 73 | posZ.onload = onload; 74 | 75 | negX.src = urls.negX; 76 | posX.src = urls.posX; 77 | negY.src = urls.negY; 78 | posY.src = urls.posY; 79 | negZ.src = urls.negZ; 80 | posZ.src = urls.posZ; 81 | }, 82 | 83 | createBox: function createBox(options) { 84 | options = options || {}; 85 | 86 | var dimensions = options.dimensions || [1, 1, 1]; 87 | var position = options.position || [-dimensions[0] / 2, -dimensions[1] / 2, -dimensions[2] / 2]; 88 | var x = position[0]; 89 | var y = position[1]; 90 | var z = position[2]; 91 | var width = dimensions[0]; 92 | var height = dimensions[1]; 93 | var depth = dimensions[2]; 94 | 95 | var fbl = {x: x, y: y, z: z + depth}; 96 | var fbr = {x: x + width, y: y, z: z + depth}; 97 | var ftl = {x: x, y: y + height, z: z + depth}; 98 | var ftr = {x: x + width, y: y + height, z: z + depth}; 99 | var bbl = {x: x, y: y, z: z }; 100 | var bbr = {x: x + width, y: y, z: z }; 101 | var btl = {x: x, y: y + height, z: z }; 102 | var btr = {x: x + width, y: y + height, z: z }; 103 | 104 | var positions = new Float32Array([ 105 | //front 106 | fbl.x, fbl.y, fbl.z, 107 | fbr.x, fbr.y, fbr.z, 108 | ftl.x, ftl.y, ftl.z, 109 | ftl.x, ftl.y, ftl.z, 110 | fbr.x, fbr.y, fbr.z, 111 | ftr.x, ftr.y, ftr.z, 112 | 113 | //right 114 | fbr.x, fbr.y, fbr.z, 115 | bbr.x, bbr.y, bbr.z, 116 | ftr.x, ftr.y, ftr.z, 117 | ftr.x, ftr.y, ftr.z, 118 | bbr.x, bbr.y, bbr.z, 119 | btr.x, btr.y, btr.z, 120 | 121 | //back 122 | fbr.x, bbr.y, bbr.z, 123 | bbl.x, bbl.y, bbl.z, 124 | btr.x, btr.y, btr.z, 125 | btr.x, btr.y, btr.z, 126 | bbl.x, bbl.y, bbl.z, 127 | btl.x, btl.y, btl.z, 128 | 129 | //left 130 | bbl.x, bbl.y, bbl.z, 131 | fbl.x, fbl.y, fbl.z, 132 | btl.x, btl.y, btl.z, 133 | btl.x, btl.y, btl.z, 134 | fbl.x, fbl.y, fbl.z, 135 | ftl.x, ftl.y, ftl.z, 136 | 137 | //top 138 | ftl.x, ftl.y, ftl.z, 139 | ftr.x, ftr.y, ftr.z, 140 | btl.x, btl.y, btl.z, 141 | btl.x, btl.y, btl.z, 142 | ftr.x, ftr.y, ftr.z, 143 | btr.x, btr.y, btr.z, 144 | 145 | //bottom 146 | bbl.x, bbl.y, bbl.z, 147 | bbr.x, bbr.y, bbr.z, 148 | fbl.x, fbl.y, fbl.z, 149 | fbl.x, fbl.y, fbl.z, 150 | bbr.x, bbr.y, bbr.z, 151 | fbr.x, fbr.y, fbr.z, 152 | ]); 153 | 154 | var uvs = new Float32Array([ 155 | //front 156 | 0, 0, 157 | 1, 0, 158 | 0, 1, 159 | 0, 1, 160 | 1, 0, 161 | 1, 1, 162 | 163 | //right 164 | 0, 0, 165 | 1, 0, 166 | 0, 1, 167 | 0, 1, 168 | 1, 0, 169 | 1, 1, 170 | 171 | //back 172 | 0, 0, 173 | 1, 0, 174 | 0, 1, 175 | 0, 1, 176 | 1, 0, 177 | 1, 1, 178 | 179 | //left 180 | 0, 0, 181 | 1, 0, 182 | 0, 1, 183 | 0, 1, 184 | 1, 0, 185 | 1, 1, 186 | 187 | //top 188 | 0, 0, 189 | 1, 0, 190 | 0, 1, 191 | 0, 1, 192 | 1, 0, 193 | 1, 1, 194 | 195 | //bottom 196 | 0, 0, 197 | 1, 0, 198 | 0, 1, 199 | 0, 1, 200 | 1, 0, 201 | 1, 1 202 | ]); 203 | 204 | var normals = new Float32Array(positions.length); 205 | var i, count; 206 | var ni; 207 | 208 | for (i = 0, count = positions.length / 3; i < count; i++) { 209 | ni = i * 3; 210 | 211 | normals[ni] = parseInt(i / 6, 10) === 1 ? 1 : 212 | parseInt(i / 6, 10) === 3 ? -1 : 0; 213 | 214 | normals[ni+1] = parseInt(i / 6, 10) === 4 ? 1 : 215 | parseInt(i / 6, 10) === 5 ? -1 : 0; 216 | 217 | normals[ni+2] = parseInt(i / 6, 10) === 0 ? 1 : 218 | parseInt(i / 6, 10) === 2 ? -1 : 0; 219 | 220 | } 221 | 222 | return { 223 | positions: positions, 224 | normals: normals, 225 | uvs: uvs 226 | }; 227 | 228 | }, 229 | 230 | createSphere: function createSphere(options) { 231 | options = options || {}; 232 | 233 | var long_bands = options.long_bands || 32; 234 | var lat_bands = options.lat_bands || 32; 235 | var radius = options.radius || 1; 236 | var lat_step = Math.PI / lat_bands; 237 | var long_step = 2 * Math.PI / long_bands; 238 | var num_positions = long_bands * lat_bands * 4; 239 | var num_indices = long_bands * lat_bands * 6; 240 | var lat_angle, long_angle; 241 | var positions = new Float32Array(num_positions * 3); 242 | var normals = new Float32Array(num_positions * 3); 243 | var uvs = new Float32Array(num_positions * 2); 244 | var indices = new Uint16Array(num_indices); 245 | var x1, x2, x3, x4, 246 | y1, y2, 247 | z1, z2, z3, z4, 248 | u1, u2, 249 | v1, v2; 250 | var i, j; 251 | var k = 0, l = 0; 252 | var vi, ti; 253 | 254 | for (i = 0; i < lat_bands; i++) { 255 | lat_angle = i * lat_step; 256 | y1 = Math.cos(lat_angle); 257 | y2 = Math.cos(lat_angle + lat_step); 258 | for (j = 0; j < long_bands; j++) { 259 | long_angle = j * long_step; 260 | x1 = Math.sin(lat_angle) * Math.cos(long_angle); 261 | x2 = Math.sin(lat_angle) * Math.cos(long_angle + long_step); 262 | x3 = Math.sin(lat_angle + lat_step) * Math.cos(long_angle); 263 | x4 = Math.sin(lat_angle + lat_step) * Math.cos(long_angle + long_step); 264 | z1 = Math.sin(lat_angle) * Math.sin(long_angle); 265 | z2 = Math.sin(lat_angle) * Math.sin(long_angle + long_step); 266 | z3 = Math.sin(lat_angle + lat_step) * Math.sin(long_angle); 267 | z4 = Math.sin(lat_angle + lat_step) * Math.sin(long_angle + long_step); 268 | u1 = 1 - j / long_bands; 269 | u2 = 1 - (j + 1) / long_bands; 270 | v1 = 1 - i / lat_bands; 271 | v2 = 1 - (i + 1) / lat_bands; 272 | vi = k * 3; 273 | ti = k * 2; 274 | 275 | positions[vi] = x1 * radius; 276 | positions[vi+1] = y1 * radius; 277 | positions[vi+2] = z1 * radius; //v0 278 | 279 | positions[vi+3] = x2 * radius; 280 | positions[vi+4] = y1 * radius; 281 | positions[vi+5] = z2 * radius; //v1 282 | 283 | positions[vi+6] = x3 * radius; 284 | positions[vi+7] = y2 * radius; 285 | positions[vi+8] = z3 * radius; // v2 286 | 287 | 288 | positions[vi+9] = x4 * radius; 289 | positions[vi+10] = y2 * radius; 290 | positions[vi+11] = z4 * radius; // v3 291 | 292 | normals[vi] = x1; 293 | normals[vi+1] = y1; 294 | normals[vi+2] = z1; 295 | 296 | normals[vi+3] = x2; 297 | normals[vi+4] = y1; 298 | normals[vi+5] = z2; 299 | 300 | normals[vi+6] = x3; 301 | normals[vi+7] = y2; 302 | normals[vi+8] = z3; 303 | 304 | normals[vi+9] = x4; 305 | normals[vi+10] = y2; 306 | normals[vi+11] = z4; 307 | 308 | uvs[ti] = u1; 309 | uvs[ti+1] = v1; 310 | 311 | uvs[ti+2] = u2; 312 | uvs[ti+3] = v1; 313 | 314 | uvs[ti+4] = u1; 315 | uvs[ti+5] = v2; 316 | 317 | uvs[ti+6] = u2; 318 | uvs[ti+7] = v2; 319 | 320 | indices[l ] = k; 321 | indices[l + 1] = k + 1; 322 | indices[l + 2] = k + 2; 323 | indices[l + 3] = k + 2; 324 | indices[l + 4] = k + 1; 325 | indices[l + 5] = k + 3; 326 | 327 | k += 4; 328 | l += 6; 329 | } 330 | } 331 | 332 | return { 333 | positions: positions, 334 | normals: normals, 335 | uvs: uvs, 336 | indices: indices 337 | }; 338 | }, 339 | 340 | computeBoundingBox: function (position, options) { 341 | options = options || {}; 342 | var geo = options.geo || false; 343 | 344 | var res = { 345 | min: vec3.create(), 346 | max: vec3.create() 347 | }; 348 | vec3.set(res.min, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY); 349 | vec3.set(res.max, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY); 350 | for ( var i = 0, len = position.length; i < len; i+=3 ) { 351 | res.min[0] = Math.min(position[i], res.min[0]); 352 | res.max[0] = Math.max(position[i], res.max[0]); 353 | res.min[1] = Math.min(position[i], res.min[1]); 354 | res.max[1] = Math.max(position[i], res.max[1]); 355 | res.min[2] = Math.min(position[i], res.min[2]); 356 | res.max[2] = Math.max(position[i], res.max[2]); 357 | } 358 | 359 | if (geo) { 360 | var size = vec3.create(); 361 | vec3.subtract(size, res.max, res.min); 362 | res.geo = utils.createBox({ 363 | position: res.min, 364 | width: size[0], 365 | height: size[1], 366 | depth: size[2] 367 | }); 368 | } 369 | 370 | return res; 371 | } 372 | } 373 | 374 | })(); 375 | --------------------------------------------------------------------------------