Three.js Portal Effect (Render-to-Texture Window)

A portal into another world in Three.js, where a second scene is rendered from the same camera into a render target and shown through a swirling, glowing disc with correct parallax and spiralling particles.

Key topics: three.js portal, render target, render to texture, screen space uv, swirl distortion, effects

What this code does

- Second Scene: an alien landscape with its own fog, lights and floating rocks renders into a `WebGLRenderTarget` every frame.
- Screen-Space Lookup: the portal samples that texture at gl_FragCoord, so the other world lines up with the camera and shows parallax.
- Swirl: lookups are rotated by an angle that grows toward the rim, with a slight radial ripple.
- Energy Rim: angular streaks and a bright edge are mixed in near the border, with a pulsing point light nearby.
- Particles: points spiral inward at speeds that rise toward the centre.
- Controls: aperture, swirl strength, energy color and particles.

Continue Learning

JavaScript Implementation

ES6+
// Render another scene into a target, then show it through a disc using screen-space UVs
const target = new THREE.WebGLRenderTarget(width, height)
const portal = new THREE.Mesh(new THREE.CircleGeometry(1.75, 96), new THREE.ShaderMaterial({
  uniforms: { uWorld: { value: target.texture }, uResolution: { value: new THREE.Vector2(width, height) } },
  vertexShader: 'varying vec2 vUv; void main () { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }',
  fragmentShader: `
    uniform sampler2D uWorld; uniform vec2 uResolution; varying vec2 vUv;
    void main () {
      float r = length(vUv - 0.5) * 2.0;
      vec2 off = gl_FragCoord.xy / uResolution - 0.5;
      float a = r * r * 2.5;                                  // swirl stronger near the rim
      off = mat2(cos(a), sin(a), -sin(a), cos(a)) * off;
      gl_FragColor = vec4(texture2D(uWorld, off + 0.5).rgb + vec3(0.66, 0.33, 0.97) * pow(r, 6.0), 1.0);
    }`
}))
scene.add(portal)

function animate () {
  requestAnimationFrame(animate)
  renderer.setRenderTarget(target); renderer.render(otherScene, camera)
  renderer.setRenderTarget(null); renderer.render(scene, camera)
}
animate()