DEV Community

Cover image for AR Unleashed: Image, Marker, and Location-Based Magic
De'Juan Ellsworth
De'Juan Ellsworth

Posted on

AR Unleashed: Image, Marker, and Location-Based Magic

Image Tracking in Augmented Reality

Image tracking in AR involves recognizing specific markers and overlaying digital content onto them in real-time. These markers serve as anchors, enabling digital objects to interact seamlessly with the physical world.

How to Implement Image Tracking:

Implementing image tracking with A-Frame and AR.js is straightforward. Define markers and associate them with digital content to create engaging AR experiences. A-Frame simplifies the process, allowing developers to focus on creativity while AR.js manages the complexities of image recognition.

 <!--Image Tracking Example -->
<!DOCTYPE html >
<html>
<head>
    <!--Framework/Library -->
    <script src="https://cdn.jsdelivr.net/gh/aframevr/aframe@1.4.2/dist/aframe-master.min.js"></script>
    <script src="https://raw.githack.com/AR-js-org/AR.js/master/aframe/build/aframe-ar-nft.js"></script>

</head>
<body style="margin: 0px; overflow: hidden;">
    <!-- Loading  -->
    <div class="arjs-loader">
        <div>Loading, please wait...</div>
    </div>
    <!-- AR scene  -->
    <a-scene vr-mode-ui="enabled: false;" embedded arjs="trackingMethod: best; sourceType: webcam; debugUIEnabled: false;">
        <!-- Image Tracking  -->
        <a-nft type="nft" url="http://localhost:8080/https://raw.githack.com/AR-js-org/AR.js/master/aframe/examples/image-tracking/nft/trex/trex-image/trex">
            <!-- 3D model -->
            <a-entity gltf-model="http://localhost:8080/https://raw.githack.com/AR-js-org/AR.js/master/aframe/examples/image-tracking/nft/trex/trex-image/trex.scene.gltf" scale="5 5 5" position="150 300 -100"></a-entity>
        </a-nft>
        <!-- Camera -->
        <a-entity camera></a-entity>
    </a-scene>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Marker-Based Augmented Reality

Marker-Based AR relies on specific visual triggers, like QR codes, to initiate augmented reality experiences. These markers act as triggers, prompting the display of digital content upon detection by the AR system.

Implementing Marker-Based AR:

Creating marker-based AR experiences involves defining custom markers and associating them with interactive digital elements. A-Frame and AR.js provide tools to seamlessly integrate custom markers into your projects.

<!--Marker-Based Example-->
<!DOCTYPE html>
    <html>

    <!--Framework/Library -->
    <script src="https://aframe.io/releases/1.4.2/aframe.min.js"></script>
    <script src="https://raw.githack.com/AR-js-org/AR.js/master/aframe/build/aframe-ar.js"></script>

    <body style="margin: 0px; overflow: hidden;">

        <!-- AR scene -->
        <a-scene embedded arjs>
            <a-marker preset="hiro">

                <!-- AR entity -->
                <a-entity
                    position="0 0 0"
                    scale="0.05 0.05 0.05"
                    gltf-model=localhost:8080/https://raw.githack.com/AR-js-org/AR.js/master/aframe/examples/image-tracking/nft/trex/scene.gltf"
                ></a-entity>
            </a-marker>

            <!-- Camera  -->
            <a-entity camera></a-entity>
        </a-scene>

    </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Location-Based Augmented Reality

Location-Based AR utilizes GPS and sensors to provide context-aware experiences, tailoring digital content based on the user's geographical location for a personalized and immersive AR experience.

Implementing Location-Based AR:

Implement Location-Based AR by creating a project with A-Frame, AR.js, and Three.js. Integrate test GPS data using latitude and longitude coordinates, anchoring your scene to a specific location. Customize virtual content based on this data, adjusting position and appearance. Enable real-time updates to dynamically modify virtual elements, ensuring a synchronized AR experience with the user's surroundings.

 <!--Location-Based Example-->
  <!DOCTYPE html>
  <html>
  <head>
      <meta charset="utf-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <title>AR.js</title>
    <!--Framework/Library -->
      <script src="https://aframe.io/releases/1.4.2/aframe.min.js"></script>
      <script src="https://unpkg.com/aframe-look-at-component@0.8.0/dist/aframe-look-at-component.min.js"></script>
      <script src="https://raw.githack.com/AR-js-org/AR.js/master/aframe/build/aframe-ar-nft.js"></script>
  </head>

  <body>
      <!-- AR scene  -->
      <a-scene
          vr-mode-ui="enabled: false"
          arjs="sourceType: webcam; videoTexture: true; debugUIEnabled: false;"
      >
          <!-- Text entity -->
          <a-text
              value="Following you like Mona Lisa."
              look-at="[gps-camera]"
              scale="120 120 120"
              gps-entity-place="latitude: 29.9574; longitude: 90.0629 "
          ></a-text>
          <!-- Camera entity with GPS functionality -->
          <a-camera gps-camera rotation-reader></a-camera>
      </a-scene>
  </body>
  </html>

Enter fullscreen mode Exit fullscreen mode

Conclusion:

In the ever-evolving landscape of augmented reality, technologies like Image Tracking, Marker-Based AR, and Location-Based AR are revolutionizing how we interact with the digital world. Image Tracking enables seamless integration of digital content with physical objects, enhancing user engagement. Marker-Based AR provides interactive experiences triggered by visual markers, adding layers of information to the physical environment. Location-Based AR, using GPS data, offers personalized and immersive experiences, blurring the lines between the real and virtual worlds.

As developers, understanding and harnessing these technologies can open a world of creative possibilities. By utilizing frameworks like A-Frame and libraries like AR.js, developers can craft captivating AR experiences that captivate users and elevate applications to new heights.

Resources:

A-Frame Documentation

AR.js Documentation

Top comments (0)