Sample.lua 5.3 KB

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