Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do I simply rotate this shader streak effect?

Discussion in 'Shaders' started by shahan, Sep 26, 2020.

  1. shahan

    shahan

    Joined:
    Jan 27, 2014
    Posts:
    25
    I want to rotate Keijiro's streak shader effect: https://github.com/keijiro/KinoStreak
    so that it's slanted on an angle and also possibly double it to make an 'X' shape.. I have it set up on my end that I can make changes to the shader.. I think I need to apply a rotation in the 'Fragment Composition' function...

    any help is really appreciated thank you.
    or any resources on how to understand shader code like this.. is this complicated? or simple.. i'm having a hard time with it..

    Code (CSharp):
    1. Shader "Hidden/Kino/PostProcess/StreakX"
    2. {
    3.     HLSLINCLUDE
    4.  
    5.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    6.     #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
    7.     #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
    8.  
    9.     struct Attributes
    10.     {
    11.         uint vertexID : SV_VertexID;
    12.         UNITY_VERTEX_INPUT_INSTANCE_ID
    13.     };
    14.  
    15.     struct Varyings
    16.     {
    17.         float4 positionCS : SV_POSITION;
    18.         float2 texcoord   : TEXCOORD0;
    19.         UNITY_VERTEX_OUTPUT_STEREO
    20.     };
    21.  
    22.     Varyings Vertex(Attributes input)
    23.     {
    24.         Varyings output;
    25.         UNITY_SETUP_INSTANCE_ID(input);
    26.         UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    27.         output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID);
    28.         output.texcoord = GetFullScreenTriangleTexCoord(input.vertexID);
    29.         return output;
    30.     }
    31.  
    32.     TEXTURE2D_X(_SourceTexture);
    33.     TEXTURE2D(_InputTexture);
    34.     TEXTURE2D(_HighTexture);
    35.  
    36.     float4 _InputTexture_TexelSize;
    37.  
    38.     float _Threshold;
    39.     float _Stretch;
    40.     float _Intensity;
    41.     float3 _Color;
    42.  
    43.     // Prefilter: Shrink horizontally and apply threshold.
    44.     float4 FragmentPrefilter(Varyings input) : SV_Target
    45.     {
    46.         UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    47.  
    48.         uint2 ss = input.texcoord * _ScreenSize.xy - float2(0, 0.5);
    49.         float3 c0 = LOAD_TEXTURE2D_X(_SourceTexture, ss).rgb;
    50.         float3 c1 = LOAD_TEXTURE2D_X(_SourceTexture, ss + uint2(0, 1)).rgb;
    51.         float3 c = (c0 + c1) / 2;
    52.  
    53.         float br = max(c.r, max(c.g, c.b));
    54.         c *= max(0, br - _Threshold) / max(br, 1e-5);
    55.  
    56.         return float4(c, 1);
    57.     }
    58.  
    59.     // Downsampler
    60.     float4 FragmentDownsample(Varyings input) : SV_Target
    61.     {
    62.         UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    63.  
    64.         float2 uv = input.texcoord;
    65.         const float dx = _InputTexture_TexelSize.x;
    66.  
    67.         float u0 = uv.x - dx * 5;
    68.         float u1 = uv.x - dx * 3;
    69.         float u2 = uv.x - dx * 1;
    70.         float u3 = uv.x + dx * 1;
    71.         float u4 = uv.x + dx * 3;
    72.         float u5 = uv.x + dx * 5;
    73.  
    74.         half3 c0 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u0, uv.y)).rgb;
    75.         half3 c1 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u1, uv.y)).rgb;
    76.         half3 c2 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u2, uv.y)).rgb;
    77.         half3 c3 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u3, uv.y)).rgb;
    78.         half3 c4 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u4, uv.y)).rgb;
    79.         half3 c5 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u5, uv.y)).rgb;
    80.  
    81.         return half4((c0 + c1 * 2 + c2 * 3 + c3 * 3 + c4 * 2 + c5) / 12, 1);
    82.     }
    83.  
    84.     // Upsampler
    85.     float4 FragmentUpsample(Varyings input) : SV_Target
    86.     {
    87.         UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    88.  
    89.         float2 uv = input.texcoord;
    90.         const float dx = _InputTexture_TexelSize.x * 1.5;
    91.  
    92.         float u0 = uv.x - dx;
    93.         float u1 = uv.x;
    94.         float u2 = uv.x + dx;
    95.  
    96.         float3 c0 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u0, uv.y)).rgb;
    97.         float3 c1 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u1, uv.y)).rgb;
    98.         float3 c2 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u2, uv.y)).rgb;
    99.         float3 c3 = SAMPLE_TEXTURE2D(_HighTexture,  s_linear_clamp_sampler, uv).rgb;
    100.  
    101.         return float4(lerp(c3, c0 / 4 + c1 / 2 + c2 / 4, _Stretch), 1);
    102.     }
    103.  
    104.     // Final composition
    105.     float4 FragmentComposition(Varyings input) : SV_Target
    106.     {
    107.         UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    108.  
    109.         float2 uv = input.texcoord;
    110.         uint2 positionSS = uv * _ScreenSize.xy;
    111.         const float dx = _InputTexture_TexelSize.x * 1.5;
    112.  
    113.         float u0 = uv.x - dx;
    114.         float u1 = uv.x;
    115.         float u2 = uv.x + dx;
    116.  
    117.         float3 c0 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u0, uv.y)).rgb;
    118.         float3 c1 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u1, uv.y)).rgb;
    119.         float3 c2 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u2, uv.y)).rgb;
    120.         float3 c3 = LOAD_TEXTURE2D_X(_SourceTexture, positionSS).rgb;
    121.         float3 cf = (c0 / 4 + c1 / 2 + c2 / 4) * _Color * _Intensity * 5;
    122.  
    123.         return float4(cf + c3, 1);
    124.     }
    125.  
    126.     ENDHLSL
    127.  
    128.     SubShader
    129.     {
    130.         Cull Off ZWrite Off ZTest Always
    131.         Pass
    132.         {
    133.             HLSLPROGRAM
    134.             #pragma vertex Vertex
    135.             #pragma fragment FragmentPrefilter
    136.             ENDHLSL
    137.         }
    138.         Pass
    139.         {
    140.             HLSLPROGRAM
    141.             #pragma vertex Vertex
    142.             #pragma fragment FragmentDownsample
    143.             ENDHLSL
    144.         }
    145.         Pass
    146.         {
    147.             HLSLPROGRAM
    148.             #pragma vertex Vertex
    149.             #pragma fragment FragmentUpsample
    150.             ENDHLSL
    151.         }
    152.         Pass
    153.         {
    154.             HLSLPROGRAM
    155.             #pragma vertex Vertex
    156.             #pragma fragment FragmentComposition
    157.             ENDHLSL
    158.         }
    159.     }
    160.     Fallback Off
    161. }
    162.  
     
  2. shahan

    shahan

    Joined:
    Jan 27, 2014
    Posts:
    25
    i just got some advice from the creator to

    "Apply a rotation matrix to calculate displaced samples points in downsampler/upsampler in the shader."

    i've been asking around for this, i feel bad.. is this like a simple thing?
    i usually learn by reverse engineering and stuff... or examples but i can't find any. does anyone have some similar to this?
    would it be a 6x6 matrix..? or is that not how it works..

    Code (CSharp):
    1. // Downsampler
    2.     float4 FragmentDownsample(Varyings input) : SV_Target
    3.     {
    4.         UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    5.  
    6.         float2 uv = input.texcoord;
    7.         const float dx = _InputTexture_TexelSize.x;
    8.  
    9.         float u0 = uv.x - dx * 5;
    10.         float u1 = uv.x - dx * 3;
    11.         float u2 = uv.x - dx * 1;
    12.         float u3 = uv.x + dx * 1;
    13.         float u4 = uv.x + dx * 3;
    14.         float u5 = uv.x + dx * 5;
    15.  
    16.         half3 c0 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u0, uv.y)).rgb;
    17.         half3 c1 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u1, uv.y)).rgb;
    18.         half3 c2 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u2, uv.y)).rgb;
    19.         half3 c3 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u3, uv.y)).rgb;
    20.         half3 c4 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u4, uv.y)).rgb;
    21.         half3 c5 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u5, uv.y)).rgb;
    22.  
    23.         return half4((c0 + c1 * 2 + c2 * 3 + c3 * 3 + c4 * 2 + c5) / 12, 1);
    24.     }
    25.  
    26.     // Upsampler
    27.     float4 FragmentUpsample(Varyings input) : SV_Target
    28.     {
    29.         UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    30.  
    31.         float2 uv = input.texcoord;
    32.         const float dx = _InputTexture_TexelSize.x * 1.5;
    33.  
    34.         float u0 = uv.x - dx;
    35.         float u1 = uv.x;
    36.         float u2 = uv.x + dx;
    37.  
    38.         float3 c0 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u0, uv.y)).rgb;
    39.         float3 c1 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u1, uv.y)).rgb;
    40.         float3 c2 = SAMPLE_TEXTURE2D(_InputTexture, s_linear_clamp_sampler, float2(u2, uv.y)).rgb;
    41.         float3 c3 = SAMPLE_TEXTURE2D(_HighTexture,  s_linear_clamp_sampler, uv).rgb;
    42.  
    43.         return float4(lerp(c3, c0 / 4 + c1 / 2 + c2 / 4, _Stretch), 1);
    44.     }
     
  3. shahan

    shahan

    Joined:
    Jan 27, 2014
    Posts:
    25
    b
     
    Last edited: Sep 27, 2021