Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Graphics shader cannot work with array in struct

Discussion in '5.4 Beta' started by buzzin, May 29, 2016.

  1. buzzin

    buzzin

    Joined:
    May 29, 2016
    Posts:
    3
    glad to know that new unity beta surpport for array use in shader.
    but my own custom shader just doesn't work at all

    so here's my code

    script
    Code (CSharp):
    1.     struct MeshData{
    2.  
    3.     void Start () {
    4.     Mdat = new MeshData[InstanceCount];
    5.     Mdat[0].GetMeshData(mesh);
    6.     var IndicData=mesh.GetIndices(0);
    7.     var IndicLenth = IndicData.Length;
    8.     IndicBuffer = new ComputeBuffer(IndicLenth,Marshal.SizeOf(typeof(int)));
    9.     GPUbuffer= new ComputeBuffer(Mdat.Length,Marshal.SizeOf(typeof(MeshData)));
    10.    
    11.     IndicBuffer.SetData(IndicData);
    12.     GPUbuffer.SetData(Mdat);
    13.    
    14.     Mat.SetBuffer("MData",GPUbuffer);
    15.     Mat.SetBuffer("IndicData",IndicBuffer);
    16.    
    17.     Mat.globalIlluminationFlags=GIFlags;
    18.    
    19.     CmdBuffer=new CommandBuffer();
    20.     CmdBuffer.name= name + "ProceduralGBuffer";
    21.     CmdBuffer.DrawProcedural (Matrix4x4.identity, Mat, 0, Topology,IndicLenth, InstanceCount);
    22.     Cam.AddCommandBuffer(CEvent,CmdBuffer);
    23.     }
    24.     void OnDestroy(){
    25.         GPUbuffer.Release();
    26.     }
    27.     void Update(){
    28.     }
    29.     struct MeshData{
    30.         public Vector3[] vertz;
    31.         public Vector3[] normal;
    32.         public Vector2[] uv;
    33.         public Vector4[] tangent;  
    34.        
    35.     public void GetMeshData(Mesh mesh){
    36.         vertz= mesh.vertices;
    37.         normal= mesh.normals;
    38.         uv =mesh.uv;
    39.         tangent = mesh.tangents;
    40.     }
    41.     public int VertCount(){
    42.         return vertz.Length;
    43.     }
    44.     public int NormalCount(){
    45.         return normal.Length;
    46.     }
    47.    
    48.     }
    49.    
    and here's my shader

    Code (CSharp):
    1. Shader "Unlit/defre"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.        
    7.     }
    8.     CGINCLUDE
    9.  
    10.     #include "UnityCG.cginc"
    11.     struct MeshData {
    12.         float3 vertz[24];
    13.         float3 normal[24];
    14.         float2 uv[24];
    15.         float4 tangent[24];
    16.     };
    17.                 struct v2f
    18.             {
    19.         float4 vertex : SV_POSITION;
    20.         float2 uv        : TEXCOORD0;
    21.         float3 normal    : TEXCOORD1;
    22.         float4 tangent    : TEXCOORD2;
    23.             };
    24.    
    25. StructuredBuffer<MeshData> MData;
    26. StructuredBuffer<uint> IndicData;
    27.     struct pOut {
    28.         half4 diffuse           : SV_Target0; // RT0: diffuse color (rgb), occlusion (a)
    29.         half4 spec_smoothness   : SV_Target1; // RT1: spec color (rgb), smoothness (a)
    30.         half4 normal            : SV_Target2; // RT2: normal (rgb), --unused, very low precision-- (a)
    31.         half4 emission          : SV_Target3; // RT3: emission (rgb), --unused-- (a)
    32.     };
    33.  
    34.             sampler2D _MainTex;
    35.             float4 _MainTex_ST;
    36.            
    37.            
    38.             v2f vert (uint vid : SV_VertexID, uint iid : SV_InstanceID)
    39.             {    uint idid= IndicData[vid];
    40.                 float4 pos = float4(MData[0].vertz[idid],1.0);
    41.                 float2 uv = MData[0].uv[idid];
    42.                 float3 normal = MData[0].normal[idid];
    43.                 float4 tangent = MData[0].tangent[idid];
    44.                 v2f o;
    45.                 o.vertex = mul(UNITY_MATRIX_MVP, pos);
    46.                 o.uv = uv;
    47.                 o.normal = normal;
    48.                 o.tangent = tangent;
    49.                 return o;
    50.             }
    51.            
    52.     pOut frag(v2f i)
    53.     {
    54.         pOut o;
    55.         o.diffuse = tex2D(_MainTex, i.uv);
    56.         o.spec_smoothness = 0.2;
    57.         o.normal = float4(i.normal*0.5 + 0.5, 0.0);
    58.         o.emission = 0.0;
    59.         return o;
    60.     }
    61.             ENDCG
    62.            
    63.            
    64.     SubShader
    65.     {
    66.         Tags { "RenderType"="Opaque" }
    67.         Cull Off
    68.         Pass
    69.         {
    70.         Name "DEFERRED"
    71.             Tags{"LightMode" = "Deferred"}
    72.             Stencil{
    73.                 Comp Always
    74.                 Pass Replace
    75.                 Ref 128
    76.             }
    77.             CGPROGRAM
    78.             #pragma vertex vert
    79.             #pragma fragment frag
    80.             // make fog work
    81.             //#pragma target 5.0
    82.             ENDCG
    83.         }
    84.     }
    85. }
    86.  
    it's just don't work out
    but if i change to another way it's working.

    Code (CSharp):
    1.     void Start () {
    2.     Mdat = new MeshData[mesh.vertexCount];
    3.     GetMeshData(mesh,Mdat);
    4.     var IndicData=mesh.GetIndices(0);
    5.     var IndicLenth = IndicData.Length;
    6.     IndicBuffer = new ComputeBuffer(IndicLenth,Marshal.SizeOf(typeof(int)));
    7.     GPUbuffer= new ComputeBuffer(Mdat.Length,Marshal.SizeOf(typeof(MeshData)));
    8.    
    9.     IndicBuffer.SetData(IndicData);
    10.     GPUbuffer.SetData(Mdat);
    11.    
    12.     Mat.SetBuffer("MData",GPUbuffer);
    13.     Mat.SetBuffer("IndicData",IndicBuffer);
    14.    
    15.     Mat.globalIlluminationFlags=GIFlags;
    16.    
    17.     CmdBuffer=new CommandBuffer();
    18.     CmdBuffer.name= name + "ProceduralGBuffer";
    19.     CmdBuffer.DrawProcedural (Matrix4x4.identity, Mat, 0, Topology,IndicLenth, InstanceCount);
    20.     Cam.AddCommandBuffer(CEvent,CmdBuffer);
    21.     }
    22.    
    23.     void OnDestroy(){
    24.         GPUbuffer.Release();
    25.     }
    26.     void GetMeshData(Mesh m , MeshData[] md){
    27.         for (int i = 0; i < m.vertexCount; i++)
    28.         {
    29.             //var vid =(int) Mathf.Repeat(i,(m.vertexCount-1));
    30.             //var iid = Mathf.FloorToInt(i/m.vertexCount);
    31.             md[i].vertz=m.vertices[i];
    32.             md[i].normal= m.normals[i];
    33.             md[i].uv = m.uv[i];
    34.             md[i].tangent=m.tangents[i];
    35.         }
    36.     }
    37.     struct MeshData{
    38.         public Vector3 vertz;
    39.         public Vector3 normal;
    40.         public Vector2 uv;
    41.         public Vector4 tangent;  
    42.    
    43.     }
    i dunno why, it's because my script not right???

    and by the way,how can i get shadow and light probe work if i am using commandbuffer.drawprocedural?