25_Urho2DParticle.lua 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. -- Urho2D particle example.
  2. -- This sample demonstrates:
  3. -- - Creating a 2D scene with particle
  4. -- - Displaying the scene using the Renderer subsystem
  5. -- - Handling mouse move to move particle
  6. require "LuaScripts/Utilities/Sample"
  7. local scene_ = nil
  8. local cameraNode = nil
  9. local particleNode = nil
  10. function Start()
  11. -- Execute the common startup for samples
  12. SampleStart()
  13. -- Set mouse visible
  14. input.mouseVisible = true
  15. -- Create the scene content
  16. CreateScene()
  17. -- Create the UI content
  18. CreateInstructions()
  19. -- Setup the viewport for displaying the scene
  20. SetupViewport()
  21. -- Hook up to the frame update events
  22. SubscribeToEvents()
  23. end
  24. function CreateScene()
  25. scene_ = Scene()
  26. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  27. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  28. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  29. -- optimizing manner
  30. scene_:CreateComponent("Octree")
  31. -- Create a scene node for the camera, which we will move around
  32. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  33. cameraNode = scene_:CreateChild("Camera")
  34. -- Set an initial position for the camera scene node above the plane
  35. cameraNode.position = Vector3(0.0, 0.0, -10.0)
  36. local camera = cameraNode:CreateComponent("Camera")
  37. camera.orthographic = true
  38. camera:SetOrthoSize(Vector2(graphics.width, graphics.height) / scene_.pixelsPerUnit)
  39. local particleModel = cache:GetResource("ParticleModel2D", "Urho2D/LavaFlow.plist")
  40. if particleModel == nil then
  41. return
  42. end
  43. particleNode = scene_:CreateChild("ParticleEmitter2D")
  44. local particleEmitter = particleNode:CreateComponent("ParticleEmitter2D")
  45. particleEmitter.model = particleModel
  46. end
  47. function CreateInstructions()
  48. -- Construct new Text object, set string to display and font to use
  49. local instructionText = ui.root:CreateChild("Text")
  50. instructionText:SetText("Use mouse to move the particle.")
  51. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  52. -- Position the text relative to the screen center
  53. instructionText.horizontalAlignment = HA_CENTER
  54. instructionText.verticalAlignment = VA_CENTER
  55. instructionText:SetPosition(0, ui.root.height / 4)
  56. end
  57. function SetupViewport()
  58. -- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  59. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  60. -- use, but now we just use full screen and default render path configured in the engine command line options
  61. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  62. renderer:SetViewport(0, viewport)
  63. end
  64. function SubscribeToEvents()
  65. SubscribeToEvent("MouseMove", "HandleMouseMove")
  66. end
  67. function HandleMouseMove(eventType, eventData)
  68. if particleNode ~= nil then
  69. local x = eventData:GetInt("x")
  70. local y = eventData:GetInt("y")
  71. local camera = cameraNode:GetComponent("Camera")
  72. particleNode.position = camera:ScreenToWorldPoint(Vector3(x / graphics.width, y / graphics.height, 10.0))
  73. end
  74. end