将片段位置保存到纹理中

时间:2016-12-06 12:28:19

标签: unity3d shader hlsl cg

我想将片段的世界坐标存储到3个纹理中,这些纹理分别代表坐标的x,y,z坐标。尽管纹理的格式为ARGB32,但我似乎无法写出大于1.0的值。这是我的着色器代码。这样做的正确方法是什么?

Shader "Custom/FirstPass"
{
    Properties { }

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

        Pass
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #pragma target 5.0

            #include "UnityCG.cginc"

            struct vertexData
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 color : COLOR;
                float4 texcoord0 : TEXCOORD0;

                //...
            };

            struct fragmentData
            {
                float4 positionC : SV_POSITION;
                float4 positionW : TEXCOORD1;


                //float4 color : COLOR;
                //...
            };
            struct fragmentOutput
            {
                float Gx : SV_Target0;
                float Gy : SV_Target1;
                float Gz : SV_Target2;
                float depth:SV_Target3;

            };

            fragmentData vert(vertexData v)
            {
                fragmentData o;
                o.positionW=mul(unity_ObjectToWorld,v.vertex);
                o.positionC=UnityObjectToClipPos(v.vertex);
                return o;
            }

            fragmentOutput frag(fragmentData fragment)
            {
                fragmentOutput output;
                output.Gx =fragment.positionW.x;
                output.Gy= fragment.positionW.y;
                output.Gz = fragment.positionW.z;
                output.depth=fragment.positionC.z;

                //... write to other renderbuffers

                return output;
            }

            ENDCG
        }
    }
}

0 个答案:

没有答案