35_SignedDistanceFieldText.as 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Signed distance field text example.
  2. // This sample demonstrates.
  3. // - Creating a 3D scene with static content
  4. // - Creating a 3D text use SDF Font
  5. // - Displaying the scene using the Renderer subsystem
  6. // - Handling keyboard and mouse input to move a freelook camera
  7. #include "Scripts/Utilities/Sample.as"
  8. void Start()
  9. {
  10. // Execute the common startup for samples
  11. SampleStart();
  12. // Create the scene content
  13. CreateScene();
  14. // Create the UI content
  15. CreateInstructions();
  16. // Setup the viewport for displaying the scene
  17. SetupViewport();
  18. // Set the mouse mode to use in the sample
  19. SampleInitMouseMode(MM_RELATIVE);
  20. // Hook up to the frame update events
  21. SubscribeToEvents();
  22. }
  23. void CreateScene()
  24. {
  25. scene_ = Scene();
  26. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  27. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  28. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  29. // optimizing manner
  30. scene_.CreateComponent("Octree");
  31. // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
  32. // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
  33. // (100 x 100 world units)
  34. Node@ planeNode = scene_.CreateChild("Plane");
  35. planeNode.scale = Vector3(100.0f, 1.0f, 100.0f);
  36. StaticModel@ planeObject = planeNode.CreateComponent("StaticModel");
  37. planeObject.model = cache.GetResource("Model", "Models/Plane.mdl");
  38. planeObject.material = cache.GetResource("Material", "Materials/StoneTiled.xml");
  39. // Create a directional light to the world so that we can see something. The light scene node's orientation controls the
  40. // light direction; we will use the SetDirection() function which calculates the orientation from a forward direction vector.
  41. // The light will use default settings (white light, no shadows)
  42. Node@ lightNode = scene_.CreateChild("DirectionalLight");
  43. lightNode.direction = Vector3(0.6f, -1.0f, 0.8f); // The direction vector does not need to be normalized
  44. Light@ light = lightNode.CreateComponent("Light");
  45. light.lightType = LIGHT_DIRECTIONAL;
  46. // Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a
  47. // quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains
  48. // LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll
  49. // see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the
  50. // same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the
  51. // scene.
  52. const uint NUM_OBJECTS = 200;
  53. for (uint i = 0; i < NUM_OBJECTS; ++i)
  54. {
  55. Node@ mushroomNode = scene_.CreateChild("Mushroom");
  56. mushroomNode.position = Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f);
  57. mushroomNode.SetScale(0.5f + Random(2.0f));
  58. StaticModel@ mushroomObject = mushroomNode.CreateComponent("StaticModel");
  59. mushroomObject.model = cache.GetResource("Model", "Models/Mushroom.mdl");
  60. mushroomObject.material = cache.GetResource("Material", "Materials/Mushroom.xml");
  61. Node@ mushroomTitleNode = mushroomNode.CreateChild("MushroomTitle");
  62. mushroomTitleNode.position = Vector3(0.0f, 1.2f, 0.0f);
  63. Text3D@ mushroomTitleText = mushroomTitleNode.CreateComponent("Text3D");
  64. mushroomTitleText.text = "Mushroom " + i;
  65. mushroomTitleText.SetFont(cache.GetResource("Font", "Fonts/BlueHighway.sdf"), 24);
  66. mushroomTitleText.color = Color(1.0f, 0.0f, 0.0f);
  67. if (i % 3 == 1)
  68. {
  69. mushroomTitleText.color = Color(0.0f, 1.0f, 0.0f);
  70. mushroomTitleText.textEffect = TE_SHADOW;
  71. mushroomTitleText.effectColor = Color(0.5f, 0.5f, 0.5f);
  72. }
  73. else if (i % 3 == 2)
  74. {
  75. mushroomTitleText.color = Color(1.0f, 1.0f, 0.0f);
  76. mushroomTitleText.textEffect = TE_STROKE;
  77. mushroomTitleText.effectColor = Color(0.5f, 0.5f, 0.5f);
  78. }
  79. mushroomTitleText.SetAlignment(HA_CENTER, VA_CENTER);
  80. }
  81. // Create a scene node for the camera, which we will move around
  82. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  83. cameraNode = scene_.CreateChild("Camera");
  84. cameraNode.CreateComponent("Camera");
  85. // Set an initial position for the camera scene node above the plane
  86. cameraNode.position = Vector3(0.0f, 5.0f, 0.0f);
  87. }
  88. void CreateInstructions()
  89. {
  90. // Construct new Text object, set string to display and font to use
  91. Text@ instructionText = ui.root.CreateChild("Text");
  92. instructionText.text = "Use WASD keys and mouse to move";
  93. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  94. // Position the text relative to the screen center
  95. instructionText.horizontalAlignment = HA_CENTER;
  96. instructionText.verticalAlignment = VA_CENTER;
  97. instructionText.SetPosition(0, ui.root.height / 4);
  98. }
  99. void SetupViewport()
  100. {
  101. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  102. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  103. // use, but now we just use full screen and default render path configured in the engine command line options
  104. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  105. renderer.viewports[0] = viewport;
  106. }
  107. void MoveCamera(float timeStep)
  108. {
  109. // Do not move if the UI has a focused element (the console)
  110. if (ui.focusElement !is null)
  111. return;
  112. // Movement speed as world units per second
  113. const float MOVE_SPEED = 20.0f;
  114. // Mouse sensitivity as degrees per pixel
  115. const float MOUSE_SENSITIVITY = 0.1f;
  116. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  117. IntVector2 mouseMove = input.mouseMove;
  118. yaw += MOUSE_SENSITIVITY * mouseMove.x;
  119. pitch += MOUSE_SENSITIVITY * mouseMove.y;
  120. pitch = Clamp(pitch, -90.0f, 90.0f);
  121. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  122. cameraNode.rotation = Quaternion(pitch, yaw, 0.0f);
  123. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  124. // Use the Translate() function (default local space) to move relative to the node's orientation.
  125. if (input.keyDown[KEY_W])
  126. cameraNode.Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  127. if (input.keyDown[KEY_S])
  128. cameraNode.Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  129. if (input.keyDown[KEY_A])
  130. cameraNode.Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  131. if (input.keyDown[KEY_D])
  132. cameraNode.Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  133. }
  134. void SubscribeToEvents()
  135. {
  136. // Subscribe HandleUpdate() function for processing update events
  137. SubscribeToEvent("Update", "HandleUpdate");
  138. }
  139. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  140. {
  141. // Take the frame time step, which is stored as a float
  142. float timeStep = eventData["TimeStep"].GetFloat();
  143. // Move the camera, scale movement with time step
  144. MoveCamera(timeStep);
  145. }
  146. // Create XML patch instructions for screen joystick layout specific to this sample app
  147. String patchInstructions = "";