webgl_materials_cubemap_dynamic.html 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - dynamic cube reflection</title>
  5. <meta charset="utf-8">
  6. <style>
  7. body {
  8. background: #000;
  9. color: #999;
  10. padding: 0;
  11. margin: 0;
  12. overflow: hidden;
  13. font-family: georgia;
  14. font-size:1em;
  15. text-align: center;
  16. }
  17. a { color: white }
  18. #info { margin-top: 2em; }
  19. #footer { width: 100%; margin: 2em auto; text-align: center; position: absolute; bottom: 0 }
  20. #loading { color: orange; margin-top: 1em }
  21. .h { color: skyblue }
  22. .c { display: inline; margin-left: 1em }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="info">
  27. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl dynamic cube reflection demo -
  28. veyron by <a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1129" target="_blank">Troyano</a> -
  29. gallardo by <a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1711" target="_blank">machman_3d</a>
  30. </div>
  31. <div id="loading">Loading...</div>
  32. <div id="container"></div>
  33. <div id="footer">
  34. cars control: <span class="h">WASD</span> / <span class="h">arrows</span>
  35. <div class="c">cameras: <span class="h">1</span> / <span class="h">2</span> / <span class="h">3</span> / <span class="h">4</span>
  36. / <span class="h">5</span> / <span class="h">6</span>
  37. </div>
  38. <div class="c">
  39. day / night: <span class="h">n</span>
  40. </div>
  41. <div class="c">
  42. motion blur: <span class="h">b</span>
  43. </div>
  44. </div>
  45. <script src="../build/Three.js"></script>
  46. <script src="js/Detector.js"></script>
  47. <script src="js/Stats.js"></script>
  48. <script src="js/ShaderExtras.js"></script>
  49. <script src="js/postprocessing/EffectComposer.js"></script>
  50. <script src="js/postprocessing/RenderPass.js"></script>
  51. <script src="js/postprocessing/BloomPass.js"></script>
  52. <script src="js/postprocessing/ShaderPass.js"></script>
  53. <script src="js/postprocessing/MaskPass.js"></script>
  54. <script src="js/postprocessing/SavePass.js"></script>
  55. <script src="js/Car.js"></script>
  56. <script>
  57. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  58. var FOLLOW_CAMERA = false;
  59. var MARGIN = 100;
  60. var SCREEN_WIDTH = window.innerWidth;
  61. var SCREEN_HEIGHT = window.innerHeight - 2 * MARGIN;
  62. var SHADOW_MAP_WIDTH = 1024, SHADOW_MAP_HEIGHT = 1024;
  63. var CUBE_HEIGHT = 0;
  64. var container, stats;
  65. var camera, cameraTarget, scene, renderer;
  66. var renderTarget, renderTargetParameters;
  67. var spotLight, ambientLight;
  68. var cubeCamera;
  69. var clock = new THREE.Clock();
  70. var controlsGallardo = {
  71. moveForward: false,
  72. moveBackward: false,
  73. moveLeft: false,
  74. moveRight: false
  75. };
  76. var controlsVeyron = {
  77. moveForward: false,
  78. moveBackward: false,
  79. moveLeft: false,
  80. moveRight: false
  81. };
  82. var mlib;
  83. var gallardo, veyron, currentCar;
  84. var effectDirt, hblur, vblur, effectBloom, effectKeep, effectBlend, effectFXAA;
  85. var config = {
  86. "veyron" : { r: 0.5, model: null, backCam: new THREE.Vector3( 550, 100, -1000 ) },
  87. "gallardo" : { r: 0.35, model: null, backCam: new THREE.Vector3( 550, 0, -1500 ) }
  88. };
  89. var flareA, flareB;
  90. var sprites = [];
  91. var ground, groundBasic;
  92. var blur = false;
  93. var v = 0.9, vdir = 1;
  94. init();
  95. animate();
  96. function init() {
  97. container = document.getElementById( 'container' );
  98. // SCENE
  99. scene = new THREE.Scene();
  100. scene.fog = new THREE.Fog( 0xffffff, 3000, 10000 );
  101. scene.fog.color.setHSV( 0.51, 0.5, 0.9 );
  102. createScene();
  103. // SCENE CAMERA
  104. camera = new THREE.PerspectiveCamera( 18, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
  105. camera.position.set( 2000, 0, 2000 );
  106. cameraTarget = new THREE.Vector3( 0, 0, 0 );
  107. scene.add( camera );
  108. // LIGHTS
  109. ambientLight = new THREE.AmbientLight( 0x555555 );
  110. scene.add( ambientLight );
  111. spotLight = new THREE.SpotLight( 0xffffff );
  112. spotLight.position.set( 0, 1800, 1500 );
  113. spotLight.target.position.set( 0, 0, 0 );
  114. spotLight.castShadow = true;
  115. scene.add( spotLight );
  116. directionalLight2 = new THREE.PointLight( 0xff9900, 0.25 );
  117. directionalLight2.position.set( 0.5, -1, 0.5 );
  118. //directionalLight2.position.normalize();
  119. //scene.add( directionalLight2 );
  120. // RENDERER
  121. renderer = new THREE.WebGLRenderer( { antialias: false } );
  122. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  123. renderer.setClearColor( scene.fog.color, 1 );
  124. renderer.domElement.style.position = "absolute";
  125. renderer.domElement.style.top = MARGIN + "px";
  126. renderer.domElement.style.left = "0px";
  127. container.appendChild( renderer.domElement );
  128. // SHADOW
  129. renderer.shadowCameraNear = 2;
  130. renderer.shadowCameraFar = camera.far;
  131. renderer.shadowCameraFov = 50;
  132. renderer.shadowMapBias = 0.003885;
  133. renderer.shadowMapDarkness = 0.5;
  134. renderer.shadowMapWidth = SHADOW_MAP_WIDTH;
  135. renderer.shadowMapHeight = SHADOW_MAP_HEIGHT;
  136. renderer.shadowMapEnabled = true;
  137. //renderer.shadowMapSoft = false;
  138. // STATS
  139. stats = new Stats();
  140. stats.domElement.style.position = 'absolute';
  141. stats.domElement.style.top = '0px';
  142. stats.domElement.style.zIndex = 100;
  143. container.appendChild( stats.domElement );
  144. stats.domElement.children[ 0 ].children[ 0 ].style.color = "#888";
  145. stats.domElement.children[ 0 ].style.background = "#000";
  146. stats.domElement.children[ 0 ].children[ 1 ].style.display = "none";
  147. // CUBE CAMERA
  148. cubeCamera = new THREE.CubeCamera( 1, 100000, CUBE_HEIGHT, 128 );
  149. // MATERIALS
  150. var cubeTarget = cubeCamera.renderTarget;
  151. mlib = {
  152. body: [],
  153. "Chrome": new THREE.MeshLambertMaterial( { color: 0xffffff, ambient: 0xffffff, envMap: cubeTarget } ),
  154. "ChromeN": new THREE.MeshLambertMaterial( { color: 0xffffff, ambient: 0xffffff, envMap: cubeTarget, combine: THREE.MixOperation, reflectivity: 0.75 } ),
  155. "Dark chrome": new THREE.MeshLambertMaterial( { color: 0x444444, ambient: 0x444444, envMap: cubeTarget } ),
  156. "Black rough": new THREE.MeshLambertMaterial( { color: 0x050505, ambient: 0x050505 } ),
  157. "Dark glass": new THREE.MeshLambertMaterial( { color: 0x101020, ambient: 0x101020, envMap: cubeTarget, opacity: 0.5, transparent: true } ),
  158. "Orange glass": new THREE.MeshLambertMaterial( { color: 0xffbb00, ambient: 0xffbb00, opacity: 0.5, transparent: true } ),
  159. "Red glass": new THREE.MeshLambertMaterial( { color: 0xff0000, ambient: 0xff0000, opacity: 0.5, transparent: true } ),
  160. "Black metal": new THREE.MeshLambertMaterial( { color: 0x222222, ambient: 0x222222, envMap: cubeTarget, combine: THREE.MultiplyOperation } ),
  161. "Orange metal": new THREE.MeshLambertMaterial( { color: 0xff6600, ambient: 0xff6600, envMap: cubeTarget, combine: THREE.MultiplyOperation } )
  162. }
  163. mlib.body.push( [ "Orange", new THREE.MeshLambertMaterial( { color: 0x883300, ambient: 0x883300, envMap: cubeTarget, combine: THREE.MixOperation, reflectivity: 0.1 } ) ] );
  164. mlib.body.push( [ "Blue", new THREE.MeshLambertMaterial( { color: 0x113355, envMap: cubeTarget, combine: THREE.MixOperation, reflectivity: 0.1 } ) ] );
  165. mlib.body.push( [ "Red", new THREE.MeshLambertMaterial( { color: 0x660000, envMap: cubeTarget, combine: THREE.MixOperation, reflectivity: 0.1 } ) ] );
  166. mlib.body.push( [ "Black", new THREE.MeshLambertMaterial( { color: 0x000000, envMap: cubeTarget, combine: THREE.MixOperation, reflectivity: 0.2 } ) ] );
  167. mlib.body.push( [ "White", new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: cubeTarget, combine: THREE.MixOperation, reflectivity: 0.2 } ) ] );
  168. mlib.body.push( [ "Carmine", new THREE.MeshPhongMaterial( { color: 0x770000, specular: 0xffaaaa, envMap: cubeTarget, combine: THREE.MultiplyOperation } ) ] );
  169. mlib.body.push( [ "Gold", new THREE.MeshPhongMaterial( { color: 0xaa9944, specular: 0xbbaa99, shininess: 50, envMap: cubeTarget, combine: THREE.MultiplyOperation } ) ] );
  170. mlib.body.push( [ "Bronze", new THREE.MeshPhongMaterial( { color: 0x150505, specular: 0xee6600, shininess: 10, envMap: cubeTarget, combine: THREE.MixOperation, reflectivity: 0.2 } ) ] );
  171. mlib.body.push( [ "Chrome", new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, envMap: cubeTarget, combine: THREE.MultiplyOperation } ) ] );
  172. // FLARES
  173. flareA = THREE.ImageUtils.loadTexture( "textures/lensflare2.jpg" );
  174. flareB = THREE.ImageUtils.loadTexture( "textures/lensflare0.png" );
  175. // CARS - VEYRON
  176. veyron = new THREE.Car();
  177. veyron.modelScale = 3;
  178. veyron.backWheelOffset = 2;
  179. veyron.callback = function( object ) {
  180. addCar( object, -300, -215, 0, 0 );
  181. setMaterialsVeyron( object );
  182. var sa = 2, sb = 5;
  183. var params = {
  184. "a" : { map: flareA, useScreenCoordinates: false, color: 0xffffff, blending: THREE.AdditiveBlending },
  185. "b" : { map: flareB, useScreenCoordinates: false, color: 0xffffff, blending: THREE.AdditiveBlending },
  186. "ar" : { map: flareA, useScreenCoordinates: false, color: 0xff0000, blending: THREE.AdditiveBlending },
  187. "br" : { map: flareB, useScreenCoordinates: false, color: 0xff0000, blending: THREE.AdditiveBlending }
  188. };
  189. var flares = [ // front
  190. [ "a", sa, [ 47, 38, 120 ] ], [ "a", sa, [ 40, 38, 120 ] ], [ "a", sa, [ 32, 38, 122 ] ],
  191. [ "b", sb, [ 47, 38, 120 ] ], [ "b", sb, [ 40, 38, 120 ] ], [ "b", sb, [ 32, 38, 122 ] ],
  192. [ "a", sa, [ -47, 38, 120 ] ], [ "a", sa, [ -40, 38, 120 ] ], [ "a", sa, [ -32, 38, 122 ] ],
  193. [ "b", sb, [ -47, 38, 120 ] ], [ "b", sb, [ -40, 38, 120 ] ], [ "b", sb, [ -32, 38, 122 ] ],
  194. // back
  195. [ "ar", sa, [ 22, 50, -123 ] ], [ "ar", sa, [ 32, 49, -123 ] ],
  196. [ "br", sb, [ 22, 50, -123 ] ], [ "br", sb, [ 32, 49, -123 ] ],
  197. [ "ar", sa, [ -22, 50, -123 ] ], [ "ar", sa, [ -32, 49, -123 ] ],
  198. [ "br", sb, [ -22, 50, -123 ] ], [ "br", sb, [ -32, 49, -123 ] ],
  199. ];
  200. for ( var i = 0; i < flares.length; i ++ ) {
  201. var p = params[ flares[ i ][ 0 ] ];
  202. var s = flares[ i ][ 1 ];
  203. var x = flares[ i ][ 2 ][ 0 ];
  204. var y = flares[ i ][ 2 ][ 1 ];
  205. var z = flares[ i ][ 2 ][ 2 ];
  206. var sprite = new THREE.Sprite( p );
  207. sprite.scale.set( s, s, s );
  208. sprite.position.set( x, y, z );
  209. object.bodyMesh.add( sprite );
  210. sprites.push( sprite );
  211. }
  212. checkStatus();
  213. };
  214. veyron.loadPartsBinary( "obj/veyron/parts/veyron_body_bin.js", "obj/veyron/parts/veyron_wheel_bin.js" );
  215. // CARS - GALLARDO
  216. gallardo = new THREE.Car();
  217. gallardo.modelScale = 2;
  218. gallardo.backWheelOffset = 45;
  219. gallardo.callback = function( object ) {
  220. addCar( object, 300, -110, 0, -110 );
  221. setMaterialsGallardo( object );
  222. var sa = 2, sb = 5;
  223. var params = {
  224. "a" : { map: flareA, useScreenCoordinates: false, color: 0xffffff, blending: THREE.AdditiveBlending },
  225. "b" : { map: flareB, useScreenCoordinates: false, color: 0xffffff, blending: THREE.AdditiveBlending },
  226. "ar" : { map: flareA, useScreenCoordinates: false, color: 0xff0000, blending: THREE.AdditiveBlending },
  227. "br" : { map: flareB, useScreenCoordinates: false, color: 0xff0000, blending: THREE.AdditiveBlending }
  228. };
  229. var flares = [ // front
  230. [ "a", sa, [ 70, 10, 160 ] ], [ "a", sa, [ 66, -1, 175 ] ], [ "a", sa, [ 66, -1, 165 ] ],
  231. [ "b", sb, [ 70, 10, 160 ] ], [ "b", sb, [ 66, -1, 175 ] ], [ "b", sb, [ 66, -1, 165 ] ],
  232. [ "a", sa, [ -70, 10, 160 ] ], [ "a", sa, [ -66, -1, 175 ] ], [ "a", sa, [ -66, -1, 165 ] ],
  233. [ "b", sb, [ -70, 10, 160 ] ], [ "b", sb, [ -66, -1, 175 ] ], [ "b", sb, [ -66, -1, 165 ] ],
  234. // back
  235. [ "ar", sa, [ 61, 19, -185 ] ], [ "ar", sa, [ 55, 19, -185 ] ],
  236. [ "br", sb, [ 61, 19, -185 ] ], [ "br", sb, [ 55, 19, -185 ] ],
  237. [ "ar", sa, [ -61, 19, -185 ] ], [ "ar", sa, [ -55, 19, -185 ] ],
  238. [ "br", sb, [ -61, 19, -185 ] ], [ "br", sb, [ -55, 19, -185 ] ],
  239. ];
  240. for ( var i = 0; i < flares.length; i ++ ) {
  241. var p = params[ flares[ i ][ 0 ] ];
  242. var s = flares[ i ][ 1 ];
  243. var x = flares[ i ][ 2 ][ 0 ];
  244. var y = flares[ i ][ 2 ][ 1 ];
  245. var z = flares[ i ][ 2 ][ 2 ];
  246. var sprite = new THREE.Sprite( p );
  247. sprite.scale.set( s, s, s );
  248. sprite.position.set( x, y, z );
  249. object.bodyMesh.add( sprite );
  250. sprites.push( sprite );
  251. }
  252. checkStatus();
  253. };
  254. gallardo.loadPartsBinary( "obj/gallardo/parts/gallardo_body_bin.js", "obj/gallardo/parts/gallardo_wheel_bin.js" );
  255. //
  256. config[ "gallardo" ].model = gallardo;
  257. config[ "veyron" ].model = veyron;
  258. currentCar = gallardo;
  259. // EVENTS
  260. document.addEventListener( 'keydown', onKeyDown, false );
  261. document.addEventListener( 'keyup', onKeyUp, false );
  262. window.addEventListener( 'resize', onWindowResize, false );
  263. // POSTPROCESSING
  264. renderer.autoClear = false;
  265. renderTargetParameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBufer: false };
  266. renderTarget = new THREE.WebGLRenderTarget( SCREEN_WIDTH, SCREEN_HEIGHT, renderTargetParameters );
  267. effectSave = new THREE.SavePass( new THREE.WebGLRenderTarget( SCREEN_WIDTH, SCREEN_HEIGHT, renderTargetParameters ) );
  268. effectBlend = new THREE.ShaderPass( THREE.ShaderExtras[ "blend" ], "tDiffuse1" );
  269. effectFXAA = new THREE.ShaderPass( THREE.ShaderExtras[ "fxaa" ] );
  270. var effectVignette = new THREE.ShaderPass( THREE.ShaderExtras[ "vignette" ] );
  271. var effectBleach = new THREE.ShaderPass( THREE.ShaderExtras[ "bleachbypass" ] );
  272. effectBloom = new THREE.BloomPass( 0.75 );
  273. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / SCREEN_WIDTH, 1 / SCREEN_HEIGHT );
  274. // tilt shift
  275. hblur = new THREE.ShaderPass( THREE.ShaderExtras[ "horizontalTiltShift" ] );
  276. vblur = new THREE.ShaderPass( THREE.ShaderExtras[ "verticalTiltShift" ] );
  277. var bluriness = 7;
  278. hblur.uniforms[ 'h' ].value = bluriness / SCREEN_WIDTH;
  279. vblur.uniforms[ 'v' ].value = bluriness / SCREEN_HEIGHT;
  280. if ( FOLLOW_CAMERA ) {
  281. if ( currentCar == gallardo ) {
  282. hblur.uniforms[ 'r' ].value = vblur.uniforms[ 'r' ].value = rMap[ "gallardo" ];
  283. } else if ( currentCar == veyron ) {
  284. hblur.uniforms[ 'r' ].value = vblur.uniforms[ 'r' ].value = rMap[ "veyron" ];
  285. }
  286. } else {
  287. hblur.uniforms[ 'r' ].value = vblur.uniforms[ 'r' ].value = 0.35;
  288. }
  289. effectVignette.uniforms[ "offset" ].value = 1.05;
  290. effectVignette.uniforms[ "darkness" ].value = 1.5;
  291. // motion blur
  292. effectBlend.uniforms[ 'tDiffuse2' ].texture = effectSave.renderTarget;
  293. effectBlend.uniforms[ 'mixRatio' ].value = 0.65;
  294. var renderModel = new THREE.RenderPass( scene, camera );
  295. effectVignette.renderToScreen = true;
  296. composer = new THREE.EffectComposer( renderer, renderTarget );
  297. composer.addPass( renderModel );
  298. composer.addPass( effectFXAA );
  299. composer.addPass( effectBlend );
  300. composer.addPass( effectSave );
  301. composer.addPass( effectBloom );
  302. composer.addPass( effectBleach );
  303. composer.addPass( hblur );
  304. composer.addPass( vblur );
  305. composer.addPass( effectVignette );
  306. }
  307. //
  308. function checkStatus() {
  309. if ( gallardo.loaded && veyron.loaded ) {
  310. document.getElementById( "loading" ).style.display = "none";
  311. }
  312. }
  313. //
  314. function setSpritesOpacity( opacity ) {
  315. for ( var i = 0; i < sprites.length; i ++ ) {
  316. sprites[ i ].opacity = opacity;
  317. }
  318. }
  319. //
  320. function createScene() {
  321. // GROUND
  322. var texture = THREE.ImageUtils.loadTexture( "textures/cube/Park3Med/ny.jpg" );
  323. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  324. texture.repeat.set( 50, 50 );
  325. groundBasic = new THREE.MeshBasicMaterial( { color: 0xffffff, map: texture } );
  326. groundBasic.color.setHSV( 0.1, 0.45, 0.995 );
  327. ground = new THREE.Mesh( new THREE.PlaneGeometry( 50000, 50000 ), groundBasic );
  328. ground.rotation.x = - Math.PI / 2;
  329. ground.position.y = - 215;
  330. scene.add( ground );
  331. ground.castShadow = false;
  332. ground.receiveShadow = true;
  333. // OBJECTS
  334. var cylinderGeometry = new THREE.CylinderGeometry( 2, 50, 1000, 32 );
  335. var sphereGeometry = new THREE.SphereGeometry( 100, 32, 16 );
  336. var sy1 = -500 + 38;
  337. var sy2 = -88;
  338. addObject( cylinderGeometry, 0xff0000, 1500, 250, 0, sy1 );
  339. addObject( cylinderGeometry, 0xffaa00, -1500, 250, 0, sy1 );
  340. addObject( cylinderGeometry, 0x00ff00, 0, 250, 1500, sy1 );
  341. addObject( cylinderGeometry, 0x00ffaa, 0, 250, -1500, sy1 );
  342. addObject( sphereGeometry, 0xff0000, 1500, -125, 200, sy2 );
  343. addObject( sphereGeometry, 0xffaa00, -1500, -125, 200, sy2 );
  344. addObject( sphereGeometry, 0x00ff00, 200, -125, 1500, sy2 );
  345. addObject( sphereGeometry, 0x00ffaa, 200, -125, -1500, sy2 );
  346. }
  347. //
  348. function addObject( geometry, color, x, y, z, sy ) {
  349. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: color, ambient: color } ) );
  350. object.position.set( x, y, z );
  351. scene.add( object );
  352. object.castShadow = true;
  353. object.receiveShadow = true;
  354. var shadowTexture = THREE.ImageUtils.loadTexture( "textures/shadowAlpha.png" );
  355. var shadowPlane = new THREE.PlaneGeometry( 400, 400 );
  356. var shadowMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.35, transparent: true,
  357. map: shadowTexture,
  358. polygonOffset: false, polygonOffsetFactor: -0.5, polygonOffsetUnits: 1 } );
  359. var shadow = new THREE.Mesh( shadowPlane, shadowMaterial );
  360. shadow.rotation.x = -Math.PI/2;
  361. shadow.rotation.z = Math.PI/2;
  362. shadow.position.y = sy;
  363. object.add( shadow );
  364. }
  365. //
  366. function generateDropShadowTexture( object, width, height, bluriness ) {
  367. var renderTargetParameters = { minFilter: THREE.LinearMipmapLinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat, stencilBufer: false };
  368. var shadowTarget = new THREE.WebGLRenderTarget( width, height, renderTargetParameters );
  369. var shadowMaterial = new THREE.MeshBasicMaterial( { color: 0x000000 } );
  370. var shadowGeometry = THREE.GeometryUtils.clone( object.geometry );
  371. var shadowObject = new THREE.Mesh( shadowGeometry, shadowMaterial );
  372. var shadowScene = new THREE.Scene();
  373. shadowScene.add( shadowObject );
  374. shadowObject.geometry.computeBoundingBox();
  375. var bb = shadowObject.geometry.boundingBox;
  376. var dimensions = new THREE.Vector3();
  377. dimensions.sub( bb.max, bb.min );
  378. var margin = 0.15,
  379. width = dimensions.z,
  380. height = dimensions.x,
  381. depth = dimensions.y,
  382. left = bb.min.z - margin * width,
  383. right = bb.max.z + margin * width,
  384. top = bb.max.x + margin * height,
  385. bottom = bb.min.x - margin * height,
  386. near = bb.max.y + margin * depth,
  387. far = bb.min.y - margin * depth;
  388. var topCamera = new THREE.OrthographicCamera( left, right, top, bottom, near, far );
  389. topCamera.position.y = bb.max.y;
  390. topCamera.lookAt( shadowScene.position );
  391. shadowScene.add( topCamera );
  392. var renderShadow = new THREE.RenderPass( shadowScene, topCamera );
  393. var blurShader = THREE.ShaderExtras[ "triangleBlur" ];
  394. var effectBlurX = new THREE.ShaderPass( blurShader, 'texture' );
  395. var effectBlurY = new THREE.ShaderPass( blurShader, 'texture' );
  396. renderShadow.clearColor = new THREE.Color( 0x000000 );
  397. renderShadow.clearAlpha = 0;
  398. var blurAmountX = bluriness / width;
  399. var blurAmountY = bluriness / height;
  400. effectBlurX.uniforms[ 'delta' ].value = new THREE.Vector2( blurAmountX, 0 );
  401. effectBlurY.uniforms[ 'delta' ].value = new THREE.Vector2( 0, blurAmountY );
  402. var shadowComposer = new THREE.EffectComposer( renderer, shadowTarget );
  403. shadowComposer.addPass( renderShadow );
  404. shadowComposer.addPass( effectBlurX );
  405. shadowComposer.addPass( effectBlurY );
  406. renderer.clear();
  407. shadowComposer.render( 0.1 );
  408. return shadowTarget;
  409. }
  410. //
  411. function addCar( object, x, y, z, s ) {
  412. object.root.position.set( x, y, z );
  413. scene.add( object.root );
  414. object.enableShadows( true );
  415. if ( FOLLOW_CAMERA && object == currentCar ) {
  416. object.root.add( camera );
  417. camera.position.set( 350, 500, 2200 );
  418. //camera.position.set( 0, 3000, -500 );
  419. cameraTarget.z = 500;
  420. cameraTarget.y = 150;
  421. camera.lookAt( cameraTarget );
  422. }
  423. var shadowTexture = generateDropShadowTexture( object.bodyMesh, 64, 32, 15 );
  424. object.bodyMesh.geometry.computeBoundingBox();
  425. var bb = object.bodyMesh.geometry.boundingBox;
  426. var ss = object.modelScale * 1.0;
  427. var shadowWidth = ss * ( bb.max.z - bb.min.z );
  428. var shadowHeight = 1.25 * ss * ( bb.max.x - bb.min.x );
  429. var shadowPlane = new THREE.PlaneGeometry( shadowWidth, shadowHeight );
  430. var shadowMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, transparent: true,
  431. map: shadowTexture,
  432. polygonOffset: false, polygonOffsetFactor: -0.5, polygonOffsetUnits: 1 } );
  433. var shadow = new THREE.Mesh( shadowPlane, shadowMaterial );
  434. shadow.rotation.x = -Math.PI/2;
  435. shadow.rotation.z = Math.PI/2;
  436. shadow.position.y = s + 10;
  437. object.root.add( shadow );
  438. }
  439. //
  440. function setCurrentCar( car, cameraType ) {
  441. var oldCar = currentCar;
  442. currentCar = config[ car ].model;
  443. if ( cameraType == "front" || cameraType == "back" ) {
  444. hblur.uniforms[ 'r' ].value = vblur.uniforms[ 'r' ].value = config[ car ].r;
  445. FOLLOW_CAMERA = true;
  446. oldCar.root.remove( camera );
  447. currentCar.root.add( camera );
  448. if ( cameraType == "front" ) {
  449. camera.position.set( 350, 500, 2200 );
  450. } else if ( cameraType == "back" ) {
  451. camera.position.copy( config[ car ].backCam );
  452. }
  453. cameraTarget.set( 0, 150, 500 );
  454. } else {
  455. FOLLOW_CAMERA = false;
  456. oldCar.root.remove( camera );
  457. camera.position.set( 2000, 0, 2000 );
  458. cameraTarget.set( 0, 0, 0 );
  459. spotLight.position.set( 0, 1800, 1500 );
  460. hblur.uniforms[ 'r' ].value = vblur.uniforms[ 'r' ].value = 0.35;
  461. }
  462. }
  463. //
  464. function onWindowResize( event ) {
  465. SCREEN_WIDTH = window.innerWidth;
  466. SCREEN_HEIGHT = window.innerHeight - 2 * MARGIN;
  467. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  468. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  469. camera.updateProjectionMatrix();
  470. renderTarget = new THREE.WebGLRenderTarget( SCREEN_WIDTH, SCREEN_HEIGHT, renderTargetParameters );
  471. composer.reset( renderTarget );
  472. hblur.uniforms[ 'h' ].value = 10.75 / SCREEN_WIDTH;
  473. vblur.uniforms[ 'v' ].value = 10.75 / SCREEN_HEIGHT;
  474. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / SCREEN_WIDTH, 1 / SCREEN_HEIGHT );
  475. }
  476. //
  477. function onKeyDown ( event ) {
  478. switch( event.keyCode ) {
  479. case 38: /*up*/ controlsGallardo.moveForward = true; break;
  480. case 87: /*W*/ controlsVeyron.moveForward = true; break;
  481. case 40: /*down*/controlsGallardo.moveBackward = true; break;
  482. case 83: /*S*/ controlsVeyron.moveBackward = true; break;
  483. case 37: /*left*/controlsGallardo.moveLeft = true; break;
  484. case 65: /*A*/ controlsVeyron.moveLeft = true; break;
  485. case 39: /*right*/controlsGallardo.moveRight = true; break;
  486. case 68: /*D*/ controlsVeyron.moveRight = true; break;
  487. case 49: /*1*/ setCurrentCar( "gallardo", "center" ); break;
  488. case 50: /*2*/ setCurrentCar( "veyron", "center" ); break;
  489. case 51: /*3*/ setCurrentCar( "gallardo", "front" ); break;
  490. case 52: /*4*/ setCurrentCar( "veyron", "front" ); break;
  491. case 53: /*5*/ setCurrentCar( "gallardo", "back" ); break;
  492. case 54: /*6*/ setCurrentCar( "veyron", "back" ); break;
  493. case 78: /*N*/ vdir *= -1; break;
  494. case 66: /*B*/ blur = !blur; break;
  495. }
  496. };
  497. function onKeyUp ( event ) {
  498. switch( event.keyCode ) {
  499. case 38: /*up*/controlsGallardo.moveForward = false; break;
  500. case 87: /*W*/ controlsVeyron.moveForward = false; break;
  501. case 40: /*down*/controlsGallardo.moveBackward = false; break;
  502. case 83: /*S*/ controlsVeyron.moveBackward = false; break;
  503. case 37: /*left*/controlsGallardo.moveLeft = false; break;
  504. case 65: /*A*/ controlsVeyron.moveLeft = false; break;
  505. case 39: /*right*/controlsGallardo.moveRight = false; break;
  506. case 68: /*D*/ controlsVeyron.moveRight = false; break;
  507. }
  508. };
  509. //
  510. function setMaterialsGallardo( car ) {
  511. // BODY
  512. var materials = car.bodyGeometry.materials;
  513. materials[ 0 ] = mlib.body[ 0 ][ 1 ]; // body
  514. materials[ 1 ] = mlib[ "Dark chrome" ]; // front under lights, back
  515. // WHEELS
  516. materials = car.wheelGeometry.materials;
  517. materials[ 0 ] = mlib[ "Chrome" ]; // insides
  518. materials[ 1 ] = mlib[ "Black rough" ]; // tire
  519. }
  520. function setMaterialsVeyron( car ) {
  521. // 0 - top, front center, back sides
  522. // 1 - front sides
  523. // 2 - engine
  524. // 3 - small chrome things
  525. // 4 - backlights
  526. // 5 - back signals
  527. // 6 - bottom, interior
  528. // 7 - windshield
  529. // BODY
  530. var materials = car.bodyGeometry.materials;
  531. materials[ 0 ] = mlib[ "Black metal" ]; // top, front center, back sides
  532. materials[ 1 ] = mlib[ "Chrome" ]; // front sides
  533. materials[ 2 ] = mlib[ "Chrome" ]; // engine
  534. materials[ 3 ] = mlib[ "Dark chrome" ]; // small chrome things
  535. materials[ 4 ] = mlib[ "Red glass" ]; // backlights
  536. materials[ 5 ] = mlib[ "Orange glass" ]; // back signals
  537. materials[ 6 ] = mlib[ "Black rough" ]; // bottom, interior
  538. materials[ 7 ] = mlib[ "Dark glass" ]; // windshield
  539. // WHEELS
  540. materials = car.wheelGeometry.materials;
  541. materials[ 0 ] = mlib[ "Chrome" ]; // insides
  542. materials[ 1 ] = mlib[ "Black rough" ]; // tire
  543. }
  544. //
  545. function animate() {
  546. requestAnimationFrame( animate );
  547. render();
  548. stats.update();
  549. }
  550. function render() {
  551. var delta = clock.getDelta();
  552. // day / night
  553. v = THREE.Math.clamp( v + 0.5 * delta * vdir, 0.1, 0.9 );
  554. scene.fog.color.setHSV( 0.51, 0.5, v );
  555. renderer.setClearColor( scene.fog.color, 1 );
  556. var vnorm = ( v - 0.05 ) / ( 0.9 - 0.05 );
  557. if ( vnorm < 0.3 ) {
  558. setSpritesOpacity( 1 - v / 0.3 );
  559. } else {
  560. setSpritesOpacity( 0 );
  561. }
  562. if ( vnorm < 0.4 ) {
  563. if ( veyron.loaded ) {
  564. veyron.bodyGeometry.materials[ 1 ] = mlib[ "ChromeN" ];
  565. veyron.bodyGeometry.materials[ 2 ] = mlib[ "ChromeN" ];
  566. veyron.wheelGeometry.materials[ 0 ] = mlib[ "ChromeN" ];
  567. }
  568. if ( gallardo.loaded ) {
  569. gallardo.wheelGeometry.materials[ 0 ] = mlib[ "ChromeN" ];
  570. }
  571. } else {
  572. if ( veyron.loaded ) {
  573. veyron.bodyGeometry.materials[ 1 ] = mlib[ "Chrome" ];
  574. veyron.bodyGeometry.materials[ 2 ] = mlib[ "Chrome" ];
  575. veyron.wheelGeometry.materials[ 0 ] = mlib[ "Chrome" ];
  576. }
  577. if ( gallardo.loaded ) {
  578. gallardo.wheelGeometry.materials[ 0 ] = mlib[ "Chrome" ];
  579. }
  580. }
  581. effectBloom.screenUniforms[ "opacity" ].value = THREE.Math.mapLinear( vnorm, 0, 1, 1, 0.75 );
  582. ambientLight.color.setHSV( 0, 0, THREE.Math.mapLinear( vnorm, 0, 1, 0.07, 0.33 ) );
  583. groundBasic.color.setHSV( 0.1, 0.45, THREE.Math.mapLinear( vnorm, 0, 1, 0.725, 0.995 ) );
  584. // blur
  585. if ( blur ) {
  586. effectSave.enabled = true;
  587. effectBlend.enabled = true;
  588. } else {
  589. effectSave.enabled = false;
  590. effectBlend.enabled = false;
  591. }
  592. // update car model
  593. veyron.updateCarModel( delta, controlsVeyron );
  594. gallardo.updateCarModel( delta, controlsGallardo );
  595. // update camera
  596. if ( ! FOLLOW_CAMERA ) {
  597. cameraTarget.x = currentCar.root.position.x;
  598. cameraTarget.z = currentCar.root.position.z;
  599. } else {
  600. spotLight.position.x = currentCar.root.position.x - 500;
  601. spotLight.position.z = currentCar.root.position.z - 500;
  602. }
  603. // update shadows
  604. spotLight.target.position.x = currentCar.root.position.x;
  605. spotLight.target.position.z = currentCar.root.position.z;
  606. // render cube map
  607. var updateCubemap = true;
  608. if ( updateCubemap ) {
  609. veyron.setVisible( false );
  610. gallardo.setVisible( false );
  611. cubeCamera.updatePosition( currentCar.root.position );
  612. renderer.autoUpdateObjects = false;
  613. renderer.initWebGLObjects( scene );
  614. renderer.autoClear = true;
  615. cubeCamera.updateCubeMap( renderer, scene );
  616. veyron.setVisible( true );
  617. gallardo.setVisible( true );
  618. }
  619. // render scene
  620. renderer.autoClear = false;
  621. renderer.shadowMapEnabled = true;
  622. renderer.autoUpdateObjects = true;
  623. camera.lookAt( cameraTarget );
  624. renderer.clearTarget( null );
  625. composer.render( 0.1 );
  626. renderer.shadowMapEnabled = false;
  627. }
  628. </script>
  629. </body>
  630. </html>