webgl_materials_car.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - car</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. color: #bbbbbb;
  11. background: #333333;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. .colorPicker {
  17. display: inline-block;
  18. margin: 0 10px
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="info">
  24. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> car materials<br/>
  25. Ferrari 458 Italia model by <a href="https://sketchfab.com/models/57bf6cc56931426e87494f554df1dab6" target="_blank" rel="noopener">vicent091036</a>
  26. <br><br>
  27. <span class="colorPicker"><input id="body-color" type="color" value="#ff0000"></input><br/>Body</span>
  28. <span class="colorPicker"><input id="details-color" type="color" value="#ffffff"></input><br/>Details</span>
  29. <span class="colorPicker"><input id="glass-color" type="color" value="#ffffff"></input><br/>Glass</span>
  30. </div>
  31. <div id="container"></div>
  32. <!-- Import maps polyfill -->
  33. <!-- Remove this when import maps will be widely supported -->
  34. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  35. <script type="importmap">
  36. {
  37. "imports": {
  38. "three": "../build/three.module.js",
  39. "three/addons/": "./jsm/"
  40. }
  41. }
  42. </script>
  43. <script type="module">
  44. import * as THREE from 'three';
  45. import Stats from 'three/addons/libs/stats.module.js';
  46. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  47. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  48. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  49. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  50. let camera, scene, renderer;
  51. let stats;
  52. let grid;
  53. let controls;
  54. const wheels = [];
  55. function init() {
  56. const container = document.getElementById( 'container' );
  57. renderer = new THREE.WebGLRenderer( { antialias: true } );
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.setAnimationLoop( render );
  61. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  62. renderer.toneMappingExposure = 0.85;
  63. container.appendChild( renderer.domElement );
  64. window.addEventListener( 'resize', onWindowResize );
  65. stats = new Stats();
  66. container.appendChild( stats.dom );
  67. //
  68. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
  69. camera.position.set( 4.25, 1.4, - 4.5 );
  70. controls = new OrbitControls( camera, container );
  71. controls.enableDamping = true;
  72. controls.maxDistance = 9;
  73. controls.target.set( 0, 0.5, 0 );
  74. controls.update();
  75. scene = new THREE.Scene();
  76. scene.background = new THREE.Color( 0x333333 );
  77. scene.environment = new RGBELoader().load( 'textures/equirectangular/venice_sunset_1k.hdr' );
  78. scene.environment.mapping = THREE.EquirectangularReflectionMapping;
  79. scene.fog = new THREE.Fog( 0x333333, 10, 15 );
  80. grid = new THREE.GridHelper( 20, 40, 0xffffff, 0xffffff );
  81. grid.material.opacity = 0.2;
  82. grid.material.depthWrite = false;
  83. grid.material.transparent = true;
  84. scene.add( grid );
  85. // materials
  86. const bodyMaterial = new THREE.MeshPhysicalMaterial( {
  87. color: 0xff0000, metalness: 1.0, roughness: 0.5, clearcoat: 1.0, clearcoatRoughness: 0.03, sheen: 0.5
  88. } );
  89. const detailsMaterial = new THREE.MeshStandardMaterial( {
  90. color: 0xffffff, metalness: 1.0, roughness: 0.5
  91. } );
  92. const glassMaterial = new THREE.MeshPhysicalMaterial( {
  93. color: 0xffffff, metalness: 0.25, roughness: 0, transmission: 1.0
  94. } );
  95. const bodyColorInput = document.getElementById( 'body-color' );
  96. bodyColorInput.addEventListener( 'input', function () {
  97. bodyMaterial.color.set( this.value );
  98. } );
  99. const detailsColorInput = document.getElementById( 'details-color' );
  100. detailsColorInput.addEventListener( 'input', function () {
  101. detailsMaterial.color.set( this.value );
  102. } );
  103. const glassColorInput = document.getElementById( 'glass-color' );
  104. glassColorInput.addEventListener( 'input', function () {
  105. glassMaterial.color.set( this.value );
  106. } );
  107. // Car
  108. const shadow = new THREE.TextureLoader().load( 'models/gltf/ferrari_ao.png' );
  109. const dracoLoader = new DRACOLoader();
  110. dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
  111. const loader = new GLTFLoader();
  112. loader.setDRACOLoader( dracoLoader );
  113. loader.load( 'models/gltf/ferrari.glb', function ( gltf ) {
  114. const carModel = gltf.scene.children[ 0 ];
  115. carModel.getObjectByName( 'body' ).material = bodyMaterial;
  116. carModel.getObjectByName( 'rim_fl' ).material = detailsMaterial;
  117. carModel.getObjectByName( 'rim_fr' ).material = detailsMaterial;
  118. carModel.getObjectByName( 'rim_rr' ).material = detailsMaterial;
  119. carModel.getObjectByName( 'rim_rl' ).material = detailsMaterial;
  120. carModel.getObjectByName( 'trim' ).material = detailsMaterial;
  121. carModel.getObjectByName( 'glass' ).material = glassMaterial;
  122. wheels.push(
  123. carModel.getObjectByName( 'wheel_fl' ),
  124. carModel.getObjectByName( 'wheel_fr' ),
  125. carModel.getObjectByName( 'wheel_rl' ),
  126. carModel.getObjectByName( 'wheel_rr' )
  127. );
  128. // shadow
  129. const mesh = new THREE.Mesh(
  130. new THREE.PlaneGeometry( 0.655 * 4, 1.3 * 4 ),
  131. new THREE.MeshBasicMaterial( {
  132. map: shadow, blending: THREE.MultiplyBlending, toneMapped: false, transparent: true
  133. } )
  134. );
  135. mesh.rotation.x = - Math.PI / 2;
  136. mesh.renderOrder = 2;
  137. carModel.add( mesh );
  138. scene.add( carModel );
  139. } );
  140. }
  141. function onWindowResize() {
  142. camera.aspect = window.innerWidth / window.innerHeight;
  143. camera.updateProjectionMatrix();
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. }
  146. function render() {
  147. controls.update();
  148. const time = - performance.now() / 1000;
  149. for ( let i = 0; i < wheels.length; i ++ ) {
  150. wheels[ i ].rotation.x = time * Math.PI * 2;
  151. }
  152. grid.position.z = - ( time ) % 1;
  153. renderer.render( scene, camera );
  154. stats.update();
  155. }
  156. init();
  157. </script>
  158. </body>
  159. </html>