@bezlepkin/nativescript-ar
Version:
NativeScript Augmented Reality plugin. ARKit on iOS and (with the help of Sceneform) ARCore on Android.
90 lines (89 loc) • 3.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ARMaterialFactory = void 0;
class ARMaterialFactory {
static getMaterial(material) {
if (!material) {
return null;
}
const mat = SCNMaterial.new();
mat.lightingModelName = SCNLightingModelPhysicallyBased;
if (typeof material === "string") {
ARMaterialFactory.applyMaterialByString(mat, material);
}
else if (material.constructor.name === "Color") {
ARMaterialFactory.applyMaterialByColor(mat, material);
}
else {
ARMaterialFactory.applyMaterialByARMaterial(mat, material);
}
return mat;
}
static applyMaterialByColor(mat, material) {
mat.diffuse.contents = material.ios;
mat.specular.contents = material.ios;
}
static applyMaterialByString(mat, material) {
this.applyMaterialProperty(mat.diffuse, {
contents: material,
wrapMode: "Repeat"
});
}
static applyMaterialByARMaterial(mat, material) {
this.applyMaterialProperty(mat.diffuse, material.diffuse);
this.applyMaterialProperty(mat.roughness, material.roughness);
this.applyMaterialProperty(mat.metalness, material.metalness);
this.applyMaterialProperty(mat.normal, material.normal);
this.applyMaterialProperty(mat.specular, material.specular);
this.applyMaterialProperty(mat.emission, material.emission);
if (material.transparency !== undefined) {
mat.transparency = material.transparency;
}
if (material.lightingModel === "CONSTANT") {
console.log("lightingmodel is constant for mat.diffuse: " + mat.diffuse);
mat.lightingModelName = SCNLightingModelConstant;
mat.writesToDepthBuffer = false;
}
}
static applyMaterialProperty(scnMaterialProperty, materialProperty) {
if (materialProperty === undefined) {
return;
}
if (typeof materialProperty === "string") {
scnMaterialProperty.contents = UIImage.imageNamed(materialProperty);
scnMaterialProperty.wrapS = 2;
scnMaterialProperty.wrapT = 2;
}
else if (materialProperty.constructor.name === "Color") {
scnMaterialProperty.contents = materialProperty.ios;
}
else {
const prop = materialProperty;
scnMaterialProperty.contents = UIImage.imageNamed(prop.contents);
if (prop.wrapMode) {
const wrapMode = ARMaterialFactory.getWrapMode(prop.wrapMode);
scnMaterialProperty.wrapS = wrapMode;
scnMaterialProperty.wrapT = wrapMode;
}
else {
scnMaterialProperty.wrapS = 2;
scnMaterialProperty.wrapT = 2;
}
}
}
static getWrapMode(wrapMode) {
if (wrapMode === "Mirror") {
return 4;
}
else if (wrapMode === "Clamp") {
return 1;
}
else if (wrapMode === "ClampToBorder") {
return 3;
}
else {
return 2;
}
}
}
exports.ARMaterialFactory = ARMaterialFactory;