I don't understand why the _Positions property of ComputeShader was given as a parameter of material.SetBuffer.
I thought SetSomething(property, value)
method give value to property.
But in this case, _Positions
property is a ComputeShader's variable then what is the mean of giving it to material
?
And experience has shown that the code actually stores the positions of the material
. How and Why it does?
I think I'm missing something fundamental about Buffers.
And Is it relative with where does ShaderGraph's Position Node input come from?
C# Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GPUGraph : MonoBehaviour
{
[SerializeField]
ComputeShader computeShader;
static int positionID = Shader.PropertyToID("_Positions");
// _Positions poperty in computeShader
// RWStructuredBuffer<float3> _Positions;
// Some position data saved in _Positions property
[SerializeField]
Material material;
[SerializeField]
Mesh mesh;
ComputeBuffer positionBuffer;
const int MaxResolution = 10;
void OnEnable() {
positionBuffer = new ComputeBuffer(MaxResolution * MaxResolution, 3 * 4);
}
void OnDisable() {
positionBuffer.Release();
positionBuffer = null;
}
void Update() {
UpdateFunctionOnGPU();
}
void UpdateFunctionOnGPU() {
computeShader.SetBuffer(0, positionID, positionBuffer);
int groups = Mathf.CeilToInt(MaxResolution / 8f);
computeShader.Dispatch(0, groups, groups, 1);
material.SetBuffer(positionID, positionBuffer);
var bounds = new Bounds(Vector3.zero, Vector3.one * (2f + 2f / resolution));
Graphics.DrawMeshInstancedProcedural(mesh, 0, material, bounds, resolution * resolution);
}
}