42_PBRMaterials.lua 7.9 KB

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