pbr.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. var createScene = function () {
  2. var scene = new BABYLON.Scene(engine);
  3. var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 4, Math.PI / 2.5, 200, BABYLON.Vector3.Zero(), scene);
  4. camera.attachControl(canvas, true);
  5. camera.minZ = 0.1;
  6. // Environment Texture
  7. var hdrTexture = new BABYLON.HDRCubeTexture("textures/room.hdr", scene, 512);
  8. var exposure = 0.6;
  9. var contrast = 1.6;
  10. // Skybox
  11. var hdrSkybox = BABYLON.Mesh.CreateBox("hdrSkyBox", 1000.0, scene);
  12. var hdrSkyboxMaterial = new BABYLON.PBRMaterial("skyBox", scene);
  13. hdrSkyboxMaterial.backFaceCulling = false;
  14. hdrSkyboxMaterial.reflectionTexture = hdrTexture.clone();
  15. hdrSkyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  16. hdrSkyboxMaterial.microSurface = 1.0;
  17. hdrSkyboxMaterial.cameraExposure = exposure;
  18. hdrSkyboxMaterial.cameraContrast = contrast;
  19. hdrSkyboxMaterial.disableLighting = true;
  20. hdrSkybox.material = hdrSkyboxMaterial;
  21. hdrSkybox.infiniteDistance = true;
  22. // Create meshes
  23. var sphereGlass = BABYLON.Mesh.CreateSphere("sphereGlass", 48, 30.0, scene);
  24. sphereGlass.translate(new BABYLON.Vector3(1, 0, 0), -60);
  25. var sphereMetal = BABYLON.Mesh.CreateSphere("sphereMetal", 48, 30.0, scene);
  26. sphereMetal.translate(new BABYLON.Vector3(1, 0, 0), 60);
  27. var spherePlastic = BABYLON.Mesh.CreateSphere("spherePlastic", 48, 30.0, scene);
  28. spherePlastic.translate(new BABYLON.Vector3(0, 0, 1), -60);
  29. var woodPlank = BABYLON.MeshBuilder.CreateBox("plane", { width: 65, height: 1, depth: 65 }, scene);
  30. // Create materials
  31. var glass = new BABYLON.PBRMaterial("glass", scene);
  32. glass.reflectionTexture = hdrTexture;
  33. glass.refractionTexture = hdrTexture;
  34. glass.linkRefractionWithTransparency = true;
  35. glass.indexOfRefraction = 0.52;
  36. glass.alpha = 0;
  37. glass.cameraExposure = exposure;
  38. glass.cameraContrast = contrast;
  39. glass.microSurface = 1;
  40. glass.reflectivityColor = new BABYLON.Color3(0.2, 0.2, 0.2);
  41. glass.albedoColor = new BABYLON.Color3(0.85, 0.85, 0.85);
  42. sphereGlass.material = glass;
  43. var metal = new BABYLON.PBRMaterial("metal", scene);
  44. metal.reflectionTexture = hdrTexture;
  45. metal.cameraExposure = exposure;
  46. metal.cameraContrast = 1.6;
  47. metal.microSurface = 0.96;
  48. metal.reflectivityColor = new BABYLON.Color3(0.85, 0.85, 0.85);
  49. metal.albedoColor = new BABYLON.Color3(0.01, 0.01, 0.01);
  50. sphereMetal.material = metal;
  51. var plastic = new BABYLON.PBRMaterial("plastic", scene);
  52. plastic.reflectionTexture = hdrTexture;
  53. plastic.cameraExposure = exposure;
  54. plastic.cameraContrast = contrast;
  55. plastic.microSurface = 0.96;
  56. plastic.albedoColor = new BABYLON.Color3(0.206, 0.94, 1);
  57. plastic.reflectivityColor = new BABYLON.Color3(0.07, 0.07, 0.07);
  58. spherePlastic.material = plastic;
  59. var wood = new BABYLON.PBRMaterial("wood", scene);
  60. wood.reflectionTexture = hdrTexture;
  61. wood.environmentIntensity = 1;
  62. wood.specularIntensity = 0.3;
  63. wood.cameraExposure = exposure;
  64. wood.cameraContrast = contrast;
  65. wood.reflectivityTexture = new BABYLON.Texture("textures/reflectivity.png", scene);
  66. wood.useMicroSurfaceFromReflectivityMapAlpha = true;
  67. wood.albedoColor = BABYLON.Color3.White();
  68. wood.albedoTexture = new BABYLON.Texture("textures/albedo.png", scene);
  69. woodPlank.material = wood;
  70. return scene;
  71. };