Skip to content

Instantly share code, notes, and snippets.

@Eideren
Created October 18, 2020 12:17
Show Gist options
  • Save Eideren/cd79fdb66039e38b797a0627b04ad3f3 to your computer and use it in GitHub Desktop.
Save Eideren/cd79fdb66039e38b797a0627b04ad3f3 to your computer and use it in GitHub Desktop.
namespace Project.Effects
{
using Stride.Rendering;
using Stride.Rendering.Materials;
using Stride.Rendering.Materials.ComputeColors;
using System.ComponentModel;
using Stride.Core;
using Stride.Core.Annotations;
using Stride.Core.Mathematics;
using Stride.Graphics;
using Stride.Shaders;
/// <summary>
/// Base class for texture nodes
/// </summary>
[DataContract("StrokesFeature", Inherited = true)]
[Display("Strokes")]
public class StrokesFeature : ComputeKeyedBase, IComputeColor
{
public virtual bool HasChanged{ get; protected set; }
/// <summary>
/// Gets or sets the default value used when no texture is set.
/// </summary>
/// <userdoc>The fallback value used when no texture is set.</userdoc>
[ NotNull ]
[ DataMember( 15 ) ]
public ComputeColor FallbackValue { get; set; }= new ComputeColor(Color4.White);
/// <summary>
/// Gets or sets the enable state of the texture.
/// </summary>
/// <userdoc>If unchecked, ignore the texture value and use the fallback value instead</userdoc>
[ DefaultValue( true ) ]
public bool Enabled{ get; set; } = true;
/// <summary>
/// The texture Reference.
/// </summary>
/// <userdoc>
/// The reference to the texture asset
/// </userdoc>
[ DataMember( 10 ) ]
[ DefaultValue( null ) ]
//[ InlineProperty ]
public Texture Ramp
{
get => _ramp;
set
{
if( _ramp == value )
return;
HasChanged = true;
_ramp = value;
}
}
public float Scale
{
get => _scale;
set
{
if( _scale == value )
return;
HasChanged = true;
_scale = value;
}
}
public float Width
{
get => _width;
set
{
if( _width == value )
return;
HasChanged = true;
_width = value;
}
}
public float Noise
{
get => _noise;
set
{
if( _noise == value )
return;
HasChanged = true;
_noise = value;
}
}
public float NoiseScale
{
get => _noiseScale;
set
{
if( _noiseScale == value )
return;
HasChanged = true;
_noiseScale = value;
}
}
Texture _ramp;
float _scale = 3f;
float _width = 3f;
float _noise = 0.3f;
float _noiseScale = 1.2f;
/// <inheritdoc/>
public override string ToString() => nameof( StrokesFeature );
ShaderSource GenerateShaderFromFallbackValue(ShaderGeneratorContext context, MaterialComputeColorKeys baseKeys)
{
return FallbackValue?.GenerateShaderSource(context, baseKeys);
}
ObjectParameterKey<Texture> GetTextureKey(ShaderGeneratorContext context, Texture texture, MaterialComputeColorKeys baseKeys)
{
var keyResolved = baseKeys.TextureBaseKey ?? MaterialKeys.GenericTexture;
return context.GetTextureKey(texture, keyResolved, baseKeys.DefaultTextureValue);
}
public override ShaderSource GenerateShaderSource(ShaderGeneratorContext context, MaterialComputeColorKeys baseKeys)
{
if (!Enabled || Ramp == null) // generate shader from default value when the texture is null or disabled
{
var fallbackValue = GenerateShaderFromFallbackValue(context, baseKeys);
if (fallbackValue != null)
return fallbackValue;
}
var textureKey = GetTextureKey(context, Ramp, baseKeys);
UsedKey = textureKey;
var otherKey = GetTextureKey( context, Ramp, baseKeys );
return new ShaderClassSource( nameof( StrokesShader ), otherKey, Scale, _width, _noise, _noiseScale );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment