23_Water.lua 9.6 KB

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