05_AnimatingScene.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 AtomicEngine;
  25. namespace FeatureExamples
  26. {
  27. public class AnimatingScene : Sample
  28. {
  29. Scene scene;
  30. public AnimatingScene() : base() { }
  31. void CreateScene()
  32. {
  33. var cache = GetSubsystem<ResourceCache>();
  34. scene = new Scene();
  35. // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
  36. // (-1000, -1000, -1000) to (1000, 1000, 1000)
  37. scene.CreateComponent<Octree>();
  38. // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree,
  39. // it also defines its volume with a bounding box, but can be rotated (so it does not need to be aligned to the world X, Y
  40. // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist
  41. var zoneNode = scene.CreateChild("Zone");
  42. var zone = zoneNode.CreateComponent<Zone>();
  43. // Set same volume as the Octree, set a close bluish fog and some ambient light
  44. zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
  45. zone.AmbientColor = new Color(0.05f, 0.1f, 0.15f);
  46. zone.FogColor = new Color(0.1f, 0.2f, 0.3f);
  47. zone.FogStart = 10;
  48. zone.FogEnd = 100;
  49. var boxesNode = scene.CreateChild("Boxes");
  50. const int numObjects = 2000;
  51. for (var i = 0; i < numObjects; ++i)
  52. {
  53. Node boxNode = new Node();
  54. boxesNode.AddChild(boxNode, 0);
  55. boxNode.Position = new Vector3(NextRandom(200f) - 100f, NextRandom(200f) - 100f, NextRandom(200f) - 100f);
  56. // Orient using random pitch, yaw and roll Euler angles
  57. boxNode.Rotation = new Quaternion(NextRandom(360.0f), NextRandom(360.0f), NextRandom(360.0f));
  58. var boxObject = boxNode.CreateComponent<StaticModel>();
  59. boxObject.Model = cache.Get<Model>("Models/Box.mdl");
  60. boxObject.SetMaterial(cache.Get<Material>("Materials/Stone.xml"));
  61. // Add our custom Rotator component which will rotate the scene node each frame, when the scene sends its update event.
  62. // The Rotator component derives from the base class CSComponent, which has convenience functionality to subscribe
  63. // to the various update events
  64. // Now we simply set same rotation speed for all objects
  65. var rotationSpeed = new Vector3(10.0f, 20.0f, 30.0f);
  66. // First style: use a Rotator instance, which is a component subclass, and
  67. // add it to the boxNode.
  68. var rotator = new Rotator() { RotationSpeed = rotationSpeed };
  69. boxNode.AddComponent(rotator);
  70. }
  71. // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can
  72. // bring the far clip plane closer for more effective culling of distant objects
  73. CameraNode = scene.CreateChild("Camera");
  74. var camera = CameraNode.CreateComponent<Camera>();
  75. camera.FarClip = 100.0f;
  76. // Create a point light to the camera scene node
  77. var light = CameraNode.CreateComponent<Light>();
  78. light.LightType = LightType.LIGHT_POINT;
  79. light.Range = 30.0f;
  80. }
  81. void SetupViewport()
  82. {
  83. var renderer = GetSubsystem<Renderer>();
  84. renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>(), null));
  85. }
  86. public override void Start()
  87. {
  88. base.Start();
  89. CreateScene();
  90. SimpleCreateInstructionsWithWasd();
  91. SetupViewport();
  92. }
  93. protected override void Update(float timeStep)
  94. {
  95. SimpleMoveCamera3D(timeStep);
  96. var input = GetSubsystem<Input>();
  97. if (input.GetKeyPress(Constants.KEY_DELETE))
  98. {
  99. scene.GetChild("Boxes", false).RemoveAllChildren();
  100. }
  101. base.Update(timeStep);
  102. }
  103. class Rotator : CSComponent
  104. {
  105. public Vector3 RotationSpeed { get; set; }
  106. void Update(float timeStep)
  107. {
  108. Node.Rotate(new Quaternion(
  109. RotationSpeed.X * timeStep,
  110. RotationSpeed.Y * timeStep,
  111. RotationSpeed.Z * timeStep),
  112. TransformSpace.TS_LOCAL);
  113. }
  114. }
  115. }
  116. }