Skip to content

Instantly share code, notes, and snippets.

@pofulu
Last active March 7, 2019 08:09
Show Gist options
  • Save pofulu/bc2f0d2e1b5775f91ebfe5f9d6fc5d87 to your computer and use it in GitHub Desktop.
Save pofulu/bc2f0d2e1b5775f91ebfe5f9d6fc5d87 to your computer and use it in GitHub Desktop.
Script for Spark AR. Transform, Rotation, Scale model by TouchGestures. The script can avoid moving the model when rotating and scaling.
/* How to use
const Scene = require('Scene');
const planeTracker = Scene.root.find('planeTracker');
const model = Scene.root.find('model');
const trs = new TouchTRS(planeTracker);
trs.AddModel(model);
*/
function TouchTRS(planeTracker) {
const TouchGestures = require('TouchGestures');
const Reactive = require('Reactive');
let panLock = false;
this.AddModel = AddModel;
function AddModel(model) {
TouchGestures.onPan(planeTracker).subscribeOnNext(gesture => {
if (panLock) return;
planeTracker.trackPoint(gesture.location, gesture.state);
});
TouchGestures.onRotate().subscribe(gesture => {
panLock = true;
gesture.state.eq("ENDED").monitor().subscribe(() => panLock = false);
AdjustRotation(model, gesture);
});
TouchGestures.onPinch().subscribe(gesture => {
panLock = true;
gesture.state.eq("ENDED").monitor().subscribe(() => panLock = false);
AdjustScale(model, gesture);
});
}
function AdjustScale(objcet, gesture) {
var lastScaleX = objcet.transform.scaleX.pinLastValue();
objcet.transform.scaleX = Reactive.mul(lastScaleX, gesture.scale);
var lastScaleY = objcet.transform.scaleY.pinLastValue();
objcet.transform.scaleY = Reactive.mul(lastScaleY, gesture.scale);
var lastScaleZ = objcet.transform.scaleZ.pinLastValue();
objcet.transform.scaleZ = Reactive.mul(lastScaleZ, gesture.scale);
}
function AdjustRotation(objcet, gesture, lastRotationY) {
var lastRotationY = objcet.transform.rotationY.pinLastValue();
objcet.transform.rotationY = Reactive.add(lastRotationY, Reactive.mul(-1, gesture.rotation));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment