webgl_materials_envmaps_ground-projected.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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> -
  18. Ground projected environment mapping. By
  19. <a href="https://twitter.com/CantBeFaraz" target="_blank" rel="noopener"
  20. >Faraz Shaikh</a
  21. >.
  22. <br>
  23. Ferrari 458 Italia model by <a href="https://sketchfab.com/models/57bf6cc56931426e87494f554df1dab6" target="_blank" rel="noopener">vicent091036</a>
  24. </div>
  25. <!-- Import maps polyfill -->
  26. <!-- Remove this when import maps will be widely supported -->
  27. <script
  28. async
  29. src="https://unpkg.com/[email protected]/dist/es-module-shims.js"
  30. ></script>
  31. <script type="importmap">
  32. {
  33. "imports": {
  34. "three": "../build/three.module.js"
  35. }
  36. }
  37. </script>
  38. <script type="module">
  39. import * as THREE from 'three';
  40. import Stats from './jsm/libs/stats.module.js';
  41. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  42. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  43. import { GroundProjectedEnv } from './jsm/objects/GroundProjectedEnv.js';
  44. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  45. import { DRACOLoader } from './jsm/loaders/DRACOLoader.js';
  46. import { FlakesTexture } from './jsm/textures/FlakesTexture.js';
  47. const params = {
  48. height: 34,
  49. radius: 440,
  50. toneMappingExposure: 1
  51. };
  52. let camera, scene, renderer, stats, env,dirLight;
  53. init();
  54. animate();
  55. function init() {
  56. initScene();
  57. initMisc();
  58. document.body.appendChild( renderer.domElement );
  59. window.addEventListener( 'resize', onWindowResize );
  60. }
  61. function initScene() {
  62. camera = new THREE.PerspectiveCamera(
  63. 45,
  64. window.innerWidth / window.innerHeight,
  65. 1,
  66. 1000
  67. );
  68. camera.position.set( - 1, 0.3, 1 ).multiplyScalar( 25 );
  69. scene = new THREE.Scene();
  70. dirLight = new THREE.DirectionalLight( 0xffffff, 0.2 );
  71. dirLight.position.set( 10, 8, 10 );
  72. dirLight.castShadow = true;
  73. dirLight.shadow.camera.near = 1;
  74. dirLight.shadow.camera.far = 100;
  75. dirLight.shadow.camera.right = 150;
  76. dirLight.shadow.camera.left = - 150;
  77. dirLight.shadow.camera.top = 150;
  78. dirLight.shadow.camera.bottom = - 150;
  79. dirLight.shadow.mapSize.width = 1024;
  80. dirLight.shadow.mapSize.height = 1024;
  81. scene.add( dirLight );
  82. const geometry = new THREE.PlaneGeometry( 1, 1 );
  83. const material = new THREE.ShadowMaterial( { opacity: 0.3 } );
  84. const ground = new THREE.Mesh( geometry, material );
  85. ground.scale.setScalar( 1000 );
  86. ground.rotation.x = - Math.PI / 2;
  87. ground.position.y = - 0.001;
  88. ground.castShadow = false;
  89. ground.receiveShadow = true;
  90. scene.add( ground );
  91. const cubeLoader = new THREE.CubeTextureLoader();
  92. cubeLoader.setPath( 'textures/cube/lake/' );
  93. const textureCube = cubeLoader.load( [
  94. 'px.png',
  95. 'nx.png',
  96. 'py.png',
  97. 'ny.png',
  98. 'pz.png',
  99. 'nz.png',
  100. ] );
  101. env = new GroundProjectedEnv( textureCube );
  102. env.scale.setScalar( 100 );
  103. scene.add( env );
  104. scene.background = textureCube;
  105. scene.environment = textureCube;
  106. const dracoLoader = new DRACOLoader();
  107. dracoLoader.setDecoderPath( 'js/libs/draco/gltf/' );
  108. const loader = new GLTFLoader();
  109. loader.setDRACOLoader( dracoLoader );
  110. const normalMap3 = new THREE.CanvasTexture( new FlakesTexture() );
  111. normalMap3.wrapS = THREE.RepeatWrapping;
  112. normalMap3.wrapT = THREE.RepeatWrapping;
  113. normalMap3.repeat.x = 10;
  114. normalMap3.repeat.y = 6;
  115. normalMap3.anisotropy = 16;
  116. const bodyMaterial = new THREE.MeshPhysicalMaterial( {
  117. clearcoat: 1.0,
  118. clearcoatRoughness: 0.1,
  119. metalness: 1,
  120. roughness: 0.4,
  121. color: 0xff2800,
  122. normalMap: normalMap3,
  123. normalScale: new THREE.Vector2( 0.15, 0.15 ),
  124. } );
  125. const wheelMaterial = new THREE.MeshPhysicalMaterial( {
  126. clearcoat: 1.0,
  127. clearcoatRoughness: 0.1,
  128. metalness: 0.9,
  129. roughness: 0.5,
  130. color: '#080808',
  131. normalMap: normalMap3,
  132. normalScale: new THREE.Vector2( 0.15, 0.15 ),
  133. } );
  134. const yellowMaterial = new THREE.MeshPhysicalMaterial( {
  135. clearcoat: 1.0,
  136. clearcoatRoughness: 0.1,
  137. metalness: 1,
  138. roughness: 0.2,
  139. color: '#e66b00',
  140. } );
  141. const lightsMaterial = new THREE.MeshPhysicalMaterial( {
  142. emissive: '#ffffff',
  143. color: 'white',
  144. } );
  145. const chromeMaterial = new THREE.MeshPhysicalMaterial( {
  146. clearcoat: 1.0,
  147. clearcoatRoughness: 0.1,
  148. metalness: 0.9,
  149. roughness: 0.5,
  150. color: 0xffffff,
  151. } );
  152. const detailsMaterial = new THREE.MeshStandardMaterial( {
  153. clearcoat: 1.0,
  154. clearcoatRoughness: 0.1,
  155. metalness: 0.9,
  156. roughness: 0.5,
  157. color: 0xffffff,
  158. } );
  159. const glassMaterial = new THREE.MeshPhysicalMaterial( {
  160. color: 0xffffff,
  161. metalness: 0.25,
  162. roughness: 0,
  163. transmission: 1.0,
  164. clearcoat: 1.0,
  165. clearcoatRoughness: 0,
  166. } );
  167. const shadow = new THREE.TextureLoader().load(
  168. 'models/gltf/ferrari_ao.png'
  169. );
  170. loader.load( 'models/gltf/ferrari.glb', function ( gltf ) {
  171. gltf.scene.scale.setScalar( 6 );
  172. const box = new THREE.Box3().setFromObject( gltf.scene );
  173. gltf.scene.position.y = - box.min.y;
  174. gltf.scene.rotation.y = THREE.MathUtils.degToRad( 180 );
  175. gltf.scene.traverse( ( obj ) => {
  176. if ( obj.isMesh ) {
  177. obj.castShadow = obj.recieveShadow = true;
  178. }
  179. } );
  180. gltf.scene.getObjectByName( 'body' ).material = bodyMaterial;
  181. gltf.scene.getObjectByName( 'rim_fl' ).material = detailsMaterial;
  182. gltf.scene.getObjectByName( 'rim_fr' ).material = detailsMaterial;
  183. gltf.scene.getObjectByName( 'rim_rr' ).material = detailsMaterial;
  184. gltf.scene.getObjectByName( 'rim_rl' ).material = detailsMaterial;
  185. gltf.scene.getObjectByName( 'trim' ).material = detailsMaterial;
  186. gltf.scene.getObjectByName( 'glass' ).material = glassMaterial;
  187. gltf.scene.getObjectByName( 'wheel' ).material = wheelMaterial;
  188. gltf.scene.getObjectByName( 'wheel_1' ).material = wheelMaterial;
  189. gltf.scene.getObjectByName( 'wheel_2' ).material = wheelMaterial;
  190. gltf.scene.getObjectByName( 'wheel_3' ).material = wheelMaterial;
  191. gltf.scene.getObjectByName( 'brake' ).material = wheelMaterial;
  192. gltf.scene.getObjectByName( 'interior_dark' ).material = wheelMaterial;
  193. gltf.scene.getObjectByName( 'brake_1' ).material = wheelMaterial;
  194. gltf.scene.getObjectByName( 'brake_2' ).material = wheelMaterial;
  195. gltf.scene.getObjectByName( 'brake_3' ).material = wheelMaterial;
  196. gltf.scene.getObjectByName( 'yellow_trim' ).material = yellowMaterial;
  197. gltf.scene.getObjectByName( 'lights' ).material = lightsMaterial;
  198. gltf.scene.getObjectByName( 'chrome' ).material = chromeMaterial;
  199. gltf.scene.getObjectByName( 'wheel_fl' ).rotation.z =
  200. THREE.MathUtils.degToRad( - 30 );
  201. gltf.scene.getObjectByName( 'wheel_fr' ).rotation.z =
  202. THREE.MathUtils.degToRad( - 30 );
  203. // shadow
  204. const mesh = new THREE.Mesh(
  205. new THREE.PlaneGeometry( 0.655 * 4, 1.3 * 4 ),
  206. new THREE.MeshBasicMaterial( {
  207. map: shadow,
  208. blending: THREE.MultiplyBlending,
  209. toneMapped: false,
  210. transparent: true,
  211. } )
  212. );
  213. mesh.rotation.x = - Math.PI / 2;
  214. mesh.renderOrder = 2;
  215. gltf.scene.add( mesh );
  216. scene.add( gltf.scene );
  217. } );
  218. }
  219. function initMisc() {
  220. renderer = new THREE.WebGLRenderer( { antialias: true } );
  221. renderer.setPixelRatio( window.devicePixelRatio );
  222. renderer.setSize( window.innerWidth, window.innerHeight );
  223. renderer.shadowMap.enabled = true;
  224. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  225. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  226. // Mouse control
  227. const controls = new OrbitControls( camera, renderer.domElement );
  228. controls.target.set( 0, 0, 0 );
  229. controls.maxPolarAngle = THREE.MathUtils.degToRad( 80 );
  230. controls.maxDistance = 100;
  231. controls.minDistance = 30;
  232. controls.enablePan = false;
  233. controls.update();
  234. stats = new Stats();
  235. document.body.appendChild( stats.dom );
  236. const gui = new GUI();
  237. gui.add( params, 'height', 20, 50, 0.1 );
  238. gui.add( params, 'radius', 200, 600, 0.1 );
  239. gui.add( renderer, 'toneMappingExposure', 0, 2, 0.1 ).name( 'exposure' );
  240. }
  241. function onWindowResize() {
  242. camera.aspect = window.innerWidth / window.innerHeight;
  243. camera.updateProjectionMatrix();
  244. renderer.setSize( window.innerWidth, window.innerHeight );
  245. }
  246. function animate() {
  247. requestAnimationFrame( animate );
  248. render();
  249. stats.update();
  250. }
  251. function renderScene() {
  252. renderer.render( scene, camera );
  253. }
  254. function render() {
  255. renderScene();
  256. env.radius = params.radius;
  257. env.height = params.height;
  258. }
  259. </script>
  260. </body>
  261. </html>