05_AnimatingScene.cs 5.7 KB

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