webgl_materials_cars.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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="http://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. <br><br>
  26. <span>Follow camera: <input type="checkbox" id="camera-toggle"></span>
  27. </div>
  28. <div id="container"></div>
  29. <script type="module">
  30. import * as THREE from '../build/three.module.js';
  31. import Stats from './jsm/libs/stats.module.js';
  32. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  33. import { DRACOLoader } from './jsm/loaders/DRACOLoader.js';
  34. import { CarControls } from './jsm/misc/CarControls.js';
  35. import { PMREMGenerator } from './jsm/pmrem/PMREMGenerator.js';
  36. import { PMREMCubeUVPacker } from './jsm/pmrem/PMREMCubeUVPacker.js';
  37. var camera, scene, renderer, stats, carModel, materialsLib, envMap;
  38. var bodyMatSelect = document.getElementById( 'body-mat' );
  39. var rimMatSelect = document.getElementById( 'rim-mat' );
  40. var glassMatSelect = document.getElementById( 'glass-mat' );
  41. var followCamera = document.getElementById( 'camera-toggle' );
  42. var clock = new THREE.Clock();
  43. var carControls = new CarControls();
  44. carControls.turningRadius = 75;
  45. var carParts = {
  46. body: [],
  47. rims: [],
  48. glass: [],
  49. };
  50. var damping = 5.0;
  51. var distance = 5;
  52. var cameraTarget = new THREE.Vector3();
  53. function init() {
  54. var container = document.getElementById( 'container' );
  55. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 200 );
  56. camera.position.set( 3.25, 2.0, - 5 );
  57. camera.lookAt( 0, 0.5, 0 );
  58. scene = new THREE.Scene();
  59. scene.fog = new THREE.Fog( 0xd7cbb1, 1, 80 );
  60. var urls = [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ];
  61. var loader = new THREE.CubeTextureLoader().setPath( 'textures/cube/skyboxsun25deg/' );
  62. loader.load( urls, function ( texture ) {
  63. scene.background = texture;
  64. var pmremGenerator = new PMREMGenerator( texture );
  65. pmremGenerator.update( renderer );
  66. var pmremCubeUVPacker = new PMREMCubeUVPacker( pmremGenerator.cubeLods );
  67. pmremCubeUVPacker.update( renderer );
  68. envMap = pmremCubeUVPacker.CubeUVRenderTarget.texture;
  69. pmremGenerator.dispose();
  70. pmremCubeUVPacker.dispose();
  71. //
  72. initCar();
  73. initMaterials();
  74. initMaterialSelectionMenus();
  75. } );
  76. var ground = new THREE.Mesh(
  77. new THREE.PlaneBufferGeometry( 2400, 2400 ),
  78. new THREE.ShadowMaterial( { color: 0x000000, opacity: 0.15, depthWrite: false }
  79. ) );
  80. ground.rotation.x = - Math.PI / 2;
  81. ground.receiveShadow = true;
  82. ground.renderOrder = 1;
  83. scene.add( ground );
  84. var grid = new THREE.GridHelper( 400, 40, 0x000000, 0x000000 );
  85. grid.material.opacity = 0.2;
  86. grid.material.depthWrite = false;
  87. grid.material.transparent = true;
  88. scene.add( grid );
  89. renderer = new THREE.WebGLRenderer( { antialias: true } );
  90. renderer.gammaOutput = true;
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. container.appendChild( renderer.domElement );
  94. stats = new Stats();
  95. container.appendChild( stats.dom );
  96. window.addEventListener( 'resize', onWindowResize, false );
  97. renderer.setAnimationLoop( function () {
  98. update();
  99. renderer.render( scene, camera );
  100. } );
  101. }
  102. function initCar() {
  103. var dracoLoader = new DRACOLoader();
  104. dracoLoader.setDecoderPath( 'js/libs/draco/gltf/' );
  105. var loader = new GLTFLoader();
  106. loader.setDRACOLoader( dracoLoader );
  107. loader.load( 'models/gltf/ferrari.glb', function ( gltf ) {
  108. carModel = gltf.scene.children[ 0 ];
  109. carControls.setModel( carModel );
  110. carModel.traverse( function ( child ) {
  111. if ( child.isMesh ) {
  112. child.material.envMap = envMap;
  113. }
  114. } );
  115. // shadow
  116. var texture = new THREE.TextureLoader().load( 'models/gltf/ferrari_ao.png' );
  117. var shadow = new THREE.Mesh(
  118. new THREE.PlaneBufferGeometry( 0.655 * 4, 1.3 * 4 ).rotateX( - Math.PI / 2 ),
  119. new THREE.MeshBasicMaterial( { map: texture, opacity: 0.8, transparent: true } )
  120. );
  121. shadow.renderOrder = 2;
  122. carModel.add( shadow );
  123. scene.add( carModel );
  124. // car parts for material selection
  125. carParts.body.push( carModel.getObjectByName( 'body' ) );
  126. carParts.rims.push(
  127. carModel.getObjectByName( 'rim_fl' ),
  128. carModel.getObjectByName( 'rim_fr' ),
  129. carModel.getObjectByName( 'rim_rr' ),
  130. carModel.getObjectByName( 'rim_rl' ),
  131. carModel.getObjectByName( 'trim' ),
  132. );
  133. carParts.glass.push(
  134. carModel.getObjectByName( 'glass' ),
  135. );
  136. updateMaterials();
  137. } );
  138. }
  139. function initMaterials() {
  140. materialsLib = {
  141. main: [
  142. new THREE.MeshStandardMaterial( { color: 0xff4400, envMap: envMap, metalness: 0.9, roughness: 0.2, name: 'orange' } ),
  143. new THREE.MeshStandardMaterial( { color: 0x001166, envMap: envMap, metalness: 0.9, roughness: 0.2, name: 'blue' } ),
  144. new THREE.MeshStandardMaterial( { color: 0x990000, envMap: envMap, metalness: 0.9, roughness: 0.2, name: 'red' } ),
  145. new THREE.MeshStandardMaterial( { color: 0x000000, envMap: envMap, metalness: 0.9, roughness: 0.5, name: 'black' } ),
  146. new THREE.MeshStandardMaterial( { color: 0xffffff, envMap: envMap, metalness: 0.9, roughness: 0.5, name: 'white' } ),
  147. new THREE.MeshStandardMaterial( { color: 0x555555, envMap: envMap, envMapIntensity: 2.0, metalness: 1.0, roughness: 0.2, name: 'metallic' } ),
  148. ],
  149. glass: [
  150. new THREE.MeshStandardMaterial( { color: 0xffffff, envMap: envMap, metalness: 1, roughness: 0, opacity: 0.2, transparent: true, premultipliedAlpha: true, name: 'clear' } ),
  151. new THREE.MeshStandardMaterial( { color: 0x000000, envMap: envMap, metalness: 1, roughness: 0, opacity: 0.2, transparent: true, premultipliedAlpha: true, name: 'smoked' } ),
  152. new THREE.MeshStandardMaterial( { color: 0x001133, envMap: envMap, metalness: 1, roughness: 0, opacity: 0.2, transparent: true, premultipliedAlpha: true, name: 'blue' } ),
  153. ],
  154. };
  155. }
  156. function initMaterialSelectionMenus() {
  157. function addOption( name, menu ) {
  158. var option = document.createElement( 'option' );
  159. option.text = name;
  160. option.value = name;
  161. menu.add( option );
  162. }
  163. materialsLib.main.forEach( function ( material ) {
  164. addOption( material.name, bodyMatSelect );
  165. addOption( material.name, rimMatSelect );
  166. } );
  167. materialsLib.glass.forEach( function ( material ) {
  168. addOption( material.name, glassMatSelect );
  169. } );
  170. bodyMatSelect.selectedIndex = 3;
  171. rimMatSelect.selectedIndex = 5;
  172. glassMatSelect.selectedIndex = 0;
  173. bodyMatSelect.addEventListener( 'change', updateMaterials );
  174. rimMatSelect.addEventListener( 'change', updateMaterials );
  175. glassMatSelect.addEventListener( 'change', updateMaterials );
  176. }
  177. // set materials to the current values of the selection menus
  178. function updateMaterials() {
  179. var bodyMat = materialsLib.main[ bodyMatSelect.selectedIndex ];
  180. var rimMat = materialsLib.main[ rimMatSelect.selectedIndex ];
  181. var glassMat = materialsLib.glass[ glassMatSelect.selectedIndex ];
  182. carParts.body.forEach( part => part.material = bodyMat );
  183. carParts.rims.forEach( part => part.material = rimMat );
  184. carParts.glass.forEach( part => part.material = glassMat );
  185. }
  186. function onWindowResize() {
  187. camera.aspect = window.innerWidth / window.innerHeight;
  188. camera.updateProjectionMatrix();
  189. renderer.setSize( window.innerWidth, window.innerHeight );
  190. }
  191. function update() {
  192. var delta = clock.getDelta();
  193. if ( carModel ) {
  194. carControls.update( delta / 3 );
  195. if ( carModel.position.length() > 200 ) {
  196. carModel.position.set( 0, 0, 0 );
  197. carControls.speed = 0;
  198. }
  199. if ( followCamera.checked ) {
  200. carModel.getWorldPosition( cameraTarget );
  201. cameraTarget.y = 2.5;
  202. cameraTarget.z += distance;
  203. camera.position.lerp( cameraTarget, delta * damping );
  204. } else {
  205. carModel.getWorldPosition( cameraTarget );
  206. cameraTarget.y += 0.5;
  207. camera.position.set( 3.25, 2.0, - 5 );
  208. }
  209. camera.lookAt( carModel.position );
  210. }
  211. stats.update();
  212. }
  213. init();
  214. </script>
  215. </body>
  216. </html>