Hello everyone,
I am creating an extension for HRv2 which supports non vulkan devices and for that I use the API DrawMeshInstanced and MaterialPropertyBlock.
but the MaterialPropertyBlock always applies the first element of each Override property to all rendered entities.
I tried to reproduce the bug with a simple script to make sure the issue was not with the HRv2 extension.
note: im using the URP SimpleLit shader.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PropertiesTest : MonoBehaviour
{
public GameObject[] gameObjects;
Vector4[] m_Float4ArrayBuffer = new Vector4[24];
Matrix4x4[] m_MatricesArray = new Matrix4x4[24];
Color color = Color.green;
Mesh mesh;
Material material;
MaterialPropertyBlock materialPropertyBlock;
// Start is called before the first frame update
void Start()
{
if (gameObjects.Length == 0)
return;
var firstElement = gameObjects[0];
material = firstElement.GetComponent<MeshRenderer>().sharedMaterial;
mesh = firstElement.GetComponent<MeshFilter>().sharedMesh;
materialPropertyBlock = new MaterialPropertyBlock();
for (var i=0; i< gameObjects.Length; i++)
{
m_MatricesArray[i] = gameObjects[i].transform.localToWorldMatrix;
m_Float4ArrayBuffer[i] = color;
}
m_Float4ArrayBuffer[0] = Color.red;
m_Float4ArrayBuffer[5] = Color.blue;
materialPropertyBlock.SetVectorArray("_BaseColor", m_Float4ArrayBuffer);
}
// Update is called once per frame
void Update()
{
Graphics.DrawMeshInstanced(mesh, 0, material, m_MatricesArray, gameObjects.Length, materialPropertyBlock);
}
}
as you can see i have applied different colors to index 0 and 5, but everything is rendered with the first Color element (red).
Check if your shader graph inputs are present, in shader properties, on the top of shader. You can see that after shader compilation into code.
Last time I used shader grap with draw mesh instannced, I had add few lines of code to 5he shade manually, after it was compiled.
With latest ECS and Shader Graph, I moved however from draw mesh entairly. But I use it in whole game before then. Now is much easier for me to use it.
The properties are properly setted but from the shader code i see that they are not getting them by instance using MaterialPropertyBlock macro.
btw even the Official Hybrid Render v2 is not working correctly with Shader Graph shaders + Property Overrides.
Because there a bits missing in shader scripts after conversion from shader graph. Need be added manually.
There 2 or 3 places, few lines need be replaced.
I am on mobile, so unable to pinpoint atm, which lines.