1
+ < html >
2
+ < head >
3
+ < meta http-equiv ="Content-Type " content ="text/html; charset=UTF-8 ">
4
+ < title > Programming 3D Applications in HTML5 and WebGL — 3D Transforms</ title >
5
+ < link rel ="stylesheet " href ="../css/3dtransforms.css " />
6
+
7
+ < script src ="../libs/jquery-1.9.1/jquery-1.9.1.js "> </ script >
8
+ < script src ="../libs/three.js.r58/three.js "> </ script >
9
+ < script src ="../libs/requestAnimationFrame/RequestAnimationFrame.js "> </ script >
10
+ < script type ="text/javascript ">
11
+
12
+ var renderer = null ,
13
+ scene = null ,
14
+ camera = null ,
15
+ cube = null ;
16
+
17
+ function run ( ) {
18
+ requestAnimationFrame ( function ( ) { run ( ) ; } ) ;
19
+
20
+ // Render the scene
21
+ renderer . render ( scene , camera ) ;
22
+
23
+ }
24
+
25
+ $ ( document ) . ready (
26
+ function ( ) {
27
+
28
+ var canvas = document . getElementById ( "webglcanvas" ) ;
29
+
30
+ // Create the Three.js renderer and attach it to our canvas
31
+ renderer = new THREE . WebGLRenderer ( { canvas : canvas , antialias : true } ) ;
32
+
33
+ // Set the viewport size
34
+ renderer . setSize ( canvas . width , canvas . height ) ;
35
+
36
+ // Create a new Three.js scene
37
+ scene = new THREE . Scene ( ) ;
38
+
39
+ // Add a camera so we can view the scene
40
+ camera = new THREE . PerspectiveCamera ( 45 , canvas . width / canvas . height , 1 , 4000 ) ;
41
+ camera . position . z = 12 ;
42
+ camera . position . y = 2 ;
43
+ camera . lookAt ( new THREE . Vector3 ) ;
44
+ scene . add ( camera ) ;
45
+
46
+ // Add a directional light to show off the object
47
+ var light = new THREE . DirectionalLight ( 0xffffff , 1.5 ) ;
48
+
49
+ // Position the light out from the scene, pointing at the origin
50
+ light . position . set ( 0 , .1 , 1 ) ;
51
+ scene . add ( light ) ;
52
+
53
+ // Create a Phong material
54
+ var material = new THREE . MeshPhongMaterial ( { color :0xaa0000 } ) ;
55
+
56
+ // Create the cube geometry
57
+ var geometry = new THREE . CubeGeometry ( 2 , 2 , 2 ) ;
58
+
59
+ // And put the geometry and material together into a mesh
60
+ cube = new THREE . Mesh ( geometry , material ) ;
61
+
62
+ // Move the mesh to the left and rotate it
63
+ cube . position . x = - 4 ;
64
+ cube . rotation . x = Math . PI / 8 ;
65
+ cube . rotation . y = Math . PI / 5 ;
66
+
67
+ // Finally, add the mesh to our scene
68
+ scene . add ( cube ) ;
69
+
70
+ // Create a Phong material
71
+ var material = new THREE . MeshPhongMaterial ( { color :0x008800 } ) ;
72
+
73
+ // Create the cube geometry
74
+ var geometry = new THREE . CubeGeometry ( 2 , 2 , 2 ) ;
75
+
76
+ // And put the geometry and material together into a mesh
77
+ cube = new THREE . Mesh ( geometry , material ) ;
78
+
79
+ // Finally, add the mesh to our scene
80
+ scene . add ( cube ) ;
81
+
82
+ // Create a Phong material
83
+ var material = new THREE . MeshPhongMaterial ( { color :0x000088 } ) ;
84
+
85
+ // Create the cube geometry
86
+ var geometry = new THREE . CubeGeometry ( 2 , 2 , 2 ) ;
87
+
88
+ // And put the geometry and material together into a mesh
89
+ cube = new THREE . Mesh ( geometry , material ) ;
90
+
91
+ // Move the mesh to the right and scale it
92
+ cube . position . x = 4 ;
93
+ cube . scale . set ( 1.5 , 1.5 , 1.5 ) ;
94
+
95
+ // Finally, add the mesh to our scene
96
+ scene . add ( cube ) ;
97
+
98
+ // Run the run loop
99
+ run ( ) ;
100
+ }
101
+ ) ;
102
+
103
+ </ script >
104
+
105
+ </ head >
106
+ < body >
107
+
108
+ < canvas id ="webglcanvas " style ="border: none;background-color:#aaaaaa " width ="800 " height ="600 "> </ canvas >
109
+ < div id ="legend-left " class ="legend ">
110
+ Translate [-4, 0, 0]< br > </ br > Rotate [PI / 8, PI / 5, 0]
111
+
112
+ </ div >
113
+ < div id ="legend-right " class ="legend ">
114
+ Scale [1.5, 1.5, 1.5]
115
+
116
+ </ div >
117
+
118
+ </ body >
119
+
120
+ </ html >
0 commit comments