addpbr.js 7.0 KB

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