Similar to VR experience, you can add AR Button to enable AR experiences. Additionally, you can specify the required and optional AR features your experience will use.
Add a controller: Returns a Group representing the so called target ray space of the controller. Use this space for visualizing 3D objects that support the user in pointing tasks like UI interaction.
When we are hitting a surface, to indicate the surface, we will create a reticle to our scene.
Let's define the onSelect event that we attached to controller. When the select event happen, meaning user decides to place the object and we create it on the chosen location.
Finally in our render function, we will check in every XRFrame if we have an hit-test source to display the reticle on the surface.
To run the code on your device, you have to give access to your camera when prompted.
Create your first AR & VR applications on the Web
In this section, we will turn our 3D experience from the last section into an immersive experience. We will develop on our local device, laptop or desktop and run the code locally.
You can follow along the tutorial using an online editor. If you want to learn how to develop on your local environment, please follow the below checklist.
Only a few loaders (e.g. ObjectLoader) are included by default with three.js — others should be added to your app individually.
Once you've imported a loader, you're ready to add a model to your scene. Syntax varies among different loaders — when using another format, check the examples and documentation for that loader. For glTF, usage with global scripts would be:
Change the onSelect function to load and place the model, instead of the Sphere mesh we were placing previously.
We can add event callbacks for loading manager.
See GLTFLoader documentation for further details.
You've spent hours modeling an artisanal masterpiece, you load it into the webpage, and — oh no! 😭 It's distorted, miscolored, or missing entirely. Start with these troubleshooting steps:
Check the JavaScript console for errors, and make sure you've used an onError callback when calling .load() to log the result.
View the model in another application. For glTF, drag-and-drop viewers are available for three.js and babylon.js. If the model appears correctly in one or more applications, file a bug against three.js. If the model cannot be shown in any application, we strongly encourage filing a bug with the application used to create the model.
Try scaling the model up or down by a factor of 1000. Many models are scaled differently, and large models may not appear if the camera is inside the model.
Try to add and position a light source. The model may be hidden in the dark.
Look for failed texture requests in the network tab, like C:\\Path\To\Model\texture.jpg. Use paths relative to your model instead, such as images/texture.jpg — this may require editing the model file in a text editor.