Skip to content

Instantly share code, notes, and snippets.

@JoseMiguelPizarro
Created February 2, 2021 16:41
Show Gist options
  • Save JoseMiguelPizarro/a5efb028b23f94bec1968d8a79e44e94 to your computer and use it in GitHub Desktop.
Save JoseMiguelPizarro/a5efb028b23f94bec1968d8a79e44e94 to your computer and use it in GitHub Desktop.
using System;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
public class RaycastCommandRaycaster: IDisposable
{
NativeArray<RaycastCommand> commands;
NativeArray<RaycastHit> results;
public RaycastCommandRaycaster(int bufferSize)
{
commands = new NativeArray<RaycastCommand>(bufferSize, Allocator.Persistent);
results = new NativeArray<RaycastHit>(bufferSize, Allocator.Persistent);
}
public void AddRaycastCommand(Ray ray, float distance, int index)
{
commands[index] = new RaycastCommand(ray.origin, ray.direction, distance);
}
public JobHandle ExecuteRaycastCommands()
{
return RaycastCommand.ScheduleBatch(commands, results, 1);
}
public void Dispose()
{
commands.Dispose();
results.Dispose();
}
}
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class RaycastCommandTest : MonoBehaviour
{
public GameObject prefab;
public Vector2 spawnSize;
public float gap;
public int iterations;
public RaycastType raycastType;
public TextMeshProUGUI messageText;
private List<GameObject> raycasters;
private RaycastCommandRaycaster commandRaycaster;
void Start()
{
raycasters = new List<GameObject>();
var positionOffset = new Vector3(spawnSize.x * gap, 0, spawnSize.y * gap) / 2.0f;
for (int i = 0; i < spawnSize.x; i++)
{
for (int j = 0; j < spawnSize.y; j++)
{
var position = new Vector3(i * gap, 1, j * gap) + positionOffset;
var p = Instantiate(prefab, position, Quaternion.identity);
raycasters.Add(p);
}
}
commandRaycaster = new RaycastCommandRaycaster(raycasters.Count * iterations);
SetMessageText();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
raycastType ^= RaycastType.Commands | RaycastType.Default;
SetMessageText();
}
switch (raycastType)
{
case RaycastType.Default:
DefaultRaycast();
break;
case RaycastType.Commands:
CommandRaycast();
break;
default:
break;
}
}
private void SetMessageText()
{
if (messageText != null)
messageText.text = raycastType == RaycastType.Commands ? "Raycast Commands: On" : "Raycast Commands: Off";
}
private void OnDestroy()
{
commandRaycaster.Dispose();
}
private void CommandRaycast()
{
var raycastIndex = 0;
foreach (var r in raycasters)
{
var ray = new Ray(r.transform.position, Vector3.down);
for (int i = 0; i < iterations; i++)
{
commandRaycaster.AddRaycastCommand(ray, 10, raycastIndex++);
}
}
var handle = commandRaycaster.ExecuteRaycastCommands();
handle.Complete();
}
private void DefaultRaycast()
{
foreach (var r in raycasters)
{
var ray = new Ray(r.transform.position, Vector3.down);
for (int i = 0; i < iterations; i++)
{
Physics.Raycast(ray, out var hitInfo, 10);
}
}
}
public enum RaycastType
{
Default,
Commands
}
}
private void SetMessageText()
{
messageText.text = raycastType == RaycastType.Commands ? "Raycast Commands: On" : "Raycast Commands: Off";
}
private void OnDestroy()
{
commandRaycaster.Dispose();
}
private void CommandRaycast()
{
var raycastIndex = 0;
foreach (var r in raycasters)
{
var ray = new Ray(r.transform.position, Vector3.down);
for (int i = 0; i < iterations; i++)
{
commandRaycaster.AddRaycastCommand(ray, 10, raycastIndex++);
}
}
var handle = commandRaycaster.ExecuteRaycastCommands();
handle.Complete();
}
private void DefaultRaycast()
{
foreach (var r in raycasters)
{
var ray = new Ray(r.transform.position, Vector3.down);
for (int i = 0; i < iterations; i++)
{
Physics.Raycast(ray, out var hitInfo, 10);
}
}
}
public enum RaycastType
{
Default,
Commands
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment