From 6ea15072a718b093e3688d22f7f6954e44e383b6 Mon Sep 17 00:00:00 2001 From: ImJezze Date: Sun, 5 Jun 2016 23:50:44 +0200 Subject: Procedural texture for vectors in HLSL * added simple procedural texture for vectors with rounded line ends and beam smoothness * added optional -vector_beam_smooth option * removed -antialias option, antialiasing is now always applied, except for plain D3D --- hlsl/distortion.fx | 21 ++++++++++----------- hlsl/vector.fx | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 17 deletions(-) (limited to 'hlsl') diff --git a/hlsl/distortion.fx b/hlsl/distortion.fx index bd78efde8c5..1ee1641e646 100644 --- a/hlsl/distortion.fx +++ b/hlsl/distortion.fx @@ -170,22 +170,18 @@ float GetSpotAddend(float2 coord, float amount) return saturate(SigmoidSpot); } -float GetRoundCornerFactor(float2 coord, float radiusAmount, float smoothAmount) +float GetRoundCornerFactor(float2 coord, float2 bounds, float radiusAmount, float smoothAmount) { // reduce smooth amount down to radius amount smoothAmount = min(smoothAmount, radiusAmount); - float2 quadDims = QuadDims; - quadDims = SwapXY - ? quadDims.yx - : quadDims.xy; - - float range = min(quadDims.x, quadDims.y) * 0.5; - float radius = range * max(radiusAmount, 0.0025f); - float smooth = 1.0 / (range * max(smoothAmount, 0.0025f)); + float range = min(bounds.x, bounds.y); + float amountMinimum = range > 0.0f ? 1.0f / range : 0.0f; + float radius = range * max(radiusAmount, amountMinimum); + float smooth = 1.0f / (range * max(smoothAmount, amountMinimum * 3.0f)); // compute box - float box = roundBox(quadDims * (coord * 2.0f), quadDims, radius); + float box = roundBox(bounds * (coord * 2.0f), bounds, radius); // apply smooth box *= smooth; @@ -279,8 +275,11 @@ float4 ps_main(PS_INPUT Input) : COLOR // Round Corners Simulation float2 RoundCornerCoord = CornerCoordCentered; + float2 RoundCornerBounds = SwapXY + ? QuadDims.yx + : QuadDims.xy; - float roundCornerFactor = GetRoundCornerFactor(RoundCornerCoord, RoundCornerAmount, SmoothBorderAmount); + float roundCornerFactor = GetRoundCornerFactor(RoundCornerCoord, RoundCornerBounds, RoundCornerAmount * 0.5f, SmoothBorderAmount * 0.5f); BaseColor.rgb *= roundCornerFactor; return BaseColor; diff --git a/hlsl/vector.fx b/hlsl/vector.fx index 95957d57c80..71d53d030bc 100644 --- a/hlsl/vector.fx +++ b/hlsl/vector.fx @@ -1,5 +1,5 @@ // license:BSD-3-Clause -// copyright-holders:Ryan Holtz +// copyright-holders:Ryan Holtz,ImJezze //----------------------------------------------------------------------------- // Vector Effect //----------------------------------------------------------------------------- @@ -13,7 +13,7 @@ struct VS_OUTPUT float4 Position : POSITION; float4 Color : COLOR0; float2 TexCoord : TEXCOORD0; - float2 LineInfo : TEXCOORD1; + float2 SizeInfo : TEXCOORD1; }; struct VS_INPUT @@ -21,16 +21,26 @@ struct VS_INPUT float3 Position : POSITION; float4 Color : COLOR0; float2 TexCoord : TEXCOORD0; - float2 LineInfo : TEXCOORD1; + float2 SizeInfo : TEXCOORD1; }; struct PS_INPUT { float4 Color : COLOR0; float2 TexCoord : TEXCOORD0; - float2 LineInfo : TEXCOORD1; // x is the line length, y is unused + float2 SizeInfo : TEXCOORD1; }; +//----------------------------------------------------------------------------- +// Functions +//----------------------------------------------------------------------------- + +// www.iquilezles.org/www/articles/distfunctions/distfunctions.htm +float roundBox(float2 p, float2 b, float r) +{ + return length(max(abs(p) - b + r, 0.0f)) - r; +} + //----------------------------------------------------------------------------- // Vector Vertex Shader //----------------------------------------------------------------------------- @@ -49,7 +59,7 @@ VS_OUTPUT vs_main(VS_INPUT Input) Output.Position.xy *= 2.0f; // zoom Output.TexCoord = Input.TexCoord; - Output.LineInfo = Input.LineInfo; + Output.SizeInfo = Input.SizeInfo; Output.Color = Input.Color; @@ -64,10 +74,35 @@ uniform float TimeRatio; // Frame time of the vector (not set) uniform float TimeScale; // How much frame time affects the vector's fade (not set) uniform float LengthRatio; // Size at which fade is maximum uniform float LengthScale; // How much length affects the vector's fade +uniform float BeamSmooth; + +float GetRoundCornerFactor(float2 coord, float2 bounds, float radiusAmount, float smoothAmount) +{ + // reduce smooth amount down to radius amount + smoothAmount = min(smoothAmount, radiusAmount); + + float range = min(bounds.x, bounds.y); + float amountMinimum = range > 0.0f ? 1.0f / range : 0.0f; + float radius = range * max(radiusAmount, amountMinimum); + float smooth = 1.0f / (range * max(smoothAmount, amountMinimum * 3.0f)); + + // compute box + float box = roundBox(bounds * (coord * 2.0f), bounds, radius); + + // apply smooth + box *= smooth; + box += 1.0f - pow(smooth * 0.5f, 0.5f); + + float border = smoothstep(1.0f, 0.0f, box); + + return saturate(border); +} float4 ps_main(PS_INPUT Input) : COLOR { - float lineLength = Input.LineInfo.x / max(QuadDims.x, QuadDims.y); // normalize + float2 lineSize = Input.SizeInfo / max(QuadDims.x, QuadDims.y); // normalize + + float lineLength = lineSize.x; float lineLengthRatio = LengthRatio; float lineLengthScale = LengthScale; @@ -78,6 +113,9 @@ float4 ps_main(PS_INPUT Input) : COLOR float4 outColor = float4(timeLengthModulate, timeLengthModulate, timeLengthModulate, 1.0f); outColor *= Input.Color; + float RoundCornerFactor = GetRoundCornerFactor(Input.TexCoord - 0.5f, Input.SizeInfo, 1.0f, BeamSmooth); + outColor.rgb *= RoundCornerFactor; + return outColor; } -- cgit v1.2.3-70-g09d2