35_SignedDistanceFieldTest.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 System;
  24. using AtomicEngine;
  25. namespace FeatureExamples
  26. {
  27. public class SignedDistanceFieldText : Sample
  28. {
  29. public SignedDistanceFieldText() : base() { }
  30. public override void Start()
  31. {
  32. base.Start();
  33. CreateScene();
  34. SimpleCreateInstructionsWithWasd();
  35. SetupViewport();
  36. }
  37. protected override void Update(float timeStep)
  38. {
  39. base.Update(timeStep);
  40. SimpleMoveCamera3D(timeStep);
  41. }
  42. void SetupViewport()
  43. {
  44. var renderer = GetSubsystem<Renderer>();
  45. renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>(), null));
  46. }
  47. void CreateScene()
  48. {
  49. var cache = GetSubsystem<ResourceCache>();
  50. scene = new Scene();
  51. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  52. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  53. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  54. // optimizing manner
  55. scene.CreateComponent<Octree>();
  56. // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
  57. // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
  58. // (100 x 100 world units)
  59. Node planeNode = scene.CreateChild("Plane");
  60. planeNode.Scale = new Vector3(100.0f, 1.0f, 100.0f);
  61. StaticModel planeObject = planeNode.CreateComponent<StaticModel>();
  62. planeObject.Model = (cache.Get<Model>("Models/Plane.mdl"));
  63. planeObject.SetMaterial(cache.Get<Material>("Materials/StoneTiled.xml"));
  64. // Create a directional light to the world so that we can see something. The light scene node's orientation controls the
  65. // light direction; we will use the SetDirection() function which calculates the orientation from a forward direction vector.
  66. // The light will use default settings (white light, no shadows)
  67. Node lightNode = scene.CreateChild("DirectionalLight");
  68. lightNode.SetDirection(new Vector3(0.6f, -1.0f, 0.8f)); // The direction vector does not need to be normalized
  69. Light light = lightNode.CreateComponent<Light>();
  70. light.LightType = LightType.LIGHT_DIRECTIONAL;
  71. // Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a
  72. // quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains
  73. // LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll
  74. // see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the
  75. // same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the
  76. // scene.
  77. const uint numObjects = 200;
  78. for (uint i = 0; i < numObjects; ++i)
  79. {
  80. Node mushroomNode = scene.CreateChild("Mushroom");
  81. mushroomNode.Position = (new Vector3(NextRandom(90.0f) - 45.0f, 0.0f, NextRandom(90.0f) - 45.0f));
  82. mushroomNode.SetScale(0.5f + NextRandom(2.0f));
  83. StaticModel mushroomObject = mushroomNode.CreateComponent<StaticModel>();
  84. mushroomObject.Model = (cache.Get<Model>("Models/Mushroom.mdl"));
  85. mushroomObject.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml"));
  86. Node mushroomTitleNode = mushroomNode.CreateChild("MushroomTitle");
  87. mushroomTitleNode.Position = (new Vector3(0.0f, 1.2f, 0.0f));
  88. Text3D mushroomTitleText = mushroomTitleNode.CreateComponent<Text3D>();
  89. mushroomTitleText.Text = "Mushroom " + i;
  90. mushroomTitleText.SetFont(cache.Get<Text3DFont>("Fonts/Anonymous Pro.sdf"), 24);//sdf, not ttf. size of font doesn't matter.
  91. mushroomTitleText.SetColor(Color.Red);
  92. if (i % 3 == 1)
  93. {
  94. mushroomTitleText.SetColor(Color.Green);
  95. mushroomTitleText.TextEffect = Text3DTextEffect.TE_SHADOW;
  96. mushroomTitleText.EffectColor = new Color(0.5f, 0.5f, 0.5f);
  97. }
  98. else if (i % 3 == 2)
  99. {
  100. mushroomTitleText.SetColor(Color.Yellow);
  101. mushroomTitleText.TextEffect = Text3DTextEffect.TE_STROKE;
  102. mushroomTitleText.EffectColor = new Color(0.5f, 0.5f, 0.5f);
  103. }
  104. mushroomTitleText.SetAlignment(Text3DHorizontalAlignment.HA_CENTER, Text3DVerticalAlignment.VA_CENTER);
  105. }
  106. // Create a scene node for the camera, which we will move around
  107. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  108. CameraNode = scene.CreateChild("Camera");
  109. CameraNode.CreateComponent<Camera>();
  110. // Set an initial position for the camera scene node above the plane
  111. CameraNode.Position = (new Vector3(0.0f, 5.0f, 0.0f));
  112. }
  113. }
  114. }