webgl_materials_envmaps_groundprojected.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>
  5. threejs webgl - materials - ground projected environment mapping
  6. </title>
  7. <meta charset="utf-8" />
  8. <meta
  9. name="viewport"
  10. content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"
  11. />
  12. <link type="text/css" rel="stylesheet" href="main.css" />
  13. </head>
  14. <body>
  15. <div id="container"></div>
  16. <div id="info">
  17. <a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> - Ground projected environment mapping. By <a href="https://twitter.com/CantBeFaraz" target="_blank" rel="noopener">Faraz Shaikh</a>.<br>
  18. Ferrari 458 Italia model by <a href="https://sketchfab.com/models/57bf6cc56931426e87494f554df1dab6" target="_blank" rel="noopener">vicent091036</a><br>
  19. <a href="https://polyhaven.com/a/blouberg_sunrise_2" target="_blank" rel="noopener">Blouberg Sunrise 2</a> by <a href="https://gregzaal.com/" target="_blank" rel="noopener">Greg Zaal</a>
  20. </div>
  21. <!-- Import maps polyfill -->
  22. <!-- Remove this when import maps will be widely supported -->
  23. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.module.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three';
  34. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. import { GroundProjectedSkybox } from 'three/addons/objects/GroundProjectedSkybox.js';
  37. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  38. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  39. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  40. const params = {
  41. height: 20,
  42. radius: 440
  43. };
  44. let camera, scene, renderer, skybox;
  45. init().then( render );
  46. async function init() {
  47. camera = new THREE.PerspectiveCamera(
  48. 40,
  49. window.innerWidth / window.innerHeight,
  50. 1,
  51. 1000
  52. );
  53. camera.position.set( - 20, 7, 20 );
  54. camera.lookAt( 0, 4, 0 );
  55. scene = new THREE.Scene();
  56. const hdrLoader = new RGBELoader();
  57. const envMap = await hdrLoader.loadAsync( 'textures/equirectangular/blouberg_sunrise_2_1k.hdr' );
  58. envMap.mapping = THREE.EquirectangularReflectionMapping;
  59. skybox = new GroundProjectedSkybox( envMap );
  60. skybox.scale.setScalar( 100 );
  61. scene.add( skybox );
  62. scene.environment = envMap;
  63. const dracoLoader = new DRACOLoader();
  64. dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
  65. const loader = new GLTFLoader();
  66. loader.setDRACOLoader( dracoLoader );
  67. const shadow = new THREE.TextureLoader().load( 'models/gltf/ferrari_ao.png' );
  68. loader.load( 'models/gltf/ferrari.glb', function ( gltf ) {
  69. const bodyMaterial = new THREE.MeshPhysicalMaterial( {
  70. color: 0x000000, metalness: 1.0, roughness: 0.8,
  71. clearcoat: 1.0, clearcoatRoughness: 0.2
  72. } );
  73. const detailsMaterial = new THREE.MeshStandardMaterial( {
  74. color: 0xffffff, metalness: 1.0, roughness: 0.5
  75. } );
  76. const glassMaterial = new THREE.MeshPhysicalMaterial( {
  77. color: 0xffffff, metalness: 0.25, roughness: 0, transmission: 1.0
  78. } );
  79. const carModel = gltf.scene.children[ 0 ];
  80. carModel.scale.multiplyScalar( 4 );
  81. carModel.rotation.y = Math.PI;
  82. carModel.getObjectByName( 'body' ).material = bodyMaterial;
  83. carModel.getObjectByName( 'rim_fl' ).material = detailsMaterial;
  84. carModel.getObjectByName( 'rim_fr' ).material = detailsMaterial;
  85. carModel.getObjectByName( 'rim_rr' ).material = detailsMaterial;
  86. carModel.getObjectByName( 'rim_rl' ).material = detailsMaterial;
  87. carModel.getObjectByName( 'trim' ).material = detailsMaterial;
  88. carModel.getObjectByName( 'glass' ).material = glassMaterial;
  89. // shadow
  90. const mesh = new THREE.Mesh(
  91. new THREE.PlaneGeometry( 0.655 * 4, 1.3 * 4 ),
  92. new THREE.MeshBasicMaterial( {
  93. map: shadow, blending: THREE.MultiplyBlending, toneMapped: false, transparent: true
  94. } )
  95. );
  96. mesh.rotation.x = - Math.PI / 2;
  97. mesh.renderOrder = 2;
  98. carModel.add( mesh );
  99. scene.add( carModel );
  100. render();
  101. } );
  102. //
  103. renderer = new THREE.WebGLRenderer( { antialias: true } );
  104. renderer.setPixelRatio( window.devicePixelRatio );
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. renderer.useLegacyLights = false;
  107. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  108. //
  109. const controls = new OrbitControls( camera, renderer.domElement );
  110. controls.addEventListener( 'change', render );
  111. controls.target.set( 0, 2, 0 );
  112. controls.maxPolarAngle = THREE.MathUtils.degToRad( 90 );
  113. controls.maxDistance = 80;
  114. controls.minDistance = 20;
  115. controls.enablePan = false;
  116. controls.update();
  117. const gui = new GUI();
  118. gui.add( params, 'height', 20, 50, 0.1 ).name( 'Skybox height' ).onChange( render );
  119. gui.add( params, 'radius', 200, 600, 0.1 ).name( 'Skybox radius' ).onChange( render );
  120. //
  121. document.body.appendChild( renderer.domElement );
  122. window.addEventListener( 'resize', onWindowResize );
  123. }
  124. function onWindowResize() {
  125. camera.aspect = window.innerWidth / window.innerHeight;
  126. camera.updateProjectionMatrix();
  127. renderer.setSize( window.innerWidth, window.innerHeight );
  128. render();
  129. }
  130. function render() {
  131. renderer.render( scene, camera );
  132. skybox.radius = params.radius;
  133. skybox.height = params.height;
  134. }
  135. </script>
  136. </body>
  137. </html>