Sample.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. -- Common sample initialization as a framework for all samples.
  2. -- - Create Urho3D logo at screen
  3. -- - Create Console and Debug HUD, and use F1 and F2 key to toggle them
  4. -- - Toggle rendering options from the keys 1-8
  5. -- - Handle Esc key down to hide Console or exit application
  6. local logoSprite = nil
  7. function SampleStart()
  8. -- Create logo
  9. CreateLogo()
  10. -- Create console and debug HUD
  11. CreateConsoleAndDebugHud()
  12. -- Subscribe key down event
  13. SubscribeToEvent("KeyDown", "HandleKeyDown")
  14. end
  15. function SetLogoVisible(enable)
  16. if logoSprite ~= nil then
  17. logoSprite.visible = enable
  18. end
  19. end
  20. function CreateLogo()
  21. -- Get logo texture
  22. local cache = GetCache()
  23. local logoTexture = cache:GetResource("Texture2D", "Textures/LogoLarge.png")
  24. if logoTexture == nil then
  25. return
  26. end
  27. -- Create logo sprite and add to the UI layout
  28. local ui = GetUI()
  29. logoSprite = ui.root:CreateChild("Sprite")
  30. -- Set logo sprite texture
  31. logoSprite:SetTexture(logoTexture)
  32. local textureWidth = logoTexture.width
  33. local textureHeight = logoTexture.height
  34. -- Set logo sprite scale
  35. logoSprite:SetScale(256 / textureWidth)
  36. -- Set logo sprite size
  37. logoSprite:SetSize(textureWidth, textureHeight)
  38. -- Set logo sprite hot spot
  39. logoSprite.hotSpot = IntVector2(0, textureHeight)
  40. -- Set logo sprite alignment
  41. logoSprite:SetAlignment(HA_LEFT, VA_BOTTOM);
  42. -- Make logo not fully opaque to show the scene underneath
  43. logoSprite.opacity = 0.75
  44. -- Set a low priority for the logo so that other UI elements can be drawn on top
  45. logoSprite.priority = -100
  46. end
  47. function CreateConsoleAndDebugHud()
  48. -- Get default style
  49. local cache = GetCache()
  50. local uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  51. if uiStyle == nil then
  52. return
  53. end
  54. -- Create console
  55. local engine = GetEngine()
  56. local console = engine:CreateConsole()
  57. console.defaultStyle = uiStyle
  58. -- Create debug HUD
  59. local debugHud = engine:CreateDebugHud()
  60. debugHud.defaultStyle = uiStyle
  61. end
  62. function HandleKeyDown(eventType, eventData)
  63. local key = eventData:GetInt("Key")
  64. -- Close console (if open) or exit when ESC is pressed
  65. if key == KEY_ESC then
  66. local console = GetConsole()
  67. if not console:IsVisible() then
  68. local engine = GetEngine()
  69. engine:Exit()
  70. else
  71. console:SetVisible(false)
  72. end
  73. elseif key == KEY_F1 then
  74. local console = GetConsole()
  75. console:Toggle()
  76. elseif key == KEY_F2 then
  77. local debugHud = GetDebugHud()
  78. debugHud:ToggleAll()
  79. end
  80. local ui = GetUI()
  81. if ui.focusElement == nil then
  82. local renderer = GetRenderer()
  83. -- Texture quality
  84. if key == KEY_1 then
  85. local quality = renderer.textureQuality
  86. quality = quality + 1
  87. if quality > QUALITY_HIGH then
  88. quality = QUALITY_LOW
  89. end
  90. renderer.textureQuality = quality
  91. -- Material quality
  92. elseif key == KEY_2 then
  93. local quality = renderer.materialQuality
  94. quality = quality + 1
  95. if quality > QUALITY_HIGH then
  96. quality = QUALITY_LOW
  97. end
  98. renderer.materialQuality = quality
  99. -- Specular lighting
  100. elseif key == KEY_3 then
  101. renderer.specularLighting = not renderer.specularLighting
  102. -- Shadow rendering
  103. elseif key == KEY_4 then
  104. renderer.drawShadows = not renderer.drawShadows
  105. -- Shadow map resolution
  106. elseif key == KEY_5 then
  107. local shadowMapSize = renderer.shadowMapSize
  108. shadowMapSize = shadowMapSize * 2
  109. if shadowMapSize > 2048 then
  110. shadowMapSize = 512
  111. end
  112. renderer.shadowMapSize = shadowMapSize
  113. -- Shadow depth and filtering quality
  114. elseif key == KEY_6 then
  115. local quality = renderer.shadowQuality
  116. quality = quality + 1
  117. if quality > SHADOWQUALITY_HIGH_24BIT then
  118. quality = SHADOWQUALITY_LOW_16BIT
  119. end
  120. renderer.shadowQuality = quality
  121. -- Occlusion culling
  122. elseif key == KEY_7 then
  123. local occlusion = renderer.maxOccluderTriangles > 0
  124. occlusion = not occlusion
  125. if occlusion then
  126. renderer.maxOccluderTriangles = 5000
  127. else
  128. renderer.maxOccluderTriangles = 0
  129. end
  130. -- Instancing
  131. elseif key == KEY_8 then
  132. renderer.dynamicInstancing = not renderer.dynamicInstancing
  133. end
  134. end
  135. end