tween on positions

This commit is contained in:
STEINNI
2025-09-28 18:28:17 +00:00
parent 34d93cecdb
commit 8be18e8d83
8 changed files with 106 additions and 88 deletions
+36 -36
View File
@@ -69,60 +69,45 @@ export class Threetobus{
processBusEvent(eventType, chan, payload, userId, x){
console.log('processBusEvent====>',eventType, chan, payload, userId)
const chanObj = this._curEventsMapping.find(item => item.chan==chan)
if(!chanObj) return
console.log('processBusEvent====>chanObj', chanObj)
const eventObj = chanObj.events.find(item => item.eventName==eventType)
if(!eventObj) return
console.log('processBusEvent====>eventObj', eventObj)
for(const mapping of eventObj.mappings){
const id = this.getValueByPath(payload, mapping.id)
console.log('agent ID:', id)
if(id){
const obj3D = this.scene.getObjectByName(id)
this.assignFromConfig(payload, mapping.assign, obj3D)
this.assignFromConfig(payload, mapping, obj3D)
}
// console.log(`found ${mappings.length} mappings`)
// mappings.forEach(snapEl => {
// const newAttr = this.assignFromConfig(payload, mapping.assign)
// if(mapping.animate){
// snapEl.animate(
// newAttr,
// 400,
// mina.linear
// )
// } else {
// snapEl.attr(newAttr)
// }
// })
}
}
assignFromConfig(payload, replaceDef, obj3D) {
console.log('assignFromConfig', payload, replaceDef)
for (const [path, rule] of Object.entries(replaceDef)) {
assignFromConfig(payload, mapping, obj3D) {
const toTween = {}
for (const [path, rule] of Object.entries(mapping.assign)) {
let value
if (typeof rule === 'string') { // plain path
value= this.getValueByPath(payload, rule)
} else if((typeof(rule) == 'object') && (typeof(rule.transformer) == 'function')) { // transformer
const fnargs = (rule.arguments || []).map(arg => this.getValueByPath(payload,arg))
value = rule.transformer(...fnargs)
}
console.log('====>',path ,value)
if (value !== undefined) {
this.setProp(obj3D, path, value)
if(mapping.tween && path.startsWith('position.')){ //TODO allow other tweenables
toTween[path.substring(9)] = value
} else {
this.setProp(obj3D, path, value)
}
}
}
}
if(mapping.tween && (Object.keys(toTween).length>0)){
toTween.object = obj3D
toTween.delay = mapping.tweenDelay
this.smoothMove(toTween)
}
}
setProp(obj3D, path, value) { console.log('====>setProp', path, value)
setProp(obj3D, path, value) {
const parts = path.split('.')
let target = obj3D
for (let i = 0; i < parts.length - 1; i++) {
@@ -194,7 +179,7 @@ export class Threetobus{
}
buildFromJSON(id, desc){ console.log('===buildFromJSON molecule1==>', desc)
buildFromJSON(id, desc){
let obj
if(desc.type === 'Mesh') {
const geom = new THREE[desc.geometry.type](...(desc.geometry.args || []))
@@ -222,8 +207,11 @@ export class Threetobus{
return obj
}
smoothRelMove(options){
smoothMove(options){ console.log('=====>', options)
// options: object, dX, dY, dZ, delay, easing, easingMode
// Absolute: x,y,z
// Relative: dx,dy,dz
// delay: ms
// easings: Linear, Quadratic, Cubic, Quartic, Quintic, Sinusoidal, Exponential, Circular, Elastic, Back, Bounce
// easingMode: In → starts slow, accelerates towards the end.
// Out → starts fast, decelerates smoothly.
@@ -231,10 +219,22 @@ export class Threetobus{
options.easing = options.easing ? options.easing : 'Quadratic'
options.easingMode = options.easingMode ? options.easingMode : 'InOut'
let newX = parseFloat(options.x)
newX = isNaN(newX) ? options.object.position.x : newX
newX += (parseFloat(options.dx) || 0)
let newY = parseFloat(options.y)
newY = isNaN(newY) ? options.object.position.y : newY
newY += (parseFloat(options.dy) || 0)
let newZ = parseFloat(options.z)
newZ = isNaN(newZ) ? options.object.position.z : newZ
newZ += (parseFloat(options.dz) || 0)
new TWEEN.Tween(options.object.position)
.to({ x: options.object.position.x + options.dX,
y: options.object.position.y + options.dY,
z: options.object.position.z + options.dZ,
.to({ x: newX,
y: newY,
z: newZ,
}, options.delay)
.easing(TWEEN.Easing[options.easing][options.easingMode])
.start()