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

73 lines
2.4 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
2024-02-14 15:47:34 +00:00
if( v.floats.length < 2 ) return console.warn('xrfragment.js: got less than 4 uv values ')
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]
2024-02-14 15:47:34 +00:00
mesh.uv.uspeed = v.floats[2] || 1.0
mesh.uv.vspeed = v.floats[3] || 1.0
mesh.uv.ushift = v.shift[0]
mesh.uv.vshift = v.shift[1]
mesh.uv.uloop = v.shift[2] || false
mesh.uv.vloop = v.shift[3] || false
debugger
2024-02-08 19:40:43 +01:00
mesh.onBeforeRender = xrf.frag.uv.scroll
}
xrf.frag.uv.init = function(mesh){
2024-02-14 15:47:34 +00:00
if( !mesh.uv ) mesh.uv = {u:0, v:0, uspeed:1, vspeed:1, uloop:false, vloop:false, uv:false, ushift:false,vshift: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(){
2024-02-14 15:47:34 +00:00
let diffU = 0.0 // distance to end-state (non-looping mode)
let diffV = 0.0 // distance to end-state (non-looping mode)
let uv = this.geometry.getAttribute("uv")
2024-02-08 19:40:43 +01:00
2024-02-14 15:47:34 +00:00
// translate!
for( let i = 0; i < uv.count; i++ ){
let u = uv.getX(i)
let v = uv.getY(i)
let uTarget = (this.uv.ushift ? u : uv.old.getX(i) ) + this.uv.u
let vTarget = (this.uv.vshift ? v : uv.old.getY(i) ) + this.uv.v
2024-02-08 19:40:43 +01:00
2024-02-14 15:47:34 +00:00
// scroll U
if( this.uv.uloop ){
u += this.uv.uspeed * xrf.clock.delta
}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 * -uTarget ) // -xrf.clock.delta)
: u + (this.uv.uspeed * uTarget ) // xrf.clock.delta)
diffU += Math.abs( u - uTarget ) // are we done yet? (non-looping mode)
}
2024-02-08 19:40:43 +01:00
2024-02-14 15:47:34 +00: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 * -vTarget ) // -xrf.clock.delta)
: v + (this.uv.vspeed * vTarget ) // xrf.clock.delta)
diffV += Math.abs( v - vTarget )
2024-02-08 19:40:43 +01:00
}
2024-02-14 15:47:34 +00:00
uv.setXY(i,u,v)
}
uv.needsUpdate = true
2024-02-08 19:40:43 +01:00
2024-02-14 15:47:34 +00:00
if( (!this.uv.uloop && diffU < 0.05) &&
(!this.uv.vloop && diffV < 0.05)
){ // stop animating if done
this.onBeforeRender = function(){}
2024-02-08 19:40:43 +01:00
}
}