|
22 | 22 | at: http://upokecenter.dreamhosters.com/articles/donate-now-2/ |
23 | 23 | */ |
24 | 24 |
|
25 | | - |
26 | | -/** |
27 | | -* Stores time values to help enable frame-rate independent |
28 | | -* animation. |
29 | | -*/ |
30 | | -var Timer=function(){ |
31 | | - this.time=null; |
32 | | - this.lastTime=null; |
33 | | -} |
34 | | -/** |
35 | | -* Gets the position of a time value within an interval. |
36 | | -* This is useful in animation cycles lasting a certain number |
37 | | -* of seconds, such as rotating a shape in a 5-second cycle. |
38 | | -* @param {number} timeInMs A time value, in milliseconds. |
39 | | -* This could be the parameter received in a |
40 | | -* <code>requestAnimationFrame()</code> callback method. |
41 | | -* </code>. |
42 | | -* @param {number} interval The length of the interval |
43 | | -* (animation cycle), in milliseconds. |
44 | | -* @return {number} A value in the range [0, 1), where closer |
45 | | -* to 0 means closer to the start, and closer to 1 means closer |
46 | | -* to the end of the interval. If an initial time wasn't set |
47 | | -* by this timer, returns 0. |
48 | | -* @example <caption>The following code sets an angle of |
49 | | -* rotation, in degrees, such that an object rotated with the |
50 | | -* angle does a 360-degree turn in 5 seconds (5000 milliseconds). |
51 | | -* The variable <code>time</code> is assumed to be a time |
52 | | -* value in milliseconds, such as the parameter of a |
53 | | -* <code>requestAnimationFrame()</code> callback method. |
54 | | -* </code> |
55 | | -* var angle = 360 * timer.getTimePosition(time, 5000); |
56 | | -*/ |
57 | | -Timer.prototype.getTimePosition=function(timeInMs,intervalInMs){ |
58 | | - if(this.time==null) { |
59 | | - this.time=timeInMs; |
60 | | - this.lastTime=timeInMs; |
61 | | - return 0; |
62 | | - } else { |
63 | | - if(this.lastTime==null)this.lastTime=timeInMs; |
64 | | - return ((timeInMs-this.time)%intervalInMs)/intervalInMs; |
65 | | - } |
66 | | -} |
67 | | -/** |
68 | | -* Returns the number of frames that occurred since |
69 | | -* last time, where a frame is 1/60 of a second. |
70 | | -* @param {number} timeInMs A time value, in milliseconds. |
71 | | -* This could be the parameter received in a |
72 | | -* <code>requestAnimationFrame()</code> callback method. |
73 | | -* </code>. |
74 | | -* @return {number} The number of frames relative to |
75 | | -* the last time value passed to the newFrames() or |
76 | | -* getTimePosition() method. The number can include |
77 | | -* fractional frames. If an |
78 | | -* initial time or last time wasn't set by this timer, returns 0. |
79 | | -*/ |
80 | | -Timer.prototype.newFrames=function(timeInMs){ |
81 | | - if(this.time==null) { |
82 | | - this.time=timeInMs; |
83 | | - this.lastTime=timeInMs; |
84 | | - return 0; |
85 | | - } else if(this.lastTime==null){ |
86 | | - this.lastTime=timeInMs; |
87 | | - return 0; |
88 | | - } else { |
89 | | - var diff=(timeInMs-this.lastTime); |
90 | | - this.lastTime=timeInMs; |
91 | | - return diff*60.0/1000.0; |
92 | | - } |
93 | | -} |
94 | | - |
95 | 25 | // Create the 3D scene; find the HTML canvas and pass it |
96 | 26 | // to Scene3D. |
97 | 27 | var scene=new Scene3D(document.getElementById("canvas")) |
98 | 28 | .cullFace(Scene3D.BACK); |
99 | 29 | var fc=new FrameCounterDiv(scene) |
100 | 30 | scene.setPerspective(45,scene.getClientAspect(),0.1,100); |
101 | 31 | scene.setLookAt([0,0,40]); |
102 | | - var timer=new Timer() |
| 32 | + var timer={} |
103 | 33 | scene.loadAndMapTexture("461223191.jpg").then(function(tex){ |
104 | 34 | var mesh=Meshes.createBox(10,20,20); |
105 | 35 | var shape=scene.makeShape(mesh).setTexture(tex) |
106 | 36 | scene.addShape(shape); |
107 | 37 | var rotation=[0,0,0]; |
108 | 38 | GLUtil.renderLoop(function(time){ |
109 | 39 | fc.update(); |
110 | | - rotation[0]=360*timer.getTimePosition(time,12000); |
111 | | - rotation[1]=360*timer.getTimePosition(time,6000); |
| 40 | + rotation[0]=360*GLUtil.getTimePosition(timer,time,12000); |
| 41 | + rotation[1]=360*GLUtil.getTimePosition(timer,time,6000); |
112 | 42 | var q=GLMath.quatFromTaitBryan(rotation); |
113 | 43 | shape.setQuaternion(q); |
114 | 44 | scene.render(); |
|
0 commit comments