Sample.lua 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. -- Common sample initialization as a framework for all samples.
  2. -- - Create Urho3D logo at screen
  3. -- - Set custom window title and icon
  4. -- - Create Console and Debug HUD, and use F1 and F2 key to toggle them
  5. -- - Toggle rendering options from the keys 1-8
  6. -- - Take screenshots with key 9
  7. -- - Handle Esc key down to hide Console or exit application
  8. -- - Init touch input on mobile platform using screen joysticks (patched for each individual sample)
  9. local logoSprite = nil
  10. scene_ = nil -- Scene
  11. screenJoystickIndex = M_MAX_UNSIGNED -- Screen joystick index for navigational controls (mobile platforms only)
  12. screenJoystickSettingsIndex = M_MAX_UNSIGNED -- Screen joystick index for settings (mobile platforms only)
  13. touchEnabled = false -- Flag to indicate whether touch input has been enabled
  14. paused = false -- Pause flag
  15. drawDebug = false -- Draw debug geometry flag
  16. cameraNode = nil -- Camera scene node
  17. yaw = 0 -- Camera yaw angle
  18. pitch = 0 -- Camera pitch angle
  19. TOUCH_SENSITIVITY = 2
  20. debugHudMode = 0
  21. function SampleStart()
  22. if GetPlatform() == "Android" or GetPlatform() == "iOS" or input.touchEmulation then
  23. -- On mobile platform, enable touch by adding a screen joystick
  24. InitTouchInput()
  25. elseif input:GetNumJoysticks() == 0 then
  26. -- On desktop platform, do not detect touch when we already got a joystick
  27. SubscribeToEvent("TouchBegin", "HandleTouchBegin")
  28. end
  29. -- Create logo
  30. CreateLogo()
  31. -- Set custom window Title & Icon
  32. SetWindowTitleAndIcon()
  33. -- Create console and debug HUD
  34. CreateConsoleAndDebugHud()
  35. -- Subscribe key down event
  36. SubscribeToEvent("KeyDown", "HandleKeyDown")
  37. -- Subscribe scene update event
  38. SubscribeToEvent("SceneUpdate", "HandleSceneUpdate")
  39. end
  40. function InitTouchInput()
  41. touchEnabled = true
  42. local layout = cache:GetResource("XMLFile", "UI/ScreenJoystick_Samples.xml")
  43. local patchString = GetScreenJoystickPatchString()
  44. if patchString ~= "" then
  45. -- Patch the screen joystick layout further on demand
  46. local patchFile = XMLFile()
  47. if patchFile:FromString(patchString) then layout:Patch(patchFile) end
  48. end
  49. screenJoystickIndex = input:AddScreenJoystick(layout, cache:GetResource("XMLFile", "UI/DefaultStyle.xml"))
  50. input:SetScreenJoystickVisible(screenJoystickSettingsIndex, true)
  51. end
  52. function SetLogoVisible(enable)
  53. if logoSprite ~= nil then
  54. logoSprite.visible = enable
  55. end
  56. end
  57. function CreateLogo()
  58. -- Get logo texture
  59. local logoTexture = cache:GetResource("Texture2D", "Textures/LogoLarge.png")
  60. if logoTexture == nil then
  61. return
  62. end
  63. -- Create logo sprite and add to the UI layout
  64. logoSprite = ui.root:CreateChild("Sprite")
  65. -- Set logo sprite texture
  66. logoSprite:SetTexture(logoTexture)
  67. local textureWidth = logoTexture.width
  68. local textureHeight = logoTexture.height
  69. -- Set logo sprite scale
  70. logoSprite:SetScale(256 / textureWidth)
  71. -- Set logo sprite size
  72. logoSprite:SetSize(textureWidth, textureHeight)
  73. -- Set logo sprite hot spot
  74. logoSprite.hotSpot = IntVector2(0, textureHeight)
  75. -- Set logo sprite alignment
  76. logoSprite:SetAlignment(HA_LEFT, VA_BOTTOM);
  77. -- Make logo not fully opaque to show the scene underneath
  78. logoSprite.opacity = 0.75
  79. -- Set a low priority for the logo so that other UI elements can be drawn on top
  80. logoSprite.priority = -100
  81. end
  82. function SetWindowTitleAndIcon()
  83. local icon = cache:GetResource("Image", "Textures/UrhoIcon.png")
  84. graphics:SetWindowIcon(icon)
  85. graphics.windowTitle = "Urho3D Sample"
  86. end
  87. function CreateConsoleAndDebugHud()
  88. -- Get default style
  89. local uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  90. if uiStyle == nil then
  91. return
  92. end
  93. -- Create console
  94. engine:CreateConsole()
  95. console.defaultStyle = uiStyle
  96. console.background.opacity = 0.8
  97. -- Create debug HUD
  98. engine:CreateDebugHud()
  99. debugHud.defaultStyle = uiStyle
  100. end
  101. function HandleKeyDown(eventType, eventData)
  102. local key = eventData["Key"]:GetInt()
  103. -- Close console (if open) or exit when ESC is pressed
  104. if key == KEY_ESC then
  105. if not console:IsVisible() then
  106. engine:Exit()
  107. else
  108. console:SetVisible(false)
  109. end
  110. elseif key == KEY_F1 then
  111. console:Toggle()
  112. elseif key == KEY_F2 then
  113. if debugHud:GetMode() == DEBUGHUD_SHOW_ALL_MEMORY or debugHud:GetMode() == 0 then
  114. debugHud:SetMode(DEBUGHUD_SHOW_ALL)
  115. else
  116. debugHud:SetMode(0)
  117. end
  118. elseif key == KEY_F3 then
  119. if debugHud:GetMode() == DEBUGHUD_SHOW_ALL or debugHud:GetMode() == 0 then
  120. debugHud:SetMode(DEBUGHUD_SHOW_ALL_MEMORY)
  121. else
  122. debugHud:SetMode(0)
  123. end
  124. end
  125. if ui.focusElement == nil then
  126. -- Preferences / Pause
  127. if key == KEY_SELECT and touchEnabled then
  128. paused = not paused
  129. if screenJoystickSettingsIndex == M_MAX_UNSIGNED then
  130. -- Lazy initialization
  131. screenJoystickSettingsIndex = input:AddScreenJoystick(cache:GetResource("XMLFile", "UI/ScreenJoystickSettings_Samples.xml"), cache:GetResource("XMLFile", "UI/DefaultStyle.xml"))
  132. else
  133. input:SetScreenJoystickVisible(screenJoystickSettingsIndex, paused)
  134. end
  135. -- Texture quality
  136. elseif key == KEY_1 then
  137. local quality = renderer.textureQuality
  138. quality = quality + 1
  139. if quality > QUALITY_HIGH then
  140. quality = QUALITY_LOW
  141. end
  142. renderer.textureQuality = quality
  143. -- Material quality
  144. elseif key == KEY_2 then
  145. local quality = renderer.materialQuality
  146. quality = quality + 1
  147. if quality > QUALITY_HIGH then
  148. quality = QUALITY_LOW
  149. end
  150. renderer.materialQuality = quality
  151. -- Specular lighting
  152. elseif key == KEY_3 then
  153. renderer.specularLighting = not renderer.specularLighting
  154. -- Shadow rendering
  155. elseif key == KEY_4 then
  156. renderer.drawShadows = not renderer.drawShadows
  157. -- Shadow map resolution
  158. elseif key == KEY_5 then
  159. local shadowMapSize = renderer.shadowMapSize
  160. shadowMapSize = shadowMapSize * 2
  161. if shadowMapSize > 2048 then
  162. shadowMapSize = 512
  163. end
  164. renderer.shadowMapSize = shadowMapSize
  165. -- Shadow depth and filtering quality
  166. elseif key == KEY_6 then
  167. local quality = renderer.shadowQuality
  168. quality = quality + 1
  169. if quality > SHADOWQUALITY_BLUR_VSM then
  170. quality = SHADOWQUALITY_SIMPLE_16BIT
  171. end
  172. renderer.shadowQuality = quality
  173. -- Occlusion culling
  174. elseif key == KEY_7 then
  175. local occlusion = renderer.maxOccluderTriangles > 0
  176. occlusion = not occlusion
  177. if occlusion then
  178. renderer.maxOccluderTriangles = 5000
  179. else
  180. renderer.maxOccluderTriangles = 0
  181. end
  182. -- Instancing
  183. elseif key == KEY_8 then
  184. renderer.dynamicInstancing = not renderer.dynamicInstancing
  185. -- Take screenshot
  186. elseif key == KEY_9 then
  187. local screenshot = Image()
  188. graphics:TakeScreenShot(screenshot)
  189. local timeStamp = Time:GetTimeStamp()
  190. timeStamp = string.gsub(timeStamp, "[:. ]", "_")
  191. -- Here we save in the Data folder with date and time appended
  192. screenshot:SavePNG(fileSystem:GetProgramDir() .. "Data/Screenshot_" .. timeStamp .. ".png")
  193. end
  194. end
  195. end
  196. function HandleSceneUpdate(eventType, eventData)
  197. -- Move the camera by touch, if the camera node is initialized by descendant sample class
  198. if touchEnabled and cameraNode then
  199. for i=0, input:GetNumTouches()-1 do
  200. local state = input:GetTouch(i)
  201. if not state.touchedElement then -- Touch on empty space
  202. if state.delta.x or state.delta.y then
  203. local camera = cameraNode:GetComponent("Camera")
  204. if not camera then return end
  205. yaw = yaw + TOUCH_SENSITIVITY * camera.fov / graphics.height * state.delta.x
  206. pitch = pitch + TOUCH_SENSITIVITY * camera.fov / graphics.height * state.delta.y
  207. -- Construct new orientation for the camera scene node from yaw and pitch; roll is fixed to zero
  208. cameraNode:SetRotation(Quaternion(pitch, yaw, 0))
  209. else
  210. -- Move the cursor to the touch position
  211. local cursor = ui:GetCursor()
  212. if cursor and cursor:IsVisible() then cursor:SetPosition(state.position) end
  213. end
  214. end
  215. end
  216. end
  217. end
  218. function HandleTouchBegin(eventType, eventData)
  219. -- On some platforms like Windows the presence of touch input can only be detected dynamically
  220. InitTouchInput()
  221. UnsubscribeFromEvent("TouchBegin")
  222. end
  223. -- Create empty XML patch instructions for screen joystick layout if none defined
  224. function GetScreenJoystickPatchString()
  225. return ""
  226. end