webgl_materials_car.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 pmremGenerator = new THREE.PMREMGenerator( renderer );
  64. scene = new THREE.Scene();
  65. scene.background = new THREE.Color( 0xeeeeee );
  66. scene.environment = pmremGenerator.fromScene( new RoomEnvironment() ).texture;
  67. scene.fog = new THREE.Fog( 0xeeeeee, 10, 50 );
  68. grid = new THREE.GridHelper( 100, 40, 0x000000, 0x000000 );
  69. grid.material.opacity = 0.1;
  70. grid.material.depthWrite = false;
  71. grid.material.transparent = true;
  72. scene.add( grid );
  73. // materials
  74. const bodyMaterial = new THREE.MeshPhysicalMaterial( {
  75. color: 0xff0000, metalness: 0.6, roughness: 0.4, clearcoat: 0.05, clearcoatRoughness: 0.05
  76. } );
  77. const detailsMaterial = new THREE.MeshStandardMaterial( {
  78. color: 0xffffff, metalness: 1.0, roughness: 0.5
  79. } );
  80. const glassMaterial = new THREE.MeshPhysicalMaterial( {
  81. color: 0xffffff, metalness: 0, roughness: 0.1, transmission: 0.9, transparent: true
  82. } );
  83. const bodyColorInput = document.getElementById( 'body-color' );
  84. bodyColorInput.addEventListener( 'input', function () {
  85. bodyMaterial.color.set( this.value );
  86. } );
  87. const detailsColorInput = document.getElementById( 'details-color' );
  88. detailsColorInput.addEventListener( 'input', function () {
  89. detailsMaterial.color.set( this.value );
  90. } );
  91. const glassColorInput = document.getElementById( 'glass-color' );
  92. glassColorInput.addEventListener( 'input', function () {
  93. glassMaterial.color.set( this.value );
  94. } );
  95. // Car
  96. const shadow = new THREE.TextureLoader().load( 'models/gltf/ferrari_ao.png' );
  97. const dracoLoader = new DRACOLoader();
  98. dracoLoader.setDecoderPath( 'js/libs/draco/gltf/' );
  99. const loader = new GLTFLoader();
  100. loader.setDRACOLoader( dracoLoader );
  101. loader.load( 'models/gltf/ferrari.glb', function ( gltf ) {
  102. const carModel = gltf.scene.children[ 0 ];
  103. carModel.getObjectByName( 'body' ).material = bodyMaterial;
  104. carModel.getObjectByName( 'rim_fl' ).material = detailsMaterial;
  105. carModel.getObjectByName( 'rim_fr' ).material = detailsMaterial;
  106. carModel.getObjectByName( 'rim_rr' ).material = detailsMaterial;
  107. carModel.getObjectByName( 'rim_rl' ).material = detailsMaterial;
  108. carModel.getObjectByName( 'trim' ).material = detailsMaterial;
  109. carModel.getObjectByName( 'glass' ).material = glassMaterial;
  110. wheels.push(
  111. carModel.getObjectByName( 'wheel_fl' ),
  112. carModel.getObjectByName( 'wheel_fr' ),
  113. carModel.getObjectByName( 'wheel_rl' ),
  114. carModel.getObjectByName( 'wheel_rr' )
  115. );
  116. // shadow
  117. const mesh = new THREE.Mesh(
  118. new THREE.PlaneGeometry( 0.655 * 4, 1.3 * 4 ),
  119. new THREE.MeshBasicMaterial( {
  120. map: shadow, blending: THREE.MultiplyBlending, toneMapped: false, transparent: true
  121. } )
  122. );
  123. mesh.rotation.x = - Math.PI / 2;
  124. mesh.renderOrder = 2;
  125. carModel.add( mesh );
  126. scene.add( carModel );
  127. } );
  128. }
  129. function onWindowResize() {
  130. camera.aspect = window.innerWidth / window.innerHeight;
  131. camera.updateProjectionMatrix();
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. }
  134. function render() {
  135. const time = - performance.now() / 1000;
  136. for ( let i = 0; i < wheels.length; i ++ ) {
  137. wheels[ i ].rotation.x = time * Math.PI;
  138. }
  139. grid.position.z = - ( time ) % 5;
  140. renderer.render( scene, camera );
  141. stats.update();
  142. }
  143. init();
  144. </script>
  145. </body>
  146. </html>