addpbr.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. window.preparePBR = function() {
  2. var pbr = new BABYLON.PBRMaterial("pbr", scene);
  3. pbr.albedoTexture = new BABYLON.Texture("/playground/textures/amiga.jpg", scene);
  4. pbr.albedoTexture.uScale = 5;
  5. pbr.albedoTexture.vScale = 5;
  6. var hdrTexture = new BABYLON.HDRCubeTexture("/playground/textures/environment.hdr", scene, 512);
  7. // Uncomment for PMREM Generation
  8. // var hdrTexture = new BABYLON.HDRCubeTexture("textures/hdr/environment.hdr", scene, 128, false, true, false, true);
  9. pbr.reflectionTexture = hdrTexture;
  10. pbr.refractionTexture = hdrTexture;
  11. pbr.linkRefractionWithTransparency = true;
  12. pbr.indexOfRefraction = 0.52;
  13. var colorGrading = new BABYLON.Texture("/playground/textures/colorGrade.png", scene, true);
  14. pbr.cameraColorGradingTexture = colorGrading;
  15. pbr.colorGradingEnabled = true;
  16. pbr.reflectivityColor = new BABYLON.Color3(0.3, 0.3, 0.3);
  17. pbr.microSurface = 0.9;
  18. // Skybox
  19. var hdrSkybox = BABYLON.Mesh.CreateBox("hdrSkyBox", 1000.0, scene);
  20. var hdrSkyboxMaterial = new BABYLON.PBRMaterial("skyBox", scene);
  21. hdrSkyboxMaterial.backFaceCulling = false;
  22. hdrSkyboxMaterial.reflectionTexture = hdrTexture.clone();
  23. hdrSkyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  24. hdrSkyboxMaterial.microSurface = 1;
  25. hdrSkyboxMaterial.specularColor = new BABYLON.Color3(1, 1, 1);
  26. hdrSkyboxMaterial.disableLighting = true;
  27. hdrSkyboxMaterial.cameraExposure = 0.6;
  28. hdrSkyboxMaterial.cameraContrast = 1.6;
  29. hdrSkyboxMaterial.directIntensity = 0;
  30. hdrSkybox.material = hdrSkyboxMaterial;
  31. hdrSkybox.infiniteDistance = true;
  32. hdrSkybox.setEnabled(false);
  33. registerButtonUI("pbr", "Default", function() {
  34. setRangeValues({
  35. "directIntensity": 1,
  36. "emissiveIntensity": 1,
  37. "environmentIntensity": 1,
  38. "specularIntensity": 1,
  39. "ShadowIntensity": 1,
  40. "ShadeIntensity": 1,
  41. "cameraExposure": 1,
  42. "cameraContrast": 1,
  43. "microSurface": 0.9,
  44. "reflectivityColorR": 0.3,
  45. "reflectivityColorG": 0.3,
  46. "reflectivityColorB": 0.3,
  47. "albedoColorR": 1,
  48. "albedoColorG": 1,
  49. "albedoColorB": 1,
  50. "albedoColorLevel": 0
  51. });
  52. });
  53. registerButtonUI("pbr", "Env Irradiance", function() {
  54. setRangeValues({
  55. "directIntensity": 0,
  56. "emissiveIntensity": 1,
  57. "environmentIntensity": 1,
  58. "specularIntensity": 1,
  59. "ShadowIntensity": 1,
  60. "ShadeIntensity": 1,
  61. "cameraExposure": 1,
  62. "cameraContrast": 1,
  63. "microSurface": 0,
  64. "reflectivityColorR": 0,
  65. "reflectivityColorG": 0,
  66. "reflectivityColorB": 0,
  67. "albedoColorR": 1,
  68. "albedoColorG": 1,
  69. "albedoColorB": 1,
  70. "albedoColorLevel": 1
  71. });
  72. hdrSkybox.setEnabled(true);
  73. });
  74. registerButtonUI("pbr", "Rough Gold", function() {
  75. setRangeValues({
  76. "directIntensity": 1.3439461727881254,
  77. "emissiveIntensity": 1,
  78. "environmentIntensity": 0.3685013699580344,
  79. "specularIntensity": 1,
  80. "ShadowIntensity": 1,
  81. "ShadeIntensity": 1,
  82. "cameraExposure": 0.7153261887420668,
  83. "cameraContrast": 1.6474178892241538,
  84. "microSurface": 0.42269274789303946,
  85. "reflectivityColorR": 1,
  86. "reflectivityColorG": 0.8453854957860789,
  87. "reflectivityColorB": 0.5093989525890475,
  88. "albedoColorR": 0,
  89. "albedoColorG": 0,
  90. "albedoColorB": 0,
  91. "albedoColorLevel": 1
  92. });
  93. });
  94. registerButtonUI("pbr", "Plastic", function() {
  95. setRangeValues({
  96. "directIntensity": 0.9971213540040931,
  97. "emissiveIntensity": 1,
  98. "environmentIntensity": 0.3685013699580344,
  99. "specularIntensity": 1,
  100. "ShadowIntensity": 0.975444802830091,
  101. "ShadeIntensity": 0.8020323934380749,
  102. "cameraExposure": 0.7586792910900708,
  103. "cameraContrast": 1.5823882357021477,
  104. "microSurface": 0.8562237713730799,
  105. "reflectivityColorR": 0.05,
  106. "reflectivityColorG": 0.05,
  107. "reflectivityColorB": 0.05,
  108. "albedoColorR": 0.20592723615301922,
  109. "albedoColorG": 0.942929976069088,
  110. "albedoColorB": 1,
  111. "albedoColorLevel": 1
  112. });
  113. });
  114. registerRangeUI("pbr", "indiceOfRefraction", 0, 2, function(value) {
  115. pbr.indexOfRefraction = value;
  116. }, function() {
  117. return pbr.indexOfRefraction;
  118. });
  119. registerRangeUI("pbr", "alpha", 0, 1, function(value) {
  120. pbr.alpha = value;
  121. }, function() {
  122. return pbr.alpha;
  123. });
  124. registerRangeUI("pbr", "directIntensity", 0, 2, function(value) {
  125. pbr.directIntensity = value;
  126. }, function() {
  127. return pbr.directIntensity;
  128. });
  129. registerRangeUI("pbr", "emissiveIntensity", 0, 2, function(value) {
  130. pbr.emissiveIntensity = value;
  131. }, function() {
  132. return pbr.emissiveIntensity;
  133. });
  134. registerRangeUI("pbr", "environmentIntensity", 0, 2, function(value) {
  135. pbr.environmentIntensity = value;
  136. }, function() {
  137. return pbr.environmentIntensity;
  138. });
  139. registerRangeUI("pbr", "specularIntensity", 0, 2, function(value) {
  140. pbr.specularIntensity = value;
  141. }, function() {
  142. return pbr.specularIntensity;
  143. });
  144. registerRangeUI("pbr", "cameraExposure", 0, 2, function(value) {
  145. pbr.cameraExposure = value;
  146. }, function() {
  147. return pbr.cameraExposure;
  148. });
  149. registerRangeUI("pbr", "cameraContrast", 0, 2, function(value) {
  150. pbr.cameraContrast = value;
  151. }, function() {
  152. return pbr.cameraContrast;
  153. });
  154. registerRangeUI("pbr", "microSurface", 0, 1, function(value) {
  155. pbr.microSurface = value;
  156. }, function() {
  157. return pbr.microSurface;
  158. });
  159. registerRangeUI("pbr", "reflectivityColorR", 0, 1, function(value) {
  160. pbr.reflectivityColor.r = value;
  161. }, function() {
  162. return pbr.reflectivityColor.r;
  163. });
  164. registerRangeUI("pbr", "reflectivityColorG", 0, 1, function(value) {
  165. pbr.reflectivityColor.g = value;
  166. }, function() {
  167. return pbr.reflectivityColor.g;
  168. });
  169. registerRangeUI("pbr", "reflectivityColorB", 0, 1, function(value) {
  170. pbr.reflectivityColor.b = value;
  171. }, function() {
  172. return pbr.reflectivityColor.b;
  173. });
  174. registerRangeUI("pbr", "albedoColorR", 0, 1, function(value) {
  175. pbr.albedoColor.r = value;
  176. }, function() {
  177. return pbr.albedoColor.r;
  178. });
  179. registerRangeUI("pbr", "albedoColorG", 0, 1, function(value) {
  180. pbr.albedoColor.g = value;
  181. }, function() {
  182. return pbr.albedoColor.g;
  183. });
  184. registerRangeUI("pbr", "albedoColorB", 0, 1, function(value) {
  185. pbr.albedoColor.b = value;
  186. }, function() {
  187. return pbr.albedoColor.b;
  188. });
  189. registerRangeUI("pbr", "albedoColorLevel", 0, 1, function(value) {
  190. pbr.albedoTexture.level = value;
  191. }, function() {
  192. return pbr.albedoTexture.level;
  193. });
  194. registerButtonUI("pbr", "Toggle Skybox", function() {
  195. hdrSkybox.setEnabled(!hdrSkybox.isEnabled());
  196. });
  197. registerButtonUI("pbr", "Color Grading", function() {
  198. pbr.cameraColorGradingTexture = pbr.cameraColorGradingTexture ? null : colorGradingTexture;
  199. });
  200. return pbr;
  201. }