42_PBRMaterials.lua 7.0 KB

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