Last active
September 22, 2022 09:12
-
-
Save coderofsalvation/c11903dc247452d36540f75f3cb07f68 to your computer and use it in GitHub Desktop.
AFRAME.utils.throttleTicks() allows central (throttle)control of all AFRAME components-ticks (without having to patch community components)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Usage: | |
* | |
* AFRAME.utils.throttleTicks({ | |
* 'aabb-collider':{ "*": 150 }, | |
* 'texturescroll':{ "*": 50 }, | |
* 'foo': { "*": AFRAME.utils.device.isMobile() ? 150 : 50 } | |
* }, true) | |
* | |
*/ | |
AFRAME.utils.throttleTicks = (opts, debug) => { | |
for ( let i in AFRAME.components ) { | |
let com = AFRAME.components[i] | |
let opt = opts[i] | |
let proto = com.Component.prototype | |
if( opt && proto.tick ){ | |
proto.init = function(init){ | |
return function(){ | |
init.apply(this, arguments) | |
this.tickOrig = this.tick | |
this.throttleTicks = {} | |
for ( let i in opt ) | |
this.throttleTicks[i] = AFRAME.utils.throttleTick(this.tickOrig, opt[i], this ) | |
this.tick = function(t, dt){ | |
let f = this.throttleTicks[ '#'+String(this.el.id ) ] || | |
this.throttleTicks[ '.'+String(this.el.className) ] || | |
this.throttleTicks[ '*' ] | |
f(t, dt) | |
} | |
} | |
}(proto.init) | |
}else if(debug && proto.tick) console.warn(`throttleTicks: ${i} was not specified`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment