CommandBuffer.DrawProcedural examples
-
-
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. -
My C# script:
My shader (simple per vertex diffuse lighting with vertex animation):Code (CSharp):-
using UnityEngine;
-
using System.Runtime.InteropServices;
-
-
public struct Point {
-
public Vector3 vertex;
-
public Vector3 normal;
-
public Vector4 tangent;
-
public Vector2 uv;
-
}
-
public class ProceduralGeometry : MonoBehaviour {
-
public Material material ;
-
public GameObject gameobject;
-
private ComputeBuffer computebuffer;
-
private int n = 0;
-
-
void Start () {
-
Mesh mesh = gameobject.GetComponent<MeshFilter>().sharedMesh;
-
n = mesh.triangles.Length;
-
Point[] points = new Point[n];
-
for (int i = 0; i < n; ++i)
-
{
-
points[i].vertex = mesh.vertices[mesh.triangles[i]];
-
points[i].normal = mesh.normals[mesh.triangles[i]];
-
points[i].tangent = mesh.tangents[mesh.triangles[i]];
-
points[i].uv = mesh.uv [mesh.triangles [i]];
-
}
-
computebuffer.SetData (points);
-
material.SetBuffer ("points", computebuffer);
-
}
-
-
void OnRenderObject() {
-
material.SetPass(0);
-
Graphics.DrawProcedural(MeshTopology.Triangles, n, 1);
-
}
-
-
void OnDestroy() {
-
computebuffer.Release ();
-
}
-
-
}
This diffuse shader works great. How create shader with PBR lighting which will work with my C# script ???Code (CSharp):-
Shader "DirectX 11 Diffuse Mapping" {
-
Properties
-
{
-
_MainTex ("Texture", 2D) = "white" {}
-
}
-
SubShader {
-
Tags {"LightMode" = "ForwardBase" }
-
-
Pass {
-
CGPROGRAM
-
#include "UnityCG.cginc"
-
#pragma target 5.0
-
#pragma vertex vertex_shader
-
#pragma fragment fragment_shader
-
-
sampler2D _MainTex;
-
float4 _MainTex_ST;
-
uniform fixed4 _LightColor0;
-
-
struct Point{
-
float3 vertex;
-
float3 normal;
-
float4 tangent;
-
float2 uv;
-
};
-
-
StructuredBuffer<Point> points;
-
-
struct v2f {
-
float4 pos : SV_POSITION;
-
float4 col : COLOR;
-
float2 uv : TEXCOORD0;
-
};
-
-
v2f vertex_shader (uint id : SV_VertexID, uint inst : SV_InstanceID)
-
{
-
v2f o;
-
float4 vertex_position = float4(points[id].vertex,1.0f);
-
float4 vertex_normal = float4(points[id].normal, 1.0f);
-
vertex_position.x+=sin(5.0*_Time.g);
-
o.pos = mul (UNITY_MATRIX_VP, vertex_position);
-
o.uv = TRANSFORM_TEX(points[id].uv, _MainTex);
-
float3 normalDirection = normalize(vertex_normal.xyz);
-
float4 AmbientLight = UNITY_LIGHTMODEL_AMBIENT;
-
float4 LightDirection = normalize(_WorldSpaceLightPos0);
-
float4 DiffuseLight = saturate(dot(LightDirection, normalDirection))*_LightColor0;
-
o.col=float4(AmbientLight + DiffuseLight);
-
return o;
-
}
-
-
fixed4 fragment_shader (v2f i) : SV_Target
-
{
-
fixed4 final = tex2D(_MainTex, i.uv);
-
final *= i.col;
-
return final;
-
}
-
-
ENDCG
-
}
-
}
-
}
dylan-hart, kosowski and polyflow3d like this. -
-
@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 -
There might be something here that is helpful: https://blogs.unity3d.com/2015/02/06/extending-unity-5-rendering-pipeline-command-buffers/
vladstorm_ likes this. -
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.Horz said: ↑@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
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 ?
posted on 2021-11-04 09:36 guanxi0808 阅读(306) 评论(0) 收藏 举报
浙公网安备 33010602011771号