/*
* ARnft.ts
* ARnft
*
* This file is part of ARnft - WebARKit.
*
* ARnft is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ARnft is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ARnft. If not, see .
*
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent modules, and to
* copy and distribute the resulting executable under terms of your choice,
* provided that you also meet, for each linked independent module, the terms and
* conditions of the license of that module. An independent module is a module
* which is neither derived from nor based on this library. If you modify this
* library, you may extend this exception to your version of the library, but you
* are not obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* Copyright 2021-2025 WebARKit.
*
* Author(s): Walter Perdan @kalwalt https://github.com/kalwalt
*
*/
import Container from "./utils/html/Container";
import { ConfigData } from "./config/ConfigData";
import Stats from "stats.js";
import { CameraViewRenderer, ICameraViewRenderer } from "./renderers/CameraViewRenderer";
import { getConfig } from "./utils/ARnftUtils";
import NFTWorker from "./NFTWorker";
import { v4 as uuidv4 } from "uuid";
import packageJson from "../package.json";
const { version } = packageJson;
/**
* Basic interface for an Entity.
* @param name the name of the Entity
* @param markerUrl the marker url associated
*/
export interface IEntity {
name: string;
markerUrl: string;
}
/**
* IInitConfig interface for the base configuration.
* @param width the width in pixels of the video camera.
* @param height the height in pixels of the video camera.
* @param configUrl the url of the config.json file.
* @param stats true if you want the stats.
* @param autoUpdate false if you want to maintain it yourself
*/
export interface IInitConfig {
/** the width in pixels of the video camera. */
width: number;
/** the height in pixels of the video camera. */
height: number;
/** the url of the config.json file. */
configUrl: string;
/** true if you want the stats. */
stats?: boolean;
/** false if you want to maintain it yourself */
autoUpdate?: boolean;
}
/**
* INameInitConfig extends IInitConfig and it is used by the initWithConfig method.
* @param markerUrls an Array of Array of marker urls.
* @param names an Array of Array of entity names.
*/
export interface INameInitConfig extends IInitConfig {
/** the Array of url of the markers (without the extension) */
markerUrls: Array>;
/** the names of the markers */
names: Array>;
}
/**
* IEntityInitConfig used by the initWithEntities method
* @param entities an Array of Entity
*/
export interface IEntityInitConfig extends IInitConfig {
/** the Array of Entity. */
entities: IEntity[];
}
/**
* IViews is used internally by ARnft
*/
export interface IViews {
container: HTMLDivElement;
canvas: HTMLCanvasElement;
video: HTMLVideoElement;
loading?: HTMLElement;
stats?: HTMLElement;
}
export default class ARnft {
public cameraView: CameraViewRenderer;
public appData: ConfigData;
public addPath: string;
public width: number;
public height: number;
public configUrl: string;
public markerUrl: string;
public camData: string;
public autoUpdate: boolean = true;
private controllers: NFTWorker[];
private static entities: IEntity[];
private target: EventTarget;
private uuid: string;
private version: string;
private initialized: boolean;
private _views: IViews;
/**
* The **ARnft** constructor to create a new instance of the ARnft class.
* Example code:
* ```javascript
* const nft = new ARnft(640, 480, 'config.json');
* ```
* @param width (number) the width in pixels of the video camera.
* @param height (number) the height in pixels of the video camera.
* @param configUrl (string) the url of the config.json file
*/
constructor(width: number, height: number, configUrl: string) {
this.width = width;
this.height = height;
this.configUrl = configUrl;
this.target = window || global;
this.uuid = uuidv4();
this.version = version;
console.log("ARnft ", this.version);
}
/**
* The init function let define the basic set-up for the NFT marker.
* Internally use the initialize function, that is responsible to load all the resources.
* @param width (number) the width in pixels of the video camera.
* @param height (number) the height in pixels of the video camera.
* @param markerUrls (Array) the Array of url of the markers (without the extension)
* @param names the names of the markers
* @param configUrl (string) the url of the config.json file
* @param stats (boolean) true if you want the stats.
* @returns (object) the nft object.
*/
static async init(
width: number,
height: number,
markerUrls: Array>,
names: Array>,
configUrl: string,
stats: boolean
): Promise