@bezlepkin/nativescript-ar
Version:
NativeScript Augmented Reality plugin. ARKit on iOS and (with the help of Sceneform) ARCore on Android.
95 lines (94 loc) • 4.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ARPlane = void 0;
const ar_common_1 = require("../../ar-common");
const arcommongeometry_1 = require("./arcommongeometry");
class ARPlane extends arcommongeometry_1.ARCommonGeometryNode {
constructor(options, node) {
if (options) {
super(options, node);
}
}
static createExternal(options) {
const dimensions = (typeof options.dimensions !== "number" ? options.dimensions : {
x: options.dimensions,
y: options.dimensions
});
const plane = SCNPlane.planeWithWidthHeight(dimensions.x, dimensions.y);
return new ARPlane(options, SCNNode.nodeWithGeometry(plane));
}
static create(anchor, opacity, material) {
const instance = new ARPlane();
instance.ios = SCNNode.new();
instance.anchor = anchor;
const anchorstr = "" + anchor;
const extentStart = anchorstr.indexOf("extent=(") + "extent=(".length;
const extentStr = anchorstr.substring(extentStart, anchorstr.indexOf(")", extentStart));
const extendParts = extentStr.split(" ");
const planeHeight = 0.01;
instance.planeGeometry = SCNBox.boxWithWidthHeightLengthChamferRadius(+extendParts[0], planeHeight, +extendParts[2], 0);
const translationStart = anchorstr.indexOf("<translation=(") + "<translation=(".length;
const translationStr = anchorstr.substring(translationStart, anchorstr.indexOf(")", translationStart));
const translationParts = translationStr.split(" ");
instance.position = new ar_common_1.ARPosition(+translationParts[0], +translationParts[1], +translationParts[2]);
instance.setMaterial(material, opacity);
const planeNode = SCNNode.nodeWithGeometry(instance.planeGeometry);
planeNode.position = { x: 0, y: -planeHeight / 2, z: 0 };
planeNode.physicsBody = SCNPhysicsBody.bodyWithTypeShape(2, SCNPhysicsShape.shapeWithGeometryOptions(instance.planeGeometry, null));
ARPlane.setTextureScale(instance.planeGeometry);
instance.ios.addChildNode(planeNode);
instance.id = instance.anchor.identifier.UUIDString;
return instance;
}
setMaterial(material, opacity) {
const transparentMaterial = SCNMaterial.new();
transparentMaterial.diffuse.contents = UIColor.colorWithWhiteAlpha(1.0, 0.0);
const materialArray = NSMutableArray.alloc().initWithCapacity(6);
materialArray.addObject(transparentMaterial);
materialArray.addObject(transparentMaterial);
materialArray.addObject(transparentMaterial);
materialArray.addObject(transparentMaterial);
if (opacity === 0 || material === null) {
materialArray.addObject(transparentMaterial);
}
else {
material.transparency = opacity;
materialArray.addObject(material);
}
materialArray.addObject(transparentMaterial);
this.planeGeometry.materials = materialArray;
}
update(anchor) {
const anchorstr = "" + anchor;
const extentStart = anchorstr.indexOf("extent=(") + "extent=(".length;
const extentStr = anchorstr.substring(extentStart, anchorstr.indexOf(")", extentStart));
const extendParts = extentStr.split(" ");
this.planeGeometry.width = +extendParts[0];
this.planeGeometry.length = +extendParts[2];
const centerStart = anchorstr.indexOf("center=(") + "center=(".length;
const centerStr = anchorstr.substring(centerStart, anchorstr.indexOf(")", centerStart));
const centerParts = centerStr.split(" ");
this.ios.position = { x: +centerParts[0], y: 0, z: +centerParts[2] };
const childNode = this.ios.childNodes.firstObject;
childNode.physicsBody = SCNPhysicsBody.bodyWithTypeShape(2, SCNPhysicsShape.shapeWithGeometryOptions(this.planeGeometry, null));
ARPlane.setTextureScale(this.planeGeometry);
}
remove() {
this.ios.removeFromParentNode();
}
static setTextureScale(planeGeometry) {
const width = planeGeometry.width;
const height = planeGeometry.length;
const material = planeGeometry.materials[4];
const scaleFactor = 1;
const m = new SCNMatrix4();
m.m11 = width * scaleFactor;
m.m22 = height * scaleFactor;
m.m33 = 1;
material.diffuse.contentsTransform = m;
material.roughness.contentsTransform = m;
material.metalness.contentsTransform = m;
material.normal.contentsTransform = m;
}
}
exports.ARPlane = ARPlane;