xrfragment-haxe/src/3rd/js/three/xrf/uv.js

72 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-02-08 19:40:43 +01:00
xrf.frag.uv = function(v, opts){
let { frag, mesh, model, camera, scene, renderer, THREE} = opts
if( !mesh.geometry ) return // nothing to do here
if( v.floats.length != 4 ) return
2024-02-08 19:40:43 +01:00
xrf.frag.uv.init(mesh)
mesh.uv.u = v.floats[0]
mesh.uv.v = v.floats[1]
mesh.uv.uspeed = v.floats[2]
mesh.uv.vspeed = v.floats[3]
mesh.uv.uloop = v.shift[2]
mesh.uv.vloop = v.shift[3]
2024-02-08 19:40:43 +01:00
mesh.onBeforeRender = xrf.frag.uv.scroll
}
xrf.frag.uv.init = function(mesh){
if( !mesh.uv ) mesh.uv = {u:0, v:0, uspeed:1, vspeed:1, uloop:false, vloop:false, uv:false}
2024-02-08 19:40:43 +01:00
let uv = mesh.geometry.getAttribute("uv")
if( !uv.old ) uv.old = mesh.geometry.getAttribute("uv").clone()
2024-02-08 19:40:43 +01:00
}
xrf.frag.uv.scroll = function(){
if( this.uv.uspeed > 0.0 || this.uv.vspeed > 0.0 ){
2024-02-08 19:40:43 +01:00
let diffU = 0.0 // distance to end-state (non-looping mode)
let diffV = 0.0 // distance to end-state (non-looping mode)
2024-02-08 19:40:43 +01:00
let uv = this.geometry.getAttribute("uv")
// translate!
for( let i = 0; i < uv.count; i++ ){
let u = uv.getX(i)
let v = uv.getY(i)
let uTarget = uv.old.getX(i) + this.uv.u
let vTarget = uv.old.getY(i) + this.uv.v
2024-02-08 19:40:43 +01:00
// scroll U
if( this.uv.uloop ){
u += this.uv.uspeed * xrf.clock.delta
2024-02-08 19:40:43 +01:00
}else{
// recover from super-high uv-values due to looped scrolling
if( Math.abs(u-uTarget) > 1.0 ) u = uv.old.getX(i)
u = u > uTarget ? u + (this.uv.uspeed * -xrf.clock.delta)
: u + (this.uv.uspeed * xrf.clock.delta)
diffU += Math.abs( u - uTarget ) // are we done yet? (non-looping mode)
}
2024-02-08 19:40:43 +01:00
// scroll V
if( this.uv.vloop ){
v += this.uv.vspeed * xrf.clock.delta
}else{
// recover from super-high uv-values due to looped scrolling
if( Math.abs(v-vTarget) > 1.0 ) v = uv.old.getY(i)
v = v > vTarget ? v + (this.uv.vspeed * -xrf.clock.delta)
: v + (this.uv.vspeed * xrf.clock.delta)
diffV += Math.abs( v - vTarget )
2024-02-08 19:40:43 +01:00
}
uv.setXY(i,u,v)
}
uv.needsUpdate = true
if( (!this.uv.uloop && diffU < 0.05) &&
(!this.uv.vloop && diffV < 0.05)
){ // stop animating if done
2024-02-08 19:40:43 +01:00
this.onBeforeRender = function(){}
}
}
}