Finding Stars and Affirmations in the Sky with Three.js for Ayra Starr
Tags: three, vue, nuxt, music
Published: 2024-04-01 15:00:00

The Afrobeats revolution was well underway at [Mavin Records](https://mavinrecords.com/) long before Universal Music Group [purchased](https://www.forbes.com/sites/imeekpo/2024/02/27/umg-buys-major-stake-in-mavin-records-set-to-boost-afrobeats-scene/?sh=299ee14b2a65) a majority stake in the label, thanks to the meteoric rise of artists like [Rema](https://en.wikipedia.org/wiki/Rema_(musician)) and [Ayra Starr](https://en.wikipedia.org/wiki/Ayra_Starr).

Ayra describes herself as a “celestial being” and her music as “heavenly.” Fans describe her lyrics as a series of affirmations meant to uplift and provide resiliency in the face of life’s hardships. I had the opportunity to build upon these themes for Ayra, alongside her new single “Commas,” and the outcome is a unique affirmations app for her fans.

Fans are encouraged to visit [affirmations.ayrastarr.com](https://affirmations.ayrastarr.com) daily to receive words of encouragement. Leaning into the celestial aspects of Ayra’s being, these daily affirmations are revealed in the user’s sky, like a message from heaven. As the reveal occurs, it is also recorded as a video, and the user is then provided a unique shareable piece of content consisting of Ayra’s words and their beautiful sky.

Here's how we used [Three.js](https://threejs.org) to place and find affirmations in the sky.

## Development

{% embed https://vimeo.com/929072102?share=copy %}

I've developed a lot of web apps which use Three.js to create a light AR experience in the user's sky. From the Jack White [Twilight Receiver](https://blog.bitsrc.io/using-three-js-to-hear-the-dawn-with-jack-white-bde73334b95a) to the Pop Smoke [Tracklist Reveal](https://leemartin.dev/building-an-interactive-tracklist-reveal-for-pop-smoke-ba0d64e2b784), it's a simple but powerful mechanic. Each time I've developed one of these, I dreamed of a little wayfinder component (inspired by [Sky Guide](https://apps.apple.com/us/app/sky-guide/id576588894)) which helps the user find the things we've placed in the sky but never actually got around to developing it. Since this particular app for Ayra was straight-forward, I put in the extra work to unlock this new piece of UX for this and future projects. I'm just going to focus on this core UX but please check out previous [dev blogs](https://www.leemartin.com) on web AR projects for a more indepth look at how I build these apps.

### Placing Star in Sky

The star in our sky is simply a Three.js [Sprite](https://threejs.org/docs/index.html?q=sprite#api/en/objects/Sprite) with a star image texture. We place it in a random place in the sky using [setFromSphericalCoords](https://threejs.org/docs/index.html?q=vector#api/en/math/Vector3.setFromSphericalCoords) by choosing any random degree higher than the horizon and any degree around the user. I've adjusted the position a little so it isn't too close to the horizon.

```javascript
// Texture
const texture = new THREE.TextureLoader().load("/images/star.png")

// Color space
texture.colorSpace = THREE.SRGBColorSpace

// Material
const material = new THREE.SpriteMaterial({
  depthTest: false,
  map: texture,
  opacity: 0.5,
  transparent: true
})

// Star
star = new THREE.Sprite(material)

// Spherical position
let vector = new THREE.Vector3().setFromSphericalCoords(10, THREE.MathUtils.degToRad(Math.random() * 90), THREE.MathUtils.degToRad(Math.random()* 360))

// Copy position
star.position.copy(vector)

// Add to scene
scene.add(star)
```

### Finding Star in Sky

In order to allow users to use their device as a controller to adjust the position of the camera and *find* stars, I use the depreciated [DeviceOrientationControls](https://gist.github.com/leemartin/6e0a5ce4a5d239eb3c84a4efbee43a9a) by [patching](https://www.npmjs.com/package/patch-package) it back into Three. In order for DeviceOrientationControls to function, we need access the user to grant access to their device's orientation. I attempt to gain access to this, alongside their camera, during a previous step of the UX using a custom [composable](https://gist.github.com/leemartin/6f9bab074f36e74a1a6034f14697bb2c) I wrote for this purpose. You can see that permission step in the mockup video above. Once this permission is granted, we can initialize our DeviceOrienationControls with a single line.

```javascript
// Device Orientation Controls
const controls = new DeviceOrientationControls(camera)
```

Just make sure to update the controls in your render step using `controls.update()`. Now the user can point their device at the sky and find the star but how do we know they are pointing at it? That's where a Raycaster comes in.

### Targeting Star

On our app, we want to prevent users from revealing an affirmation unless they find and target the star in the sky. In order to determine if the user is currently pointing at the star, we can use a Raycaster. The raycaster, as it sounds, points a ray from the camera to a point on the screen. In our case, we're just interested in the center of the screen so a default (0, 0) point should work fine. Let's initialize both.

```javascript
// Raycaster
const raycaster = new THREE.Raycaster()

// Pointer
const pointer = new THREE.Vector3()
```

Now, back in our render step. We can update the raycaster with the latest camera position and use the [intersectObject]() method to determine if the raycaster intersects our star. Then you can do "something." In our case, we activate the ability to reveal the associated affirmation in the sky.

```javascript
// Update ray
raycaster.setFromCamera(pointer, camera)

// Check for intersections
const intersects = raycaster.intersectObject(star)

// If intersects
if (intersects.length) {
  // Targeting star

} else {
  // Not targeting star

}
```

Now, let's look at the new piece of UX: the wayfinder.

### Star Wayfinder

As I mentioned, I've always wanted to add a little 2D wayfinder to help direct users to the position of objects in the sky but I couldn't quite wrap my head around turning a 3D direction into a 2D element. After a lot of research and trial and error, I landed on a rather simple solution that works well. What we do is clone the star's position and then use the camera to convert it to [local space](https://medium.com/nerd-for-tech/local-space-vs-world-space-in-unity-6a9944470478). We can then calculate the directional angle in degrees using [atan2](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2) and adjust it by -90°. We then have a degree angle we can use with CSS to rotate the wayfinder element.

```javascript
// Clone star position
let local = star.position.clone()

// Convert from world space to camera's local space
camera.worldToLocal(local)

// Calculate angle in degrees
let angleDeg = Math.atan2(-local.y - 0, local.x - 0) * 180 / Math.PI + 180

// Adjust by 90 degrees
angleDeg -= 90

// Rotate compass
document.querySelector("#wayfinder").style.transform = `rotate(${angleDeg}deg)` 
```

Be on the lookout for an evolution of this component in future projects.