编写一个简单的光线投射着色器

时间:2013-10-18 04:52:32

标签: unity3d shader volume-rendering

我想做一个简单的单通道光线投射着色器。也就是说,我在顶点着色器中传递顶点的世界位置,并且在片段着色器中开始光线行进到Z轴(我使用正投影)来获取世界坐标中的样本位置,然后我需要计算世界坐标到模型的坐标以获得3d纹理中的样本(纹理)位置,我现在陷入困境。 我在使用opengl的cpp中做了同样的事情,我想在unity3d中尝试并使用gui的好处。 代码在这里,但它没有正常工作。感谢任何人的帮助,并为我可怜的英语而烦恼。

Shader "Customer/RayCast"
{
    Properties
    {
        _Volume ("Volume (Scalar)", 3D) = "white"{}
        //_VolumeGradient ("VolumeGradient (Scalar)", 3D) = "white"{}
        //_TransferMap_Red ("TransferMap_Red", 2D) = "white"{}
        //_TransferMap_Green ("TransferMap_Green", 2D) = "white"{}
        //_TransferMap_Blue ("TransferMap_Blue", 2D) = "white"{}
        //_TransferMap_Alpha ("TransferMap_Alpha", 2D) = "white"{}
        _Slider("slider", Float) = 0.2
    }
    SubShader 
    {
        Tags { "Queue" = "Transparent" } 
        Pass 
        {
        Cull Back
        Blend SrcAlpha OneMinusSrcAlpha
        CGPROGRAM
        // Upgrade NOTE: excluded shader from DX11 and Xbox360; has structs without          semantics (struct v2f members eye_pos)
        #pragma exclude_renderers d3d11 xbox360
        #pragma target 3.0
        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"

        struct v2f
        {
            float4 pos : POSITION;
            float3 world_pos;
        };

        v2f vert(appdata_base v)
        {
            v2f output;
            output.pos = mul (UNITY_MATRIX_MVP, v.vertex);
            output.world_pos = mul(_Object2World, v.vertex);
            return output;
        }

        sampler3D _Volume;
        float _Slider;
        float4 frag( v2f input ) : COLOR 
        {
            float3 start = input.world_pos;
            float4 dst = float4(0.0, 0.0, 0.0, 0.0);//final color.
            float3 ray_step = 0.01 * float3(0.0f, 0.0f, 1.0f);//camera look at positive z-            axis with ortho projection.
            float4 value;
            float3 sample_pos = start;
           float3 offset = float3(0.5f, 0.5f, 0.5f);//model vertex is -0.5 to 0.5, make it 0 to 1.0.
            for(int i = 0; i < 100; ++i)
            {
                sample_pos += (i * ray_step);
                value = tex3D(_Volume, mul(_World2Object, sample_pos).xyz + offset);
                value.a = 0.1f;
                value.xyz *= value.a;
                dst += (1.0 - dst.a) * value;
            }
           dst += float4(0.5f, 0.0f, 0.0f, 0.0f);
           return dst;
        }

        ENDCG
        }
     }
}
p.s.one更多的问题是#program target 3.0似乎不允许for循环有超过300次,而在opengl我使用500没有问题与相同的视频卡。并且不支持#program target 4.0,有没有办法在unity3d中改进它?

1 个答案:

答案 0 :(得分:2)

感谢Jerdak的解释。 我的视频卡支持SM4.0,但我不仅可以在着色器脚本中输入#pragma target 4.0。 来自http://docs.unity3d.com/Documentation/Components/SL-ShaderPrograms.html ,它说

pragma target 4.0 - 编译为DX10着色器模型4.0。目前,此目标仅受DirectX 11渲染器支持。

所以我需要在团结播放中将渲染器更改为dx11。 下面的代码是一个简单的单通道光线投射着色器,效率很低。

Shader "Customer/RayCast" 
    {
    Properties{
    _Volume            ("Volume (Scalar)", 3D) = "white"{}
    _TransferMap_Red   ("TransferMap_Red", 2D) = "white"{}
    _TransferMap_Green ("TransferMap_Green", 2D) = "white"{}
    _TransferMap_Blue  ("TransferMap_Blue", 2D) = "white"{}
    _TransferMap_Alpha ("TransferMap_Alpha", 2D) = "white"{}
    }
    SubShader {
    Tags { "Queue" = "Transparent" } 
        Pass {
            Cull Back
            Blend SrcAlpha OneMinusSrcAlpha
            CGPROGRAM 
            #pragma target 4.0
            #pragma vertex vert
            #pragma fragment frag 
            #include "UnityCG.cginc"

            struct v2f
            {
                float4 pos : POSITION;
                float3 world_pos : TEXCOORD;
            };

            v2f vert(appdata_base v)
            {
                v2f output;
                output.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                output.world_pos = mul(_Object2World, v.vertex).xyz;
                return output;
            }

            sampler3D _Volume;
            sampler2D _TransferMap_Red;
            sampler2D _TransferMap_Green;
            sampler2D _TransferMap_Blue;
            sampler2D _TransferMap_Alpha;
            float4 frag( v2f input ) : COLOR 
            {  
                float3 offset = float3(0.5f, 0.5f, 0.5f);
                float3 ray_step = 0.004f * float3(0.0f, 0.0f, 1.0f);//ortho camera look at positive z-axis.
                float3 start = input.world_pos;
                float4 dst = float4(0.0, 0.0, 0.0, 0.0);
                float value;
                float3 sample_pos = mul(_World2Object, float4(start.xyz, 1.0f)); 
                float4 sampled_color = float4(0.0f, 0.0f, 0.0f, 0.0f);
                for(int i = 0; i < 400; ++i)
                {
                    float3 world_pos = start + (i * ray_step);
                    sample_pos = mul(_World2Object, float4(world_pos.xyz, 1.0f)).xyz + offset;//cube from unity is -0.5 to 0.5.
                    value = tex3D(_Volume, sample_pos).a;
                    if(0.0 < sample_pos.x && 1.0 > sample_pos.x && 0.0 < sample_pos.y && 1.0 > sample_pos.y && 0.0 < sample_pos.z && 1.0 > sample_pos.z)
                    {
                        if(value > 0.02 && dst.a < 0.98)
                        {
                            sampled_color.r = tex2D(_TransferMap_Red,   float2(value, 0.5f)).a; 
                            sampled_color.g = tex2D(_TransferMap_Green, float2(value, 0.5f)).a;
                            sampled_color.b = tex2D(_TransferMap_Blue,  float2(value, 0.5f)).a;
                            sampled_color.a = tex2D(_TransferMap_Alpha, float2(value, 0.5f)).a;
                            dst = (sampled_color.a) * sampled_color + (1 - sampled_color.a) * dst;
                        }
                    }
                }
                if(dst.a < 0.05f)
                    dst.a = 0.0f;
                return dst;
            }

            ENDCG
        }
    }
}