unity-game-engineshadercgshaderlab

How can i convert uv coordinates to world space?


I am trying to implement a shader. It will use with Unity LineRenderer. Shader will have noise that scrolling overtime raltive to texture coordinates. For example in parallel to x axis of uv space of texture. I have an implementation, but i dont'know how to get direction relative to texture uv (consider the texture rotation) in a vert function. I am only have a world space-relativew scrolling.

Main problem - how to convert uv coordinates (for example (0, 0) or (1, 0)) to world space?

Here is a my shader:

Shader "LineRendering/Test"
{
    Properties
    {
        [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}

        _Freq("Frequency", Float) = 1
        _Speed("Speed", Float) = 1
    }

    SubShader
    {
        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
            "PreviewType" = "Plane"
            "CanUseSpriteAtlas" = "True"
        }

        LOD 200

        Cull Off
        Lighting Off
        ZWrite Off
        Fog { Mode Off }
        Blend One OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM

            #pragma target 3.0          
            #pragma vertex vert
            #pragma fragment frag
            #pragma enable_d3d11_debug_symbols

            #include "noiseSimplex.cginc"

            struct appdata_t
            {
                fixed4 vertex : POSITION;
                fixed2 uv : TEXCOORD0;
            };

            struct v2f
            {
                fixed4 vertex : SV_POSITION;
                fixed2 texcoord : TEXCOORD0;
                fixed2 srcPos : TEXCOORD1;

            };

            uniform fixed _Freq;
            uniform fixed _Speed;


            v2f vert(appdata_t IN)
            {
                v2f OUT;

                OUT.vertex = UnityObjectToClipPos(IN.vertex);

                OUT.texcoord = IN.uv;
                OUT.srcPos = mul(unity_ObjectToWorld, IN.vertex).xy;
                OUT.srcPos *= _Freq;

                //This is my trying to convert uv coordinates to world coodinates, but it is still unsuccessfully.

                //fixed2 v0Pos = mul(unity_WorldToObject, fixed3(0, 0, 0)).xy;
                //fixed2 v1Pos = mul(unity_WorldToObject, fixed3(1, 0, 0)).xy;
                //fixed2 scrollOffset = v1Pos - v0Pos;

                fixed2 scrollOffset = fixed2(1, 0);
                OUT.srcPos.xy -= fixed2(scrollOffset.x, scrollOffset.y) * _Time.y * _Speed;

                return OUT;
            }

            fixed4 frag(v2f IN) : COLOR
            {
                fixed4 output;
                float ns = snoise(IN.srcPos) / 2 + 0.5f;

                output.rgb = fixed3(ns, ns, ns);
                output.a = ns;

                output.rgb *= output.a;
                return output;
            }

            ENDCG
        }
    }
}

Noise library getted form here: https://forum.unity.com/threads/2d-3d-4d-optimised-perlin-noise-cg-hlsl-library-cginc.218372/#post-2445598. Please help me.


Solution

  • Texture coordinates are already in texture space. If I understand correctly, you should be able to just do this:

        v2f vert(appdata_t IN)
        {
            v2f OUT;
    
            OUT.vertex = UnityObjectToClipPos(IN.vertex);
    
            OUT.texcoord = IN.uv;
            OUT.srcPos = IN.uv;
            OUT.srcPos *= _Freq;
    
            fixed2 scrollOffset = fixed2(1, 0);
            OUT.srcPos.xy -= fixed2(scrollOffset.x, scrollOffset.y) * _Time.y * _Speed;
    
            return OUT;
        }