|
46 | 46 | return group; |
47 | 47 | } |
48 | 48 |
|
49 | | -function moveStarField(group, range){ |
| 49 | +function moveStarField(group, range,frames){ |
50 | 50 | for(var i=0;i<group.shapes.length;i++){ |
51 | 51 | var pos=group.shapes[i].getMatrix() |
52 | 52 | if(pos[14]>range/2){ |
53 | 53 | // once the star is too close, move it elsewhere |
54 | 54 | setStarPos(group.shapes[i],range) |
55 | 55 | group.shapes[i].getTransform().movePosition(0,0,-range); |
56 | 56 | } else { |
57 | | - group.shapes[i].getTransform().movePosition(0,0,range/250); |
| 57 | + group.shapes[i].getTransform().movePosition(0,0,range/250*frames); |
58 | 58 | } |
59 | 59 | } |
60 | 60 | } |
61 | 61 |
|
| 62 | + |
| 63 | +/** |
| 64 | +* Stores time values to help enable frame-rate independent |
| 65 | +* animation. |
| 66 | +*/ |
| 67 | +var Timer=function(){ |
| 68 | + this.time=null; |
| 69 | + this.lastTime=null; |
| 70 | +} |
| 71 | +/** |
| 72 | +* Gets the position of a time value within an interval. |
| 73 | +* This is useful in animation cycles lasting a certain number |
| 74 | +* of seconds, such as rotating a shape in a 5-second cycle. |
| 75 | +* @param {number} timeInMs A time value, in milliseconds. |
| 76 | +* This could be the parameter received in a |
| 77 | +* <code>requestAnimationFrame()</code> callback method. |
| 78 | +* </code>. |
| 79 | +* @param {number} interval The length of the interval |
| 80 | +* (animation cycle), in milliseconds. |
| 81 | +* @return {number} A value in the range [0, 1), where closer |
| 82 | +* to 0 means closer to the start, and closer to 1 means closer |
| 83 | +* to the end of the interval. If an initial time wasn't set |
| 84 | +* by this timer, returns 0. |
| 85 | +* @example <caption>The following code sets an angle of |
| 86 | +* rotation, in degrees, such that an object rotated with the |
| 87 | +* angle does a 360-degree turn in 5 seconds (5000 milliseconds). |
| 88 | +* The variable <code>time</code> is assumed to be a time |
| 89 | +* value in milliseconds, such as the parameter of a |
| 90 | +* <code>requestAnimationFrame()</code> callback method. |
| 91 | +* </code> |
| 92 | +* var angle = 360 * timer.getTimePosition(time, 5000); |
| 93 | +*/ |
| 94 | +Timer.prototype.getTimePosition=function(timeInMs,intervalInMs){ |
| 95 | + if(this.time==null) { |
| 96 | + this.time=timeInMs; |
| 97 | + this.lastTime=timeInMs; |
| 98 | + return 0; |
| 99 | + } else { |
| 100 | + if(this.lastTime==null)this.lastTime=timeInMs; |
| 101 | + return ((timeInMs-this.time)%intervalInMs)/intervalInMs; |
| 102 | + } |
| 103 | +} |
| 104 | +/** |
| 105 | +* Returns the number of frames that occurred since |
| 106 | +* last time, where a frame is 1/60 of a second. |
| 107 | +* @param {number} timeInMs A time value, in milliseconds. |
| 108 | +* This could be the parameter received in a |
| 109 | +* <code>requestAnimationFrame()</code> callback method. |
| 110 | +* </code>. |
| 111 | +* @return {number} The number of frames relative to |
| 112 | +* the last time value passed to the newFrames() or |
| 113 | +* getTimePosition() method. The number can include |
| 114 | +* fractional frames. If an |
| 115 | +* initial time or last time wasn't set by this timer, returns 0. |
| 116 | +*/ |
| 117 | +Timer.prototype.newFrames=function(timeInMs){ |
| 118 | + if(this.time==null) { |
| 119 | + this.time=timeInMs; |
| 120 | + this.lastTime=timeInMs; |
| 121 | + return 0; |
| 122 | + } else if(this.lastTime==null){ |
| 123 | + this.lastTime=timeInMs; |
| 124 | + return 0; |
| 125 | + } else { |
| 126 | + var diff=(timeInMs-this.lastTime); |
| 127 | + this.lastTime=timeInMs; |
| 128 | + return diff*60.0/1000.0; |
| 129 | + } |
| 130 | +} |
| 131 | + |
62 | 132 | // Create the 3D scene; find the HTML canvas and pass it |
63 | 133 | // to Scene3D. |
64 | | - var scene=new Scene3D(document.getElementById("canvas")); |
| 134 | + var scene=new Scene3D(document.getElementById("canvas")) |
| 135 | + .cullFace(Scene3D.BACK); |
65 | 136 | var fc=new FrameCounterDiv(scene) |
| 137 | + var timer=new Timer(); |
66 | 138 | scene.setPerspective(45,scene.getClientAspect(),5,1000).setLookAt([0,0,500]); |
67 | 139 | var group=starField(scene,1000) |
68 | 140 | scene.addShape(group); |
69 | | - GLUtil.renderLoop(function(){ |
| 141 | + timer.newFrames(); |
| 142 | + GLUtil.renderLoop(function(time){ |
| 143 | + moveStarField(group,1000,timer.newFrames(time)) |
70 | 144 | fc.update() |
71 | 145 | scene.render(); |
72 | | - moveStarField(group,1000) |
73 | 146 | }) |
74 | 147 | //--> |
75 | 148 | </script> |
|
0 commit comments