webgl_materials_car.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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: #444;
  11. }
  12. a {
  13. color: #08f;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="info">
  19. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> car materials demo<br/>
  20. Ferrari 458 Italia model by <a href="https://sketchfab.com/models/57bf6cc56931426e87494f554df1dab6" target="_blank" rel="noopener">vicent091036</a>
  21. <br><br>
  22. <span>Body: <select id="body-mat"></select></span>
  23. <span>Details: <select id="rim-mat"></select></span>
  24. <span>Glass: <select id="glass-mat"></select></span>
  25. </div>
  26. <div id="container"></div>
  27. <script type="module">
  28. import * as THREE from '../build/three.module.js';
  29. import Stats from './jsm/libs/stats.module.js';
  30. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  31. import { DRACOLoader } from './jsm/loaders/DRACOLoader.js';
  32. import { RGBELoader } from './jsm/loaders/RGBELoader.js';
  33. var camera, scene, renderer;
  34. var stats, carModel, materialsLib;
  35. var bodyMatSelect = document.getElementById( 'body-mat' );
  36. var rimMatSelect = document.getElementById( 'rim-mat' );
  37. var glassMatSelect = document.getElementById( 'glass-mat' );
  38. var carParts = {
  39. body: [],
  40. rims: [],
  41. glass: [],
  42. };
  43. var grid, wheels = [];
  44. function init() {
  45. var container = document.getElementById( 'container' );
  46. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 200 );
  47. scene = new THREE.Scene();
  48. // scene.fog = new THREE.Fog( 0xd7cbb1, 1, 80 );
  49. new RGBELoader()
  50. .setDataType( THREE.UnsignedByteType )
  51. .setPath( 'textures/equirectangular/' )
  52. .load( 'quarry_01_1k.hdr', function ( texture ) {
  53. var envMap = pmremGenerator.fromEquirectangular( texture ).texture;
  54. pmremGenerator.dispose();
  55. scene.background = envMap;
  56. scene.environment = envMap;
  57. //
  58. initCar();
  59. initMaterials();
  60. initMaterialSelectionMenus();
  61. } );
  62. var ground = new THREE.Mesh(
  63. new THREE.PlaneBufferGeometry( 400, 400 ),
  64. new THREE.MeshBasicMaterial( { color: 0x6e6a62, depthWrite: false } )
  65. );
  66. ground.rotation.x = - Math.PI / 2;
  67. ground.renderOrder = 1;
  68. scene.add( ground );
  69. grid = new THREE.GridHelper( 400, 80, 0x000000, 0x000000 );
  70. grid.material.opacity = 0.1;
  71. grid.material.depthWrite = false;
  72. grid.material.transparent = true;
  73. scene.add( grid );
  74. renderer = new THREE.WebGLRenderer( { antialias: true } );
  75. renderer.setPixelRatio( window.devicePixelRatio );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. container.appendChild( renderer.domElement );
  78. renderer.outputEncoding = THREE.sRGBEncoding;
  79. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  80. var pmremGenerator = new THREE.PMREMGenerator( renderer );
  81. pmremGenerator.compileEquirectangularShader();
  82. stats = new Stats();
  83. container.appendChild( stats.dom );
  84. window.addEventListener( 'resize', onWindowResize, false );
  85. renderer.setAnimationLoop( render );
  86. }
  87. function initCar() {
  88. var dracoLoader = new DRACOLoader();
  89. dracoLoader.setDecoderPath( 'js/libs/draco/gltf/' );
  90. var loader = new GLTFLoader();
  91. loader.setDRACOLoader( dracoLoader );
  92. loader.load( 'models/gltf/ferrari.glb', function ( gltf ) {
  93. carModel = gltf.scene.children[ 0 ];
  94. // shadow
  95. var texture = new THREE.TextureLoader().load( 'models/gltf/ferrari_ao.png' );
  96. var shadow = new THREE.Mesh(
  97. new THREE.PlaneBufferGeometry( 0.655 * 4, 1.3 * 4 ),
  98. new THREE.MeshBasicMaterial( {
  99. map: texture, opacity: 0.7, transparent: true
  100. } )
  101. );
  102. shadow.rotation.x = - Math.PI / 2;
  103. shadow.renderOrder = 2;
  104. carModel.add( shadow );
  105. scene.add( carModel );
  106. // car parts for material selection
  107. carParts.body.push( carModel.getObjectByName( 'body' ) );
  108. carParts.rims.push(
  109. carModel.getObjectByName( 'rim_fl' ),
  110. carModel.getObjectByName( 'rim_fr' ),
  111. carModel.getObjectByName( 'rim_rr' ),
  112. carModel.getObjectByName( 'rim_rl' ),
  113. carModel.getObjectByName( 'trim' ),
  114. );
  115. carParts.glass.push(
  116. carModel.getObjectByName( 'glass' ),
  117. );
  118. wheels.push(
  119. carModel.getObjectByName( 'wheel_fl' ),
  120. carModel.getObjectByName( 'wheel_fr' ),
  121. carModel.getObjectByName( 'wheel_rl' ),
  122. carModel.getObjectByName( 'wheel_rr' )
  123. );
  124. updateMaterials();
  125. } );
  126. }
  127. function initMaterials() {
  128. materialsLib = {
  129. main: [
  130. new THREE.MeshStandardMaterial( {
  131. color: 0xff4400, metalness: 1.0, roughness: 0.2, name: 'orange'
  132. } ),
  133. new THREE.MeshStandardMaterial( {
  134. color: 0x001166, metalness: 1.0, roughness: 0.2, name: 'blue'
  135. } ),
  136. new THREE.MeshStandardMaterial( {
  137. color: 0x990000, metalness: 1.0, roughness: 0.2, name: 'red'
  138. } ),
  139. new THREE.MeshStandardMaterial( {
  140. color: 0x000000, metalness: 1.0, roughness: 0.4, name: 'black'
  141. } ),
  142. new THREE.MeshStandardMaterial( {
  143. color: 0xffffff, metalness: 0.1, roughness: 0.2, name: 'white'
  144. } ),
  145. new THREE.MeshStandardMaterial( {
  146. color: 0xffffff, metalness: 1.0, roughness: 0.2, name: 'metallic'
  147. } ),
  148. ],
  149. glass: [
  150. new THREE.MeshPhysicalMaterial( {
  151. color: 0xffffff, metalness: 1, roughness: 0, transparency: 1.0, transparent: true, name: 'clear'
  152. } ),
  153. new THREE.MeshPhysicalMaterial( {
  154. color: 0x000000, metalness: 1, roughness: 0, transparency: 0.7, transparent: true, name: 'smoked'
  155. } ),
  156. new THREE.MeshPhysicalMaterial( {
  157. color: 0x001133, metalness: 1, roughness: 0, transparency: 0.7, transparent: true, name: 'blue'
  158. } ),
  159. ],
  160. };
  161. }
  162. function initMaterialSelectionMenus() {
  163. function addOption( name, menu ) {
  164. var option = document.createElement( 'option' );
  165. option.text = name;
  166. option.value = name;
  167. menu.add( option );
  168. }
  169. materialsLib.main.forEach( function ( material ) {
  170. addOption( material.name, bodyMatSelect );
  171. addOption( material.name, rimMatSelect );
  172. } );
  173. materialsLib.glass.forEach( function ( material ) {
  174. addOption( material.name, glassMatSelect );
  175. } );
  176. bodyMatSelect.selectedIndex = 2;
  177. rimMatSelect.selectedIndex = 5;
  178. glassMatSelect.selectedIndex = 0;
  179. bodyMatSelect.addEventListener( 'change', updateMaterials );
  180. rimMatSelect.addEventListener( 'change', updateMaterials );
  181. glassMatSelect.addEventListener( 'change', updateMaterials );
  182. }
  183. // set materials to the current values of the selection menus
  184. function updateMaterials() {
  185. var bodyMat = materialsLib.main[ bodyMatSelect.selectedIndex ];
  186. var rimMat = materialsLib.main[ rimMatSelect.selectedIndex ];
  187. var glassMat = materialsLib.glass[ glassMatSelect.selectedIndex ];
  188. carParts.body.forEach( part => part.material = bodyMat );
  189. carParts.rims.forEach( part => part.material = rimMat );
  190. carParts.glass.forEach( part => part.material = glassMat );
  191. }
  192. function onWindowResize() {
  193. camera.aspect = window.innerWidth / window.innerHeight;
  194. camera.updateProjectionMatrix();
  195. renderer.setSize( window.innerWidth, window.innerHeight );
  196. }
  197. function render() {
  198. var time = - performance.now() / 1000;
  199. camera.position.x = Math.cos( time / 10 ) * 6;
  200. camera.position.y = 1.5;
  201. camera.position.z = Math.sin( time / 10 ) * 6;
  202. camera.lookAt( 0, 0.5, 0 );
  203. for ( var i = 0; i < wheels.length; i ++ ) {
  204. wheels[ i ].rotation.x = time * Math.PI;
  205. }
  206. grid.position.z = - ( time ) % 5;
  207. renderer.render( scene, camera );
  208. stats.update();
  209. }
  210. init();
  211. </script>
  212. </body>
  213. </html>