3
$\begingroup$

I'm new to OSL and trying to see an example of how someone would convert this unity shader to OSL in object space? https://andreashackel.de/tech-art/stripes-shader-1/

Shader "Unlit/Stripes"
{
    Properties {
        _Color1 ("Color 1", Color) = (0,0,0,1)
        _Color2 ("Color 2", Color) = (1,1,1,1)
        _Tiling ("Tiling", Range(1, 500)) = 10
        _Direction ("Direction", Range(0, 1)) = 0
        _WarpScale ("Warp Scale", Range(0, 1)) = 0
        _WarpTiling ("Warp Tiling", Range(1, 10)) = 1
    }

    SubShader
    {

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            fixed4 _Color1;
            fixed4 _Color2;
            int _Tiling;
            float _Direction;
            float _WarpScale;
            float _WarpTiling;

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                const float PI = 3.14159;

                float2 pos;
                pos.x = lerp(i.uv.x, i.uv.y, _Direction);
                pos.y = lerp(i.uv.y, 1 - i.uv.x, _Direction);

                pos.x += sin(pos.y * _WarpTiling * PI * 2) * _WarpScale;
                pos.x *= _Tiling;

                fixed value = floor(frac(pos.x) + 0.5);
                return lerp(_Color1, _Color2, value);
            }
            ENDCG
        }
    }
}
$\endgroup$
3
$\begingroup$

Sticking to the original as closely as possible:

shader Stripes(
    point UV = 0,
    float Scale_X = 10,
    float Scale_Y = 10,
    float Direction = 0,
    float Amplitude = 1,
    float Frequency = 1,
    color Color1 = 0,
    color Color2 = 1,
    output color Color = 0
)
{
    //Scaling
    float x = UV[0]*Scale_X;
    float y = UV[1]*Scale_Y;

    //Smeared 'Rotation'
    x = mix(x,y,Direction);
    y = mix(y,1-x,Direction);

    //Wave
    x += Amplitude * sin (Frequency * M_2PI * y);

    Color = (x-floor(x) > 0.5 ? Color1 : Color2);
}

This script returns a color, rather than a shader (a closure).. which would be strictly equivalent.

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks so much, really interesting to see! I'm intrigued by your comment about crunching it a bit more by doing the math differently. Would you mind sharing an example of what your thinking? $\endgroup$ – simhod Dec 15 '20 at 16:43
  • $\begingroup$ @simhod see edit.. I just remembered.. OSL's mix() is a lerp(). $\endgroup$ – Robin Betts Dec 16 '20 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.