-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaterialWaveDisplacementFeature.cs
47 lines (41 loc) · 2.07 KB
/
MaterialWaveDisplacementFeature.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Stride.Core;
using Stride.Rendering;
using Stride.Rendering.Materials;
using Stride.Shaders;
using StrideShaderDemo.Rendering.DisplacementParameters;
using System.Collections.Generic;
namespace StrideShaderDemo.Rendering;
[DataContract]
[Display("Wave Displacement")]
public class MaterialWaveDisplacementFeature : MaterialFeature, IMaterialDisplacementFeature
{
// Special material key, used by MaterialWaveDisplacementFeature to tell MaterialWaveDisplacementFeature it can read the output stream variable.
// There is a quirk where streams variables passing through different stages (vertex -> pixel) will crash if the variables in the prior
// stage is not set. ie. If the Displacement feature is not enabled (in vertex stage), the Surface Normal feature (in pixel stage)
// will crash. This key makes MaterialWaveSurfaceNormalFeature allow reading streams.WaveDisplacementPositionOffset in MaterialWaveSurfaceNormal shader
// only if it has been flagged by MaterialWaveDisplacementFeature.
public static readonly PermutationParameterKey<bool> IsFeatureEnabled = ParameterKeys.NewPermutation<bool>();
[DataMember(10)]
[Display("Waves")]
public List<WaveDisplacementBase> Waves { get; set; } = new();
public override void GenerateShader(MaterialGeneratorContext context)
{
if (Waves?.Count > 0)
{
context.Parameters.Set(IsFeatureEnabled, true);
var mixin = new ShaderMixinSource();
mixin.Mixins.Add(new ShaderClassSource("MaterialWaveDisplacement"));
const string ComposeShaderArrayPropertyName = "DisplacementFunctions"; // Name of property array in MaterialWaveDisplacement.sdsl
foreach (var wave in Waves)
{
if (wave is null)
{
continue;
}
var shaderSource = wave.GenerateShaderSource();
mixin.AddCompositionToArray(ComposeShaderArrayPropertyName, shaderSource);
}
context.AddShaderSource(MaterialShaderStage.Vertex, mixin);
}
}
}