webarkit / ARnft

A small javascript library for WebAR with NFT
GNU Lesser General Public License v3.0
219 stars 53 forks source link

solved (use high dpi): No AR on detected images #301

Closed SwaggerNiels closed 1 year ago

SwaggerNiels commented 1 year ago

But when I use NFT creator (web/node.js) and

It detects it! (as outputted in the browser console) but... then it does not show the AR (red cube, flamingo, etc.)

What could be the problem here?

Edit: Sometimes I see a brief flash of the AR object on my image, but it dissapears within a fraction of a second

SwaggerNiels commented 1 year ago

I think my method of editing the nft tracker is wrong. I have used the pinball image to make my "own" fset-, fset3-, iset-files. (named: pinball_own) And changed the code to fit these names.

It then fails to show the AR objects, while it does detect the image with the "pinball_own" files.

_ARnft\examples\arNFT_autoupdateexample.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>ARnft example showing a simple red cube</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=0.5, maximum-scale=1" />
        <link rel="stylesheet" href="css/nft-style.css" />
    </head>

    <body>
        <a href="https://raw.githubusercontent.com/artoolkitx/artoolkit5/master/doc/Marker%20images/pinball.jpg" class="ui marker" target="_blank">
            🖼 Marker Image
        </a>

        <script src="js/third_party/three.js/three.min.js"></script>
        <script src="js/ARnftThreejs.js"></script>
        <script src="../dist/ARnft.js"></script>

        <script>
            let width = 480;
            let height = 640;
            ARnft.ARnft.initWithConfig({
                width: width,
                height: height,
                markerUrls: [["examples/DataNFT/pinball_own"]],
                names: [["pinball_own"]],
                configUrl: "config.json",
                stats: true,
                autoUpdate: false,
            })
                .then((nft) => {
                    let mat = new THREE.MeshLambertMaterial({
                        color: 0xff0000,
                    });
                    let boxGeom = new THREE.BoxGeometry(1, 1, 1);
                    let cube = new THREE.Mesh(boxGeom, mat);
                    cube.position.z = 90;
                    cube.scale.set(1, 1, 180);

                    let mat2 = new THREE.MeshLambertMaterial({
                        color: 0xff0000,
                    });
                    let boxGeom2 = new THREE.BoxGeometry(1, 1, 1);
                    let cube2 = new THREE.Mesh(boxGeom, mat);
                    cube2.position.z = 10;
                    cube2.scale.set(10, 10, 10);

                    document.addEventListener("containerEvent", function (ev) {
                        let canvas = document.getElementById("canvas");
                        let fov = (0.8 * 180) / Math.PI;
                        let ratio = width / height;
                        let config = {
                            renderer: {
                                alpha: true,
                                antialias: true,
                                context: null,
                                precision: "mediump",
                                premultipliedAlpha: true,
                                stencil: true,
                                depth: true,
                                logarithmicDepthBuffer: true,
                            },
                            camera: {
                                fov: fov,
                                ratio: ratio,
                                near: 0.01,
                                far: 1000,
                            },
                        };

                        let sceneThreejs = new ARnftThreejs.SceneRendererTJS(config, canvas, nft.uuid, true);
                        sceneThreejs.initRenderer();

                        let nftAddTJS = new ARnftThreejs.NFTaddTJS(nft.uuid);
                        nftAddTJS.add(cube, "pinball", false);
                        nftAddTJS.add(cube2, "pinball_own", false);

                        const fpsInterval = 1000 / 30;
                        let now = Date.now(), elapsed = 0, then = now;
                        const tick = () => {
                            now = Date.now();
                        elapsed = now - then;
                            sceneThreejs.draw();
                            if (elapsed > fpsInterval) {
                                then = now - (elapsed % fpsInterval);
                                nft.update();
                            }
                            window.requestAnimationFrame(tick);
                        };
                        tick();
                    });
                })
                .catch((error) => {
                    console.log(error);
                });
        </script>
    </body>
</html>
kalwalt commented 1 year ago

Hi @SwaggerNiels welcome to webarkit maybe as you said in this issue you were using bad dpi?

SwaggerNiels commented 1 year ago

Yes so it turns out I was indeed using a dpi that was too low, but also some other things...

My recommendations for future users of this version: (0.14.2)

  1. use high dpi images in the node.js version of the nft marker creator
    • you can set the dpi in an image editor
    • and you overwrite the default dpi values in the app.js script from "72" to "300".
  2. run the marker generator in the command line: node .\app.js -i .\<your_image.jpg> -dpi=f -max_dpi=300
  3. set replace the marker names "pinball" to ""
    • make sure you are loading these files whenever you deploy the website (rename them to reload them in the browser cache or do an API call to reload the files)
  4. use good camera (phone camera's are usually better)
    • you can use ngrok to forward your localhost to a website (fast) or use github pages (slow).
  5. to limit flickering: use a big image as the marker nft and small models, if possible.

At this point in time multiple NFT markers is still quite buggy, so I used a single one with multiple 3D models.

I could make a poster this way: https://user-images.githubusercontent.com/58084010/204385400-70717901-6359-4149-ae92-48d681a4e733.mp4

SwaggerNiels commented 1 year ago

afbeelding