using System; using Microsoft.Xna.Framework; using Terraria.Graphics.Shaders; namespace CalamityMod.Graphics.Primitives { /// /// Controls how U texture coordinates are generated for primitive ribbons. /// public enum PrimitiveTextureMode { /// /// U coordinates run from 0 to 1 across the full trail length. This matches the legacy behavior. /// Normalized, /// /// U coordinates advance according to distance along the trail. One full texture cycle occurs every pixels. /// Distance } /// /// Controls how adjacent segments are joined together when expanding points into quads. /// public enum PrimitiveJoinStyle { /// /// Legacy behavior. Each segment is expanded using its forward direction only. /// Flat, /// /// Uses the averaged tangent of neighbouring segments to smooth corners. /// Smooth, /// /// Uses a mitered join to keep width continuous around sharp corners. /// Miter } /// /// Determines how intermediate sample points are generated for smoothing. /// public enum PrimitiveSmoothingType { /// /// Classic Catmull-Rom interpolation between points. /// CatmullRom, /// /// Cardinal spline interpolation with configurable tension. /// Cardinal, /// /// Straight-line interpolation between neighbouring points. /// Linear, /// /// Cubic Hermite interpolation using central difference tangents. /// Hermite, /// /// Cubic Bezier interpolation using Catmull-Rom handles. /// CubicBezier } /// /// Selects how orientation frames propagate along the trail. /// public enum PrimitiveFrameTransportMode { /// /// Derives the normal from the instantaneous tangent (legacy behaviour). /// Basic, /// /// Uses parallel transport to minimise twisting. /// ParallelTransport } /// /// Determines the topology used when submitting geometry to the GPU. /// public enum PrimitiveTopology { /// /// Expands geometry into independent triangles (legacy behaviour). /// TriangleList, /// /// Connects geometry using a continuous triangle strip. /// TriangleStrip } /// /// Controls how the start and end of the primitive are capped. /// public enum PrimitiveCapStyle { /// /// Leaves the ribbon open (legacy behaviour). /// None, /// /// Adds a flat triangular cap across the ribbon ends. /// Flat } /// /// Contains all the various options to use when creating a primitive trail. New members can be added freely without breaking existing implementations. /// public readonly struct PrimitiveSettings { #region Delegates /// /// A delegate to dynamically determine the width of the trail at each position. /// /// The current position along the trail as a 0-1 interlopant value. /// The width for the current point. public delegate float VertexWidthFunction(float trailLengthInterpolant, Vector2 vertexPosition); /// /// A delegate to dynamically determine the color of the trail at each position. /// /// The current position along the trail as a 0-1 interlopant value. /// The color for the current point. public delegate Color VertexColorFunction(float trailLengthInterpolant, Vector2 vertexPosition); /// /// A delegate to dynamically determine the offset of the trail at each position. /// /// The current position along the trail as a 0-1 interlopant value. /// The offset for the current point. public delegate Vector2 VertexOffsetFunction(float trailLengthInterpolant, Vector2 vertexPosition); #endregion #region Instance Fields /// /// Used to determine the width of each vertex. /// public readonly VertexWidthFunction WidthFunction; /// /// Used to determine the color of each vertex. /// public readonly VertexColorFunction ColorFunction; /// /// Used to offset each position when generating them. /// public readonly VertexOffsetFunction OffsetFunction; /// /// Whether to use smoothening when generating the vertex positions from the provided ones Recommended to be on by default. /// public readonly bool Smoothen; /// /// Whether to pixelate the primitives. Recommended to be on by default. /// public readonly bool Pixelate; /// /// Whether to use unscaled perspective matrices when rendering the primitives. Recommended only when the system is being used outside of its typical context /// i.e. rendering primitives in the background via a CustomSky. /// public readonly bool UseUnscaledMatrices; /// /// The shader to apply when rendering. /// public readonly MiscShaderData Shader; /// /// An optional override to force the trail to use the provided positions as the side positions of the initial vertex. They are the left and right positions respectively. /// public readonly (Vector2, Vector2)? InitialVertexPositionsOverride; /// /// Determines how U texture coordinates are generated. /// public readonly PrimitiveTextureMode TextureCoordinateMode; /// /// Controls the scale of generated U coordinates. In this is a multiplier, in this is the distance (in pixels) for one full texture cycle. /// public readonly float TextureCycleLength; /// /// Adds an additional offset to the generated U coordinate. In distance mode this value is interpreted in pixels. /// public readonly float TextureScrollOffset; /// /// Allows overriding the automatically generated U coordinate with a custom function. Receives the normalized completion ratio (0-1) and should return the desired U coordinate. /// public readonly Func TextureCoordinateFunction; /// /// Determines how consecutive segments are joined together when expanding into quads. /// public readonly PrimitiveJoinStyle JoinStyle; /// /// Maximum multiplier applied to miter joins to keep spikes under control. /// public readonly float JoinMiterLimit; /// /// If true, a debug wireframe will be rendered on top of the primitive. /// public readonly bool DebugWireframe; /// /// The color used for the optional wireframe overlay. /// public readonly Color WireframeColor; /// /// Determines the interpolation style when smoothing the provided point list. /// public readonly PrimitiveSmoothingType SmoothingType; /// /// Controls how many intermediate subdivisions are generated per control segment when smoothing. Set to 0 for automatic distribution. /// public readonly int SmoothingSegments; /// /// Additional tension used by smoothing functions that support it (e.g. Cardinal splines). /// public readonly float SmoothingTension; /// /// Determines how normals are transported along the curve. /// public readonly PrimitiveFrameTransportMode FrameTransportMode; /// /// Chooses the primitive topology when submitting geometry to the GPU. /// public readonly PrimitiveTopology Topology; /// /// Determines how the ribbon is capped at its extremities. /// public readonly PrimitiveCapStyle CapStyle; #endregion /// /// Contains all the various options to use when creating a primitive trail. /// /// Used to determine the width of each vertex. /// Used to determine the color of each vertex. /// Used to offset each position when generating them. /// Whether to use smoothening when generating the vertex positions from the provided ones. Recommended to be enabled. /// Whether to pixelate the primitives. Recommended to be enabled if the effect was designed with this in mind. /// The shader to apply when rendering the primitives. /// Whether to use unscaled perspective matrices when rendering primitives. Recommended when using this system outside of its typical context; i.e. rendering primitives in the background via a CustomSky. /// An optional override to force the trail to use the provided positions as the side positions of the initial vertex. They are the left and right positions respectively and should be in screen space. /// Determines how U texture coordinates are generated. /// Controls how fast U coordinates advance. Acts as a multiplier in normalized mode, or the distance per cycle in distance mode. /// Adds an offset to generated U coordinates. Interpreted in pixels when using distance mode. /// Optional override that receives the completion ratio and returns a custom U coordinate. /// Determines how neighbouring segments are joined when expanding into quads. /// Maximum multiplier applied to miter joins to prevent spikes. Only used when is . /// When true, draws a wireframe overlay useful for debugging primitive geometry. /// Optional override for the wireframe color. If null, a vivid green is used. /// Determines the interpolation style used when smoothing the control points. /// When greater than zero, specifies the number of subdivisions created per control edge. /// Optional tension parameter used by certain smoothing types such as cardinal splines. /// Controls how frame orientation is propagated along the curve. /// Specifies the primitive topology used when submitting geometry. /// Selects how the ribbon's start and end are capped. public PrimitiveSettings(VertexWidthFunction widthFunction, VertexColorFunction colorFunction, VertexOffsetFunction offsetFunction = null, bool smoothen = true, bool pixelate = false, MiscShaderData shader = null, bool useUnscaledMatrices = false, (Vector2, Vector2)? initialVertexPositionsOverride = null, PrimitiveTextureMode textureCoordinateMode = PrimitiveTextureMode.Normalized, float textureCycleLength = 1f, float textureScrollOffset = 0f, Func textureCoordinateFunction = null, PrimitiveJoinStyle joinStyle = PrimitiveJoinStyle.Smooth, float joinMiterLimit = 4f, bool debugWireframe = false, Color? wireframeColor = null, PrimitiveSmoothingType smoothingType = PrimitiveSmoothingType.CatmullRom, int smoothingSegments = 0, float smoothingTension = 0f, PrimitiveFrameTransportMode frameTransportMode = PrimitiveFrameTransportMode.ParallelTransport, PrimitiveTopology topology = PrimitiveTopology.TriangleStrip, PrimitiveCapStyle capStyle = PrimitiveCapStyle.None) { WidthFunction = widthFunction; ColorFunction = colorFunction; OffsetFunction = offsetFunction; Smoothen = smoothen; Pixelate = pixelate; Shader = shader; UseUnscaledMatrices = useUnscaledMatrices; InitialVertexPositionsOverride = initialVertexPositionsOverride; TextureCoordinateMode = textureCoordinateMode; TextureCycleLength = Math.Abs(textureCycleLength) <= PrimitiveRenderer.Epsilon ? 1f : textureCycleLength; TextureScrollOffset = textureScrollOffset; TextureCoordinateFunction = textureCoordinateFunction; JoinStyle = joinStyle; JoinMiterLimit = Math.Max(joinMiterLimit, 1f); DebugWireframe = debugWireframe; WireframeColor = wireframeColor ?? Color.LimeGreen; SmoothingType = smoothingType; SmoothingSegments = Math.Max(smoothingSegments, 0); SmoothingTension = smoothingTension; FrameTransportMode = frameTransportMode; Topology = topology; CapStyle = PrimitiveCapStyle.Flat; } } }