09_MultipleViewports.cs 8.1 KB

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