09_MultipleViewports.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 MultipleViewportsSample : Sample
  28. {
  29. bool drawDebug;
  30. Node rearCameraNode;
  31. public MultipleViewportsSample() : base() { }
  32. public override void Start()
  33. {
  34. base.Start();
  35. CreateScene();
  36. SimpleCreateInstructionsWithWasd(
  37. "\nB to toggle bloom, F to toggle FXAA\n" +
  38. "Space to toggle debug geometry\n");
  39. SetupViewport();
  40. SubscribeToEvents();
  41. }
  42. void SubscribeToEvents()
  43. {
  44. SubscribeToEvent<PostRenderUpdateEvent>(e =>
  45. {
  46. // If draw debug mode is enabled, draw viewport debug geometry, which will show eg. drawable bounding boxes and skeleton
  47. // bones. Note that debug geometry has to be separately requested each frame. Disable depth test so that we can see the
  48. // bones properly
  49. if (drawDebug)
  50. GetSubsystem<Renderer>().DrawDebugGeometry(false);
  51. });
  52. }
  53. protected override void Update(float timeStep)
  54. {
  55. base.Update(timeStep);
  56. SimpleMoveCamera3D(timeStep);
  57. var input = GetSubsystem<Input>();
  58. var viewport = GetSubsystem<Renderer>().GetViewport(0);
  59. var effectRenderPath = viewport.GetRenderPath();
  60. if (input.GetKeyPress(Constants.KEY_B))
  61. effectRenderPath.ToggleEnabled("Bloom");
  62. if (input.GetKeyPress(Constants.KEY_F))
  63. effectRenderPath.ToggleEnabled("FXAA2");
  64. if (input.GetKeyPress(Constants.KEY_SPACE))
  65. drawDebug = !drawDebug;
  66. }
  67. void SetupViewport()
  68. {
  69. var renderer = GetSubsystem<Renderer>();
  70. var graphics = GetSubsystem<Graphics>();
  71. renderer.NumViewports = 2;
  72. // Set up the front camera viewport
  73. Viewport viewport = new Viewport(scene, CameraNode.GetComponent<Camera>());
  74. renderer.SetViewport(0, viewport);
  75. // Clone the default render path so that we do not interfere with the other viewport, then add
  76. // bloom and FXAA post process effects to the front viewport. Render path commands can be tagged
  77. // for example with the effect name to allow easy toggling on and off. We start with the effects
  78. // disabled.
  79. var cache = GetSubsystem<ResourceCache>();
  80. RenderPath effectRenderPath = viewport.GetRenderPath().Clone();
  81. effectRenderPath.Append(cache.Get<XMLFile>("PostProcess/Bloom.xml"));
  82. effectRenderPath.Append(cache.Get<XMLFile>("PostProcess/FXAA2.xml"));
  83. // Make the bloom mixing parameter more pronounced
  84. effectRenderPath.SetShaderParameter("BloomMix", new Vector2(0.9f, 0.6f));
  85. effectRenderPath.SetEnabled("Bloom", false);
  86. effectRenderPath.SetEnabled("FXAA2", false);
  87. viewport.SetRenderPath(effectRenderPath);
  88. // Set up the rear camera viewport on top of the front view ("rear view mirror")
  89. // The viewport index must be greater in that case, otherwise the view would be left behind
  90. IntRect rect = new IntRect(graphics.Width*2/3, 32, graphics.Width - 32, graphics.Height/3);
  91. Viewport rearViewport = new Viewport(scene, rearCameraNode.GetComponent<Camera>(), rect);
  92. renderer.SetViewport(1, rearViewport);
  93. }
  94. void CreateScene()
  95. {
  96. var cache = GetSubsystem<ResourceCache>();
  97. scene = new Scene();
  98. // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  99. // Also create a DebugRenderer component so that we can draw debug geometry
  100. scene.CreateComponent<Octree>();
  101. scene.CreateComponent<DebugRenderer>();
  102. // Create scene node & StaticModel component for showing a static plane
  103. var planeNode = scene.CreateChild("Plane");
  104. planeNode.Scale =new Vector3(100.0f, 1.0f, 100.0f);
  105. var planeObject = planeNode.CreateComponent<StaticModel>();
  106. planeObject.Model=cache.Get<Model>("Models/Plane.mdl");
  107. planeObject.SetMaterial(cache.Get<Material>("Materials/StoneTiled.xml"));
  108. // Create a Zone component for ambient lighting & fog control
  109. var zoneNode = scene.CreateChild("Zone");
  110. var zone = zoneNode.CreateComponent<Zone>();
  111. zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
  112. zone.AmbientColor=new Color(0.15f, 0.15f, 0.15f);
  113. zone.FogColor=new Color(0.5f, 0.5f, 0.7f);
  114. zone.FogStart=100.0f;
  115. zone.FogEnd=300.0f;
  116. // Create a directional light to the world. Enable cascaded shadows on it
  117. var lightNode = scene.CreateChild("DirectionalLight");
  118. lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f));
  119. var light = lightNode.CreateComponent<Light>();
  120. light.LightType = LightType.LIGHT_DIRECTIONAL;
  121. light.CastShadows=true;
  122. light.ShadowBias=new BiasParameters(0.00025f, 0.5f);
  123. // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
  124. light.ShadowCascade=new CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f);
  125. // Create some mushrooms
  126. const uint numMushrooms = 240;
  127. for (uint i = 0; i < numMushrooms; ++i)
  128. {
  129. var mushroomNode = scene.CreateChild("Mushroom");
  130. mushroomNode.Position = new Vector3(NextRandom(90.0f) - 45.0f, 0.0f, NextRandom(90.0f) - 45.0f);
  131. mushroomNode.Rotation = new Quaternion(0.0f, NextRandom(360.0f), 0.0f);
  132. mushroomNode.SetScale(0.5f + NextRandom(2.0f));
  133. StaticModel 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. // Create randomly sized boxes. If boxes are big enough, make them occluders
  139. const uint numBoxes = 20;
  140. for (uint i = 0; i < numBoxes; ++i)
  141. {
  142. var boxNode = scene.CreateChild("Box");
  143. float size = 1.0f + NextRandom(10.0f);
  144. boxNode.Position=new Vector3(NextRandom(80.0f) - 40.0f, size * 0.5f, NextRandom(80.0f) - 40.0f);
  145. boxNode.SetScale(size);
  146. StaticModel boxObject = boxNode.CreateComponent<StaticModel>();
  147. boxObject.Model = cache.Get<Model>("Models/Box.mdl");
  148. boxObject.SetMaterial(cache.Get<Material>("Materials/Stone.xml"));
  149. boxObject.CastShadows=true;
  150. if (size >= 3.0f)
  151. boxObject.Occluder=true;
  152. }
  153. // Create the cameras. Limit far clip distance to match the fog
  154. CameraNode = scene.CreateChild("Camera");
  155. Camera camera = CameraNode.CreateComponent<Camera>();
  156. camera.FarClip = 300.0f;
  157. // Parent the rear camera node to the front camera node and turn it 180 degrees to face backward
  158. // Here, we use the angle-axis constructor for Quaternion instead of the usual Euler angles
  159. rearCameraNode = CameraNode.CreateChild("RearCamera");
  160. rearCameraNode.Rotate(Quaternion.FromAxisAngle(Vector3.UnitY, 180.0f), TransformSpace.TS_LOCAL);
  161. Camera rearCamera = rearCameraNode.CreateComponent<Camera>();
  162. rearCamera.FarClip = 300.0f;
  163. // Because the rear viewport is rather small, disable occlusion culling from it. Use the camera's
  164. // "view override flags" for this. We could also disable eg. shadows or force low material quality
  165. // if we wanted
  166. rearCamera.ViewOverrideFlags = Constants.VO_DISABLE_OCCLUSION;
  167. // Set an initial position for the front camera scene node above the plane
  168. CameraNode.Position = new Vector3(0.0f, 5.0f, 0.0f);
  169. }
  170. }
  171. }