Sample.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. local logoSprite = nil
  9. function SampleStart()
  10. -- Create logo
  11. CreateLogo()
  12. -- Set custom window Title & Icon
  13. SetWindowTitleAndIcon()
  14. -- Create console and debug HUD
  15. CreateConsoleAndDebugHud()
  16. -- Subscribe key down event
  17. SubscribeToEvent("KeyDown", "HandleKeyDown")
  18. end
  19. function SetLogoVisible(enable)
  20. if logoSprite ~= nil then
  21. logoSprite.visible = enable
  22. end
  23. end
  24. function CreateLogo()
  25. -- Get logo texture
  26. local cache = GetCache()
  27. local logoTexture = cache:GetResource("Texture2D", "Textures/LogoLarge.png")
  28. if logoTexture == nil then
  29. return
  30. end
  31. -- Create logo sprite and add to the UI layout
  32. local ui = GetUI()
  33. logoSprite = ui.root:CreateChild("Sprite")
  34. -- Set logo sprite texture
  35. logoSprite:SetTexture(logoTexture)
  36. local textureWidth = logoTexture.width
  37. local textureHeight = logoTexture.height
  38. -- Set logo sprite scale
  39. logoSprite:SetScale(256 / textureWidth)
  40. -- Set logo sprite size
  41. logoSprite:SetSize(textureWidth, textureHeight)
  42. -- Set logo sprite hot spot
  43. logoSprite.hotSpot = IntVector2(0, textureHeight)
  44. -- Set logo sprite alignment
  45. logoSprite:SetAlignment(HA_LEFT, VA_BOTTOM);
  46. -- Make logo not fully opaque to show the scene underneath
  47. logoSprite.opacity = 0.75
  48. -- Set a low priority for the logo so that other UI elements can be drawn on top
  49. logoSprite.priority = -100
  50. end
  51. function SetWindowTitleAndIcon()
  52. local cache = GetCache()
  53. local graphics = GetGraphics()
  54. local icon = cache:GetResource("Image", "Textures/LogoLarge.png")
  55. graphics:SetWindowIcon(icon)
  56. graphics.windowTitle = "Urho3D Sample"
  57. end
  58. function CreateConsoleAndDebugHud()
  59. -- Get default style
  60. local cache = GetCache()
  61. local uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  62. if uiStyle == nil then
  63. return
  64. end
  65. -- Create console
  66. local engine = GetEngine()
  67. local console = engine:CreateConsole()
  68. console.defaultStyle = uiStyle
  69. -- Create debug HUD
  70. local debugHud = engine:CreateDebugHud()
  71. debugHud.defaultStyle = uiStyle
  72. end
  73. function HandleKeyDown(eventType, eventData)
  74. local key = eventData:GetInt("Key")
  75. -- Close console (if open) or exit when ESC is pressed
  76. if key == KEY_ESC then
  77. local console = GetConsole()
  78. if not console:IsVisible() then
  79. local engine = GetEngine()
  80. engine:Exit()
  81. else
  82. console:SetVisible(false)
  83. end
  84. elseif key == KEY_F1 then
  85. local console = GetConsole()
  86. console:Toggle()
  87. elseif key == KEY_F2 then
  88. local debugHud = GetDebugHud()
  89. debugHud:ToggleAll()
  90. end
  91. local ui = GetUI()
  92. if ui.focusElement == nil then
  93. local renderer = GetRenderer()
  94. -- Texture quality
  95. if key == KEY_1 then
  96. local quality = renderer.textureQuality
  97. quality = quality + 1
  98. if quality > QUALITY_HIGH then
  99. quality = QUALITY_LOW
  100. end
  101. renderer.textureQuality = quality
  102. -- Material quality
  103. elseif key == KEY_2 then
  104. local quality = renderer.materialQuality
  105. quality = quality + 1
  106. if quality > QUALITY_HIGH then
  107. quality = QUALITY_LOW
  108. end
  109. renderer.materialQuality = quality
  110. -- Specular lighting
  111. elseif key == KEY_3 then
  112. renderer.specularLighting = not renderer.specularLighting
  113. -- Shadow rendering
  114. elseif key == KEY_4 then
  115. renderer.drawShadows = not renderer.drawShadows
  116. -- Shadow map resolution
  117. elseif key == KEY_5 then
  118. local shadowMapSize = renderer.shadowMapSize
  119. shadowMapSize = shadowMapSize * 2
  120. if shadowMapSize > 2048 then
  121. shadowMapSize = 512
  122. end
  123. renderer.shadowMapSize = shadowMapSize
  124. -- Shadow depth and filtering quality
  125. elseif key == KEY_6 then
  126. local quality = renderer.shadowQuality
  127. quality = quality + 1
  128. if quality > SHADOWQUALITY_HIGH_24BIT then
  129. quality = SHADOWQUALITY_LOW_16BIT
  130. end
  131. renderer.shadowQuality = quality
  132. -- Occlusion culling
  133. elseif key == KEY_7 then
  134. local occlusion = renderer.maxOccluderTriangles > 0
  135. occlusion = not occlusion
  136. if occlusion then
  137. renderer.maxOccluderTriangles = 5000
  138. else
  139. renderer.maxOccluderTriangles = 0
  140. end
  141. -- Instancing
  142. elseif key == KEY_8 then
  143. renderer.dynamicInstancing = not renderer.dynamicInstancing
  144. -- Take screenshot
  145. elseif key == KEY_9 then
  146. local graphics = GetGraphics()
  147. local screenshot = Image()
  148. graphics:TakeScreenShot(screenshot)
  149. local timeStamp = Time:GetTimeStamp()
  150. timeStamp = string.gsub(timeStamp, "[:. ]", "_")
  151. -- Here we save in the Data folder with date and time appended
  152. screenshot:SavePNG(GetFileSystem():GetProgramDir() .. "Data/Screenshot_" .. timeStamp .. ".png")
  153. end
  154. end
  155. end