30_LightAnimation.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. // Copyright (c) 2015 Xamarin Inc
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. using AtomicEngine;
  24. namespace FeatureExamples
  25. {
  26. public class LightAnimationSample : Sample
  27. {
  28. Scene scene;
  29. public LightAnimationSample() : base() { }
  30. public override void Start()
  31. {
  32. base.Start();
  33. SetupInstructions();
  34. CreateScene();
  35. SetupViewport();
  36. }
  37. void SetupInstructions()
  38. {
  39. SimpleCreateInstructions("Use WASD keys and mouse/touch to move");
  40. }
  41. protected override void Update(float timeStep)
  42. {
  43. SimpleMoveCamera3D(timeStep);
  44. base.Update(timeStep);
  45. }
  46. void SetupViewport()
  47. {
  48. var renderer = GetSubsystem<Renderer>();
  49. renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>()));
  50. }
  51. void CreateScene()
  52. {
  53. var cache = GetSubsystem<ResourceCache>();
  54. scene = new Scene();
  55. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  56. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  57. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  58. // optimizing manner
  59. scene.CreateComponent<Octree>();
  60. // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
  61. // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
  62. // (100 x 100 world units)
  63. Node planeNode = scene.CreateChild("Plane");
  64. planeNode.Scale=new Vector3(100.0f, 1.0f, 100.0f);
  65. StaticModel planeObject = planeNode.CreateComponent<StaticModel>();
  66. planeObject.Model = (cache.Get<Model>("Models/Plane.mdl"));
  67. planeObject.SetMaterial(cache.Get<Material>("Materials/StoneTiled.xml"));
  68. // Create a point light to the world so that we can see something.
  69. Node lightNode = scene.CreateChild("PointLight");
  70. Light light = lightNode.CreateComponent<Light>();
  71. light.LightType = LightType.LIGHT_POINT;
  72. light.Range = (10.0f);
  73. // Create light animation
  74. ObjectAnimation lightAnimation=new ObjectAnimation();
  75. // Create light position animation
  76. ValueAnimation positionAnimation=new ValueAnimation();
  77. // Use spline interpolation method
  78. positionAnimation.InterpolationMethod= InterpMethod.IM_SPLINE;
  79. // Set spline tension
  80. positionAnimation.SplineTension=0.7f;
  81. positionAnimation.SetKeyFrame(0.0f, new Vector3(-30.0f, 5.0f, -30.0f));
  82. positionAnimation.SetKeyFrame(1.0f, new Vector3(30.0f, 5.0f, -30.0f));
  83. positionAnimation.SetKeyFrame(2.0f, new Vector3(30.0f, 5.0f, 30.0f));
  84. positionAnimation.SetKeyFrame(3.0f, new Vector3(-30.0f, 5.0f, 30.0f));
  85. positionAnimation.SetKeyFrame(4.0f, new Vector3(-30.0f, 5.0f, -30.0f));
  86. // Set position animation
  87. lightAnimation.AddAttributeAnimation("Position", positionAnimation, WrapMode.WM_LOOP, 1f);
  88. // Create text animation
  89. /*
  90. ValueAnimation textAnimation=new ValueAnimation();
  91. textAnimation.SetKeyFrame(0.0f, "WHITE");
  92. textAnimation.SetKeyFrame(1.0f, "RED");
  93. textAnimation.SetKeyFrame(2.0f, "YELLOW");
  94. textAnimation.SetKeyFrame(3.0f, "GREEN");
  95. textAnimation.SetKeyFrame(4.0f, "WHITE");
  96. var uiElement = UI.Root.GetChild("animatingText", false);
  97. uiElement.SetAttributeAnimation("Text", textAnimation, WrapMode.Loop, 1f);
  98. */
  99. // Create light color animation
  100. ValueAnimation colorAnimation=new ValueAnimation();
  101. colorAnimation.SetKeyFrame(0.0f, Color.White);
  102. colorAnimation.SetKeyFrame(1.0f, Color.Red);
  103. colorAnimation.SetKeyFrame(2.0f, Color.Yellow);
  104. colorAnimation.SetKeyFrame(3.0f, Color.Green);
  105. colorAnimation.SetKeyFrame(4.0f, Color.White);
  106. // Set Light component's color animation
  107. lightAnimation.AddAttributeAnimation("@Light/Color", colorAnimation, WrapMode.WM_LOOP, 1f);
  108. // Apply light animation to light node
  109. lightNode.ObjectAnimation=lightAnimation;
  110. // Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a
  111. // quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains
  112. // LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll
  113. // see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the
  114. // same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the
  115. // scene.
  116. const uint numObjects = 200;
  117. for (uint i = 0; i < numObjects; ++i)
  118. {
  119. Node mushroomNode = scene.CreateChild("Mushroom");
  120. mushroomNode.Position = (new Vector3(NextRandom(90.0f) - 45.0f, 0.0f, NextRandom(90.0f) - 45.0f));
  121. mushroomNode.Rotation=new Quaternion(0.0f, NextRandom(360.0f), 0.0f);
  122. mushroomNode.SetScale(0.5f + NextRandom(2.0f));
  123. StaticModel mushroomObject = mushroomNode.CreateComponent<StaticModel>();
  124. mushroomObject.Model = (cache.Get<Model>("Models/Mushroom.mdl"));
  125. mushroomObject.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml"));
  126. }
  127. // Create a scene node for the camera, which we will move around
  128. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  129. CameraNode = scene.CreateChild("Camera");
  130. CameraNode.CreateComponent<Camera>();
  131. // Set an initial position for the camera scene node above the plane
  132. CameraNode.Position = (new Vector3(0.0f, 5.0f, 0.0f));
  133. }
  134. }
  135. }