Skip to content

Commit 8b66e44

Browse files
authored
Merge pull request #148 from HydrogenC/master
Add additional render pass tutorial (toon outline sample)
2 parents 33d0ec4 + d83d4c9 commit 8b66e44

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Additional render pass
2+
3+
In this tutorial you will learn how to inject a custom render pass into the pipeline. This technique is widely adopted in a lot of custom effects. This tutorial will implement a Toon-style outline by the method of back face fattening, which requires an additional outline pass after the opaque object is rendered.
4+
5+
## 1. Create the outline material
6+
7+
Firstly, create a new material as the toon outline material. You can do it manually or use Editor and *right-click* in Content window content folder **New -> Material -> Material**.
8+
9+
To implement back face fattening, You have to expand the model by a little through moving the vertex a bit in the direction of the normal. Only back faces of the enlarged model is rendered so that the original model won't be occluded. Open the material and set the Blend Mode to **Transparent** and set the Cull Mode to **Inverted**, turn off Z Test and Z Write. Then setup the graph as the image below:
10+
11+
![Toon outline](media/toon-outline-material.png)
12+
13+
## 2. Write rendering code
14+
15+
Next step is to inject a custom render pass by the `PostProcessEffect` class and render it.
16+
Create C# script and add it to the any actor on the scene. You can use [this tutorial](../../scripting/new-script.md) to learn how to do it. Then, write the following code:
17+
18+
```cs
19+
using System.Runtime.InteropServices;
20+
using FlaxEngine;
21+
22+
namespace Game;
23+
24+
/// <summary>
25+
/// OutlineRenderer Script.
26+
/// </summary>
27+
public class OutlineRenderer : PostProcessEffect
28+
{
29+
30+
private Material _material;
31+
private Model _model;
32+
33+
public Material OutlineMaterial
34+
{
35+
get => _material;
36+
set
37+
{
38+
if (_material != value)
39+
{
40+
_material = value;
41+
}
42+
}
43+
}
44+
45+
public override unsafe void OnEnable()
46+
{
47+
// This postfx overdraws the input buffer without using output
48+
UseSingleTarget = true;
49+
// Custom draw location in a pipeline, before forward pass so that the draw call could be executed together with other forward draw calls
50+
Location = PostProcessEffectLocation.BeforeForwardPass;
51+
52+
// Get the actor as a `StaticModel` (or `AnimatedModel` instead)
53+
var modelInstance = Actor as StaticModel;
54+
// Get the actual `Model`
55+
_model = modelInstance.Model;
56+
57+
// Register postFx to all game views (including editor)
58+
SceneRenderTask.AddGlobalCustomPostFx(this);
59+
}
60+
61+
public override void OnDisable()
62+
{
63+
// Remember to unregister from events and release created resources (it's gamedev, not webdev)
64+
SceneRenderTask.RemoveGlobalCustomPostFx(this);
65+
}
66+
67+
public override bool CanRender()
68+
{
69+
return base.CanRender() && _material;
70+
}
71+
72+
public override unsafe void Render(GPUContext context, ref RenderContext renderContext, GPUTexture input, GPUTexture output)
73+
{
74+
// Second pass: draw the model with the outline material
75+
76+
// Get the transform of the actor
77+
Actor.GetLocalToWorldMatrix(out var world);
78+
79+
// Submit the draw call to the render list, which will be handled later in the forward pass
80+
_model.Draw(ref renderContext, _material, ref world, StaticFlags.None, false);
81+
}
82+
}
83+
```
84+
85+
It overrides **PostProcessEffect** class which is used to inject custom rendering code into the in-build graphics pipeline.
86+
As you can see, the script registers in `OnEnable` and disposes in `OnDisable`. The actual rendering is performed in `Render` method that submits the draw call.
87+
88+
Then **add script** to a model actor on scene and **assign the Outline Material** created in step 2 to **Outline Material** property.
89+
90+
## 4. See results
91+
92+
Once you setup material, script and add it to a scene model you should be able to see the outline pass working (under game mode). In case of problems see *Output Log* window in Editor as it may contain any compilation errors.
93+
94+
![Toon Outline Example](media/toon-outline-example.png)
Loading
Loading

0 commit comments

Comments
 (0)