Search Unity

CommandBuffer.DrawProcedural examples

Discussion in 'General Graphics' started by Deleted User, Jul 23, 2016.

  1. Deleted User

    Deleted User

    Guest

    I am looking for simple examples of usage CommandBuffer.DrawProcedural in C# scripts. Official documentation lacks of "getting started" scripts:

    http://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.DrawProcedural.html

    I found only one example:
    https://github.com/i-saint/Unity5Ef...st/BezierPatch/Scripts/BezierPatchRenderer.cs


    I use Graphics.DrawProcedural with DX11 structured buffers and custom shaders. Simple diffuse textured and specular shaders work great but Unity3D PBR standard shader doesn't work properly (geometry is OK, but object surface is black). Also I read topics where people had problems with casting and receiving shadows with Graphics.DrawProcedural. I wonder whether it has been fixed with new CommandBuffer.DrawProcedural function.
     
  2. Deleted User

    Deleted User

    Guest

    My C# script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Runtime.InteropServices;
    3.  
    4. public struct Point {
    5.     public Vector3 vertex;
    6.     public Vector3 normal;
    7.     public Vector4 tangent;
    8.     public Vector2 uv;
    9. }
    10. public class ProceduralGeometry : MonoBehaviour {  
    11.      public Material material ;
    12.     public GameObject gameobject;
    13.     private ComputeBuffer computebuffer;
    14.     private int n = 0;
    15.  
    16.     void Start () {
    17.         Mesh mesh = gameobject.GetComponent<MeshFilter>().sharedMesh;
    18.         n = mesh.triangles.Length;
    19.         Point[] points = new Point[n];
    20.         for (int i = 0; i < n; ++i)
    21.         {
    22.             points[i].vertex = mesh.vertices[mesh.triangles[i]];
    23.             points[i].normal = mesh.normals[mesh.triangles[i]];
    24.             points[i].tangent = mesh.tangents[mesh.triangles[i]];
    25.             points[i].uv = mesh.uv [mesh.triangles [i]];
    26.         }
    27.         computebuffer= new ComputeBuffer (n, Marshal.SizeOf(typeof(Point)), ComputeBufferType.Default);
    28.         computebuffer.SetData (points);
    29.         material.SetBuffer ("points", computebuffer);      
    30.      }
    31.  
    32.      void OnRenderObject() {
    33.         material.SetPass(0);
    34.         Graphics.DrawProcedural(MeshTopology.Triangles, n, 1);
    35.      }
    36.    
    37.      void OnDestroy() {
    38.         computebuffer.Release ();
    39.      }
    40.    
    41. }
    My shader (simple per vertex diffuse lighting with vertex animation):
    Code (CSharp):
    1. Shader "DirectX 11 Diffuse Mapping" {
    2.    Properties
    3.     {
    4.         _MainTex ("Texture", 2D) = "white" {}
    5.     }
    6.      SubShader {
    7.          Tags {"LightMode" = "ForwardBase" }
    8.                  
    9.          Pass {
    10.          CGPROGRAM
    11.          #include "UnityCG.cginc"
    12.          #pragma target 5.0  
    13.          #pragma vertex vertex_shader
    14.          #pragma fragment fragment_shader
    15.        
    16.          sampler2D _MainTex;
    17.          float4 _MainTex_ST;
    18.          uniform fixed4 _LightColor0;
    19.                
    20.          struct Point{
    21.              float3         vertex;
    22.              float3         normal;
    23.              float4         tangent;
    24.              float2 uv;
    25.          };      
    26.  
    27.          StructuredBuffer<Point> points;
    28.  
    29.          struct v2f {
    30.              float4 pos : SV_POSITION;
    31.              float4 col : COLOR;
    32.              float2 uv : TEXCOORD0;
    33.          };
    34.  
    35.          v2f vertex_shader (uint id : SV_VertexID, uint inst : SV_InstanceID)
    36.          {
    37.              v2f o;
    38.              float4 vertex_position =  float4(points[id].vertex,1.0f);
    39.              float4 vertex_normal = float4(points[id].normal, 1.0f);
    40.              vertex_position.x+=sin(5.0*_Time.g);
    41.              o.pos = mul (UNITY_MATRIX_VP, vertex_position);
    42.              o.uv =  TRANSFORM_TEX(points[id].uv, _MainTex);
    43.              float3 normalDirection = normalize(vertex_normal.xyz);
    44.              float4 AmbientLight = UNITY_LIGHTMODEL_AMBIENT;
    45.              float4 LightDirection = normalize(_WorldSpaceLightPos0);
    46.              float4 DiffuseLight = saturate(dot(LightDirection, normalDirection))*_LightColor0;
    47.              o.col=float4(AmbientLight + DiffuseLight);
    48.              return o;
    49.          }
    50.  
    51.          fixed4 fragment_shader (v2f i) : SV_Target
    52.          {
    53.              fixed4 final = tex2D(_MainTex, i.uv);
    54.             final *= i.col;
    55.             return final;
    56.          }
    57.        
    58.          ENDCG
    59.      }
    60.      }
    61. }
    This diffuse shader works great. How create shader with PBR lighting which will work with my C# script ???
     
  3. vladstorm_

    vladstorm_

    Joined:
    Nov 26, 2013
    Posts:
    184
    @przemek90
    did u find in the end how to connect compute shader output to unity PBR default shader ?
    coz I'm looking for the same thing right now
     
  4. jason-fisher

    jason-fisher

    Joined:
    Mar 19, 2014
    Posts:
    133
  5. Deleted User

    Deleted User

    Guest

    Unfortunately I abandoned my experiments associated with this case. However, it is not wasted time. I recreated Standard PBR shader into my custom vertex/fragment shaders, and one way could be to write custom shadows etc.
    But it can take a lot of work. DrawProcedural doesn't "see" scene data like realtime GI.
    Now I can only use this technique with static lighting and shadows baked into lightmaps.
    But maybe I will be back to this topic, because I found this one:

    https://forum.unity3d.com/threads/structuredbuffer-with-surface-shader.173000/

    I wonder, does it work ?
     
    vladstorm_ likes this.