1
0

webgl_materials_car.html 6.1 KB

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