webgl_materials_cars.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - cube reflection [cars]</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. <style>
  8. body {
  9. background:#000;
  10. color:#fff;
  11. padding:0;
  12. margin:0;
  13. overflow:hidden;
  14. font-family:georgia;
  15. text-align:center;
  16. }
  17. h1 { }
  18. a { color:skyblue; text-decoration:none }
  19. canvas { pointer-events:none; z-index:10; position:relative; }
  20. #d { position:absolute; width: 100%; text-align:center; margin:1em 0 -4.5em 0; z-index:1000; }
  21. .bwrap { margin:0.5em 0 0 0 }
  22. button { font-family:georgia; border:0; background:#000; color:#fff; padding:0.2em 0.5em; cursor:pointer; border-radius:3px; }
  23. button:hover { background:#333 }
  24. #buttons_cars button { color:#fa0 }
  25. #car_info { text-align:center; }
  26. #car_name { font-size:1em }
  27. #car_author { font-size:1em }
  28. #oldie { background:rgb(50,0,0) !important; color:#fff !important; margin-top:7em!important }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="d">
  33. <div id="info">
  34. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl demo :
  35. texture by <a href="http://www.humus.name/index.php?page=Textures" target="_blank" rel="noopener">Humus</a> :
  36. <span id="car_info">
  37. <span id="car_name">Bugatti Veyron model</span>
  38. by <span id="car_author"><a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1129" target="_blank" rel="noopener">Troyano</a></span>
  39. </span>
  40. </div>
  41. <div id="buttons_cars" class="bwrap">
  42. <button id="veyron">Bugatti Veyron</button>
  43. <button id="gallardo">Lamborghini Gallardo</button>
  44. <button id="f50">Ferrari F50</button>
  45. <button id="camaro">Chevrolet Camaro</button>
  46. </div>
  47. <div id="buttons_materials" class="bwrap"></div>
  48. </div>
  49. <script src="../build/three.js"></script>
  50. <script src="js/loaders/BinaryLoader.js"></script>
  51. <script src="js/Detector.js"></script>
  52. <script src="js/libs/stats.min.js"></script>
  53. <script>
  54. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  55. var STATS_ENABLED = false;
  56. var CARS = {
  57. "veyron": {
  58. name: "Bugatti Veyron",
  59. url: "obj/veyron/VeyronNoUv_bin.js",
  60. author: '<a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1129" target="_blank" rel="noopener">Troyano</a>',
  61. init_rotation: [ 0, 0, 0 ],
  62. scale: 5.5,
  63. init_material: 4,
  64. body_materials: [ 2 ],
  65. object: null,
  66. buttons: null,
  67. materials: null
  68. },
  69. "gallardo": {
  70. name: "Lamborghini Gallardo",
  71. url: "obj/gallardo/GallardoNoUv_bin.js",
  72. author: '<a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1711" target="_blank" rel="noopener">machman_3d</a>',
  73. init_rotation: [ 0, 0, 0 ],
  74. scale: 3.7,
  75. init_material: 9,
  76. body_materials: [ 3 ],
  77. object: null,
  78. buttons: null,
  79. materials: null
  80. },
  81. "f50": {
  82. name: "Ferrari F50",
  83. url: "obj/f50/F50NoUv_bin.js",
  84. author: '<a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1687" target="_blank" rel="noopener">daniel sathya</a>',
  85. init_rotation: [ 0, 0, 0 ],
  86. scale: 0.175,
  87. init_material: 2,
  88. body_materials: [ 3, 6, 7, 8, 9, 10, 23, 24 ],
  89. object: null,
  90. buttons: null,
  91. materials: null
  92. },
  93. "camaro": {
  94. name: "Chevrolet Camaro",
  95. url: "obj/camaro/CamaroNoUv_bin.js",
  96. author: '<a href="http://www.turbosquid.com/3d-models/blender-camaro/411348" target="_blank" rel="noopener">dskfnwn</a>',
  97. init_rotation: [ 0.0, 0.0, 0.0 /*0, 1, 0*/ ],
  98. scale: 75,
  99. init_material: 0,
  100. body_materials: [ 0 ],
  101. object: null,
  102. buttons: null,
  103. materials: null
  104. }
  105. };
  106. var container, stats;
  107. var camera, scene, renderer;
  108. var m, mi;
  109. var directionalLight, pointLight;
  110. var mouseX = 0, mouseY = 0;
  111. var windowHalfX = window.innerWidth / 2;
  112. var windowHalfY = window.innerHeight / 2;
  113. var loader = new THREE.BinaryLoader();
  114. init();
  115. animate();
  116. function init() {
  117. container = document.createElement( 'div' );
  118. document.body.appendChild( container );
  119. // CAMERAS
  120. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 100000 );
  121. // SCENE
  122. var textureCube = new THREE.CubeTextureLoader()
  123. .setPath( 'textures/cube/Bridge2/')
  124. .load( [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ] );
  125. scene = new THREE.Scene();
  126. scene.background = textureCube;
  127. // LIGHTS
  128. var ambient = new THREE.AmbientLight( 0x050505 );
  129. scene.add( ambient );
  130. directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
  131. directionalLight.position.set( 2, 1.2, 10 ).normalize();
  132. scene.add( directionalLight );
  133. directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  134. directionalLight.position.set( -2, 1.2, -10 ).normalize();
  135. scene.add( directionalLight );
  136. pointLight = new THREE.PointLight( 0xffaa00, 2 );
  137. pointLight.position.set( 2000, 1200, 10000 );
  138. scene.add( pointLight );
  139. //
  140. renderer = new THREE.WebGLRenderer();
  141. renderer.setPixelRatio( window.devicePixelRatio );
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  143. container.appendChild( renderer.domElement );
  144. if ( STATS_ENABLED ) {
  145. stats = new Stats();
  146. container.appendChild( stats.dom );
  147. }
  148. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  149. // common materials
  150. var mlib = {
  151. "Orange": new THREE.MeshLambertMaterial( { color: 0xff6600, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ),
  152. "Blue": new THREE.MeshLambertMaterial( { color: 0x001133, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.3 } ),
  153. "Red": new THREE.MeshLambertMaterial( { color: 0x660000, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.25 } ),
  154. "Black": new THREE.MeshLambertMaterial( { color: 0x000000, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.15 } ),
  155. "White": new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.25 } ),
  156. "Carmine": new THREE.MeshPhongMaterial( { color: 0x770000, specular:0xffaaaa, envMap: textureCube, combine: THREE.MultiplyOperation } ),
  157. "Gold": new THREE.MeshPhongMaterial( { color: 0xaa9944, specular:0xbbaa99, shininess:50, envMap: textureCube, combine: THREE.MultiplyOperation } ),
  158. "Bronze": new THREE.MeshPhongMaterial( { color: 0x150505, specular:0xee6600, shininess:10, envMap: textureCube, combine: THREE.MixOperation, reflectivity: 0.25 } ),
  159. "Chrome": new THREE.MeshPhongMaterial( { color: 0xffffff, specular:0xffffff, envMap: textureCube, combine: THREE.MultiplyOperation } ),
  160. "Orange metal": new THREE.MeshLambertMaterial( { color: 0xff6600, envMap: textureCube, combine: THREE.MultiplyOperation } ),
  161. "Blue metal": new THREE.MeshLambertMaterial( { color: 0x001133, envMap: textureCube, combine: THREE.MultiplyOperation } ),
  162. "Red metal": new THREE.MeshLambertMaterial( { color: 0x770000, envMap: textureCube, combine: THREE.MultiplyOperation } ),
  163. "Green metal": new THREE.MeshLambertMaterial( { color: 0x007711, envMap: textureCube, combine: THREE.MultiplyOperation } ),
  164. "Black metal": new THREE.MeshLambertMaterial( { color: 0x222222, envMap: textureCube, combine: THREE.MultiplyOperation } ),
  165. "Pure chrome": new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: textureCube } ),
  166. "Dark chrome": new THREE.MeshLambertMaterial( { color: 0x444444, envMap: textureCube } ),
  167. "Darker chrome":new THREE.MeshLambertMaterial( { color: 0x222222, envMap: textureCube } ),
  168. "Black glass": new THREE.MeshLambertMaterial( { color: 0x101016, envMap: textureCube, opacity: 0.975, transparent: true } ),
  169. "Dark glass": new THREE.MeshLambertMaterial( { color: 0x101046, envMap: textureCube, opacity: 0.25, transparent: true } ),
  170. "Blue glass": new THREE.MeshLambertMaterial( { color: 0x668899, envMap: textureCube, opacity: 0.75, transparent: true } ),
  171. "Light glass": new THREE.MeshBasicMaterial( { color: 0x223344, envMap: textureCube, opacity: 0.25, transparent: true, combine: THREE.MixOperation, reflectivity: 0.25 } ),
  172. "Red glass": new THREE.MeshLambertMaterial( { color: 0xff0000, opacity: 0.75, transparent: true } ),
  173. "Yellow glass": new THREE.MeshLambertMaterial( { color: 0xffffaa, opacity: 0.75, transparent: true } ),
  174. "Orange glass": new THREE.MeshLambertMaterial( { color: 0x995500, opacity: 0.75, transparent: true } ),
  175. "Orange glass 50": new THREE.MeshLambertMaterial( { color: 0xffbb00, opacity: 0.5, transparent: true } ),
  176. "Red glass 50": new THREE.MeshLambertMaterial( { color: 0xff0000, opacity: 0.5, transparent: true } ),
  177. "Fullblack rough": new THREE.MeshLambertMaterial( { color: 0x000000 } ),
  178. "Black rough": new THREE.MeshLambertMaterial( { color: 0x050505 } ),
  179. "Darkgray rough": new THREE.MeshLambertMaterial( { color: 0x090909 } ),
  180. "Red rough": new THREE.MeshLambertMaterial( { color: 0x330500 } ),
  181. "Darkgray shiny": new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x050505 } ),
  182. "Gray shiny": new THREE.MeshPhongMaterial( { color: 0x050505, shininess: 20 } )
  183. };
  184. // Gallardo materials
  185. CARS[ "gallardo" ].materials = {
  186. body: [
  187. [ "Orange", mlib[ "Orange" ] ],
  188. [ "Blue", mlib[ "Blue" ] ],
  189. [ "Red", mlib[ "Red" ] ],
  190. [ "Black", mlib[ "Black" ] ],
  191. [ "White", mlib[ "White" ] ],
  192. [ "Orange metal", mlib[ "Orange metal" ] ],
  193. [ "Blue metal", mlib[ "Blue metal" ] ],
  194. [ "Green metal", mlib[ "Green metal" ] ],
  195. [ "Black metal", mlib[ "Black metal" ] ],
  196. [ "Carmine", mlib[ "Carmine" ] ],
  197. [ "Gold", mlib[ "Gold" ] ],
  198. [ "Bronze", mlib[ "Bronze" ] ],
  199. [ "Chrome", mlib[ "Chrome" ] ]
  200. ]
  201. };
  202. m = CARS[ "gallardo" ].materials;
  203. mi = CARS[ "gallardo" ].init_material;
  204. CARS[ "gallardo" ].mmap = {
  205. 0: mlib[ "Pure chrome" ], // wheels chrome
  206. 1: mlib[ "Black rough" ], // tire
  207. 2: mlib[ "Black glass" ], // windshield
  208. 3: m.body[ mi ][ 1 ], // body
  209. 4: mlib[ "Red glass" ], // back lights
  210. 5: mlib[ "Yellow glass" ], // front lights
  211. 6: mlib[ "Dark chrome" ] // windshield rim
  212. };
  213. // Veyron materials
  214. CARS[ "veyron" ].materials = {
  215. body: [
  216. [ "Orange metal", mlib[ "Orange metal" ] ],
  217. [ "Blue metal", mlib[ "Blue metal" ] ],
  218. [ "Red metal", mlib[ "Red metal" ] ],
  219. [ "Green metal", mlib[ "Green metal" ] ],
  220. [ "Black metal", mlib[ "Black metal" ] ],
  221. [ "Gold", mlib[ "Gold" ] ],
  222. [ "Bronze", mlib[ "Bronze" ] ],
  223. [ "Chrome", mlib[ "Chrome" ] ]
  224. ]
  225. };
  226. m = CARS[ "veyron" ].materials;
  227. mi = CARS[ "veyron" ].init_material;
  228. CARS[ "veyron" ].mmap = {
  229. 0: mlib[ "Black rough" ], // tires + inside
  230. 1: mlib[ "Pure chrome" ], // wheels + extras chrome
  231. 2: m.body[ mi ][ 1 ], // back / top / front torso
  232. 3: mlib[ "Dark glass" ], // glass
  233. 4: mlib[ "Pure chrome" ], // sides torso
  234. 5: mlib[ "Pure chrome" ], // engine
  235. 6: mlib[ "Red glass 50" ], // backlights
  236. 7: mlib[ "Orange glass 50" ] // backsignals
  237. };
  238. // F50 materials
  239. CARS[ "f50" ].materials = {
  240. body: [
  241. [ "Orange", mlib[ "Orange" ] ],
  242. [ "Blue", mlib[ "Blue" ] ],
  243. [ "Red", mlib[ "Red" ] ],
  244. [ "Black", mlib[ "Black" ] ],
  245. [ "White", mlib[ "White" ] ],
  246. [ "Orange metal", mlib[ "Orange metal" ] ],
  247. [ "Blue metal", mlib[ "Blue metal" ] ],
  248. [ "Black metal", mlib[ "Black metal" ] ],
  249. [ "Carmine", mlib[ "Carmine" ] ],
  250. [ "Gold", mlib[ "Gold" ] ],
  251. [ "Bronze", mlib[ "Bronze" ] ],
  252. [ "Chrome", mlib[ "Chrome" ] ]
  253. ]
  254. };
  255. m = CARS[ "f50" ].materials;
  256. mi = CARS[ "f50" ].init_material;
  257. CARS[ "f50" ].mmap = {
  258. 0: mlib[ "Dark chrome" ], // interior + rim
  259. 1: mlib[ "Pure chrome" ], // wheels + gears chrome
  260. 2: mlib[ "Blue glass" ], // glass
  261. 3: m.body[ mi ][ 1 ], // torso mid + front spoiler
  262. 4: mlib[ "Darkgray shiny" ], // interior + behind seats
  263. 5: mlib[ "Darkgray shiny" ], // tiny dots in interior
  264. 6: m.body[ mi ][ 1 ], // back torso
  265. 7: m.body[ mi ][ 1 ], // right mirror decal
  266. 8: m.body[ mi ][ 1 ], // front decal
  267. 9: m.body[ mi ][ 1 ], // front torso
  268. 10: m.body[ mi ][ 1 ], // left mirror decal
  269. 11: mlib[ "Pure chrome" ], // engine
  270. 12: mlib[ "Darkgray rough" ], // tires side
  271. 13: mlib[ "Darkgray rough" ], // tires bottom
  272. 14: mlib[ "Darkgray shiny" ], // bottom
  273. 15: mlib[ "Black rough" ], // ???
  274. 16: mlib[ "Orange glass" ], // front signals
  275. 17: mlib[ "Dark chrome" ], // wheels center
  276. 18: mlib[ "Red glass" ], // back lights
  277. 19: mlib[ "Black rough" ], // ???
  278. 20: mlib[ "Red rough" ], // seats
  279. 21: mlib[ "Black rough" ], // back plate
  280. 22: mlib[ "Black rough" ], // front light dots
  281. 23: m.body[ mi ][ 1 ], // back torso
  282. 24: m.body[ mi ][ 1 ] // back torso center
  283. };
  284. // Camero materials
  285. CARS[ "camaro" ].materials = {
  286. body: [
  287. [ "Orange", mlib[ "Orange" ] ],
  288. [ "Blue", mlib[ "Blue" ] ],
  289. [ "Red", mlib[ "Red" ] ],
  290. [ "Black", mlib[ "Black" ] ],
  291. [ "White", mlib[ "White" ] ],
  292. [ "Orange metal", mlib[ "Orange metal" ] ],
  293. [ "Blue metal", mlib[ "Blue metal" ] ],
  294. [ "Red metal", mlib[ "Red metal" ] ],
  295. [ "Green metal", mlib[ "Green metal" ] ],
  296. [ "Black metal", mlib[ "Black metal" ] ],
  297. [ "Gold", mlib[ "Gold" ] ],
  298. [ "Bronze", mlib[ "Bronze" ] ],
  299. [ "Chrome", mlib[ "Chrome" ] ]
  300. ]
  301. };
  302. m = CARS[ "camaro" ].materials;
  303. mi = CARS[ "camaro" ].init_material;
  304. CARS[ "camaro" ].mmap = {
  305. 0: m.body[ mi ][ 1 ], // car body
  306. 1: mlib[ "Pure chrome" ], // wheels chrome
  307. 2: mlib[ "Pure chrome" ], // grille chrome
  308. 3: mlib[ "Dark chrome" ], // door lines
  309. 4: mlib[ "Light glass" ], // windshield
  310. 5: mlib[ "Gray shiny" ], // interior
  311. 6: mlib[ "Black rough" ], // tire
  312. 7: mlib[ "Fullblack rough" ], // tireling
  313. 8: mlib[ "Fullblack rough" ] // behind grille
  314. };
  315. loader.load( CARS[ "veyron" ].url, function( geometry ) { createScene( geometry, "veyron" ) } );
  316. for( var c in CARS ) initCarButton( c );
  317. //
  318. window.addEventListener( 'resize', onWindowResize, false );
  319. }
  320. function onWindowResize() {
  321. windowHalfX = window.innerWidth / 2;
  322. windowHalfY = window.innerHeight / 2;
  323. camera.aspect = window.innerWidth / window.innerHeight;
  324. camera.updateProjectionMatrix();
  325. renderer.setSize( window.innerWidth, window.innerHeight );
  326. }
  327. function initCarButton( car ) {
  328. $( car ).addEventListener( 'click', function() {
  329. if ( ! CARS[ car ].object ) {
  330. loader.load( CARS[ car ].url, function( geometry ) { createScene( geometry, car ) } );
  331. } else {
  332. switchCar( car );
  333. }
  334. }, false );
  335. }
  336. function $( id ) { return document.getElementById( id ) }
  337. function button_name( car, index ) { return "m_" + car + "_" + index }
  338. function switchCar( car ) {
  339. for ( var c in CARS ) {
  340. if ( c != car && CARS[ c ].object ) {
  341. CARS[ c ].object.visible = false;
  342. CARS[ c ].buttons.style.display = "none";
  343. }
  344. }
  345. CARS[ car ].object.visible = true;
  346. CARS[ car ].buttons.style.display = "block";
  347. $( "car_name" ).innerHTML = CARS[ car ].name + " model";
  348. $( "car_author" ).innerHTML = CARS[ car ].author;
  349. }
  350. function createButtons( materials, car ) {
  351. var buttons, i, src = "";
  352. for( i = 0; i < materials.length; i ++ ) {
  353. src += '<button id="' + button_name( car, i ) + '">' + materials[ i ][ 0 ] + '</button> ';
  354. }
  355. buttons = document.createElement( "div" );
  356. buttons.innerHTML = src;
  357. $( "buttons_materials" ).appendChild( buttons );
  358. return buttons;
  359. }
  360. function attachButtonMaterials( materials, faceMaterials, material_indices, car ) {
  361. for( var i = 0; i < materials.length; i ++ ) {
  362. $( button_name( car, i ) ).counter = i;
  363. $( button_name( car, i ) ).addEventListener( 'click', function() {
  364. for ( var j = 0; j < material_indices.length; j ++ ) {
  365. faceMaterials[ material_indices [ j ] ] = materials[ this.counter ][ 1 ];
  366. }
  367. }, false );
  368. }
  369. }
  370. function createScene( geometry, car ) {
  371. geometry.sortFacesByMaterialIndex();
  372. var m = [],
  373. s = CARS[ car ].scale * 1,
  374. r = CARS[ car ].init_rotation,
  375. materials = CARS[ car ].materials,
  376. mi = CARS[ car ].init_material,
  377. bm = CARS[ car ].body_materials;
  378. for ( var i in CARS[ car ].mmap ) {
  379. m[ i ] = CARS[ car ].mmap[ i ];
  380. }
  381. var mesh = new THREE.Mesh( geometry, m );
  382. mesh.rotation.x = r[ 0 ];
  383. mesh.rotation.y = r[ 1 ];
  384. mesh.rotation.z = r[ 2 ];
  385. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  386. scene.add( mesh );
  387. CARS[ car ].object = mesh;
  388. CARS[ car ].buttons = createButtons( materials.body, car );
  389. attachButtonMaterials( materials.body, m, bm, car );
  390. switchCar( car );
  391. }
  392. function onDocumentMouseMove(event) {
  393. mouseY = ( event.clientY - window.innerHeight );
  394. }
  395. //
  396. function animate() {
  397. requestAnimationFrame( animate );
  398. render();
  399. }
  400. function render() {
  401. var timer = -0.0002 * Date.now();
  402. camera.position.x = 1000 * Math.cos( timer );
  403. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  404. camera.position.z = 1000 * Math.sin( timer );
  405. camera.lookAt( scene.position );
  406. renderer.render( scene, camera );
  407. if ( STATS_ENABLED ) stats.update();
  408. }
  409. </script>
  410. </body>
  411. </html>