23_Water.lua 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. -- Water example.
  2. -- This sample demonstrates:
  3. -- - Creating a large plane to represent a water body for rendering
  4. -- - Setting up a second camera to render reflections on the water surface
  5. require "LuaScripts/Utilities/Sample"
  6. local scene_ = nil
  7. local cameraNode = nil
  8. local reflectionCameraNode = nil
  9. local waterNode = nil
  10. local waterPlane = Plane()
  11. local waterClipPlane = Plane()
  12. local yaw = 0.0
  13. local pitch = 0.0
  14. function Start()
  15. -- Execute the common startup for samples
  16. SampleStart()
  17. -- Create the scene content
  18. CreateScene()
  19. -- Create the UI content
  20. CreateInstructions()
  21. -- Setup the viewport for displaying the scene
  22. SetupViewport()
  23. -- Hook up to the frame update event
  24. SubscribeToEvents()
  25. end
  26. function CreateScene()
  27. scene_ = Scene()
  28. -- Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  29. scene_:CreateComponent("Octree")
  30. -- Create a Zone component for ambient lighting & fog control
  31. local zoneNode = scene_:CreateChild("Zone")
  32. local zone = zoneNode:CreateComponent("Zone")
  33. zone.boundingBox = BoundingBox(-1000.0, 1000.0)
  34. zone.ambientColor = Color(0.15, 0.15, 0.15)
  35. zone.fogColor = Color(1.0, 1.0, 1.0)
  36. zone.fogStart = 500.0
  37. zone.fogEnd = 750.0
  38. -- Create a directional light to the world. Enable cascaded shadows on it
  39. local lightNode = scene_:CreateChild("DirectionalLight")
  40. lightNode.direction = Vector3(0.6, -1.0, 0.8)
  41. local light = lightNode:CreateComponent("Light")
  42. light.lightType = LIGHT_DIRECTIONAL
  43. light.castShadows = true
  44. light.shadowBias = BiasParameters(0.00025, 0.5)
  45. light.shadowCascade = CascadeParameters(10.0, 50.0, 200.0, 0.0, 0.8)
  46. light.specularIntensity = 0.5;
  47. -- Apply slightly overbright lighting to match the skybox
  48. light.color = Color(1.2, 1.2, 1.2);
  49. -- Create skybox. The Skybox component is used like StaticModel, but it will be always located at the camera, giving the
  50. -- illusion of the box planes being far away. Use just the ordinary Box model and a suitable material, whose shader will
  51. -- generate the necessary 3D texture coordinates for cube mapping
  52. local skyNode = scene_:CreateChild("Sky")
  53. skyNode:SetScale(500.0) -- The scale actually does not matter
  54. local skybox = skyNode:CreateComponent("Skybox")
  55. skybox.model = cache:GetResource("Model", "Models/Box.mdl")
  56. skybox.material = cache:GetResource("Material", "Materials/Skybox.xml")
  57. -- Create heightmap terrain
  58. local terrainNode = scene_:CreateChild("Terrain")
  59. terrainNode.position = Vector3(0.0, 0.0, 0.0)
  60. local terrain = terrainNode:CreateComponent("Terrain")
  61. terrain.patchSize = 64
  62. terrain.spacing = Vector3(2.0, 0.5, 2.0) -- Spacing between vertices and vertical resolution of the height map
  63. terrain.smoothing = true
  64. terrain.heightMap = cache:GetResource("Image", "Textures/HeightMap.png")
  65. terrain.material = cache:GetResource("Material", "Materials/Terrain.xml")
  66. -- The terrain consists of large triangles, which fits well for occlusion rendering, as a hill can occlude all
  67. -- terrain patches and other objects behind it
  68. terrain.occluder = true
  69. -- Create 1000 boxes in the terrain. Always face outward along the terrain normal
  70. local NUM_OBJECTS = 1000
  71. for i = 1, NUM_OBJECTS do
  72. local objectNode = scene_:CreateChild("Box")
  73. local position = Vector3(Random(2000.0) - 1000.0, 0.0, Random(2000.0) - 1000.0)
  74. position.y = terrain:GetHeight(position) + 2.25
  75. objectNode.position = position
  76. -- Create a rotation quaternion from up vector to terrain normal
  77. objectNode.rotation = Quaternion(Vector3(0.0, 1.0, 0.0), terrain:GetNormal(position))
  78. objectNode:SetScale(5.0)
  79. local object = objectNode:CreateComponent("StaticModel")
  80. object.model = cache:GetResource("Model", "Models/Box.mdl")
  81. object.material = cache:GetResource("Material", "Materials/Stone.xml")
  82. object.castShadows = true
  83. end
  84. -- Create a water plane object that is as large as the terrain
  85. waterNode = scene_:CreateChild("Water")
  86. waterNode.scale = Vector3(2048.0, 1.0, 2048.0)
  87. waterNode.position = Vector3(0.0, 5.0, 0.0)
  88. local water = waterNode:CreateComponent("StaticModel")
  89. water.model = cache:GetResource("Model", "Models/Plane.mdl")
  90. water.material = cache:GetResource("Material", "Materials/Water.xml")
  91. -- Set a different viewmask on the water plane to be able to hide it from the reflection camera
  92. water.viewMask = 0x80000000
  93. -- Create the camera. Set far clip to match the fog. Note: now we actually create the camera node outside
  94. -- the scene, because we want it to be unaffected by scene load / save
  95. cameraNode = Node()
  96. local camera = cameraNode:CreateComponent("Camera")
  97. camera.farClip = 750.0
  98. -- Set an initial position for the camera scene node above the floor
  99. cameraNode.position = Vector3(0.0, 7.0, -20.0)
  100. end
  101. function CreateInstructions()
  102. -- Construct new Text object, set string to display and font to use
  103. local instructionText = ui.root:CreateChild("Text")
  104. instructionText:SetText("Use WASD keys and mouse to move")
  105. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  106. instructionText.textAlignment = HA_CENTER
  107. -- Position the text relative to the screen center
  108. instructionText.horizontalAlignment = HA_CENTER
  109. instructionText.verticalAlignment = VA_CENTER
  110. instructionText:SetPosition(0, ui.root.height / 4)
  111. end
  112. function SetupViewport()
  113. -- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  114. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  115. renderer:SetViewport(0, viewport)
  116. -- Create a mathematical plane to represent the water in calculations
  117. waterPlane = Plane(waterNode.worldRotation * Vector3(0.0, 1.0, 0.0), waterNode.worldPosition)
  118. -- Create a downward biased plane for reflection view clipping. Biasing is necessary to avoid too aggressive clipping
  119. waterClipPlane = Plane(waterNode.worldRotation * Vector3(0.0, 1.0, 0.0), waterNode.worldPosition -
  120. Vector3(0.0, 0.1, 0.0))
  121. -- Create camera for water reflection
  122. -- It will have the same farclip and position as the main viewport camera, but uses a reflection plane to modify
  123. -- its position when rendering
  124. reflectionCameraNode = cameraNode:CreateChild()
  125. local reflectionCamera = reflectionCameraNode:CreateComponent("Camera")
  126. reflectionCamera.farClip = 750.0
  127. reflectionCamera.viewMask = 0x7fffffff -- Hide objects with only bit 31 in the viewmask (the water plane)
  128. reflectionCamera.autoAspectRatio = false
  129. reflectionCamera.useReflection = true
  130. reflectionCamera.reflectionPlane = waterPlane
  131. reflectionCamera.useClipping = true -- Enable clipping of geometry behind water plane
  132. reflectionCamera.clipPlane = waterClipPlane
  133. -- The water reflection texture is rectangular. Set reflection camera aspect ratio to match
  134. reflectionCamera.aspectRatio = graphics.width / graphics.height
  135. -- View override flags could be used to optimize reflection rendering. For example disable shadows
  136. --reflectionCamera.viewOverrideFlags = VO_DISABLE_SHADOWS
  137. -- Create a texture and setup viewport for water reflection. Assign the reflection texture to the diffuse
  138. -- texture unit of the water material
  139. local texSize = 1024
  140. local renderTexture = Texture2D:new()
  141. renderTexture:SetSize(texSize, texSize, Graphics:GetRGBFormat(), TEXTURE_RENDERTARGET)
  142. renderTexture.filterMode = FILTER_BILINEAR
  143. local surface = renderTexture.renderSurface
  144. local rttViewport = Viewport:new(scene_, reflectionCamera)
  145. surface:SetViewport(0, rttViewport)
  146. local waterMat = cache:GetResource("Material", "Materials/Water.xml")
  147. waterMat:SetTexture(TU_DIFFUSE, renderTexture)
  148. end
  149. function SubscribeToEvents()
  150. -- Subscribe HandleUpdate() function for processing update events
  151. SubscribeToEvent("Update", "HandleUpdate")
  152. end
  153. function MoveCamera(timeStep)
  154. -- Do not move if the UI has a focused element (the console)
  155. if ui.focusElement ~= nil then
  156. return
  157. end
  158. -- Movement speed as world units per second
  159. local MOVE_SPEED = 20.0
  160. -- Mouse sensitivity as degrees per pixel
  161. local MOUSE_SENSITIVITY = 0.1
  162. -- Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  163. local mouseMove = input.mouseMove
  164. yaw = yaw + MOUSE_SENSITIVITY * mouseMove.x
  165. pitch = pitch + MOUSE_SENSITIVITY * mouseMove.y
  166. pitch = Clamp(pitch, -90.0, 90.0)
  167. -- Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  168. cameraNode.rotation = Quaternion(pitch, yaw, 0.0)
  169. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  170. if input:GetKeyDown(KEY_W) then
  171. cameraNode:TranslateRelative(Vector3(0.0, 0.0, 1.0) * MOVE_SPEED * timeStep)
  172. end
  173. if input:GetKeyDown(KEY_S) then
  174. cameraNode:TranslateRelative(Vector3(0.0, 0.0, -1.0) * MOVE_SPEED * timeStep)
  175. end
  176. if input:GetKeyDown(KEY_A) then
  177. cameraNode:TranslateRelative(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  178. end
  179. if input:GetKeyDown(KEY_D) then
  180. cameraNode:TranslateRelative(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  181. end
  182. -- In case resolution has changed, adjust the reflection camera aspect ratio
  183. local reflectionCamera = reflectionCameraNode:GetComponent("Camera")
  184. reflectionCamera.aspectRatio = graphics.width / graphics.height
  185. end
  186. function HandleUpdate(eventType, eventData)
  187. -- Take the frame time step, which is stored as a float
  188. local timeStep = eventData:GetFloat("TimeStep")
  189. -- Move the camera, scale movement with time step
  190. MoveCamera(timeStep)
  191. end