61 lines
1.5 KiB
C#
61 lines
1.5 KiB
C#
using UnityEngine;
|
|
|
|
public class VoxelRaycastGPU : MonoBehaviour
|
|
{
|
|
public VoxelRaycastGPU(ComputeShader computeShader)
|
|
{
|
|
raycastShader = computeShader;
|
|
}
|
|
|
|
public ComputeShader raycastShader;
|
|
|
|
public struct Ray
|
|
{
|
|
public float pad;
|
|
public Vector3 direction;
|
|
}
|
|
|
|
public struct BatchData
|
|
{
|
|
public Vector3 origin;
|
|
public float maxDistance;
|
|
};
|
|
|
|
public struct Hit
|
|
{
|
|
public float penetrationFactor;
|
|
public float reflexionFactor;
|
|
public float lastDistance;
|
|
private float _pad1;
|
|
public Vector3 origin;
|
|
private float _pad2;
|
|
public Vector3 position;
|
|
private float _pad3;
|
|
public float distance;
|
|
public uint hit;
|
|
private float _pad4;
|
|
private float _pad5;
|
|
}
|
|
|
|
public void Init( ComputeShader computeShader )
|
|
{
|
|
raycastShader = computeShader;
|
|
}
|
|
|
|
public void Raycast( in Ray[] rays, float maxDistance, ref ComputeBuffer rayBuffer, ref ComputeBuffer hitBuffer )
|
|
{
|
|
int kernel = raycastShader.FindKernel("CSMain");
|
|
|
|
rayBuffer.SetData(rays);
|
|
raycastShader.SetBuffer(kernel, "rays", rayBuffer);
|
|
raycastShader.SetBuffer(kernel, "hits", hitBuffer);
|
|
raycastShader.SetFloat("maxDistance", maxDistance);
|
|
|
|
int threadGroups = Mathf.CeilToInt(rays.Length / 64f);
|
|
raycastShader.Dispatch(kernel, threadGroups, 1, 1);
|
|
|
|
Hit[] hits = new Hit[rays.Length];
|
|
hitBuffer.GetData(hits);
|
|
}
|
|
}
|