frame interpolation version 2

test

by pkin159

HTML

<div>
    <button id='hitOnce' class='buttonClass'>Hit Once</button>
</div>
<div>
    <button id='playScore' class='buttonclass'>playscore</button>
    <button id='playScore1' class='buttonclass'>playscore1</button>
    <button id='returnBasic' class='buttonclass'>Basic</button>
</div>
<div>Set bpm
    <input id='bpm' value=120 style='width:6em'></input>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
<script src="https://dl.dropboxusercontent.com/u/3587259/Code/Threejs/OrbitControls.js">
    
</script>

CSS

#info {
    position: absolute;
    top: 0px;
    width: 100%;
    padding: 10px;
    text-align: center;
    color: #ffff00
}
body {
    overflow: hidden;
}

JavaScript

var frameStart, frameEnd;


var Stick = function () {
    // properties
    this.T = 4; // in seconds
    this.keys = [{
        timeS: 0,
        value: 0
    }, {
        timeS: 0.25,
        value: 0.2//3.14 / 4
    }, {
        timeS: 0.3,
        value: -.2
    },
       {
        timeS: 0.5,
        value: 0
    },{
        timeS: 1,
        value: 0
    }
                ];

    this.frame1 = new THREE.Object3D();  // starting & ending frames
    this.frame2 = new THREE.Object3D();  // of startOneCycle
                                         // default frame1 = frame2 = frameChair.clone();
    
    this.startTime = 0;
    this.angle = 0; // for 'text-based' rendering
    this.instrument = 's';
    this.group = newMesh();
    
    // methods
    this.setT = function (period) {
        this.T = period;
    }
    this.setMaxKeysValue = function (intensity){
        this.keys[1].value = intensity;
    }

    this.interpolate = function (s) { // normalized time
        if (s > 1) // end of cycle
        return this.keys[this.keys.length - 1].value;

        for (var i = 1; i < this.keys.length; i++) {
            if (s < this.keys[i].timeS) break;
        }

        var vv = this.keys[i - 1].value + (s - this.keys[i - 1].timeS) / (this.keys[i].timeS - this.keys[i - 1].timeS) * (this.keys[i].value - this.keys[i - 1].value);
        return vv;
    };

    this.update = function (time) { // time: elapsedTime
        var s = (time - this.startTime) / this.T;
        
        // change stickmesh position/orientation
        this.angle = this.interpolate(s);
        this.group.rotation.x = this.angle;
        
        //var intFrame = (1-s)*this.frame1 + s*this.frame2;// 示意
        // set pos/quaternion to this.group (stick.mesh)
        var pos = this.frame1.position.lerp(this.frame2.position, s);
        this.group.position.set(pos.x, pos.y, pos.z);
        

    };

    this.startOneCycle = function () {
        this.startTime = clock.getElapsedTime();
       ...