11var Class = require ( '../utils/Class' ) ;
2-
3- var util = require ( './vecutil' ) ;
4-
2+ var Matrix4 = require ( '../math/Matrix4' ) ;
3+ var RotateVec3 = require ( '../math/RotateVec3' ) ;
54var Vector3 = require ( '../math/Vector3' ) ;
65var Vector4 = require ( '../math/Vector4' ) ;
7- var Matrix4 = require ( '../math/Matrix4' ) ;
6+
7+ // Local cache vars
88
99var tmpVec3 = new Vector3 ( ) ;
1010var tmpVec4 = new Vector4 ( ) ;
@@ -16,7 +16,8 @@ var tmpVec4 = new Vector4();
1616 */
1717var Camera3D = new Class ( {
1818
19- initialize : function ( ) {
19+ initialize : function ( )
20+ {
2021 this . direction = new Vector3 ( 0 , 0 , - 1 ) ;
2122 this . up = new Vector3 ( 0 , 1 , 0 ) ;
2223 this . position = new Vector3 ( ) ;
@@ -46,9 +47,12 @@ var Camera3D = new Class({
4647 * @param {Number } width the viewport width
4748 * @param {Number } height the viewport height
4849 */
49- setViewport : function ( width , height ) {
50+ setViewport : function ( width , height )
51+ {
5052 this . viewportWidth = width ;
5153 this . viewportHeight = height ;
54+
55+ return this ;
5256 } ,
5357
5458 /**
@@ -59,109 +63,133 @@ var Camera3D = new Class({
5963 * @param {[type] } vec [description]
6064 * @return {[type] } [description]
6165 */
62- translate : function ( x , y , z ) {
63- if ( typeof x === "object" ) {
66+ translate : function ( x , y , z )
67+ {
68+ if ( typeof x === 'object' )
69+ {
6470 this . position . x += x . x || 0 ;
6571 this . position . y += x . y || 0 ;
6672 this . position . z += x . z || 0 ;
67- } else {
73+ }
74+ else
75+ {
6876 this . position . x += x || 0 ;
6977 this . position . y += y || 0 ;
7078 this . position . z += z || 0 ;
7179 }
80+
81+ return this ;
7282 } ,
7383
74- lookAt : function ( x , y , z ) {
75- var dir = this . direction ,
76- up = this . up ;
84+ lookAt : function ( x , y , z )
85+ {
86+ var dir = this . direction ;
87+ var up = this . up ;
7788
78- if ( typeof x === "object" ) {
89+ if ( typeof x === 'object' )
90+ {
7991 dir . copy ( x ) ;
80- } else {
92+ }
93+ else
94+ {
8195 dir . set ( x , y , z ) ;
8296 }
8397
8498 dir . sub ( this . position ) . normalize ( ) ;
8599
86- //calculate right vector
100+ // Calculate right vector
87101 tmpVec3 . copy ( dir ) . cross ( up ) . normalize ( ) ;
88102
89- //calculate up vector
103+ // Calculate up vector
90104 up . copy ( tmpVec3 ) . cross ( dir ) . normalize ( ) ;
105+
106+ return this ;
91107 } ,
92108
93- rotate : function ( radians , axis ) {
94- util . rotate ( this . direction , axis , radians ) ;
95- util . rotate ( this . up , axis , radians ) ;
109+ rotate : function ( radians , axis )
110+ {
111+ RotateVec3 ( this . direction , axis , radians ) ;
112+ RotateVec3 ( this . up , axis , radians ) ;
113+
114+ return this ;
96115 } ,
97116
98- rotateAround : function ( point , radians , axis ) {
117+ rotateAround : function ( point , radians , axis )
118+ {
99119 tmpVec . copy ( point ) . sub ( this . position ) ;
120+
100121 this . translate ( tmpVec ) ;
101122 this . rotate ( radians , axis ) ;
102123 this . translate ( tmpVec . negate ( ) ) ;
124+
125+ return this ;
103126 } ,
104127
105- project : function ( vec , out ) {
106- if ( ! out )
107- out = new Vector4 ( ) ;
128+ project : function ( vec , out )
129+ {
130+ if ( out === undefined ) { out = new Vector4 ( ) ; }
108131
109- //TODO: support viewport XY
110- var viewportWidth = this . viewportWidth ,
111- viewportHeight = this . viewportHeight ,
112- n = Camera3D . NEAR_RANGE ,
113- f = Camera3D . FAR_RANGE ;
132+ // TODO: support viewport XY
133+ var viewportWidth = this . viewportWidth ;
134+ var viewportHeight = this . viewportHeight ;
135+ var n = Camera3D . NEAR_RANGE ;
136+ var f = Camera3D . FAR_RANGE ;
114137
115- // for useful Z and W values we should do the usual steps...
116- // clip space -> NDC -> window coords
138+ // For useful Z and W values we should do the usual steps: clip space -> NDC -> window coords
117139
118- //implicit 1.0 for w component
140+ // Implicit 1.0 for w component
119141 tmpVec4 . set ( vec . x , vec . y , vec . z , 1.0 ) ;
120142
121- //transform into clip space
143+ // Transform into clip space
122144 tmpVec4 . transformMat4 ( this . combined ) ;
123145
124- //now into NDC
146+ // Now into NDC
125147 tmpVec4 . x = tmpVec4 . x / tmpVec4 . w ;
126148 tmpVec4 . y = tmpVec4 . y / tmpVec4 . w ;
127149 tmpVec4 . z = tmpVec4 . z / tmpVec4 . w ;
128150
129- //and finally into window coordinates
151+ // And finally into window coordinates
130152 out . x = viewportWidth / 2 * tmpVec4 . x + ( 0 + viewportWidth / 2 ) ;
131153 out . y = viewportHeight / 2 * tmpVec4 . y + ( 0 + viewportHeight / 2 ) ;
132154 out . z = ( f - n ) / 2 * tmpVec4 . z + ( f + n ) / 2 ;
133155
134- //if the out vector has a fourth component, we also store (1/clip.w)
135- //same idea as gl_FragCoord.w
156+ // If the out vector has a fourth component, we also store (1/clip.w), same idea as gl_FragCoord.w
136157 if ( out . w === 0 || out . w )
158+ {
137159 out . w = 1 / tmpVec4 . w ;
160+ }
138161
139162 return out ;
140163 } ,
141164
142- unproject : function ( vec , out ) {
143- if ( ! out )
144- out = new Vector3 ( ) ;
165+ unproject : function ( vec , out )
166+ {
167+ if ( out === undefined ) { out = new Vector3 ( ) ; }
145168
146169 var viewport = tmpVec4 . set ( 0 , 0 , this . viewportWidth , this . viewportHeight ) ;
170+
147171 return out . copy ( vec ) . unproject ( viewport , this . invProjectionView ) ;
148172 } ,
149173
150- getPickRay : function ( x , y ) {
151- var origin = this . ray . origin . set ( x , y , 0 ) ,
152- direction = this . ray . direction . set ( x , y , 1 ) ,
153- viewport = tmpVec4 . set ( 0 , 0 , this . viewportWidth , this . viewportHeight ) ,
154- mtx = this . invProjectionView ;
174+ getPickRay : function ( x , y )
175+ {
176+ var origin = this . ray . origin . set ( x , y , 0 ) ;
177+ var direction = this . ray . direction . set ( x , y , 1 ) ;
178+ var viewport = tmpVec4 . set ( 0 , 0 , this . viewportWidth , this . viewportHeight ) ;
179+ var mtx = this . invProjectionView ;
155180
156181 origin . unproject ( viewport , mtx ) ;
182+
157183 direction . unproject ( viewport , mtx ) ;
158184
159185 direction . sub ( origin ) . normalize ( ) ;
186+
160187 return this . ray ;
161188 } ,
162189
163- update : function ( ) {
164- //left empty for subclasses
190+ update : function ( )
191+ {
192+ // Left empty for subclasses
165193 }
166194} ) ;
167195
0 commit comments