23_Water.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 WaterSample : Sample
  28. {
  29. Node waterNode;
  30. Node reflectionCameraNode;
  31. Plane waterPlane;
  32. Plane waterClipPlane;
  33. Graphics graphics;
  34. public WaterSample() : base() {}
  35. public override void Start()
  36. {
  37. graphics = GetSubsystem<Graphics>();
  38. base.Start();
  39. CreateScene();
  40. SimpleCreateInstructionsWithWasd();
  41. SetupViewport();
  42. }
  43. protected override void Update(float timeStep)
  44. {
  45. base.Update(timeStep);
  46. SimpleMoveCamera3D(timeStep, 100f);
  47. var camera = reflectionCameraNode.GetComponent<Camera>();
  48. camera.AspectRatio = (float)graphics.Width / graphics.Height;
  49. }
  50. void SetupViewport()
  51. {
  52. var renderer = GetSubsystem<Renderer>();
  53. var cache = GetSubsystem<ResourceCache>();
  54. renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>()));
  55. // Create a mathematical plane to represent the water in calculations
  56. waterPlane = new Plane(waterNode.WorldRotation * new Vector3(0.0f, 1.0f, 0.0f), waterNode.WorldPosition);
  57. // Create a downward biased plane for reflection view clipping. Biasing is necessary to avoid too aggressive clipping
  58. waterClipPlane = new Plane(waterNode.WorldRotation * new Vector3(0.0f, 1.0f, 0.0f), waterNode.WorldPosition - new Vector3(0.0f, 0.1f, 0.0f));
  59. // Create camera for water reflection
  60. // It will have the same farclip and position as the main viewport camera, but uses a reflection plane to modify
  61. // its position when rendering
  62. reflectionCameraNode = CameraNode.CreateChild();
  63. var reflectionCamera = reflectionCameraNode.CreateComponent<Camera>();
  64. reflectionCamera.FarClip = 750.0f;
  65. reflectionCamera.ViewMask= 0x7fffffff; // Hide objects with only bit 31 in the viewmask (the water plane)
  66. reflectionCamera.AutoAspectRatio = false;
  67. reflectionCamera.UseReflection = true;
  68. reflectionCamera.ReflectionPlane = waterPlane;
  69. reflectionCamera.UseClipping = true; // Enable clipping of geometry behind water plane
  70. reflectionCamera.ClipPlane = waterClipPlane;
  71. // The water reflection texture is rectangular. Set reflection camera aspect ratio to match
  72. reflectionCamera.AspectRatio = (float)graphics.Width / graphics.Height;
  73. // View override flags could be used to optimize reflection rendering. For example disable shadows
  74. //reflectionCamera.ViewOverrideFlags = ViewOverrideFlags.DisableShadows;
  75. // Create a texture and setup viewport for water reflection. Assign the reflection texture to the diffuse
  76. // texture unit of the water material
  77. int texSize = 1024;
  78. Texture2D renderTexture = new Texture2D();
  79. renderTexture.SetSize(texSize, texSize, Graphics.GetRGBFormat(), TextureUsage.TEXTURE_RENDERTARGET);
  80. renderTexture.FilterMode = TextureFilterMode.FILTER_BILINEAR;
  81. RenderSurface surface = renderTexture.RenderSurface;
  82. var rttViewport = new Viewport(scene, reflectionCamera);
  83. surface.SetViewport(0, rttViewport);
  84. var waterMat = cache.Get<Material>("Materials/Water.xml");
  85. waterMat.SetTexture(TextureUnit.TU_DIFFUSE, renderTexture);
  86. }
  87. void CreateScene()
  88. {
  89. var cache = GetSubsystem<ResourceCache>();
  90. scene = new Scene();
  91. // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  92. scene.CreateComponent<Octree>();
  93. // Create a Zone component for ambient lighting & fog control
  94. var zoneNode = scene.CreateChild("Zone");
  95. var zone = zoneNode.CreateComponent<Zone>();
  96. zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
  97. zone.AmbientColor = new Color(0.15f, 0.15f, 0.15f);
  98. zone.FogColor = new Color(1.0f, 1.0f, 1.0f);
  99. zone.FogStart = 500.0f;
  100. zone.FogEnd = 750.0f;
  101. // Create a directional light to the world. Enable cascaded shadows on it
  102. var lightNode = scene.CreateChild("DirectionalLight");
  103. lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f));
  104. var light = lightNode.CreateComponent<Light>();
  105. light.LightType = LightType.LIGHT_DIRECTIONAL;
  106. light.CastShadows = true;
  107. light.ShadowBias = new BiasParameters(0.00025f, 0.5f);
  108. light.ShadowCascade = new CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f);
  109. light.SpecularIntensity = 0.5f;
  110. // Apply slightly overbright lighting to match the skybox
  111. light.Color = new Color(1.2f, 1.2f, 1.2f);
  112. // Create skybox. The Skybox component is used like StaticModel, but it will be always located at the camera, giving the
  113. // illusion of the box planes being far away. Use just the ordinary Box model and a suitable material, whose shader will
  114. // generate the necessary 3D texture coordinates for cube mapping
  115. var skyNode = scene.CreateChild("Sky");
  116. skyNode.SetScale(500.0f); // The scale actually does not matter
  117. var skybox = skyNode.CreateComponent<Skybox>();
  118. skybox.Model = cache.Get<Model>("Models/Box.mdl");
  119. skybox.SetMaterial(cache.Get<Material>("Materials/Skybox.xml"));
  120. // Create heightmap terrain
  121. var terrainNode = scene.CreateChild("Terrain");
  122. terrainNode.Position = new Vector3(0.0f, 0.0f, 0.0f);
  123. var terrain = terrainNode.CreateComponent<Terrain>();
  124. terrain.PatchSize = 64;
  125. terrain.Spacing = new Vector3(2.0f, 0.5f, 2.0f); // Spacing between vertices and vertical resolution of the height map
  126. terrain.Smoothing =true;
  127. terrain.SetHeightMap(cache.Get<Image>("Textures/HeightMap.png"));
  128. terrain.Material = cache.Get<Material>("Materials/Terrain.xml");
  129. // The terrain consists of large triangles, which fits well for occlusion rendering, as a hill can occlude all
  130. // terrain patches and other objects behind it
  131. terrain.Occluder = true;
  132. // Create 1000 boxes in the terrain. Always face outward along the terrain normal
  133. uint numObjects = 1000;
  134. for (uint i = 0; i < numObjects; ++i)
  135. {
  136. var objectNode = scene.CreateChild("Box");
  137. Vector3 position = new Vector3(NextRandom(2000.0f) - 1000.0f, 0.0f, NextRandom(2000.0f) - 1000.0f);
  138. position.Y = terrain.GetHeight(position) + 2.25f;
  139. objectNode.Position = position;
  140. // Create a rotation quaternion from up vector to terrain normal
  141. objectNode.Rotation = Quaternion.FromRotationTo(new Vector3(0.0f, 1.0f, 0.0f), terrain.GetNormal(position));
  142. objectNode.SetScale(5.0f);
  143. var obj = objectNode.CreateComponent<StaticModel>();
  144. obj.Model = cache.Get<Model>("Models/Box.mdl");
  145. obj.SetMaterial(cache.Get<Material>("Materials/Stone.xml"));
  146. obj.CastShadows = true;
  147. }
  148. // Create a water plane object that is as large as the terrain
  149. waterNode = scene.CreateChild("Water");
  150. waterNode.Scale = new Vector3(2048.0f, 1.0f, 2048.0f);
  151. waterNode.Position = new Vector3(0.0f, 5.0f, 0.0f);
  152. var water = waterNode.CreateComponent<StaticModel>();
  153. water.Model = cache.Get<Model>("Models/Plane.mdl");
  154. water.SetMaterial(cache.Get<Material>("Materials/Water.xml"));
  155. // Set a different viewmask on the water plane to be able to hide it from the reflection camera
  156. water.ViewMask = 0x80000000;
  157. // Create the camera. Limit far clip distance to match the fog
  158. CameraNode = new Node();
  159. var camera = CameraNode.CreateComponent<Camera>();
  160. camera.FarClip = 750.0f;
  161. // Set an initial position for the camera scene node above the plane
  162. CameraNode.Position = new Vector3(0.0f, 7.0f, -20.0f);
  163. }
  164. }
  165. }