42_PBRMaterials.as 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // PBR materials example.
  2. // This sample demonstrates:
  3. // - Loading a scene that showcases physically based materials & shaders
  4. //
  5. // To use with deferred rendering, a PBR deferred renderpath should be chosen:
  6. // CoreData/RenderPaths/PBRDeferred.xml or CoreData/RenderPaths/PBRDeferredHWDepth.xml
  7. #include "Scripts/Utilities/Sample.as"
  8. Material@ dynamicMaterial;
  9. Text@ roughnessLabel;
  10. Text@ metallicLabel;
  11. Text@ ambientLabel;
  12. Zone@ zone;
  13. void Start()
  14. {
  15. // Execute the common startup for samples
  16. SampleStart();
  17. // Create the scene content
  18. CreateScene();
  19. // Create the UI content and subscribe to UI events
  20. CreateUI();
  21. CreateInstructions();
  22. // Setup the viewport for displaying the scene
  23. SetupViewport();
  24. // Subscribe to global events for camera movement
  25. SubscribeToEvents();
  26. }
  27. void CreateInstructions()
  28. {
  29. // Construct new Text object, set string to display and font to use
  30. Text@ instructionText = ui.root.CreateChild("Text");
  31. instructionText.text = "Use sliders to change Roughness and Metallic\n" +
  32. "Hold RMB and use WASD keys and mouse to move";
  33. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  34. // Position the text relative to the screen center
  35. instructionText.horizontalAlignment = HA_CENTER;
  36. instructionText.verticalAlignment = VA_CENTER;
  37. instructionText.SetPosition(0, ui.root.height / 4);
  38. }
  39. void CreateScene()
  40. {
  41. scene_ = Scene();
  42. // Load scene content prepared in the editor (XML format). GetFile() returns an open file from the resource system
  43. // which scene.LoadXML() will read
  44. scene_.LoadXML(cache.GetFile("Scenes/PBRExample.xml"));
  45. Node@ sphereWithDynamicMatNode = scene_.GetChild("SphereWithDynamicMat");
  46. StaticModel@ staticModel = sphereWithDynamicMatNode.GetComponent("StaticModel");
  47. dynamicMaterial = staticModel.materials[0];
  48. Node@ zoneNode = scene_.GetChild("Zone");
  49. zone = zoneNode.GetComponent("Zone");
  50. // Create the camera (not included in the scene file)
  51. cameraNode = scene_.CreateChild("Camera");
  52. cameraNode.CreateComponent("Camera");
  53. cameraNode.position = sphereWithDynamicMatNode.position + Vector3(2.0f, 2.0f, 2.0f);
  54. cameraNode.LookAt(sphereWithDynamicMatNode.position);
  55. yaw = cameraNode.rotation.yaw;
  56. pitch = cameraNode.rotation.pitch;
  57. }
  58. void CreateUI()
  59. {
  60. // Set up global UI style into the root UI element
  61. XMLFile@ style = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  62. ui.root.defaultStyle = style;
  63. // Create a Cursor UI element because we want to be able to hide and show it at will. When hidden, the mouse cursor will
  64. // control the camera, and when visible, it will interact with the UI
  65. Cursor@ cursor = Cursor();
  66. cursor.SetStyleAuto();
  67. ui.cursor = cursor;
  68. // Set starting position of the cursor at the rendering window center
  69. cursor.SetPosition(graphics.width / 2, graphics.height / 2);
  70. roughnessLabel = ui.root.CreateChild("Text");
  71. roughnessLabel.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  72. roughnessLabel.SetPosition(370, 50);
  73. roughnessLabel.textEffect = TE_SHADOW;
  74. metallicLabel = ui.root.CreateChild("Text");
  75. metallicLabel.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  76. metallicLabel.SetPosition(370, 100);
  77. metallicLabel.textEffect = TE_SHADOW;
  78. ambientLabel = ui.root.CreateChild("Text");
  79. ambientLabel.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  80. ambientLabel.SetPosition(370, 150);
  81. ambientLabel.textEffect = TE_SHADOW;
  82. Slider@ roughnessSlider = ui.root.CreateChild("Slider");
  83. roughnessSlider.SetStyleAuto();
  84. roughnessSlider.SetPosition(50, 50);
  85. roughnessSlider.SetSize(300, 20);
  86. roughnessSlider.range = 1.0f; // 0 - 1 range
  87. SubscribeToEvent(roughnessSlider, "SliderChanged", "HandleRoughnessSliderChanged");
  88. roughnessSlider.value = 0.5f;
  89. Slider@ metallicSlider = ui.root.CreateChild("Slider");
  90. metallicSlider.SetStyleAuto();
  91. metallicSlider.SetPosition(50, 100);
  92. metallicSlider.SetSize(300, 20);
  93. metallicSlider.range = 1.0f; // 0 - 1 range
  94. SubscribeToEvent(metallicSlider, "SliderChanged", "HandleMetallicSliderChanged");
  95. metallicSlider.value = 0.5f;
  96. Slider@ ambientSlider = ui.root.CreateChild("Slider");
  97. ambientSlider.SetStyleAuto();
  98. ambientSlider.SetPosition(50, 150);
  99. ambientSlider.SetSize(300, 20);
  100. ambientSlider.range = 10.0f; // 0 - 10 range
  101. SubscribeToEvent(ambientSlider, "SliderChanged", "HandleAmbientSliderChanged");
  102. ambientSlider.value = zone.ambientColor.a;
  103. }
  104. void HandleRoughnessSliderChanged(StringHash eventType, VariantMap& eventData)
  105. {
  106. float newValue = eventData["Value"].GetFloat();
  107. dynamicMaterial.shaderParameters["Roughness"] = newValue;
  108. roughnessLabel.text = "Roughness: " + newValue;
  109. }
  110. void HandleMetallicSliderChanged(StringHash eventType, VariantMap& eventData)
  111. {
  112. float newValue = eventData["Value"].GetFloat();
  113. dynamicMaterial.shaderParameters["Metallic"] = newValue;
  114. metallicLabel.text = "Metallic: " + newValue;
  115. }
  116. void HandleAmbientSliderChanged(StringHash eventType, VariantMap& eventData)
  117. {
  118. float newValue = eventData["Value"].GetFloat();
  119. Color col = Color(0.0, 0.0, 0.0, newValue);
  120. zone.ambientColor = col;
  121. ambientLabel.text = "Ambient HDR Scale: " + zone.ambientColor.a;
  122. }
  123. void SetupViewport()
  124. {
  125. renderer.hdrRendering = true;
  126. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  127. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  128. renderer.viewports[0] = viewport;
  129. // Add post-processing effects appropriate with the example scene
  130. RenderPath@ effectRenderPath = viewport.renderPath.Clone();
  131. effectRenderPath.Append(cache.GetResource("XMLFile", "PostProcess/FXAA2.xml"));
  132. effectRenderPath.Append(cache.GetResource("XMLFile", "PostProcess/GammaCorrection.xml"));
  133. effectRenderPath.Append(cache.GetResource("XMLFile", "PostProcess/Tonemap.xml"));
  134. effectRenderPath.Append(cache.GetResource("XMLFile", "PostProcess/AutoExposure.xml"));
  135. viewport.renderPath = effectRenderPath;
  136. }
  137. void SubscribeToEvents()
  138. {
  139. // Subscribe HandleUpdate() function for camera motion
  140. SubscribeToEvent("Update", "HandleUpdate");
  141. }
  142. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  143. {
  144. // Take the frame time step, which is stored as a float
  145. float timeStep = eventData["TimeStep"].GetFloat();
  146. // Move the camera, scale movement with time step
  147. MoveCamera(timeStep);
  148. }
  149. void MoveCamera(float timeStep)
  150. {
  151. // Right mouse button controls mouse cursor visibility: hide when pressed
  152. ui.cursor.visible = !input.mouseButtonDown[MOUSEB_RIGHT];
  153. // Do not move if the UI has a focused element
  154. if (ui.focusElement !is null)
  155. return;
  156. // Movement speed as world units per second
  157. const float MOVE_SPEED = 10.0f;
  158. // Mouse sensitivity as degrees per pixel
  159. const float MOUSE_SENSITIVITY = 0.1f;
  160. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  161. // Only move the camera when the cursor is hidden
  162. if (!ui.cursor.visible)
  163. {
  164. IntVector2 mouseMove = input.mouseMove;
  165. yaw += MOUSE_SENSITIVITY * mouseMove.x;
  166. pitch += MOUSE_SENSITIVITY * mouseMove.y;
  167. pitch = Clamp(pitch, -90.0f, 90.0f);
  168. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  169. cameraNode.rotation = Quaternion(pitch, yaw, 0.0f);
  170. }
  171. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  172. if (input.keyDown[KEY_W])
  173. cameraNode.Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  174. if (input.keyDown[KEY_S])
  175. cameraNode.Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  176. if (input.keyDown[KEY_A])
  177. cameraNode.Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  178. if (input.keyDown[KEY_D])
  179. cameraNode.Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  180. }
  181. // Create XML patch instructions for screen joystick layout specific to this sample app
  182. String patchInstructions = "";