Skip to content

Commit

Permalink
First draft of ParticleSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwidlund committed May 9, 2018
1 parent 0907e9e commit 6d2174f
Show file tree
Hide file tree
Showing 9 changed files with 608 additions and 2 deletions.
17 changes: 16 additions & 1 deletion debug/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@
MeshPhongMaterial
MeshNormalMaterial
Light
ParticleSystem
} = require '../form.coffee'

scene = new Scene
width: Screen.width
height: Screen.height
camera:
orbitControls: true
enableRotate: true

ps = new ParticleSystem
parent: scene
color: 0xff4444
particleNoiseTexture: './images/perlin-512.png'
particleSpriteTexture: './images/particle2.png'


###
new Model
path: './models/flamingo/flamingo.json'
Expand Down Expand Up @@ -45,4 +58,6 @@ new Model
curve: 'easeInOutQuart'
scene.onClick ->
scene.camera.stateCycle('first', 'second', 'third')
scene.camera.stateCycle('first', 'second', 'third')
###
Binary file added debug/images/particle2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added debug/images/perlin-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions form.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ require './form/GA.coffee'
{Model} = require './form/Model.coffee'
{Mesh} = require './form/Mesh.coffee'
{Light} = require './form/Light.coffee'
{ParticleSystem} = require './form/ParticleSystem.coffee'

module.exports =
Scene: Scene
Studio: Studio
Model: Model
Mesh: Mesh
Light: Light
ParticleSystem: ParticleSystem

# MATERIALS

Expand Down
83 changes: 83 additions & 0 deletions form/ParticleSystem.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require './lib/GPUParticleSystem.js'

_ = Framer._

{BaseClass} = require './_BaseClass.coffee'
{Animation} = require './_Animation.coffee'
{States} = require './_States.coffee'

class exports.ParticleSystem extends BaseClass
constructor: (properties={}) ->
super()

_.defaults properties,
position: new THREE.Vector3()
positionRandomness: .3
velocity: new THREE.Vector3()
velocityRandomness: .5
color: 0xaa88ff
colorRandomness: .2
turbulence: .5
lifetime: 2
size: 5
sizeRandomness: 1
spawnRate: 15000
horizontalSpeed: 1.5
verticalSpeed: 1.33
timeScale: 1
particleNoiseTexture: './modules/form/lib/textures/perlin-512.png'
particleSpriteTexture: './modules/form/lib/textures/particle2.png'

@properties = properties

@clock = new THREE.Clock
@tick = 0
@textureLoader = new THREE.TextureLoader()

@particleSystem = new THREE.GPUParticleSystem
maxParticles: 250000
particleNoiseTex: @textureLoader.load properties.particleNoiseTexture
particleSpriteTex: @textureLoader.load properties.particleSpriteTexture

@addToRenderingInstance properties.parent

Framer.CurrentContext.on 'reset', =>
cancelAnimationFrame @animationLoopRequestId

@animationLoopRequestId = requestAnimationFrame @loop

addToRenderingInstance: (parent) ->
if parent.scene then parent.scene.add @particleSystem
else parent.add @particleSystem

loop: () =>

@animationLoopRequestId = requestAnimationFrame @loop

delta = @clock.getDelta() * @properties.timeScale
@tick += delta

if @tick < 0
@tick = 0

if delta > 0

@properties.position.x = Math.sin( @tick * @properties.horizontalSpeed ) * 20
@properties.position.y = Math.sin( @tick * @properties.verticalSpeed ) * 10
@properties.position.z = Math.sin( @tick * @properties.horizontalSpeed + @properties.verticalSpeed ) * 5


for [0..@properties.spawnRate * delta]
@particleSystem.spawnParticle
position: @properties.position
positionRandomness: @properties.positionRandomness
velocity: @properties.velocity
velocityRandomness: @properties.velocityRandomness
color: @properties.color
colorRandomness: @properties.colorRandomness
turbulence: @properties.turbulence
lifetime: @properties.lifetime
size: @properties.size
sizeRandomness: @properties.sizeRandomness

@particleSystem.update @tick
Loading

0 comments on commit 6d2174f

Please sign in to comment.