07_Billboards.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. bool drawDebug;
  31. public BillboardsSample() : base() { }
  32. public override void Start()
  33. {
  34. base.Start();
  35. CreateScene();
  36. SimpleCreateInstructionsWithWasd("\nSpace to toggle debug geometry");
  37. SetupViewport();
  38. SubscribeToEvents();
  39. }
  40. void SubscribeToEvents()
  41. {
  42. SubscribeToEvent<PostRenderUpdateEvent>(e =>
  43. {
  44. // If draw debug mode is enabled, draw viewport debug geometry, which will show eg. drawable bounding boxes and skeleton
  45. // bones. Note that debug geometry has to be separately requested each frame. Disable depth test so that we can see the
  46. // bones properly
  47. if (drawDebug)
  48. GetSubsystem<Renderer>().DrawDebugGeometry(false);
  49. });
  50. }
  51. protected override void Update(float timeStep)
  52. {
  53. SimpleMoveCamera3D(timeStep);
  54. AnimateScene(timeStep);
  55. if (GetSubsystem<Input>().GetKeyPress(Constants.KEY_SPACE))
  56. drawDebug = !drawDebug;
  57. base.Update(timeStep);
  58. }
  59. void AnimateScene(float timeStep)
  60. {
  61. var lightNodes = new Vector<Node>();
  62. var billboardNodes = new Vector<Node>();
  63. scene.GetChildrenWithComponent<Light>(lightNodes);
  64. scene.GetChildrenWithComponent<BillboardSet>(billboardNodes);
  65. const float lightRotationSpeed = 20.0f;
  66. const float billboardRotationSpeed = 50.0f;
  67. foreach (var lightNode in lightNodes)
  68. {
  69. lightNode.Rotate(new Quaternion(0f, lightRotationSpeed * timeStep, 0f), TransformSpace.TS_WORLD);
  70. }
  71. foreach (var billboardNode in billboardNodes)
  72. {
  73. var billboardSet = billboardNode.GetComponent<BillboardSet>();
  74. for (uint i = 0; i < billboardSet.NumBillboards; i++)
  75. {
  76. var bb = billboardSet.GetBillboard(i);
  77. bb.Rotation += billboardRotationSpeed * timeStep;
  78. }
  79. billboardSet.Commit();
  80. }
  81. }
  82. void SetupViewport()
  83. {
  84. var renderer = GetSubsystem<Renderer>();
  85. renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>()));
  86. }
  87. void CreateScene()
  88. {
  89. var cache = GetSubsystem<ResourceCache>();
  90. scene = new Scene();
  91. // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  92. // Also create a DebugRenderer component so that we can draw debug geometry
  93. scene.CreateComponent<Octree>();
  94. scene.CreateComponent<DebugRenderer>();
  95. var zoneNode = scene.CreateChild("Zone");
  96. Zone zone = zoneNode.CreateComponent<Zone>();
  97. zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
  98. zone.AmbientColor = new Color(0.1f, 0.1f, 0.1f);
  99. zone.FogStart = 100.0f;
  100. zone.FogEnd = 300.0f;
  101. var lightNode = scene.CreateChild("DirectionalLight");
  102. lightNode.SetDirection(new Vector3(0.5f, -1.0f, 0.5f));
  103. var light = lightNode.CreateComponent<Light>();
  104. light.LightType = LightType.LIGHT_DIRECTIONAL;
  105. light.Color = new Color(0.2f, 0.2f, 0.2f);
  106. light.SpecularIntensity = 1.0f;
  107. for (int y = -5; y <= 5; ++y)
  108. {
  109. for (int x = -5; x <= 5; ++x)
  110. {
  111. var floorNode = scene.CreateChild("FloorTile");
  112. floorNode.Position = new Vector3(x * 20.5f, -0.5f, y * 20.5f);
  113. floorNode.Scale = new Vector3(20.0f, 1.0f, 20.0f);
  114. var floorObject = floorNode.CreateComponent<StaticModel>();
  115. floorObject.Model = cache.Get<Model>("Models/Box.mdl");
  116. floorObject.SetMaterial(cache.Get<Material>("Materials/Stone.xml"));
  117. }
  118. }
  119. // Create groups of mushrooms, which act as shadow casters
  120. const uint numMushroomgroups = 25;
  121. const uint numMushrooms = 25;
  122. for (uint i = 0; i < numMushroomgroups; ++i)
  123. {
  124. var groupNode = scene.CreateChild("MushroomGroup");
  125. groupNode.Position = new Vector3(NextRandom(190.0f) - 95.0f, 0.0f, NextRandom(190.0f) - 95.0f);
  126. for (uint j = 0; j < numMushrooms; ++j)
  127. {
  128. var mushroomNode = groupNode.CreateChild("Mushroom");
  129. mushroomNode.Position = new Vector3(NextRandom(25.0f) - 12.5f, 0.0f, NextRandom(25.0f) - 12.5f);
  130. mushroomNode.Rotation = new Quaternion(0.0f, NextRandom() * 360.0f, 0.0f);
  131. mushroomNode.SetScale(1.0f + NextRandom() * 4.0f);
  132. var mushroomObject = mushroomNode.CreateComponent<StaticModel>();
  133. mushroomObject.Model = cache.Get<Model>("Models/Mushroom.mdl");
  134. mushroomObject.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml"));
  135. mushroomObject.CastShadows = true;
  136. }
  137. }
  138. // Create billboard sets (floating smoke)
  139. const uint numBillboardnodes = 25;
  140. const uint numBillboards = 10;
  141. for (uint i = 0; i < numBillboardnodes; ++i)
  142. {
  143. var smokeNode = scene.CreateChild("Smoke");
  144. smokeNode.Position = new Vector3(NextRandom(200.0f) - 100.0f, NextRandom(20.0f) + 10.0f, NextRandom(200.0f) - 100.0f);
  145. var billboardObject = smokeNode.CreateComponent<BillboardSet>();
  146. billboardObject.NumBillboards = numBillboards;
  147. billboardObject.Material = cache.Get<Material>("Materials/LitSmoke.xml");
  148. billboardObject.Sorted = true;
  149. for (uint j = 0; j < numBillboards; ++j)
  150. {
  151. var bb = billboardObject.GetBillboard(j);
  152. bb.Position = new Vector3(NextRandom(12.0f) - 6.0f, NextRandom(8.0f) - 4.0f, NextRandom(12.0f) - 6.0f);
  153. bb.Size = new Vector2(NextRandom(2.0f) + 3.0f, NextRandom(2.0f) + 3.0f);
  154. bb.Rotation = NextRandom() * 360.0f;
  155. bb.Enabled = true;
  156. }
  157. // After modifying the billboards, they need to be "commited" so that the BillboardSet updates its internals
  158. billboardObject.Commit();
  159. }
  160. // Create shadow casting spotlights
  161. const uint numLights = 9;
  162. for (uint i = 0; i < numLights; ++i)
  163. {
  164. lightNode = scene.CreateChild("SpotLight");
  165. light = lightNode.CreateComponent<Light>();
  166. float angle = 0.0f;
  167. Vector3 position = new Vector3((i % 3) * 60.0f - 60.0f, 45.0f, (i / 3) * 60.0f - 60.0f);
  168. 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);
  169. lightNode.Position = position;
  170. lightNode.SetDirection(new Vector3((float)Math.Sin(angle), -1.5f, (float)Math.Cos(angle)));
  171. light.LightType = LightType.LIGHT_SPOT;
  172. light.Range = 90.0f;
  173. light.RampTexture = cache.Get<Texture2D>("Textures/RampExtreme.png");
  174. light.Fov = 45.0f;
  175. light.Color = color;
  176. light.SpecularIntensity = 1.0f;
  177. light.CastShadows = true;
  178. light.ShadowBias = new BiasParameters(0.00002f, 0.0f);
  179. // Configure shadow fading for the lights. When they are far away enough, the lights eventually become unshadowed for
  180. // better GPU performance. Note that we could also set the maximum distance for each object to cast shadows
  181. light.ShadowFadeDistance = 100.0f; // Fade start distance
  182. light.ShadowDistance = 125.0f; // Fade end distance, shadows are disabled
  183. // Set half resolution for the shadow maps for increased performance
  184. light.ShadowResolution = 0.5f;
  185. // The spot lights will not have anything near them, so move the near plane of the shadow camera farther
  186. // for better shadow depth resolution
  187. light.ShadowNearFarRatio = 0.01f;
  188. }
  189. // Create the camera. Limit far clip distance to match the fog
  190. CameraNode = scene.CreateChild("Camera");
  191. var camera = CameraNode.CreateComponent<Camera>();
  192. camera.FarClip = 300.0f;
  193. // Set an initial position for the camera scene node above the plane
  194. CameraNode.Position = new Vector3(0.0f, 5.0f, 0.0f);
  195. }
  196. }
  197. }