Graphics.DrawProcedural questions

Hello guys :slight_smile: this is my first time when I’m asking for help :slight_smile:

I want to create complex procedural geometry structure which is fully lightning with shadows, postprocess effects like SSAO etc … simple example shows my idea …

I have few questions :

  1. is this possible to create surface shader for Graphics.DrawProcedural function so I can use lights, shadows, post effects etc ?
  2. why in my shader example _LightColor0 and _WorldSpaceLightPos0 doesn’t work ?

// simple example of procedural structures with DX11 and Graphics.DrawProcedural function

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

using System.Runtime.InteropServices;



public struct Geom {
	public Vector3 		vertex;
	public Vector3 		normal;
	public Vector4 		tangent;
}


public struct Obj {
	public Matrix4x4 	localTransform;
	public Vector3 		globalPosition;
}


// =========================================================================================================




public class DECreateGeometry : MonoBehaviour {

	public GameObject 			template;
	public Material 			material = null;
	public int					numberOfObjects = 4000;
	public float				scale = 1.0f;
	public float 				area = 5.0f;
	public Transform 			offsetObject;


	private ComputeBuffer		templateBuffer = null;
	private int 				numberOfGeoms = 0;

	private ComputeBuffer 		objectsBuffer = null;
	private Obj[] 				objects;
	private Matrix4x4[] 		localTransforms;
	private Vector3[]			globalPositions;






	// ================================================================================================
	
	void Awake () {
		InitGeometry();
		InitObjects();
	}
	

	void Update () {
		UpdateObjects();
	}


	void OnRenderObject() {
		material.SetPass(0);
		Graphics.DrawProcedural(MeshTopology.Triangles, numberOfGeoms, numberOfObjects);
	}
	

	void OnDisable() {
		ClearBuffers();
	}
	
	// ===============================================================================================





	
	void InitGeometry() {
		Mesh mesh = template.GetComponent<MeshFilter>().mesh;
		numberOfGeoms = mesh.triangles.Length;
		Geom[] geoms = new Geom[numberOfGeoms];
		for (int i = 0; i < numberOfGeoms; ++i)
		{
			geoms_.vertex = mesh.vertices[mesh.triangles*];*_

geoms_.normal = mesh.normals[mesh.triangles*];
geoms.tangent = mesh.tangents[mesh.triangles];
}
templateBuffer = new ComputeBuffer (numberOfGeoms, Marshal.SizeOf(typeof(Geom)));
templateBuffer.SetData (geoms);
material.SetBuffer (“geoms”, templateBuffer);
}*_

* void InitObjects() {*
* objects = new Obj[numberOfObjects];*
* localTransforms = new Matrix4x4[numberOfObjects];*
* globalPositions = new Vector3[numberOfObjects];*
* for (int i = 0; i < numberOfObjects; ++i)*
* {*
* Obj obj = new Obj();*
localTransforms = Matrix4x4.TRS(Vector3.zero, Random.rotationUniform, Vector3.one*scale);
* Vector3 newPos = new Vector3(Random.Range(-area, area), Random.Range(-area, area), Random.Range(-area, area));*
_ globalPositions = newPos;
obj.localTransform = localTransforms*;
obj.globalPosition = newPos;
objects = obj;
}*_

* objectsBuffer = new ComputeBuffer (numberOfObjects, Marshal.SizeOf(typeof(Obj)));*
* objectsBuffer.SetData (objects);*
* material.SetBuffer (“objects”, objectsBuffer);*
* }*

* void UpdateObjects() {*
* for (int i = 0; i < numberOfObjects; ++i)*
* {*
objects_.globalPosition = globalPositions*+offsetObject.position;
}
objectsBuffer.SetData (objects);
}*_

* void ClearBuffers() {*
* if (templateBuffer != null) templateBuffer.Release();*
* templateBuffer = null;*
* if (objectsBuffer != null) objectsBuffer.Release();*
* objectsBuffer = null;*
* }*
}
Shader “Custom/DEProceduralVert” {
* Properties {*
* DiffColor (“Diffuse Color”, Color) = (1,1,1,1)
SpecColor (“Specular Color”, Color) = (1,1,1,1)
Shininess (“Shininess” , Float) = 10*
* }*

* SubShader {
Tags {“LightMode” = “ForwardBase” }
Lighting On*_

* Pass {*
* CGPROGRAM*
* #pragma target 5.0*

* #pragma vertex vert*
* #pragma fragment frag*

* #include “UnityCG.cginc”*

* // ===========================================================================================*

* struct Obj {*
* float4x4 localTransform;*
* float3 globalPosition;*
* };*

* struct Geom {*
* float3 vertex;*
* float3 normal;*
* float4 tangent;*
* }; *

* StructuredBuffer objects;*
* StructuredBuffer geoms;*

* // ===========================================================================================*

* uniform fixed4 _DiffColor;
uniform fixed4 _SpecColor;
uniform float _Shininess;*

* uniform float4 _LightDir;
uniform fixed4 _LightColor;
uniform float _LightIntensity;
uniform fixed4 _AmbientColor;*

* uniform fixed4 _LightColor0; // doesn’t work ??*

* struct fragmentInput {*
* float4 pos : SV_POSITION;
_ float4 col : COLOR;
};*_

* fragmentInput vert (uint id : SV_VertexID, uint inst : SV_InstanceID)
_ {
fragmentInput o;*_

* float3 vertexTransform = mul(objects[inst].localTransform, float4(geoms[id].vertex,1.0f)).xyz;*
* float4 worldVertex = float4(objects[inst].globalPosition + vertexTransform,1.0f);*
* float4 worldNormal = mul(objects[inst].localTransform, float4(geoms[id].normal, 1.0f));*

* o.pos = mul (UNITY_MATRIX_VP, worldVertex);*

* float3 normalDirection = normalize(worldNormal.xyz);*
* //float3 lightDirection = normalize(float3(_WorldSpaceLightPos0.xyz)); // doesn’t work ??
float3 lightDirection = normalize(float3(_LightDir.xyz));
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - worldVertex.xyz);*

float3 lightComponent = _LightIntensity * max( 0.0f, dot(normalDirection, lightDirection));
float3 diffuse = _LightColor.xyz * lightComponent;
float3 specular = _SpecColor.xyz * lightComponent * pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)),_Shininess);
float3 lightFinal = (diffuse + specular +AmbientColor) * DiffColor.rgb;
* o.col = float4(lightFinal, 1.0f);*

* return o;*
* }*

* fixed4 frag (fragmentInput i) : COLOR*
* {*
* return i.col;*
* }*

* ENDCG*
* }*
* }*
* //FallBack “Diffuse”*
}
// setup globals for shaders -------------------------------------------------------
using UnityEngine;
using System.Collections;

public class DEShaderGlobals : MonoBehaviour {

* public Light lightDir;*
* public Color ambient;*

* // Use this for initialization*
* void Start () {*

* }*

* // Update is called once per frame*
* void Update () {*
* Shader.SetGlobalVector("LightDir",-lightDir.transform.forward);
Shader.SetGlobalVector("LightColor",lightDir.color);
Shader.SetGlobalFloat("LightIntensity",lightDir.intensity);
Shader.SetGlobalVector("AmbientColor",ambient);*
* }*

}

now question is how to write surface shader not cg shader … examples not explain how to use SV_VertexID, SV_InstanceID semantics