Skip to content

Commit 882b887

Browse files
committed
Update custom model drawing tutorial to the latest API incl. animated model example
https://forum.flaxengine.com/t/how-to-render-outline-for-an-animated-model/2598
1 parent 36a0602 commit 882b887

1 file changed

Lines changed: 17 additions & 25 deletions

File tree

manual/graphics/tutorials/additional-render-pass.md

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ In this tutorial you will learn how to inject a custom render pass into the pipe
66

77
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**.
88

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:
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 Depth Write**. Then setup the graph as the image below:
1010

1111
![Toon outline](media/toon-outline-material.png)
1212

@@ -22,38 +22,26 @@ using FlaxEngine;
2222
namespace Game;
2323

2424
/// <summary>
25-
/// OutlineRenderer Script.
25+
/// Outline mesh rendering script.
2626
/// </summary>
2727
public class OutlineRenderer : PostProcessEffect
2828
{
2929
private Material _material;
30-
private Model _model;
3130

31+
/// <summary>
32+
/// ss
33+
/// </summary>
3234
public Material OutlineMaterial
3335
{
3436
get => _material;
35-
set
36-
{
37-
if (_material != value)
38-
{
39-
_material = value;
40-
}
41-
}
37+
set => _material = value;
4238
}
4339

4440
/// <inheritdoc />
4541
public override unsafe void OnEnable()
4642
{
4743
// This postfx overdraws the input buffer without using output
48-
UseSingleTarget = true;
49-
50-
// Custom draw location in a pipeline, before forward pass so that the draw call could be executed together with other forward draw calls
51-
Location = PostProcessEffectLocation.BeforeForwardPass;
52-
53-
// Get the actor as a `StaticModel` (or `AnimatedModel` instead)
54-
var modelInstance = Actor as StaticModel;
55-
// Get the actual `Model`
56-
_model = modelInstance.Model;
44+
UseSingleTarget = true;
5745

5846
// Register postFx to all game views (including editor)
5947
SceneRenderTask.AddGlobalCustomPostFx(this);
@@ -69,19 +57,23 @@ public class OutlineRenderer : PostProcessEffect
6957
/// <inheritdoc />
7058
public override bool CanRender()
7159
{
72-
return base.CanRender() && _material;
60+
return base.CanRender() && _material && Actor;
7361
}
7462

7563
/// <inheritdoc />
76-
public override unsafe void Render(GPUContext context, ref RenderContext renderContext, GPUTexture input, GPUTexture output)
64+
public override void CollectDrawCalls(ref RenderContextBatch renderContextBatch)
7765
{
78-
// Second pass: draw the model with the outline material
79-
8066
// Get the transform of the actor
8167
Actor.GetLocalToWorldMatrix(out var world);
82-
68+
69+
// Render to main view only, drawing into other contexts can be used to draw shadows of custom meshes
70+
ref RenderContext renderContext = ref renderContextBatch.MainContext;
71+
8372
// Submit the draw call to the render list, which will be handled later in the forward pass
84-
_model.Draw(ref renderContext, _material, ref world, StaticFlags.None, false);
73+
if (Actor is StaticModel staticModel)
74+
staticModel.Model.Draw(ref renderContext, _material, ref world, StaticFlags.None, false);
75+
else if (Actor is AnimatedModel animatedModel)
76+
animatedModel.SkinnedModel.Draw(ref renderContext, animatedModel.SkinnedMeshBones, _material, ref world, StaticFlags.None, false);
8577
}
8678
}
8779
```

0 commit comments

Comments
 (0)