| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- -- Common sample initialization as a framework for all samples.
- -- - Create Urho3D logo at screen
- -- - Set custom window title and icon
- -- - Create Console and Debug HUD, and use F1 and F2 key to toggle them
- -- - Toggle rendering options from the keys 1-8
- -- - Take screenshots with key 9
- -- - Handle Esc key down to hide Console or exit application
- local logoSprite = nil
- function SampleStart()
- -- Create logo
- CreateLogo()
- -- Set custom window Title & Icon
- SetWindowTitleAndIcon()
- -- Create console and debug HUD
- CreateConsoleAndDebugHud()
- -- Subscribe key down event
- SubscribeToEvent("KeyDown", "HandleKeyDown")
- end
- function SetLogoVisible(enable)
- if logoSprite ~= nil then
- logoSprite.visible = enable
- end
- end
- function CreateLogo()
- -- Get logo texture
- local cache = GetCache()
- local logoTexture = cache:GetResource("Texture2D", "Textures/LogoLarge.png")
- if logoTexture == nil then
- return
- end
-
- -- Create logo sprite and add to the UI layout
- local ui = GetUI()
- logoSprite = ui.root:CreateChild("Sprite")
-
- -- Set logo sprite texture
- logoSprite:SetTexture(logoTexture)
-
- local textureWidth = logoTexture.width
- local textureHeight = logoTexture.height
-
- -- Set logo sprite scale
- logoSprite:SetScale(256 / textureWidth)
-
- -- Set logo sprite size
- logoSprite:SetSize(textureWidth, textureHeight)
-
- -- Set logo sprite hot spot
- logoSprite.hotSpot = IntVector2(0, textureHeight)
-
- -- Set logo sprite alignment
- logoSprite:SetAlignment(HA_LEFT, VA_BOTTOM);
-
- -- Make logo not fully opaque to show the scene underneath
- logoSprite.opacity = 0.75
-
- -- Set a low priority for the logo so that other UI elements can be drawn on top
- logoSprite.priority = -100
- end
- function SetWindowTitleAndIcon()
- local cache = GetCache()
- local graphics = GetGraphics()
- local icon = cache:GetResource("Image", "Textures/LogoLarge.png")
- graphics:SetWindowIcon(icon)
- graphics.windowTitle = "Urho3D Sample"
- end
- function CreateConsoleAndDebugHud()
- -- Get default style
- local cache = GetCache()
- local uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
- if uiStyle == nil then
- return
- end
-
- -- Create console
- local engine = GetEngine()
- local console = engine:CreateConsole()
- console.defaultStyle = uiStyle
-
- -- Create debug HUD
- local debugHud = engine:CreateDebugHud()
- debugHud.defaultStyle = uiStyle
- end
- function HandleKeyDown(eventType, eventData)
- local key = eventData:GetInt("Key")
- -- Close console (if open) or exit when ESC is pressed
- if key == KEY_ESC then
- local console = GetConsole()
- if not console:IsVisible() then
- local engine = GetEngine()
- engine:Exit()
- else
- console:SetVisible(false)
- end
- elseif key == KEY_F1 then
- local console = GetConsole()
- console:Toggle()
- elseif key == KEY_F2 then
- local debugHud = GetDebugHud()
- debugHud:ToggleAll()
- end
- local ui = GetUI()
- if ui.focusElement == nil then
- local renderer = GetRenderer()
- -- Texture quality
- if key == KEY_1 then
- local quality = renderer.textureQuality
- quality = quality + 1
- if quality > QUALITY_HIGH then
- quality = QUALITY_LOW
- end
- renderer.textureQuality = quality
- -- Material quality
- elseif key == KEY_2 then
- local quality = renderer.materialQuality
- quality = quality + 1
- if quality > QUALITY_HIGH then
- quality = QUALITY_LOW
- end
- renderer.materialQuality = quality
- -- Specular lighting
- elseif key == KEY_3 then
- renderer.specularLighting = not renderer.specularLighting
- -- Shadow rendering
- elseif key == KEY_4 then
- renderer.drawShadows = not renderer.drawShadows
- -- Shadow map resolution
- elseif key == KEY_5 then
- local shadowMapSize = renderer.shadowMapSize
- shadowMapSize = shadowMapSize * 2
- if shadowMapSize > 2048 then
- shadowMapSize = 512
- end
- renderer.shadowMapSize = shadowMapSize
- -- Shadow depth and filtering quality
- elseif key == KEY_6 then
- local quality = renderer.shadowQuality
- quality = quality + 1
- if quality > SHADOWQUALITY_HIGH_24BIT then
- quality = SHADOWQUALITY_LOW_16BIT
- end
- renderer.shadowQuality = quality
- -- Occlusion culling
- elseif key == KEY_7 then
- local occlusion = renderer.maxOccluderTriangles > 0
- occlusion = not occlusion
- if occlusion then
- renderer.maxOccluderTriangles = 5000
- else
- renderer.maxOccluderTriangles = 0
- end
- -- Instancing
- elseif key == KEY_8 then
- renderer.dynamicInstancing = not renderer.dynamicInstancing
-
- -- Take screenshot
- elseif key == KEY_9 then
- local graphics = GetGraphics()
- local screenshot = Image()
- graphics:TakeScreenShot(screenshot)
- local timeStamp = Time:GetTimeStamp()
- timeStamp = string.gsub(timeStamp, "[:. ]", "_")
- -- Here we save in the Data folder with date and time appended
- screenshot:SavePNG(GetFileSystem():GetProgramDir() .. "Data/Screenshot_" .. timeStamp .. ".png")
- end
- end
- end
|