Sample.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. useMouseMode_ = MM_ABSOLUTE
  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 key up event
  38. SubscribeToEvent("KeyUp", "HandleKeyUp")
  39. -- Subscribe scene update event
  40. SubscribeToEvent("SceneUpdate", "HandleSceneUpdate")
  41. end
  42. function InitTouchInput()
  43. touchEnabled = true
  44. local layout = cache:GetResource("XMLFile", "UI/ScreenJoystick_Samples.xml")
  45. local patchString = GetScreenJoystickPatchString()
  46. if patchString ~= "" then
  47. -- Patch the screen joystick layout further on demand
  48. local patchFile = XMLFile()
  49. if patchFile:FromString(patchString) then layout:Patch(patchFile) end
  50. end
  51. screenJoystickIndex = input:AddScreenJoystick(layout, cache:GetResource("XMLFile", "UI/DefaultStyle.xml"))
  52. input:SetScreenJoystickVisible(screenJoystickSettingsIndex, true)
  53. end
  54. function SampleInitMouseMode(mode)
  55. useMouseMode_ = mode
  56. if GetPlatform() ~= "Web" then
  57. if useMouseMode_ == MM_FREE then
  58. input.mouseVisible = true
  59. end
  60. if useMouseMode_ ~= MM_ABSOLUTE then
  61. input.mouseMode = useMouseMode_
  62. if console ~= nil and console.visible then
  63. input:SetMouseMode(MM_ABSOLUTE, true)
  64. end
  65. end
  66. else
  67. input.mouseVisible = true
  68. SubscribeToEvent("MouseButtonDown", "HandleMouseModeRequest")
  69. SubscribeToEvent("MouseModeChanged", "HandleMouseModeChange")
  70. end
  71. end
  72. function SetLogoVisible(enable)
  73. if logoSprite ~= nil then
  74. logoSprite.visible = enable
  75. end
  76. end
  77. function CreateLogo()
  78. -- Get logo texture
  79. local logoTexture = cache:GetResource("Texture2D", "Textures/FishBoneLogo.png")
  80. if logoTexture == nil then
  81. return
  82. end
  83. -- Create logo sprite and add to the UI layout
  84. logoSprite = ui.root:CreateChild("Sprite")
  85. -- Set logo sprite texture
  86. logoSprite:SetTexture(logoTexture)
  87. local textureWidth = logoTexture.width
  88. local textureHeight = logoTexture.height
  89. -- Set logo sprite scale
  90. logoSprite:SetScale(256 / textureWidth)
  91. -- Set logo sprite size
  92. logoSprite:SetSize(textureWidth, textureHeight)
  93. -- Set logo sprite hot spot
  94. logoSprite.hotSpot = IntVector2(textureWidth, textureHeight)
  95. -- Set logo sprite alignment
  96. logoSprite:SetAlignment(HA_RIGHT, VA_BOTTOM)
  97. -- Make logo not fully opaque to show the scene underneath
  98. logoSprite.opacity = 0.9
  99. -- Set a low priority for the logo so that other UI elements can be drawn on top
  100. logoSprite.priority = -100
  101. end
  102. function SetWindowTitleAndIcon()
  103. local icon = cache:GetResource("Image", "Textures/UrhoIcon.png")
  104. graphics:SetWindowIcon(icon)
  105. graphics.windowTitle = "Urho3D Sample"
  106. end
  107. function CreateConsoleAndDebugHud()
  108. -- Get default style
  109. local uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  110. if uiStyle == nil then
  111. return
  112. end
  113. -- Create console
  114. engine:CreateConsole()
  115. console.defaultStyle = uiStyle
  116. console.background.opacity = 0.8
  117. -- Create debug HUD
  118. engine:CreateDebugHud()
  119. debugHud.defaultStyle = uiStyle
  120. end
  121. function HandleKeyUp(eventType, eventData)
  122. local key = eventData["Key"]:GetInt()
  123. -- Close console (if open) or exit when ESC is pressed
  124. if key == KEY_ESCAPE then
  125. if console:IsVisible() then
  126. console:SetVisible(false)
  127. else
  128. if GetPlatform() == "Web" then
  129. input.mouseVisible = true
  130. if (useMouseMode_ ~= MM_ABSOLUTE) then
  131. input.mouseMode = MM_FREE
  132. end
  133. else
  134. engine:Exit()
  135. end
  136. end
  137. end
  138. end
  139. function HandleKeyDown(eventType, eventData)
  140. local key = eventData["Key"]:GetInt()
  141. if key == KEY_F1 then
  142. console:Toggle()
  143. elseif key == KEY_F2 then
  144. debugHud:ToggleAll()
  145. elseif ui.focusElement == nil then
  146. -- Preferences / Pause
  147. if key == KEY_SELECT and touchEnabled then
  148. paused = not paused
  149. if screenJoystickSettingsIndex == M_MAX_UNSIGNED then
  150. -- Lazy initialization
  151. screenJoystickSettingsIndex = input:AddScreenJoystick(cache:GetResource("XMLFile", "UI/ScreenJoystickSettings_Samples.xml"), cache:GetResource("XMLFile", "UI/DefaultStyle.xml"))
  152. else
  153. input:SetScreenJoystickVisible(screenJoystickSettingsIndex, paused)
  154. end
  155. -- Texture quality
  156. elseif key == KEY_1 then
  157. local quality = renderer.textureQuality
  158. quality = quality + 1
  159. if quality > QUALITY_HIGH then
  160. quality = QUALITY_LOW
  161. end
  162. renderer.textureQuality = quality
  163. -- Material quality
  164. elseif key == KEY_2 then
  165. local quality = renderer.materialQuality
  166. quality = quality + 1
  167. if quality > QUALITY_HIGH then
  168. quality = QUALITY_LOW
  169. end
  170. renderer.materialQuality = quality
  171. -- Specular lighting
  172. elseif key == KEY_3 then
  173. renderer.specularLighting = not renderer.specularLighting
  174. -- Shadow rendering
  175. elseif key == KEY_4 then
  176. renderer.drawShadows = not renderer.drawShadows
  177. -- Shadow map resolution
  178. elseif key == KEY_5 then
  179. local shadowMapSize = renderer.shadowMapSize
  180. shadowMapSize = shadowMapSize * 2
  181. if shadowMapSize > 2048 then
  182. shadowMapSize = 512
  183. end
  184. renderer.shadowMapSize = shadowMapSize
  185. -- Shadow depth and filtering quality
  186. elseif key == KEY_6 then
  187. local quality = renderer.shadowQuality
  188. quality = quality + 1
  189. if quality > SHADOWQUALITY_BLUR_VSM then
  190. quality = SHADOWQUALITY_SIMPLE_16BIT
  191. end
  192. renderer.shadowQuality = quality
  193. -- Occlusion culling
  194. elseif key == KEY_7 then
  195. local occlusion = renderer.maxOccluderTriangles > 0
  196. occlusion = not occlusion
  197. if occlusion then
  198. renderer.maxOccluderTriangles = 5000
  199. else
  200. renderer.maxOccluderTriangles = 0
  201. end
  202. -- Instancing
  203. elseif key == KEY_8 then
  204. renderer.dynamicInstancing = not renderer.dynamicInstancing
  205. -- Take screenshot
  206. elseif key == KEY_9 then
  207. local screenshot = Image()
  208. graphics:TakeScreenShot(screenshot)
  209. local timeStamp = Time:GetTimeStamp()
  210. timeStamp = string.gsub(timeStamp, "[:. ]", "_")
  211. -- Here we save in the Data folder with date and time appended
  212. screenshot:SavePNG(fileSystem:GetProgramDir() .. "Data/Screenshot_" .. timeStamp .. ".png")
  213. end
  214. end
  215. end
  216. function HandleSceneUpdate(eventType, eventData)
  217. -- Move the camera by touch, if the camera node is initialized by descendant sample class
  218. if touchEnabled and cameraNode then
  219. for i=0, input:GetNumTouches()-1 do
  220. local state = input:GetTouch(i)
  221. if not state.touchedElement then -- Touch on empty space
  222. if state.delta.x or state.delta.y then
  223. local camera = cameraNode:GetComponent("Camera")
  224. if not camera then return end
  225. yaw = yaw + TOUCH_SENSITIVITY * camera.fov / graphics.height * state.delta.x
  226. pitch = pitch + TOUCH_SENSITIVITY * camera.fov / graphics.height * state.delta.y
  227. -- Construct new orientation for the camera scene node from yaw and pitch; roll is fixed to zero
  228. cameraNode:SetRotation(Quaternion(pitch, yaw, 0))
  229. else
  230. -- Move the cursor to the touch position
  231. local cursor = ui:GetCursor()
  232. if cursor and cursor:IsVisible() then cursor:SetPosition(state.position) end
  233. end
  234. end
  235. end
  236. end
  237. end
  238. function HandleTouchBegin(eventType, eventData)
  239. -- On some platforms like Windows the presence of touch input can only be detected dynamically
  240. InitTouchInput()
  241. UnsubscribeFromEvent("TouchBegin")
  242. end
  243. -- If the user clicks the canvas, attempt to switch to relative mouse mode on web platform
  244. function HandleMouseModeRequest(eventType, eventData)
  245. if console ~= nil and console.visible then
  246. return
  247. end
  248. if input.mouseMode == MM_ABSOLUTE then
  249. input.mouseVisible = false
  250. elseif useMouseMode_ == MM_FREE then
  251. input.mouseVisible = true
  252. end
  253. input.mouseMode = useMouseMode_
  254. end
  255. -- If the user clicks the canvas, attempt to switch to relative mouse mode on web platform
  256. function HandleMouseModeChange(eventType, eventData)
  257. mouseLocked = eventData["MouseLocked"]:GetBool()
  258. input.mouseVisible = not mouseLocked
  259. end
  260. -- Create empty XML patch instructions for screen joystick layout if none defined
  261. function GetScreenJoystickPatchString()
  262. return ""
  263. end