   #controls.addEventListener 'change', render
 
   curves = new FlyingCurves
-    
-  mesh = curves.group
-  scene.add mesh
+
+  lines = new THREE.Object3D
+  lines.add curves.group
+  scene.add lines
   
   #
   renderer = new THREE.WebGLRenderer(antialias: false)

camera = undefined
scene = undefined
renderer = undefined
mesh = undefined
controls = undefined

palette = [0xfe8f0f, 0xf7325e, 0x7dc410, 0xfef8cf, 0x0264ed]

    @setDirty()

class FlyingCurves
  constructor: ()->
    pattern = parseRle "$3b2o$2bobob2o$2bo5bo$7b2o$b2o$bo5bo$2b2obobo$5b2oo"
    simulator = new Simulator 64,64 #field size
    patW = Math.max( (xy[0] for xy in pattern) ... )
    patH = Math.max( (xy[1] for xy in pattern) ... )
    #Offset to put pattern to the center
    cx = ((simulator.width - patW)/2) & ~1
    cy = ((simulator.height - patH)/2) & ~1
    console.log "Putting pattern at #{cx}, #{cy}"
    simulator.put pattern, cx, cy #pattern roughly at the center
    
    order = 3
    interpSteps = 1
    smoothing = 4
    @isim = new CircularInterpolatingSimulator simulator, order, interpSteps, smoothing

    #Create geometry
    @segments = 500
    scale = 30
    state = @isim.getInterpolatedState()
    z0 = -1400/scale
    z1 = 400/scale
    
    @group = new THREE.Object3D

    @lines = for i in [0...state.length] by 2
      line = new FlowingLine @segments, palette[ (i/2) % palette.length ]
      line.initial state[i], state[i+1], z0, z1
      @group.add line.mesh
      line
      
    @group.scale.set scale, scale, scale
    @group.position.set -0.5*simulator.width*scale, -0.5*simulator.height*scale, 0
    @group.updateMatrix()
    console.log "Created #{@lines.length} lines"
  step: ->
    @isim.nextTime 1
    state = @isim.getInterpolatedState()
    for line, i in @lines
      i2 = i*2
      line.flow state[i2], state[i2+1]
    return
      
curves = undefined    
