pbr.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 = BABYLON.CubeTexture.CreateFromPrefilteredData("textures/environment.dds", scene);
  8. scene.imageProcessingConfiguration.exposure = 0.6;
  9. scene.imageProcessingConfiguration.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.disableLighting = true;
  18. hdrSkybox.material = hdrSkyboxMaterial;
  19. hdrSkybox.infiniteDistance = true;
  20. // Create meshes
  21. var sphereGlass = BABYLON.Mesh.CreateSphere("sphereGlass", 48, 30.0, scene);
  22. sphereGlass.translate(new BABYLON.Vector3(1, 0, 0), -60);
  23. var sphereMetal = BABYLON.Mesh.CreateSphere("sphereMetal", 48, 30.0, scene);
  24. sphereMetal.translate(new BABYLON.Vector3(1, 0, 0), 60);
  25. var spherePlastic = BABYLON.Mesh.CreateSphere("spherePlastic", 48, 30.0, scene);
  26. spherePlastic.translate(new BABYLON.Vector3(0, 0, 1), -60);
  27. var woodPlank = BABYLON.MeshBuilder.CreateBox("plane", { width: 65, height: 1, depth: 65 }, scene);
  28. // Create materials
  29. var glass = new BABYLON.PBRMaterial("glass", scene);
  30. glass.reflectionTexture = hdrTexture;
  31. glass.refractionTexture = hdrTexture;
  32. glass.linkRefractionWithTransparency = true;
  33. glass.indexOfRefraction = 0.52;
  34. glass.alpha = 0;
  35. glass.microSurface = 1;
  36. glass.reflectivityColor = new BABYLON.Color3(0.2, 0.2, 0.2);
  37. glass.albedoColor = new BABYLON.Color3(0.85, 0.85, 0.85);
  38. sphereGlass.material = glass;
  39. var metal = new BABYLON.PBRMaterial("metal", scene);
  40. metal.reflectionTexture = hdrTexture;
  41. metal.microSurface = 0.96;
  42. metal.reflectivityColor = new BABYLON.Color3(0.85, 0.85, 0.85);
  43. metal.albedoColor = new BABYLON.Color3(0.01, 0.01, 0.01);
  44. sphereMetal.material = metal;
  45. var plastic = new BABYLON.PBRMaterial("plastic", scene);
  46. plastic.reflectionTexture = hdrTexture;
  47. plastic.microSurface = 0.96;
  48. plastic.albedoColor = new BABYLON.Color3(0.206, 0.94, 1);
  49. plastic.reflectivityColor = new BABYLON.Color3(0.003, 0.003, 0.003);
  50. spherePlastic.material = plastic;
  51. var wood = new BABYLON.PBRMaterial("wood", scene);
  52. wood.reflectionTexture = hdrTexture;
  53. wood.environmentIntensity = 1;
  54. wood.specularIntensity = 0.3;
  55. wood.reflectivityTexture = new BABYLON.Texture("textures/reflectivity.png", scene);
  56. wood.useMicroSurfaceFromReflectivityMapAlpha = true;
  57. wood.albedoColor = BABYLON.Color3.White();
  58. wood.albedoTexture = new BABYLON.Texture("textures/albedo.png", scene);
  59. woodPlank.material = wood;
  60. return scene;
  61. };