07_Billboards.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. // Copyright (c) 2015 Xamarin Inc
  4. // Copyright (c) 2016 THUNDERBEAST GAMES LLC
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. using System;
  25. using AtomicEngine;
  26. namespace FeatureExamples
  27. {
  28. public class BillboardsSample : Sample
  29. {
  30. Scene scene;
  31. bool drawDebug;
  32. public BillboardsSample() : base() { }
  33. public override void Start()
  34. {
  35. base.Start();
  36. CreateScene();
  37. SimpleCreateInstructionsWithWasd("\nSpace to toggle debug geometry");
  38. SetupViewport();
  39. SubscribeToEvents();
  40. }
  41. void SubscribeToEvents()
  42. {
  43. SubscribeToEvent<PostRenderUpdateEvent>(e =>
  44. {
  45. // If draw debug mode is enabled, draw viewport debug geometry, which will show eg. drawable bounding boxes and skeleton
  46. // bones. Note that debug geometry has to be separately requested each frame. Disable depth test so that we can see the
  47. // bones properly
  48. if (drawDebug)
  49. GetSubsystem<Renderer>().DrawDebugGeometry(false);
  50. });
  51. }
  52. protected override void Update(float timeStep)
  53. {
  54. SimpleMoveCamera3D(timeStep);
  55. AnimateScene(timeStep);
  56. if (GetSubsystem<Input>().GetKeyPress(Constants.KEY_SPACE))
  57. drawDebug = !drawDebug;
  58. base.Update(timeStep);
  59. }
  60. void AnimateScene(float timeStep)
  61. {
  62. var lightNodes = new Vector<Node>();
  63. var billboardNodes = new Vector<Node>();
  64. scene.GetChildrenWithComponent<Light>(lightNodes);
  65. scene.GetChildrenWithComponent<BillboardSet>(billboardNodes);
  66. const float lightRotationSpeed = 20.0f;
  67. const float billboardRotationSpeed = 50.0f;
  68. foreach (var lightNode in lightNodes)
  69. {
  70. lightNode.Rotate(new Quaternion(0f, lightRotationSpeed * timeStep, 0f), TransformSpace.TS_WORLD);
  71. }
  72. foreach (var billboardNode in billboardNodes)
  73. {
  74. var billboardSet = billboardNode.GetComponent<BillboardSet>();
  75. for (uint i = 0; i < billboardSet.NumBillboards; i++)
  76. {
  77. var bb = billboardSet.GetBillboard(i);
  78. bb.Rotation += billboardRotationSpeed * timeStep;
  79. }
  80. billboardSet.Commit();
  81. }
  82. }
  83. void SetupViewport()
  84. {
  85. var renderer = GetSubsystem<Renderer>();
  86. renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>()));
  87. }
  88. void CreateScene()
  89. {
  90. var cache = GetSubsystem<ResourceCache>();
  91. scene = new Scene();
  92. // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  93. // Also create a DebugRenderer component so that we can draw debug geometry
  94. scene.CreateComponent<Octree>();
  95. scene.CreateComponent<DebugRenderer>();
  96. var zoneNode = scene.CreateChild("Zone");
  97. Zone zone = zoneNode.CreateComponent<Zone>();
  98. zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
  99. zone.AmbientColor = new Color(0.1f, 0.1f, 0.1f);
  100. zone.FogStart = 100.0f;
  101. zone.FogEnd = 300.0f;
  102. var lightNode = scene.CreateChild("DirectionalLight");
  103. lightNode.SetDirection(new Vector3(0.5f, -1.0f, 0.5f));
  104. var light = lightNode.CreateComponent<Light>();
  105. light.LightType = LightType.LIGHT_DIRECTIONAL;
  106. light.Color = new Color(0.2f, 0.2f, 0.2f);
  107. light.SpecularIntensity = 1.0f;
  108. for (int y = -5; y <= 5; ++y)
  109. {
  110. for (int x = -5; x <= 5; ++x)
  111. {
  112. var floorNode = scene.CreateChild("FloorTile");
  113. floorNode.Position = new Vector3(x * 20.5f, -0.5f, y * 20.5f);
  114. floorNode.Scale = new Vector3(20.0f, 1.0f, 20.0f);
  115. var floorObject = floorNode.CreateComponent<StaticModel>();
  116. floorObject.Model = cache.Get<Model>("Models/Box.mdl");
  117. floorObject.SetMaterial(cache.Get<Material>("Materials/Stone.xml"));
  118. }
  119. }
  120. // Create groups of mushrooms, which act as shadow casters
  121. const uint numMushroomgroups = 25;
  122. const uint numMushrooms = 25;
  123. for (uint i = 0; i < numMushroomgroups; ++i)
  124. {
  125. var groupNode = scene.CreateChild("MushroomGroup");
  126. groupNode.Position = new Vector3(NextRandom(190.0f) - 95.0f, 0.0f, NextRandom(190.0f) - 95.0f);
  127. for (uint j = 0; j < numMushrooms; ++j)
  128. {
  129. var mushroomNode = groupNode.CreateChild("Mushroom");
  130. mushroomNode.Position = new Vector3(NextRandom(25.0f) - 12.5f, 0.0f, NextRandom(25.0f) - 12.5f);
  131. mushroomNode.Rotation = new Quaternion(0.0f, NextRandom() * 360.0f, 0.0f);
  132. mushroomNode.SetScale(1.0f + NextRandom() * 4.0f);
  133. var mushroomObject = mushroomNode.CreateComponent<StaticModel>();
  134. mushroomObject.Model = cache.Get<Model>("Models/Mushroom.mdl");
  135. mushroomObject.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml"));
  136. mushroomObject.CastShadows = true;
  137. }
  138. }
  139. // Create billboard sets (floating smoke)
  140. const uint numBillboardnodes = 25;
  141. const uint numBillboards = 10;
  142. for (uint i = 0; i < numBillboardnodes; ++i)
  143. {
  144. var smokeNode = scene.CreateChild("Smoke");
  145. smokeNode.Position = new Vector3(NextRandom(200.0f) - 100.0f, NextRandom(20.0f) + 10.0f, NextRandom(200.0f) - 100.0f);
  146. var billboardObject = smokeNode.CreateComponent<BillboardSet>();
  147. billboardObject.NumBillboards = numBillboards;
  148. billboardObject.Material = cache.Get<Material>("Materials/LitSmoke.xml");
  149. billboardObject.Sorted = true;
  150. for (uint j = 0; j < numBillboards; ++j)
  151. {
  152. var bb = billboardObject.GetBillboard(j);
  153. bb.Position = new Vector3(NextRandom(12.0f) - 6.0f, NextRandom(8.0f) - 4.0f, NextRandom(12.0f) - 6.0f);
  154. bb.Size = new Vector2(NextRandom(2.0f) + 3.0f, NextRandom(2.0f) + 3.0f);
  155. bb.Rotation = NextRandom() * 360.0f;
  156. bb.Enabled = true;
  157. }
  158. // After modifying the billboards, they need to be "commited" so that the BillboardSet updates its internals
  159. billboardObject.Commit();
  160. }
  161. // Create shadow casting spotlights
  162. const uint numLights = 9;
  163. for (uint i = 0; i < numLights; ++i)
  164. {
  165. lightNode = scene.CreateChild("SpotLight");
  166. light = lightNode.CreateComponent<Light>();
  167. float angle = 0.0f;
  168. Vector3 position = new Vector3((i % 3) * 60.0f - 60.0f, 45.0f, (i / 3) * 60.0f - 60.0f);
  169. Color color = new Color(((i + 1) & 1) * 0.5f + 0.5f, (((i + 1) >> 1) & 1) * 0.5f + 0.5f, (((i + 1) >> 2) & 1) * 0.5f + 0.5f);
  170. lightNode.Position = position;
  171. lightNode.SetDirection(new Vector3((float)Math.Sin(angle), -1.5f, (float)Math.Cos(angle)));
  172. light.LightType = LightType.LIGHT_SPOT;
  173. light.Range = 90.0f;
  174. light.RampTexture = cache.Get<Texture2D>("Textures/RampExtreme.png");
  175. light.Fov = 45.0f;
  176. light.Color = color;
  177. light.SpecularIntensity = 1.0f;
  178. light.CastShadows = true;
  179. light.ShadowBias = new BiasParameters(0.00002f, 0.0f);
  180. // Configure shadow fading for the lights. When they are far away enough, the lights eventually become unshadowed for
  181. // better GPU performance. Note that we could also set the maximum distance for each object to cast shadows
  182. light.ShadowFadeDistance = 100.0f; // Fade start distance
  183. light.ShadowDistance = 125.0f; // Fade end distance, shadows are disabled
  184. // Set half resolution for the shadow maps for increased performance
  185. light.ShadowResolution = 0.5f;
  186. // The spot lights will not have anything near them, so move the near plane of the shadow camera farther
  187. // for better shadow depth resolution
  188. light.ShadowNearFarRatio = 0.01f;
  189. }
  190. // Create the camera. Limit far clip distance to match the fog
  191. CameraNode = scene.CreateChild("Camera");
  192. var camera = CameraNode.CreateComponent<Camera>();
  193. camera.FarClip = 300.0f;
  194. // Set an initial position for the camera scene node above the plane
  195. CameraNode.Position = new Vector3(0.0f, 5.0f, 0.0f);
  196. }
  197. }
  198. }