How to make a wormhole like Stargate SG-1 in JavaScript?
Tags: javascript, webdev, showdev, frontend
Published: 2021-09-21 18:46:02

---
title: How to make a wormhole like Stargate SG-1 in JavaScript?
published: true
description: Today we're going to talk about the effect that impressed the most people in my latest project. The wormhole !
tags: #javascript #webdev #showdev #frontend
canonical_url: https://www.jesuisundev.com/en/wormhole-in-javascript/
cover_image: https://i.imgur.com/683OgKY.jpg
---

Today we’re going to talk about the effect that impressed the most people in my latest project. A central effect that moved the game’s plot forward. An effect that I forced in front of everyone’s eyes several times because I was so proud of it: the wormhole of [Across The Multiverse](https://across-multiverse.com/)!

# In the previous episode

This article is the direct continuation of last [Monday’s article](https://www.jesuisundev.com/en/i-built-the-entire-universe-in-javascript/) where I explained how I created the whole universe. Yes, yes. A free 3D game in JavaScript in your browser.

**[A game that just won a prestigious fucking internet award.](https://thefwa.com/cases/across-the-multiverse)**

![https://across-multiverse.com/](https://across-multiverse.com/preview.jpg)

Ha I’m proud of my baby.
You don’t know my game yet ?
Here's a 3 minutes trailer to get started.

{% youtube 1ogk43jp8Qg %}

Anyway, where were we? Oh yes!

# How to build a wormhole?

We are at the point in the project where I generate infinite star fields with excellent performance. At this moment, the beautiful game you see in the YouTube video, it looks like this.

<video controls="" style="width:107%;overflow: auto;margin-left: -30px !important;margin-right: -30px !important;margin-top: -20px;max-width: 120vw;"><source src="https://i.imgur.com/6gQstLs.mp4" type="video/mp4"></video>

Clearly at that point, the possibilities are endless. I have 10,000 ideas per second. So obviously I’m thinking of adding nebulae, galaxies, red giants and everything else that makes up the universe.

**And we’ll get to that in the next article.**

But I have one particular idea that comes to mind. If I can generate one universe, it means that I can generate several! There is a lot of theory around the multiverse.

**Some theories claim that a black hole is in fact a wormhole that leads into another universe.**

![](https://www.gateworld.net/wp-content/uploads/2019/01/stargate_sgc_unending.jpg)

I am obsessed with this idea!

I have absolutely no idea how to do that. But I know it’s possible to create a wormhole in JavaScript. So I’m going to get started on that right away.

**Step 1: I need a visual reference.**

As I said in the previous article, I always used a reference image or video. A real representation of what I wanted to recreate. Something to look at to get as close as possible to a “realistic” rendering.

Very quickly, I decide to do the tunnel effect of the stargate SG-1 series again!

{% youtube oj4jYFyt8YY %}

It looks pretty complicated, though! Almost too complicated for the knowledge I have at this time. And what do you do when the problem is too complicated?

# Reducing complexity

**Building a wormhole?** Too complicated. I don’t even know where to start. OK, so let’s narrow it down.

**Moving the camera in a tube?** That means almost nothing to me at this point. You have to build a tube first!

**Building an infinite curved tube?** Still too complicated, let’s reduce it.

**Build a simple tube?**

Ha !

That sounds doable to me!

**It’s time to get started.**

# How to build a simple tube?

I immediately think that making a simple tube, in a big library like Three.js., it already exists.

[And I am right.](https://threejs.org/docs/#api/en/geometries/TubeGeometry)

A ready-made geometry object exists and is ready to use. Even better, the code already exists. This means that the first step is what?

A big copy and paste of the code from the doc as we like.


```javascript
class CustomSinCurve extends THREE.Curve {
    constructor( scale = 1 ) {
        super()
        this.scale = scale
    }

    getPoint( t, optionalTarget = new THREE.Vector3() ) {
        const tx = t * 3 - 1.5
        const ty = Math.sin( 2 * Math.PI * t )
        const tz = 0
        return optionalTarget.set( tx, ty, tz ).multiplyScalar( this.scale )
    }
}

const path = new CustomSinCurve( 10 )
const geometry = new THREE.TubeGeometry( path, 20, 2, 8, false )
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } )
const mesh = new THREE.Mesh( geometry, material )
scene.add( mesh )
```

<video controls="" style="width:107%;overflow: auto;margin-left: -30px !important;margin-right: -30px !important;margin-top: -20px;max-width: 120vw;"><source src="https://i.imgur.com/XxR0nFs.mp4" type="video/mp4"></video>

Now, if we want to continue our wormhole, we have to understand what we just copied and pasted. First, we see a **CustomSinCurve** class that extends another **THREE.curve** class.

**I understand very quickly that this is the Three.js way to create 3D curves!**

It’s very good that I have that in mind at this point in the development of the game. I’m going to use curves a lot in the future. I’m happy and I don’t know yet how important it will be.

![](https://guimauve.linfotoutcourt.com/wp-content/uploads/2018/02/Stargate-SG1-Jack-Sam.jpg)

In this Custom class, there is a simple constructor and a getPoint function.

I will understand later in detail what this last function corresponds to. I deduce for the moment that it is linked to the curvature of the tube! Indeed, the complicated mathematical model on the three axes (x, y, z) is what allows this shape of the tube.

The rest is just basic Three.js.

**I still pay attention to these two lines.**

```javascript
const path = new CustomSinCurve( 10 )
const geometry = new THREE.TubeGeometry( path, 20, 2, 8, false )
```

Here, I can see that the tube is waiting for a curve to be displayed on the screen. So what? So, it means that we can give our tube the shape we want!

I have my next goal!

# How to build an infinite curved tube?

I then cast an eye back to my reference model, the wormhole in Stargate.

It’s a permanent near-curve that goes a bit in every direction. However, it doesn’t do any tight turn madness. Looks like several tortured ellipses.

I decide to keep looking at the doc in search of a geometry that fits what I want.

And bingo.

**The [TorusKnotGeometry](https://threejs.org/docs/?q=TorusKnot#api/en/geometries/TorusKnotGeometry) is a perfect candidate for my wormhole shape.**

All we have to do is instantiate a TorusKnot, take its shape and add it to our previously made tube!

Easy.

Let’s write this down.

```javascript
const wormhole = {}
wormhole.shape = new THREE.Curves.TorusKnot(500)
const wormholeMaterial = new THREE.MeshBasicMaterial({
      map: null,
      wireframe: true
})
const wormholeGeometry = new THREE.TubeGeometry(wormhole.shape, 800, 5, 12, true)
const wormholeTubeMesh = new THREE.Mesh( wormholeGeometry, wormholeMaterial )
scene.add(wormholeTubeMesh)
```
<video controls="" style="width:107%;overflow: auto;margin-left: -30px !important;margin-right: -30px !important;margin-top: -20px;max-width: 120vw;"><source src="https://i.imgur.com/VT4k91u.mp4" type="video/mp4"></video>

I am very happy with the curve of this tube. You will notice that I managed the infinite side of the tube in the simplest way in the world.

**By closing the tube!**

```javascript
const wormholeGeometry = new THREE.TubeGeometry(wormhole.shape, 800, 5, 12, true)
```

Indeed, the last parameter of TubeGeometry is set to true, which instructs the script behind to join the two ends of the tube.

That’s a quick win!

**As a developer, we’re not here to do complicated things, we’re here to respond to needs.**

You don’t need to write three kilometers of hieroglyphics to get what you want!

![](https://images.squarespace-cdn.com/content/v1/591cbd04be65940e1e18b676/1609678579822-HI6D7V6X46KP6MTM8960/il_1140xN.2618494945_ozkt.jpg?format=1500w)

It perfectly meets my needs without taking the headache.

Exactly what we want.

Now, how do we move in there?

# How to move the camera in a tube?

The idea is to move the camera forward, inside the tube, with each image. Really, put the user’s eyes inside the tube! Thinking about it, I say to myself that it is enough to update the position of the camera at each image.

Just do it according to the curves of the tube.

And then I remembered the getPoint() function which I didn’t really understand at first.

**This function returns the coordinates of the curve according to a given value.**

Wait… But isn’t that **EXACTLY** what we want?

It seems to me that it is.

This said, we will have the right position in time for the camera, but we will not have the right angle. So we will have to force the camera to look forward, permanently. The lookAt function of the camera object will do this for us.

OK, let’s write this down.

```javascript
let wormhole = {
    CameraPositionIndex: 0,
    speed: 1500
}

function updatePositionInWormhole () {
    wormhole.CameraPositionIndex++

    if (wormhole.CameraPositionIndex > wormhole.speed) {
        wormhole.CameraPositionIndex = 0
    }

    const wormholeCameraPosition = wormhole.shape.getPoint(wormhole.CameraPositionIndex / wormhole.speed)

    camera.position.x = wormholeCameraPosition.x
    camera.position.y = wormholeCameraPosition.y
    camera.position.z = wormholeCameraPosition.z

    camera.lookAt(wormhole.shape.getPoint((wormhole.CameraPositionIndex + 1) / wormhole.speed))

    renderer.render(scene, camera)
}

function animate() {
    updatePositionInWormhole()
    requestAnimationFrame(animate)
}
```
<video controls="" style="width:107%;overflow: auto;margin-left: -30px !important;margin-right: -30px !important;margin-top: -20px;max-width: 120vw;"><source src="https://i.imgur.com/qfYiSaG.mp4" type="video/mp4"></video>

And it works!

We have a beautiful trip in roller coaster mode in a tube.

The hardest part is behind us, now it’s just dressing up.

And the little trick to make something pretty is to put several materials with different textures. Then you play with GSAP for the opacity and the order of appearance of the materials. You can get sublime things by playing with all this.

```javascript
this.wormholeTubeMesh = SceneUtils.createMultiMaterialObject(this.wormholeGeometry, [
      this.wireframedStarsSpeederMaterial,
      this.auraSpeederMaterial,
      this.nebulaSpeederMaterial,
      this.starsSpeederMaterial,
      this.clusterSpeederMaterial
])

 async animate () {
    this.wormholeTimeline = gsap.timeline()

    // initial massive boost at wormhole enter
    this.wormholeTimeline
      .to(this.starsSpeederMaterial, { duration: 7, opacity: 1 }, 0)
      .to(this.wireframedStarsSpeederMaterial, { duration: 7, ease: 'expo.out', opacity: 1 }, 0)
      .to(this.auraSpeederMaterial, { duration: 7, ease: 'expo.out', opacity: 1 }, 0)
      .to(window.wormhole, { duration: 7, ease: 'expo.out', speed: 2500 }, 0)

    // adding speed and noises
    this.wormholeTimeline
      .to(this.clusterSpeederMaterial, { duration: 6, opacity: 1 }, 7)
      .to(this.auraSpeederMaterial, { duration: 2, opacity: 0 }, 7)
      .to(window.wormhole, { duration: 6, speed: 2000 }, 7)

    // adding speed and nebula distorded
    this.wormholeTimeline
      .to(this.nebulaSpeederMaterial, { duration: 6, opacity: 1 }, 13)
      .to(this.clusterSpeederMaterial, { duration: 6, opacity: 0 }, 13)
      .to(this.auraSpeederMaterial, { duration: 6, opacity: 0.7 }, 13)
      .to(window.wormhole, { duration: 6, speed: 1800 }, 13)

    if (!window.isMobileOrTabletFlag) {
      window.controls.velocity.x = 0
      window.controls.velocity.z = 0
    }

    return this.wormholeTimeline.then(() => true)
  }
```
{% codesandbox across-multiverse-wormhole-materials-kqnuo %}

As said in the previous article, for the sake of length and simplification, sometimes I don’t explain everything. And especially, I don’t show everything. This is the case for this part where it’s just a presentation and not pure logic.

**[You have the full source code if you want to see everything.](https://github.com/jesuisundev/acrossthemultiverse/)**

And more precisely the wormhole class is [here](https://github.com/jesuisundev/acrossthemultiverse/blob/main/src/sequencer/wormhole/Wormhole.js).

# Epilogue

And here we have a perfect wormhole. Very good performance and very pretty! We are ready to go from universe to universe with this. In the next article, we will see how to create nebulae and more particularly [supernova remnants](https://www.sciencesetavenir.fr/assets/img/2019/11/08/cover-r4x3w1000-5dc5889862502-g299-remnants-supernova-type1a-20150218.jpg)!