three.js Login & Auth minimal example

Learn how to clone a three.js example project then import the VIVERSE SDK to add login and authentication features

This tutorial demonstrates how to extend a basic three.js example project with VIVERSE SDK functionality, creating an engaging collectible driving game with Rapier physics.

In this initial tutorial, we'll add the Authentication SDK, as well as several gameplay features that we'll continue to build on in future steps with the Storage and Leaderboard SDK examples.

Pre-requisite #1: Create a World and App ID in VIVERSE Studio

All SDK usage requires an App ID tied to a specific VIVERSE World, which can be created via VIVERSE Studio. This process is described in detail in our documentation on VIVERSE Studio — but simply create a new app and copy its App ID to get started.

NOTE: VIVERSE SDKs cannot be used with projects published via the PlayCanvas Create SDK extension, which do not have App IDs.

Pre-requisite #2: Clone the three.js Vehicle Controller Example

Start by copying the official three.js Rapier vehicle controller sample as your foundation. This example provides a complete three.js scene setup with Rapier physics tied to a functional vehicle controller with WASD controls — drive too fast before turning and you can fully flip over, providing a fun challenge driven by Rapier's realistic physics simulation.

Step 1: Add VIVERSE SDK Integration

Building on the base three.js vehicle controller example, we'll now add VIVERSE authentication and gameplay enhancements. Simply copy the CDN URL into the <head> of your HTML document.

Step 2: Implement VIVERSE Authentication

Client Initialization

This process is described generically in our documentation, Login & Authentication for the SDK, but here is how it would apply to a modular three.js script:

Authentication Check

Once the viverseClient is instantiated, use its checkAuth() function to determine whether the user is logged in. It will return undefined if they are not logged-in, or credentials if they are.

Login Flow

Now that we're checking auth status, let's add some UI to our demo scene to prompt the login process if they are logged out.

UI State Management

Once uploaded and running on VIVERSE, all this authentication functionality should now be working! But what can we do with a logged in VIVERSE user? To find out, we have to add some gameplay features and a scoring system.

Step 3: Enhance the OrbitControls Camera System

The base vehicle controller example uses a simple static camera setup which points at the car but does not follow it closely. We'll upgrade this with smart car following behavior for a more engaging experience that will still allow the user to view the vehicle and scene from a variety of angles.

Default Settings + Events for Camera Following

Camera Updates

Step 5: Add Game Mechanics - Collectibles System

Now we'll add a scoring system with collectible items to transform the basic driving demo into an engaging game. This will also give us a score to track with the Leaderboard and Storage SDKs in future tutorials.

Collectible Creation

Collectible Spawning System

Collision Detection and Scoring

For this demo, a simple distance-based collision check should suffice.

Step 6: Integration and Initialization

Building on the existing three.js vehicle controller, add these integrations:

Step 6: Build and Publish to VIVERSE

That's it! That should fulfil our minimal requirements create a small mini game we can publish to VIVERSE to then run login and authentication check features. Now we just need to build and publish.

Whatever your build process, simply zip the final build folder to prepare to upload. In the demo project we're using Vite.js, so just run npm run build to engage that. This will create all necessary static build files in the /dist folder of your project.

Once the build .zip is ready, navigate to VIVERSE Studio's Upload section, click "Manage Content" next to the app you created earlier, and use the "Upload Content" section to select the .zip.

From there, you can preview your build, or submit it for content review and approval. This process is explored in great detail in the VIVERSE Studio section of our docs if you have further questions.

Final Build

That's it! The build is ready. Here are the relevant links:

three.js example project source code:

38KB
Open

Demo scene live on VIVERSE: https://worlds.viverse.com/DQC9Lpd

Last updated

Was this helpful?