35_SignedDistanceFieldText.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. -- Signed distance field text example.
  2. -- This sample demonstrates:
  3. -- - Creating a 3D scene with static content
  4. -- - Creating a 3D text use SDF Font
  5. -- - Displaying the scene using the Renderer subsystem
  6. -- - Handling keyboard and mouse input to move a freelook camera
  7. require "LuaScripts/Utilities/Sample"
  8. function Start()
  9. -- Execute the common startup for samples
  10. SampleStart()
  11. -- Create the scene content
  12. CreateScene()
  13. -- Create the UI content
  14. CreateInstructions()
  15. -- Setup the viewport for displaying the scene
  16. SetupViewport()
  17. -- Hook up to the frame update events
  18. SubscribeToEvents()
  19. end
  20. function CreateScene()
  21. scene_ = Scene()
  22. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  23. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  24. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  25. -- optimizing manner
  26. scene_:CreateComponent("Octree")
  27. -- Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
  28. -- plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
  29. -- (100 x 100 world units)
  30. local planeNode = scene_:CreateChild("Plane")
  31. planeNode.scale = Vector3(100.0, 1.0, 100.0)
  32. local planeObject = planeNode:CreateComponent("StaticModel")
  33. planeObject.model = cache:GetResource("Model", "Models/Plane.mdl")
  34. planeObject.material = cache:GetResource("Material", "Materials/StoneTiled.xml")
  35. -- Create a directional light to the world so that we can see something. The light scene node's orientation controls the
  36. -- light direction we will use the SetDirection() function which calculates the orientation from a forward direction vector.
  37. -- The light will use default settings (white light, no shadows)
  38. local lightNode = scene_:CreateChild("DirectionalLight")
  39. lightNode.direction = Vector3(0.6, -1.0, 0.8) -- The direction vector does not need to be normalized
  40. local light = lightNode:CreateComponent("Light")
  41. light.lightType = LIGHT_DIRECTIONAL
  42. -- Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a
  43. -- quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains
  44. -- LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll
  45. -- see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the
  46. -- same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the
  47. -- scene.
  48. local NUM_OBJECTS = 200
  49. for i = 1, NUM_OBJECTS do
  50. local mushroomNode = scene_:CreateChild("Mushroom")
  51. mushroomNode.position = Vector3(Random(90.0) - 45.0, 0.0, Random(90.0) - 45.0)
  52. mushroomNode:SetScale(0.5 + Random(2.0))
  53. local mushroomObject = mushroomNode:CreateComponent("StaticModel")
  54. mushroomObject.model = cache:GetResource("Model", "Models/Mushroom.mdl")
  55. mushroomObject.material = cache:GetResource("Material", "Materials/Mushroom.xml")
  56. local mushroomTitleNode = mushroomNode:CreateChild("MushroomTitle")
  57. mushroomTitleNode.position = Vector3(0.0, 1.2, 0.0)
  58. local mushroomTitleText = mushroomTitleNode:CreateComponent("Text3D")
  59. mushroomTitleText.text = "Mushroom " .. i
  60. mushroomTitleText:SetFont(cache:GetResource("Font", "Fonts/BlueHighway.sdf"), 24)
  61. mushroomTitleText.color = Color(1.0, 0.0, 0.0)
  62. if (i % 3) == 1 then
  63. mushroomTitleText.color = Color(0.0, 1.0, 0.0)
  64. mushroomTitleText.textEffect = TE_SHADOW
  65. mushroomTitleText.effectColor = Color(0.5, 0.5, 0.5)
  66. end
  67. if (i % 3) == 2 then
  68. mushroomTitleText.color = Color(1.0, 1.0, 0.0)
  69. mushroomTitleText.textEffect = TE_STROKE
  70. mushroomTitleText.effectColor = Color(0.5, 0.5, 0.5)
  71. end
  72. mushroomTitleText:SetAlignment(HA_CENTER, VA_CENTER)
  73. end
  74. -- Create a scene node for the camera, which we will move around
  75. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  76. cameraNode = scene_:CreateChild("Camera")
  77. cameraNode:CreateComponent("Camera")
  78. -- Set an initial position for the camera scene node above the plane
  79. cameraNode.position = Vector3(0.0, 5.0, 0.0)
  80. end
  81. function CreateInstructions()
  82. -- Construct new Text object, set string to display and font to use
  83. local instructionText = ui.root:CreateChild("Text")
  84. instructionText:SetText("Use WASD keys and mouse to move")
  85. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  86. -- Position the text relative to the screen center
  87. instructionText.horizontalAlignment = HA_CENTER
  88. instructionText.verticalAlignment = VA_CENTER
  89. instructionText:SetPosition(0, ui.root.height / 4)
  90. end
  91. function SetupViewport()
  92. -- 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
  93. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  94. -- use, but now we just use full screen and default render path configured in the engine command line options
  95. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  96. renderer:SetViewport(0, viewport)
  97. end
  98. function MoveCamera(timeStep)
  99. -- Do not move if the UI has a focused element (the console)
  100. if ui.focusElement ~= nil then
  101. return
  102. end
  103. -- Movement speed as world units per second
  104. local MOVE_SPEED = 20.0
  105. -- Mouse sensitivity as degrees per pixel
  106. local MOUSE_SENSITIVITY = 0.1
  107. -- Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  108. local mouseMove = input.mouseMove
  109. yaw = yaw +MOUSE_SENSITIVITY * mouseMove.x
  110. pitch = pitch + MOUSE_SENSITIVITY * mouseMove.y
  111. pitch = Clamp(pitch, -90.0, 90.0)
  112. -- Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  113. cameraNode.rotation = Quaternion(pitch, yaw, 0.0)
  114. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  115. -- Use the Translate() function (default local space) to move relative to the node's orientation.
  116. if input:GetKeyDown(KEY_W) then
  117. cameraNode:Translate(Vector3(0.0, 0.0, 1.0) * MOVE_SPEED * timeStep)
  118. end
  119. if input:GetKeyDown(KEY_S) then
  120. cameraNode:Translate(Vector3(0.0, 0.0, -1.0) * MOVE_SPEED * timeStep)
  121. end
  122. if input:GetKeyDown(KEY_A) then
  123. cameraNode:Translate(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  124. end
  125. if input:GetKeyDown(KEY_D) then
  126. cameraNode:Translate(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  127. end
  128. end
  129. function SubscribeToEvents()
  130. -- Subscribe HandleUpdate() function for processing update events
  131. SubscribeToEvent("Update", "HandleUpdate")
  132. end
  133. function HandleUpdate(eventType, eventData)
  134. -- Take the frame time step, which is stored as a float
  135. local timeStep = eventData:GetFloat("TimeStep")
  136. -- Move the camera, scale movement with time step
  137. MoveCamera(timeStep)
  138. end