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.
  2. Dismiss Notice

Help with vertex shader, need to add texture and color

Discussion in 'Shaders' started by theDawckta, Feb 11, 2015.

  1. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    49
    Hello, I am trying to use ComputeBuffer to draw out a TON of little geometry that I am doing some procedural animation on. I am using the shader below to do the actual drawing. I have no real experience with shaders, I have been beating my head against this problem for about a day and a half. Seems like it should be easy. Instead of drawing the vertex lines of the mesh I would like to visualize the meshes by just adding a texture and a color, pretty much the standard diffuse. It would be nice to have shadows to, I am not sure if by using ComputeBuffer I lose them.

    Maybe I am going about drawing all this geometry incorrectly. CoputeBuffer just seems like a great way for the gpu to do a ton of drawing very quickly which is exactly what I need. I can provide the computeBuffer code as well if it is needed.

    Thanks for any help given.

    Code (CSharp):
    1. Shader "DX11/Instanced from compute buffer" {
    2.     SubShader {
    3.         Pass {
    4.        
    5.         CGPROGRAM
    6.         #pragma target 5.0
    7.        
    8.         #pragma vertex vert
    9.         #pragma fragment frag
    10.        
    11.         #include "UnityCG.cginc"
    12.        
    13.         StructuredBuffer<float3> buf_Points;
    14.         StructuredBuffer<float3> buf_Positions;
    15.        
    16.         struct ps_input {
    17.             float4 pos : SV_POSITION;
    18.         };
    19.        
    20.         ps_input vert (uint id : SV_VertexID, uint inst : SV_InstanceID)
    21.         {
    22.             ps_input o;
    23.             float3 worldPos = buf_Points[id] + buf_Positions[inst];
    24.             o.pos = mul (UNITY_MATRIX_VP, float4(worldPos,1.0f));
    25.             return o;
    26.         }
    27.        
    28.         float4 frag (ps_input i) : COLOR
    29.         {
    30.             return float4(1,0.5f,0.5f,1);
    31.         }
    32.        
    33.         ENDCG
    34.        
    35.         }
    36.     }
    37.    
    38.     Fallback Off
    39. }
    40.  
     
  2. TechnoCraft

    TechnoCraft

    Joined:
    Apr 6, 2012
    Posts:
    28
    You can solve this problem by using RenderTexture (Unity Pro) or ComputeBuffer (Unity Free). See example (CustomComputeShader.7z) here. Both use texture to transport data to and from shader.
     
  3. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    49
    I am using compute buffers in my project, it is how I am getting the buf_Points(vertex points of my mesh) and buf_Positions (where to draw each mesh) information to my shader. I just have no idea how to render that mesh with a texture and color to fill it in.

    Here is an example slice of my current project, you can see the meshes are just wireframes, I need to make them solid objects with shadows. Thanks for the help, I just have no idea how to accomplish this.
     

    Attached Files:

  4. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    49
    Bump.

    Can someone download my project above and take a look. I have no idea how to get the meshes to render as a solid color. Shaders are hard.
     
  5. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    49
    Well, I finally figured it out if anyone is interested. Silly me I was trying to figure out why the shader would only draw lines. It isn't the shader at all, you have to pass a different MeshTopology to DrawProcedural.

    So I tried passing MeshTopology.Triangles in my DrawProcedural call and it at least drew some triangles even though it looked nothing like the mesh I was using. So then I realized I need to pass the vertices for all the triangles in my mesh instead of just passing the vertices. I then grabbed the normals for those triangle vertices and passed those into the shader as well. Now it works and I can draw a TON of procedural geometry with very little perf hit. Of course I am dev'ing on a GTX 980 so that helps :)

    Worst part of using DrawProcedural is it doesn't look like you can use shadows at all which is a real bummer. Now I need help with that :p
     
  6. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    I've been reading your posts on this topic heavily interested as I'm working on procedural terrain right now... So, yeah, interested. I was trying to figure out the best way to put in procedural detail and you've shown me a good way - I think... I thought I had to use a geometry shader to expand the number of triangles from the base mesh.

    Although I guess what you're using is DX11 only which is awesome, but still pretty high level hardware.

    Anyway, I always feel weird answering my own posts because I feel like it might be worthless to others. Just so you know it's appreciated.