本文介绍了如何将uv坐标转换为世界空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现着色器.它将与Unity LineRenderer一起使用.着色器将产生噪音,随着时间的推移滚动到纹理坐标.例如,平行于纹理的uv空间的x轴.我有一个实现,但是我不知道如何在vert函数中获得相对于纹理uv的方向(考虑纹理旋转).我只有一个相对于世界滚动的世界.

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.

主要问题-如何将uv坐标(例如(0,0)或(1,0))转换为世界空间?

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

这是我的着色器:

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
        }
    }
}

噪声库在此处获取表格: https://forum.unity.com/threads/2d-3d-4d-optimised-perlin-noise-cg-hlsl-library-cginc.218372/#post-2445598 .请帮助我.

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.

推荐答案

纹理坐标已在纹理空间中.如果我理解正确,那么您应该能够做到这一点:

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;
    }

这篇关于如何将uv坐标转换为世界空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!