23_Water.cs 8.3 KB

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