TimerNode.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.TimerNode = function ( scale, scope ) {
  5. THREE.FloatNode.call( this );
  6. this.scale = scale !== undefined ? scale : 1;
  7. this.scope = scope || THREE.TimerNode.GLOBAL;
  8. this.timeScale = this.scale !== 1;
  9. };
  10. THREE.TimerNode.GLOBAL = 'global';
  11. THREE.TimerNode.LOCAL = 'local';
  12. THREE.TimerNode.DELTA = 'delta';
  13. THREE.TimerNode.prototype = Object.create( THREE.FloatNode.prototype );
  14. THREE.TimerNode.prototype.constructor = THREE.TimerNode;
  15. THREE.TimerNode.prototype.nodeType = "Timer";
  16. THREE.TimerNode.prototype.isReadonly = function ( builder ) {
  17. return false;
  18. };
  19. THREE.TimerNode.prototype.isUnique = function ( builder ) {
  20. // share TimerNode "uniform" input if is used on more time with others TimerNode
  21. return this.timeScale && ( this.scope === THREE.TimerNode.GLOBAL || this.scope === THREE.TimerNode.DELTA );
  22. };
  23. THREE.TimerNode.prototype.updateFrame = function ( frame ) {
  24. var scale = this.timeScale ? this.scale : 1;
  25. switch( this.scope ) {
  26. case THREE.TimerNode.LOCAL:
  27. this.value += frame.delta * scale;
  28. break;
  29. case THREE.TimerNode.DELTA:
  30. this.value = frame.delta * scale;
  31. break;
  32. default:
  33. this.value = frame.time * scale;
  34. }
  35. };
  36. THREE.TimerNode.prototype.toJSON = function ( meta ) {
  37. var data = this.getJSONNode( meta );
  38. if ( ! data ) {
  39. data = this.createJSONNode( meta );
  40. data.scope = this.scope;
  41. data.scale = this.scale;
  42. data.timeScale = this.timeScale;
  43. }
  44. return data;
  45. };