update method
WebXRController
update(
- XRInputSource? inputSource,
- XRFrame frame,
- XRReferenceSpace? referenceSpace
Implementation
WebXRController update(XRInputSource? inputSource, XRFrame frame, XRReferenceSpace? referenceSpace ) {
XRPose? inputPose;
XRPose? gripPose;
bool handPose = false;
final WebXRController? targetRay = _targetRay;
final WebXRController? grip = _grip;
final WebXRController? hand = _hand;
if ( inputSource != null && frame.session.visibilityState != 'visible-blurred' ) {
if ( targetRay != null ) {
inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace );
if ( inputPose != null ) {
targetRay.matrix.copyFromUnknown( inputPose.transform.matrix );
targetRay.matrix.decomposeEuler( targetRay.position, targetRay.rotation, targetRay.scale );
if ( inputPose.linearVelocity != null) {
targetRay.hasLinearVelocity = true;
targetRay.linearVelocity.copyFromUnknown( inputPose.linearVelocity );
}
else {
targetRay.hasLinearVelocity = false;
}
if ( inputPose.angularVelocity != null) {
targetRay.hasAngularVelocity = true;
targetRay.angularVelocity.copyFromUnknown( inputPose.angularVelocity );
}
else {
targetRay.hasAngularVelocity = false;
}
dispatchEvent( _moveEvent );
}
}
if ( hand != null && inputSource.hand != null) {
handPose = true;
final map = inputSource.hand!.dartify() as Map;
for ( final inputjoint in map.keys) {
// Update the joints groups with the XRJoint poses
final jointPose = frame.getJointPose( inputjoint, referenceSpace );
if ( hand.joints[ inputjoint['jointName'] ] == null ) {
// The transform of this joint will be updated with the joint pose on each frame
final joint = Group();
joint.matrixAutoUpdate = false;
joint.visible = false;
hand.joints[ inputjoint['jointName'] ] = joint;
// ??
hand.add( joint );
}
final joint = hand.joints[ inputjoint['jointName'] ] as Object3D;
if ( jointPose != null ) {
joint.matrix.copyFromUnknown( jointPose.transform.matrix.dartify() );
joint.matrix.decomposeEuler( joint.position, joint.rotation, joint.scale );
joint.userData['jointRadius'] = jointPose.radius;
}
joint.visible = jointPose != null;
}
// Custom events
// Check pinchz
final indexTip = hand.joints[ 'index-finger-tip' ];
final thumbTip = hand.joints[ 'thumb-tip' ];
final distance = indexTip.position.distanceTo( thumbTip.position );
const distanceToPinch = 0.02;
const threshold = 0.005;
if ( hand.inputState['pinching'] && distance > distanceToPinch + threshold ) {
hand.inputState['pinching'] = false;
dispatchEvent(Event(
type: 'pinchend',
handedness: inputSource.handedness,
target: this
));
}
else if ( ! hand.inputState['pinching'] && distance <= distanceToPinch - threshold ) {
hand.inputState['pinching'] = true;
dispatchEvent(Event(
type: 'pinchstart',
handedness: inputSource.handedness,
target: this
));
}
}
else {
if ( grip != null && inputSource.gripSpace != null) {
gripPose = frame.getPose( inputSource.gripSpace!, referenceSpace );
if ( gripPose != null ) {
grip.matrix.copyFromUnknown( gripPose.transform.matrix );
grip.matrix.decomposeEuler( grip.position, grip.rotation, grip.scale );
if ( gripPose.linearVelocity != null) {
grip.hasLinearVelocity = true;
grip.linearVelocity.copyFromUnknown( gripPose.linearVelocity );
}
else {
grip.hasLinearVelocity = false;
}
if ( gripPose.angularVelocity != null) {
grip.hasAngularVelocity = true;
grip.angularVelocity.copyFromUnknown( gripPose.angularVelocity );
}
else {
grip.hasAngularVelocity = false;
}
}
}
}
}
if ( targetRay != null ) {
targetRay.visible = ( inputPose != null );
}
if ( grip != null ) {
grip.visible = ( gripPose != null );
}
if ( hand != null ) {
hand.visible = handPose;
}
return this;
}