我想将C#代码中的坐标值发送到Unity中的着色器代码

时间:2017-11-03 12:25:39

标签: c# unity3d shader vertex-shader

我想将 C#代码中的坐标值发送到Unity中的着色器代码。

C#代码:

public class SendWorldPositionToShaderProgram : MonoBehaviour {

    void Start () {
        Renderer renderer = GetComponent<Renderer>();
        renderer.material.SetVector("_WorldPosition", transform.position);
        }
    }
}

着色器代码:

Shader "Custom/MyRotateShader2" {

    Properties {
        _WorldPosition("WorldPosition", Vector) = (0, 0, 0, 0)
    }
    SubShader {
        Tags
        {
            "RenderType" = "Opaque"
        }

        CGPROGRAM

        #pragma surface surf Lambert vertex:vertex_program
        #include "UnityCG.cginc"

        float4 _WorldPosition;

        void vertex_program(inout appdata_full v) 
        {
            float4 wp = _WorldPosition;
            wp.x; // is not real coordinate!
            wp.y; // not too!
            wp.z; // not too!
        }

        void surf( Input IN, inout SurfaceOutput o) 
        {

            ...

        }

        ENDCG
    }
    FallBack "Diffuse"
}

0 个答案:

没有答案