31_MaterialAnimation.as 7.4 KB

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