简体   繁体   中英

Unity relative directions in shader

I have a scene in Unity with:

  • An empty gameobject, let's call it " Center "
  • A mesh projected onto a spherical surface (with Center as center of the sphere) by a shader

It works fine. I tried to add 3dnoise based center -> vertex (let's call it " direction "). It works fine too. Now I'm trying to use the same 3dnoise based on direction , but relative to Center orientation. Unity has a feature called transform.WorldToLocalMatrix (working well in C#), but since it's not possible to pass matrices as properties in Unity shaders, I've passed it as four separated *Vector4*s:

void Update() {
    terrainMaterial.SetVector("_Center", transform.position);
    terrainMaterial.SetVector("_Matrix1", new Vector4(transform.worldToLocalMatrix.m00, transform.worldToLocalMatrix.m01, transform.worldToLocalMatrix.m02, 0.0f));
    terrainMaterial.SetVector("_Matrix2", new Vector4(transform.worldToLocalMatrix.m10, transform.worldToLocalMatrix.m11, transform.worldToLocalMatrix.m12, 0.0f));
    terrainMaterial.SetVector("_Matrix3", new Vector4(transform.worldToLocalMatrix.m20, transform.worldToLocalMatrix.m21, transform.worldToLocalMatrix.m22, 0.0f));
    terrainMaterial.SetVector("_Matrix4", new Vector4(0.0f, 0.0f, 0.0f, 0.0f));
}

Then, in shader:

Shader "Custom/testShader"

{ Properties { _Color("Color", Color) = (1,1,1,1) _MainTex("Albedo (RGB)", 2D) = "white" {} _Glossiness("Smoothness", Range(0,1)) = 0.5 _Metallic("Metallic", Range(0,1)) = 0.0

    _Center("Center of the Planet", Vector) = (0.0, 0.0, 0.0)
    _Matrix1("1", Vector) = (0.0, 0.0, 0.0, 0.0)
    _Matrix2("2", Vector) = (0.0, 0.0, 0.0, 0.0)
    _Matrix3("3", Vector) = (0.0, 0.0, 0.0, 0.0)
    _Matrix4("4", Vector) = (0.0, 0.0, 0.0, 0.0)

    _Scale("Scale of Noise", Range(0.001, 200.0)) = 1.0
    _Height("Height of Noise", Range(1.0, 500.0)) = 5.0
}

    SubShader
    {
        Tags{ "RenderType" = "Opaque" }

        CGPROGRAM

        #pragma target 3.0
        #pragma surface surf Standard vertex:vert

        #include "libs/noiseSimplex.cginc"

        sampler2D _MainTex;
        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        float3 _Center;
        float4 _Matrix1;
        float4 _Matrix2;
        float4 _Matrix3;
        float4 _Matrix4;

        float _Scale;
        float _Height;

        struct Input {
            float2 uv_MainTex;
        };

        void vert(inout appdata_full v)
        {
            float3 direction = mul(unity_ObjectToWorld, v.vertex) - _Center;
            float4x4 worldToLocal_matrix = (_Matrix1.x, _Matrix1.y, _Matrix1.z, _Matrix1.w,
                                            _Matrix2.x, _Matrix2.y, _Matrix2.z, _Matrix2.w,
                                            _Matrix3.x, _Matrix3.y, _Matrix3.z, _Matrix3.w,
                                            _Matrix4.x, _Matrix4.y, _Matrix4.z, _Matrix4.w);

            float3 local_direction = mul(worldToLocal_matrix, direction);

            direction = _Center + normalize(direction) * (_Radius + snoise(local_direction * _Scale) * _Height);
            v.vertex = mul(unity_WorldToObject, float4(direction, 1));
        }

        void surf(Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;

            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }

        ENDCG
}

Fallback "Diffuse"
}

The thing that I don't understand at all is... without changing anything else, this line works:

direction = _Center + normalize(direction) * (_Radius + snoise(direction * _Scale) * _Height);

This one doesn't:

direction = _Center + normalize(direction) * (_Radius + snoise(local_direction * _Scale) * _Height);

Any idea about what I'm doing wrong?

RESOLVED:

Passing WorldToLocalMatrix through SetMatrix() is fine, but splitting it and reconstructing in the shader doesn't work. I'm confused

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM