webgl_materials_car.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. background: #eee;
  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. <script type="module">
  33. import * as THREE from '../build/three.module.js';
  34. import Stats from './jsm/libs/stats.module.js';
  35. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  36. import { RoomEnvironment } from './jsm/environments/RoomEnvironment.js';
  37. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  38. import { DRACOLoader } from './jsm/loaders/DRACOLoader.js';
  39. let camera, scene, renderer;
  40. let stats;
  41. let grid;
  42. let controls;
  43. const wheels = [];
  44. function init() {
  45. const container = document.getElementById( 'container' );
  46. renderer = new THREE.WebGLRenderer( { antialias: true } );
  47. renderer.setPixelRatio( window.devicePixelRatio );
  48. renderer.setSize( window.innerWidth, window.innerHeight );
  49. renderer.setAnimationLoop( render );
  50. renderer.outputEncoding = THREE.sRGBEncoding;
  51. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  52. renderer.toneMappingExposure = 0.85;
  53. container.appendChild( renderer.domElement );
  54. window.addEventListener( 'resize', onWindowResize );
  55. stats = new Stats();
  56. container.appendChild( stats.dom );
  57. //
  58. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
  59. camera.position.set( 4.25, 1.4, - 4.5 );
  60. controls = new OrbitControls( camera, container );
  61. controls.target.set( 0, 0.5, 0 );
  62. controls.update();
  63. const environment = new RoomEnvironment();
  64. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  65. scene = new THREE.Scene();
  66. scene.background = new THREE.Color( 0xeeeeee );
  67. scene.environment = pmremGenerator.fromScene( environment ).texture;
  68. scene.fog = new THREE.Fog( 0xeeeeee, 10, 50 );
  69. grid = new THREE.GridHelper( 100, 40, 0x000000, 0x000000 );
  70. grid.material.opacity = 0.1;
  71. grid.material.depthWrite = false;
  72. grid.material.transparent = true;
  73. scene.add( grid );
  74. // materials
  75. const bodyMaterial = new THREE.MeshPhysicalMaterial( {
  76. color: 0xff0000, metalness: 0.6, roughness: 0.4, clearcoat: 0.05, clearcoatRoughness: 0.05
  77. } );
  78. const detailsMaterial = new THREE.MeshStandardMaterial( {
  79. color: 0xffffff, metalness: 1.0, roughness: 0.5
  80. } );
  81. const glassMaterial = new THREE.MeshPhysicalMaterial( {
  82. color: 0xffffff, metalness: 0, roughness: 0.1, transmission: 0.9, transparent: true
  83. } );
  84. const bodyColorInput = document.getElementById( 'body-color' );
  85. bodyColorInput.addEventListener( 'input', function () {
  86. bodyMaterial.color.set( this.value );
  87. } );
  88. const detailsColorInput = document.getElementById( 'details-color' );
  89. detailsColorInput.addEventListener( 'input', function () {
  90. detailsMaterial.color.set( this.value );
  91. } );
  92. const glassColorInput = document.getElementById( 'glass-color' );
  93. glassColorInput.addEventListener( 'input', function () {
  94. glassMaterial.color.set( this.value );
  95. } );
  96. // Car
  97. const shadow = new THREE.TextureLoader().load( 'models/gltf/ferrari_ao.png' );
  98. const dracoLoader = new DRACOLoader();
  99. dracoLoader.setDecoderPath( 'js/libs/draco/gltf/' );
  100. const loader = new GLTFLoader();
  101. loader.setDRACOLoader( dracoLoader );
  102. loader.load( 'models/gltf/ferrari.glb', function ( gltf ) {
  103. const carModel = gltf.scene.children[ 0 ];
  104. carModel.getObjectByName( 'body' ).material = bodyMaterial;
  105. carModel.getObjectByName( 'rim_fl' ).material = detailsMaterial;
  106. carModel.getObjectByName( 'rim_fr' ).material = detailsMaterial;
  107. carModel.getObjectByName( 'rim_rr' ).material = detailsMaterial;
  108. carModel.getObjectByName( 'rim_rl' ).material = detailsMaterial;
  109. carModel.getObjectByName( 'trim' ).material = detailsMaterial;
  110. carModel.getObjectByName( 'glass' ).material = glassMaterial;
  111. wheels.push(
  112. carModel.getObjectByName( 'wheel_fl' ),
  113. carModel.getObjectByName( 'wheel_fr' ),
  114. carModel.getObjectByName( 'wheel_rl' ),
  115. carModel.getObjectByName( 'wheel_rr' )
  116. );
  117. // shadow
  118. const mesh = new THREE.Mesh(
  119. new THREE.PlaneGeometry( 0.655 * 4, 1.3 * 4 ),
  120. new THREE.MeshBasicMaterial( {
  121. map: shadow, blending: THREE.MultiplyBlending, toneMapped: false, transparent: true
  122. } )
  123. );
  124. mesh.rotation.x = - Math.PI / 2;
  125. mesh.renderOrder = 2;
  126. carModel.add( mesh );
  127. scene.add( carModel );
  128. } );
  129. }
  130. function onWindowResize() {
  131. camera.aspect = window.innerWidth / window.innerHeight;
  132. camera.updateProjectionMatrix();
  133. renderer.setSize( window.innerWidth, window.innerHeight );
  134. }
  135. function render() {
  136. const time = - performance.now() / 1000;
  137. for ( let i = 0; i < wheels.length; i ++ ) {
  138. wheels[ i ].rotation.x = time * Math.PI;
  139. }
  140. grid.position.z = - ( time ) % 5;
  141. renderer.render( scene, camera );
  142. stats.update();
  143. }
  144. init();
  145. </script>
  146. </body>
  147. </html>