Sample.as 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. Sprite@ logoSprite;
  7. void SampleStart()
  8. {
  9. // Create logo
  10. CreateLogo();
  11. // Create console and debug HUD
  12. CreateConsoleAndDebugHud();
  13. // Subscribe key down event
  14. SubscribeToEvent("KeyDown", "HandleKeyDown");
  15. }
  16. void SetLogoVisible(bool enable)
  17. {
  18. if (logoSprite !is null)
  19. logoSprite.visible = enable;
  20. }
  21. void CreateLogo()
  22. {
  23. // Get logo texture
  24. Texture2D@ logoTexture = cache.GetResource("Texture2D", "Textures/LogoLarge.png");
  25. if (logoTexture is null)
  26. return;
  27. // Create logo sprite and add to the UI layout
  28. logoSprite = ui.root.CreateChild("Sprite");
  29. // Set logo sprite texture
  30. logoSprite.texture = logoTexture;
  31. int textureWidth = logoTexture.width;
  32. int textureHeight = logoTexture.height;
  33. // Set logo sprite scale
  34. logoSprite.SetScale(256.0f / textureWidth);
  35. // Set logo sprite size
  36. logoSprite.SetSize(textureWidth, textureHeight);
  37. // Set logo sprite hot spot
  38. logoSprite.SetHotSpot(0, textureHeight);
  39. // Set logo sprite alignment
  40. logoSprite.SetAlignment(HA_LEFT, VA_BOTTOM);
  41. // Make logo not fully opaque to show the scene underneath
  42. logoSprite.opacity = 0.75f;
  43. // Set a low priority for the logo so that other UI elements can be drawn on top
  44. logoSprite.priority = -100;
  45. }
  46. void CreateConsoleAndDebugHud()
  47. {
  48. // Get default style
  49. XMLFile@ xmlFile = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  50. if (xmlFile is null)
  51. return;
  52. // Create console
  53. Console@ console = engine.CreateConsole();
  54. console.defaultStyle = xmlFile;
  55. // Create debug HUD
  56. DebugHud@ debugHud = engine.CreateDebugHud();
  57. debugHud.defaultStyle = xmlFile;
  58. }
  59. void HandleKeyDown(StringHash eventType, VariantMap& eventData)
  60. {
  61. int key = eventData["Key"].GetInt();
  62. // Close console (if open) or exit when ESC is pressed
  63. if (key == KEY_ESC)
  64. {
  65. if (!console.visible)
  66. engine.Exit();
  67. else
  68. console.visible = false;
  69. }
  70. // Toggle console with F1
  71. else if (key == KEY_F1)
  72. console.Toggle();
  73. // Toggle debug HUD with F2
  74. else if (key == KEY_F2)
  75. debugHud.ToggleAll();
  76. // Common rendering quality controls, only when UI has no focused element
  77. if (ui.focusElement is null)
  78. {
  79. // Texture quality
  80. if (key == '1')
  81. {
  82. int quality = renderer.textureQuality;
  83. ++quality;
  84. if (quality > QUALITY_HIGH)
  85. quality = QUALITY_LOW;
  86. renderer.textureQuality = quality;
  87. }
  88. // Material quality
  89. else if (key == '2')
  90. {
  91. int quality = renderer.materialQuality;
  92. ++quality;
  93. if (quality > QUALITY_HIGH)
  94. quality = QUALITY_LOW;
  95. renderer.materialQuality = quality;
  96. }
  97. // Specular lighting
  98. else if (key == '3')
  99. renderer.specularLighting = !renderer.specularLighting;
  100. // Shadow rendering
  101. else if (key == '4')
  102. renderer.drawShadows = !renderer.drawShadows;
  103. // Shadow map resolution
  104. else if (key == '5')
  105. {
  106. int shadowMapSize = renderer.shadowMapSize;
  107. shadowMapSize *= 2;
  108. if (shadowMapSize > 2048)
  109. shadowMapSize = 512;
  110. renderer.shadowMapSize = shadowMapSize;
  111. }
  112. // Shadow depth and filtering quality
  113. else if (key == '6')
  114. {
  115. int quality = renderer.shadowQuality;
  116. ++quality;
  117. if (quality > SHADOWQUALITY_HIGH_24BIT)
  118. quality = SHADOWQUALITY_LOW_16BIT;
  119. renderer.shadowQuality = quality;
  120. }
  121. // Occlusion culling
  122. else if (key == '7')
  123. {
  124. bool occlusion = renderer.maxOccluderTriangles > 0;
  125. occlusion = !occlusion;
  126. renderer.maxOccluderTriangles = occlusion ? 5000 : 0;
  127. }
  128. // Instancing
  129. else if (key == '8')
  130. renderer.dynamicInstancing = !renderer.dynamicInstancing;
  131. }
  132. }