Quantcast
Channel: Envato Tuts+ Game Development
Viewing all articles
Browse latest Browse all 728

Quick Tip: How to Render to a Texture in Three.js

$
0
0

By default, everything you render in Three.js gets sent to the screen. After all, what's the point of rendering something if you can't see it? It turns out there's a very important point: capturing the data before it gets sent to the screen (and thereby lost). 

This makes it much easier to apply post-processing effects, like color correction, color shifting, or blurring, and it's useful for shader effects, too.

This technique is known as rendering to a texture or rendering to a frame buffer; your final result is stored in a texture. which you can then render to the screen. In this Quick Tip, I'll show you how to do it, and then walk you through a practical example of rendering a moving cube onto the surfaces of another moving cube.

Note: This tutorial assumes you have some basic familiarity with Three.js. If not, check out How to Learn Three.js for Game Development.

Basic Implementation

There are a lot of examples out there on how to do this that tend to be embedded in more complicated effects. Here is the bare minimum you need to render something onto a texture in Three.js:

We first have the basic scene setup. Then, we create another scene, bufferScene; any object we add to this scene will be drawn to our off-screen target instead of to the screen.

We then create bufferTexture, which is a WebGLRenderTarget. This is what Three.js uses to let us render onto something other than the screen. 

Finally, we tell Three.js to render bufferScene:

This is just like rendering a normal scene, except we specify a third argument: the render target. 

So the steps are:

  1. Create a scene to hold your objects.
  2. Create a texture to store what you render
  3. Render your scene onto your texture

This is essentially what we need to do. It's not very exciting, though, since we can't see anything. Even if you did add things to the bufferScene, you still won't see anything; this is because you need to somehow render the texture you created onto your main scene. The following is an example of how to do that.

Example Usage

We're going to create a cube in a scene, draw it onto a texture, and then use that as a texture for a new cube!

1. Start With a Basic Scene

Here is our basic scene with a red rotating cube and a blue plane behind it. There's nothing special going on here, but you can check out the code by switching to the CSS or JS tabs in the demo.

You can fork and edit this on CodePen.

2. Render This Scene Onto a Texture

Now we're going to take that and render it onto a texture. All we need to do is create a bufferScene just like in the above basic implementation, and add our objects to it.

You can fork and edit this on CodePen.

If done right, we won't see anything, since now nothing is being rendered onto the screen. Instead, our scene is rendered and saved in bufferTexture.

3. Render a Textured Cube

bufferTexture is no different from any other texture. We can simply create a new object and use it as our texture:

You can fork and edit this on CodePen.

You could potentially draw anything in the first texture, and then render it on whatever you like! 

Potential Uses

The most straightforward use is any sort of post-processing effect. If you wanted to apply some sort of color correction or shifting to your scene, instead of applying to every single object, you could just render your entire scene onto one texture, and then apply whatever effect you want to that final texture before rendering it to the screen. 

Any sort of shader that requires multiple passes (such as blur) will make use of this technique. I explain how to use frame buffers to create a smoke effect in this tutorial.

Hopefully you've found this little tip useful! If you spot any errors or have any questions, please let me know in the comments! 


Viewing all articles
Browse latest Browse all 728

Trending Articles