require "luascape.framework"
require "luascape.physics"
require "materials"

function init(system, crowd, levelRenderPass)
  system.maxParticleCount = 20
  system.output = 20
  system.velocity = Vector(1,0,1)
  system.velocityRange = Vector(0.5,0.5,0.5)
  system.gravity = Vector(0,0,0)
  system.lifetime = 1
  system.rotationSpeed = 3.1465 / 4
  system.alphaInFading = true
  system.alphaOutFading = true
  system:setRotation(-3.1465, 3.1465)

  local shader = crowd.material
  local shaderProgramState = shader:getOrCreateShaderProgramState()
  shaderProgramState.vertexProgram = ShaderProgram.loadShaderProgram("clouds_vp.cg", ShaderProgram.TYPE_VERTEX_PROGRAM)
  shaderProgramState.fragmentProgram = ShaderProgram.loadShaderProgram("clouds_fp.cg", ShaderProgram.TYPE_FRAGMENT_PROGRAM)
  local depthScaleUniform = shaderProgramState.fragmentUniforms:add(FloatUniform(3, "depthScale"))
  local fadeDepthUniform = shaderProgramState.fragmentUniforms:add(FloatUniform(0.05, "fadeDepth"))
  local contrastPowerUniform = shaderProgramState.fragmentUniforms:add(FloatUniform(2, "contrastPower"))
  
  InputManager.instance().bindings:add(UniformInputBinding(KeyboardState.KEY_U, KeyboardState.KEY_I, 0.1, depthScaleUniform))
  InputManager.instance().bindings:add(UniformInputBinding(KeyboardState.KEY_J, KeyboardState.KEY_K, 0.01, fadeDepthUniform))
  
  local alphaTestState = shader:getOrCreateAlphaTestState()
  alphaTestState["function"] = AlphaTestState.COMPARE_GREATER
  alphaTestState.reference = 0.025
  alphaTestState.enabled = true
  
  local textureState = shader:getOrCreateTextureState()
  local depthBuffer = materials.getDepthBuffer(levelRenderPass)
  textureState:setTexture(1, depthBuffer)
  textureState:setFilter(1, TextureUnit.FILTER_NEAREST)
end
