index.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. (function initDemo() {
  2. if (!BABYLON.Engine.isSupported()) {
  3. return;
  4. }
  5. var canvas = document.getElementById("renderCanvas");
  6. var engine = new BABYLON.Engine(canvas, true);
  7. var divFps = document.getElementById("fps");
  8. // Scene + camera.
  9. var scene = new BABYLON.Scene(engine);
  10. var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 4, Math.PI / 2.5, 200, BABYLON.Vector3.Zero(), scene);
  11. camera.attachControl(canvas, true);
  12. camera.minZ = 0.1;
  13. // Register a render loop to repeatedly render the scene
  14. engine.runRenderLoop(function () {
  15. scene.render();
  16. divFps.innerHTML = engine.getFps().toFixed() + " fps";
  17. });
  18. // Light
  19. // Not necessary but add known spec effect...
  20. new BABYLON.PointLight("point", new BABYLON.Vector3(20, 20, 10), scene);
  21. // Environment Texture
  22. var hdrTexture = new BABYLON.HDRCubeTexture("textures/room.hdr", scene, 512);
  23. // Skybox
  24. var hdrSkybox = BABYLON.Mesh.CreateBox("hdrSkyBox", 1000.0, scene);
  25. var hdrSkyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  26. hdrSkyboxMaterial.backFaceCulling = false;
  27. hdrSkyboxMaterial.reflectionTexture = hdrTexture.clone();
  28. hdrSkyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  29. hdrSkyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  30. hdrSkyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  31. hdrSkyboxMaterial.disableLighting = true;
  32. hdrSkybox.material = hdrSkyboxMaterial;
  33. hdrSkybox.infiniteDistance = true;
  34. // Create meshes
  35. var sphereGlass = BABYLON.Mesh.CreateSphere("sphere", 48, 30.0, scene);
  36. sphereGlass.translate(new BABYLON.Vector3(1, 0, 0), -50);
  37. var sphereMetal = BABYLON.Mesh.CreateSphere("sphere", 48, 30.0, scene);
  38. sphereMetal.translate(new BABYLON.Vector3(1, 0, 0), 50);
  39. var woodPlank = BABYLON.MeshBuilder.CreateBox("plane", { width: 45, height: 1, depth: 90 }, scene);
  40. // Create materials
  41. var glass = new BABYLON.PBRMaterial("glass", scene);
  42. glass.reflectionTexture = hdrTexture;
  43. glass.refractionTexture = hdrTexture;
  44. glass.linkRefractionWithTransparency = true;
  45. glass.indexOfRefraction = 0.52;
  46. glass.alpha = 0;
  47. glass.directIntensity = 0.0;
  48. glass.environmentIntensity = 0.5;
  49. glass.cameraExposure = 0.5;
  50. glass.cameraContrast = 1.7;
  51. glass.microSurface = 1;
  52. glass.reflectivityColor = new BABYLON.Color3(0.1, 0.1, 0.1);
  53. glass.albedoColor = new BABYLON.Color3(0.3, 0.3, 0.3);
  54. sphereGlass.material = glass;
  55. var metal = new BABYLON.PBRMaterial("metal", scene);
  56. metal.reflectionTexture = hdrTexture;
  57. metal.directIntensity = 0.3;
  58. metal.environmentIntensity = 0.7;
  59. metal.cameraExposure = 0.6;
  60. metal.cameraContrast = 1.6;
  61. metal.microSurface = 0.96;
  62. metal.reflectivityColor = new BABYLON.Color3(0.9, 0.9, 0.9);
  63. metal.albedoColor = new BABYLON.Color3(1, 1, 1);
  64. sphereMetal.material = metal;
  65. var wood = new BABYLON.PBRMaterial("wood", scene);
  66. wood.reflectionTexture = hdrTexture;
  67. wood.directIntensity = 1.5;
  68. wood.environmentIntensity = 0.5;
  69. wood.specularIntensity = 0.3;
  70. wood.cameraExposure = 0.9;
  71. wood.cameraContrast = 1.6;
  72. wood.reflectivityTexture = new BABYLON.Texture("textures/reflectivity.png", scene);
  73. wood.useMicroSurfaceFromReflectivityMapAlpha = true;
  74. wood.albedoColor = BABYLON.Color3.White();
  75. wood.albedoTexture = new BABYLON.Texture("textures/albedo.png", scene);
  76. woodPlank.material = wood;
  77. })();