1+ if ( ! Detector . webgl ) Detector . addGetWebGLMessage ( ) ;
2+
3+ var SCREEN_WIDTH = window . innerWidth ;
4+ var SCREEN_HEIGHT = window . innerHeight ;
5+
6+ var container , stats ;
7+
8+ var camera , scene , renderer ;
9+
10+ var lightMesh ;
11+
12+ var directionalLight , pointLight ;
13+
14+ var mouseX = 0 , mouseY = 0 ;
15+
16+ var windowHalfX = window . innerWidth / 2 ;
17+ var windowHalfY = window . innerHeight / 2 ;
18+
19+ init ( ) ;
20+ animate ( ) ;
21+
22+ function init ( ) {
23+
24+ container = document . createElement ( 'div' ) ;
25+ document . body . appendChild ( container ) ;
26+
27+ scene = new THREE . Scene ( ) ;
28+
29+ // CAMERA
30+
31+ camera = new THREE . PerspectiveCamera ( 70 , SCREEN_WIDTH / SCREEN_HEIGHT , 1 , 10000 ) ;
32+ camera . position . z = 1000 ;
33+ scene . add ( camera ) ;
34+
35+ // LIGHTS
36+
37+ var ambient = new THREE . AmbientLight ( 0x020202 ) ;
38+ scene . add ( ambient ) ;
39+
40+ directionalLight = new THREE . DirectionalLight ( 0xffffff ) ;
41+ directionalLight . position . set ( 1 , 1 , 0.5 ) . normalize ( ) ;
42+ scene . add ( directionalLight ) ;
43+
44+ pointLight = new THREE . PointLight ( 0xffaa00 ) ;
45+ pointLight . position . set ( 0 , 0 , 0 ) ;
46+ scene . add ( pointLight ) ;
47+
48+ sphere = new THREE . SphereGeometry ( 100 , 16 , 8 ) ;
49+ lightMesh = new THREE . Mesh ( sphere , new THREE . MeshBasicMaterial ( { color : 0xffaa00 } ) ) ;
50+ lightMesh . scale . set ( 0.05 , 0.05 , 0.05 ) ;
51+ lightMesh . position = pointLight . position ;
52+ scene . add ( lightMesh ) ;
53+
54+ renderer = new THREE . WebGLRenderer ( ) ;
55+ renderer . setSize ( SCREEN_WIDTH , SCREEN_HEIGHT ) ;
56+ renderer . setFaceCulling ( 0 ) ;
57+
58+ container . appendChild ( renderer . domElement ) ;
59+
60+ stats = new Stats ( ) ;
61+ stats . domElement . style . position = 'absolute' ;
62+ stats . domElement . style . top = '0px' ;
63+ stats . domElement . style . zIndex = 100 ;
64+
65+ container . appendChild ( stats . domElement ) ;
66+
67+ // renderer.domElement.classList.add('flip-horizontally');
68+
69+ // document.addEventListener('mousemove', onDocumentMouseMove, false);
70+
71+ var r = "js/threejs/textures/cube/SwedishRoyalCastle/" ;
72+ var urls = [ r + "px.jpg" , r + "nx.jpg" ,
73+ r + "py.jpg" , r + "ny.jpg" ,
74+ r + "pz.jpg" , r + "nz.jpg" ] ;
75+
76+ var textureCube = THREE . ImageUtils . loadTextureCube ( urls ) ;
77+
78+ var camaroMaterials = {
79+
80+ body : [ ] ,
81+ chrome : new THREE . MeshLambertMaterial ( { color : 0xffffff , envMap : textureCube } ) ,
82+ darkchrome : new THREE . MeshLambertMaterial ( { color : 0x444444 , envMap : textureCube } ) ,
83+ glass : new THREE . MeshBasicMaterial ( { color : 0x223344 , envMap : textureCube , opacity : 0.25 , combine : THREE . MixOperation , reflectivity : 0.25 , transparent : true } ) ,
84+ tire : new THREE . MeshLambertMaterial ( { color : 0x050505 } ) ,
85+ interior : new THREE . MeshPhongMaterial ( { color : 0x050505 , shininess : 20 } ) ,
86+ black : new THREE . MeshLambertMaterial ( { color : 0x000000 } )
87+
88+ }
89+
90+ camaroMaterials . body . push ( [ "Orange" , new THREE . MeshLambertMaterial ( { color : 0xff6600 , envMap : textureCube , combine : THREE . MixOperation , reflectivity : 0.3 } ) ] ) ;
91+ camaroMaterials . body . push ( [ "Blue" , new THREE . MeshLambertMaterial ( { color : 0x226699 , envMap : textureCube , combine : THREE . MixOperation , reflectivity : 0.3 } ) ] ) ;
92+ camaroMaterials . body . push ( [ "Red" , new THREE . MeshLambertMaterial ( { color : 0x660000 , envMap : textureCube , combine : THREE . MixOperation , reflectivity : 0.5 } ) ] ) ;
93+ camaroMaterials . body . push ( [ "Black" , new THREE . MeshLambertMaterial ( { color : 0x000000 , envMap : textureCube , combine : THREE . MixOperation , reflectivity : 0.5 } ) ] ) ;
94+ camaroMaterials . body . push ( [ "White" , new THREE . MeshLambertMaterial ( { color : 0xffffff , envMap : textureCube , combine : THREE . MixOperation , reflectivity : 0.5 } ) ] ) ;
95+
96+ camaroMaterials . body . push ( [ "Carmine" , new THREE . MeshPhongMaterial ( { color : 0x770000 , specular :0xffaaaa , envMap : textureCube , combine : THREE . MultiplyOperation } ) ] ) ;
97+ camaroMaterials . body . push ( [ "Gold" , new THREE . MeshPhongMaterial ( { color : 0xaa9944 , specular :0xbbaa99 , shininess :50 , envMap : textureCube , combine : THREE . MultiplyOperation } ) ] ) ;
98+ camaroMaterials . body . push ( [ "Bronze" , new THREE . MeshPhongMaterial ( { color : 0x150505 , specular :0xee6600 , shininess :10 , envMap : textureCube , combine : THREE . MixOperation , reflectivity : 0.5 } ) ] ) ;
99+ camaroMaterials . body . push ( [ "Chrome" , new THREE . MeshPhongMaterial ( { color : 0xffffff , specular :0xffffff , envMap : textureCube , combine : THREE . MultiplyOperation } ) ] ) ;
100+
101+ var loader = new THREE . BinaryLoader ( ) ;
102+ loader . load ( "js/threejs/obj/camaro/CamaroNoUv_bin.js" , function ( geometry ) { createScene ( geometry , camaroMaterials ) } ) ;
103+
104+ }
105+
106+ function $ ( id ) { return document . getElementById ( id ) }
107+
108+
109+ function createButtons ( materials , geometry ) {
110+
111+ var i , src = "" , parent = $ ( "buttons" ) ;
112+
113+ for ( i = 0 ; i < materials . length ; i ++ ) {
114+
115+ src += '<button id="m' + i + '">' + materials [ i ] [ 0 ] + '</button>' ;
116+
117+ }
118+
119+ parent . innerHTML = src ;
120+
121+ for ( i = 0 ; i < materials . length ; i ++ ) {
122+
123+ $ ( "m" + i ) . counter = i ;
124+ $ ( "m" + i ) . addEventListener ( 'click' , function ( ) { geometry . materials [ 0 ] = materials [ this . counter ] [ 1 ] } , false ) ;
125+
126+ }
127+
128+ }
129+
130+ function createScene ( geometry , materials ) {
131+
132+ var s = 75 , m = new THREE . MeshFaceMaterial ( ) ;
133+
134+ geometry . materials [ 0 ] = materials . body [ 0 ] [ 1 ] ; // car body
135+ geometry . materials [ 1 ] = materials . chrome ; // wheels chrome
136+ geometry . materials [ 2 ] = materials . chrome ; // grille chrome
137+ geometry . materials [ 3 ] = materials . darkchrome ; // door lines
138+ geometry . materials [ 4 ] = materials . glass ; // windshield
139+ geometry . materials [ 5 ] = materials . interior ; // interior
140+ geometry . materials [ 6 ] = materials . tire ; // tire
141+ geometry . materials [ 7 ] = materials . black ; // tireling
142+ geometry . materials [ 8 ] = materials . black ; // behind grille
143+
144+ var mesh = new THREE . Mesh ( geometry , m ) ;
145+ mesh . rotation . y = 1 ;
146+ mesh . scale . set ( s , s , s ) ;
147+ scene . add ( mesh ) ;
148+
149+ createButtons ( materials . body , geometry ) ;
150+
151+ }
152+
153+ // function onDocumentMouseMove(event) {
154+
155+ // mouseX = ( event.clientX - windowHalfX );
156+ // mouseY = ( event.clientY - windowHalfY );
157+
158+ // }
159+
160+ mouseX = mouseY = 0 ;
161+
162+ //
163+
164+ function animate ( ) {
165+
166+ requestAnimationFrame ( animate ) ;
167+
168+ render ( ) ;
169+ stats . update ( ) ;
170+
171+ }
172+
173+ function render ( ) {
174+
175+ var timer = - 0.0002 * Date . now ( ) ;
176+
177+ camera . position . x += ( mouseX - camera . position . x ) * .08 ;
178+ camera . position . y += ( - mouseY - camera . position . y ) * .09 ;
179+
180+ camera . lookAt ( scene . position ) ;
181+
182+ lightMesh . position . x = 1500 * Math . cos ( timer ) ;
183+ lightMesh . position . z = 1500 * Math . sin ( timer ) ;
184+
185+ renderer . render ( scene , camera ) ;
186+
187+ }
0 commit comments