├── .gitignore ├── LICENSE.md ├── README.md ├── demo.js ├── index.html ├── package.json └── shaders ├── index.js ├── logic.frag ├── logic.vert ├── render.frag └── render.vert /.gitignore: -------------------------------------------------------------------------------- 1 | bundle.js 2 | node_modules 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | ## The MIT License (MIT) ## 2 | 3 | Copyright (c) 2013 Hugh Kennedy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all 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, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # particle-excess-demo [](https://flattr.com/submit/auto?user_id=hughskennedy&url=http://github.com/hughsk/particle-excess-demo&title=particle-excess-demo&description=hughsk/particle-excess-demo%20on%20GitHub&language=en_GB&tags=flattr,github,javascript&category=software) # 2 | 3 | Simulating and rendering 262,144 particles with GLSL. 4 | 5 |  6 | 7 | **[view demo](http://hughsk.github.io/particle-excess-demo)** 8 | 9 | ## License ## 10 | 11 | MIT. See [LICENSE.md](http://github.com/hughsk/particle-excess-demo/blob/master/LICENSE.md) for details. 12 | -------------------------------------------------------------------------------- /demo.js: -------------------------------------------------------------------------------- 1 | var createBuffer = require('gl-buffer') 2 | var createShell = require('gl-now') 3 | var createFBO = require('gl-fbo') 4 | var createVAO = require('gl-vao') 5 | 6 | var ndarray = require('ndarray') 7 | var fill = require('ndarray-fill') 8 | 9 | var particleVertices 10 | var screenVertices 11 | var nextState 12 | var prevState 13 | var shaders 14 | 15 | var t = 0 16 | var shell = createShell({ 17 | clearColor: [0,0,0,1] 18 | }) 19 | 20 | shell.on('gl-render', render) 21 | shell.on('gl-init', init) 22 | 23 | function init() { 24 | var gl = shell.gl 25 | 26 | shaders = require('./shaders')(gl) 27 | 28 | nextState = createFBO(gl, 512, 512, { 'float': true }) 29 | prevState = createFBO(gl, 512, 512, { 'float': true }) 30 | 31 | var initialState = ndarray(new Float32Array(512 * 512 * 4), [512, 512, 4]) 32 | fill(initialState, function(x, y, ch) { 33 | if (ch > 2) return 1 34 | return (Math.random() - 0.5) * 800.6125 35 | }) 36 | 37 | nextState.color.setPixels(initialState) 38 | prevState.color.setPixels(initialState) 39 | 40 | screenVertices = createVAO(gl, null, [{ 41 | type: gl.FLOAT 42 | , size: 2 43 | , buffer: createBuffer(gl, new Float32Array([ 44 | -1, -1, +1, -1, -1, +1, 45 | -1, +1, +1, -1, +1, +1, 46 | ])) 47 | }]) 48 | 49 | var index = new Float32Array(512 * 512 * 2) 50 | var i = 0 51 | for (var x = 0; x < 512; x++) 52 | for (var y = 0; y < 512; y++) { 53 | index[i++] = x / 512 54 | index[i++] = y / 512 55 | } 56 | 57 | particleVertices = createVAO(gl, null, [{ 58 | type: gl.FLOAT 59 | , size: 2 60 | , buffer: createBuffer(gl, index) 61 | }]) 62 | } 63 | 64 | var cleared = false 65 | 66 | function render() { 67 | var gl = shell.gl 68 | 69 | // Switch to clean FBO for GPGPU 70 | // particle motion 71 | nextState.bind() 72 | gl.disable(gl.DEPTH_TEST) 73 | gl.viewport(0, 0, 512, 512) 74 | 75 | var shader = shaders.logic 76 | shader.bind() 77 | shader.uniforms.uState = prevState.color.bind(0) 78 | shader.uniforms.uTime = t++ 79 | screenVertices.bind() 80 | gl.drawArrays(gl.TRIANGLES, 0, 6) 81 | 82 | // Reset, draw to screen 83 | gl.bindFramebuffer(gl.FRAMEBUFFER, null) 84 | gl.disable(gl.DEPTH_TEST) 85 | gl.viewport(0, 0, shell.width, shell.height) 86 | 87 | var shader = shaders.render 88 | shader.bind() 89 | shader.uniforms.uState = nextState.color.bind(0) 90 | shader.uniforms.uScreen = [shell.width, shell.height] 91 | 92 | particleVertices.bind() 93 | 94 | // Additive blending! 95 | gl.enable(gl.BLEND) 96 | gl.blendFunc(gl.ONE, gl.ONE) 97 | gl.drawArrays(gl.POINTS, 0, 512 * 512) 98 | gl.disable(gl.BLEND) 99 | 100 | // Switch 101 | var tmp = prevState 102 | prevState = nextState 103 | nextState = tmp 104 | } 105 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |