154 lines
4.8 KiB
C#
154 lines
4.8 KiB
C#
// using GPUInstancerPro;
|
|
// using Unity.Burst;
|
|
// using Unity.Collections;
|
|
// using Unity.Jobs;
|
|
// using Unity.Mathematics;
|
|
// using UnityEngine;
|
|
//
|
|
// namespace Elbyss.Optimization
|
|
// {
|
|
// public class MassiveSpawner : MonoBehaviour
|
|
// {
|
|
// [Header("Spawn Settings")]
|
|
// public GameObject prefab;
|
|
// public GPUIProfile profile;
|
|
// public int instanceCount = 100000;
|
|
// public float spacing = 1.5f;
|
|
//
|
|
// [Header("Update Settings")]
|
|
// public bool runUpdate = true;
|
|
// public float movementSpeed = 1.0f;
|
|
// public float amplitude = 2.0f;
|
|
//
|
|
// private int _rendererKey;
|
|
// private NativeArray<Matrix4x4> _matrices;
|
|
// private JobHandle _jobHandle;
|
|
// private bool _isInitialized;
|
|
//
|
|
// private void OnEnable()
|
|
// {
|
|
// Initialize();
|
|
// }
|
|
//
|
|
// private void OnDisable()
|
|
// {
|
|
// Dispose();
|
|
// }
|
|
//
|
|
// private void OnValidate()
|
|
// {
|
|
// if (Application.isPlaying && _isInitialized)
|
|
// {
|
|
// // Re-initialize if count changes during play (optional, but good for testing)
|
|
// Initialize();
|
|
// }
|
|
// }
|
|
//
|
|
// public void Initialize()
|
|
// {
|
|
// Dispose();
|
|
// if (prefab == null) return;
|
|
//
|
|
// if (GPUICoreAPI.RegisterRenderer(this, prefab, profile, out _rendererKey))
|
|
// {
|
|
// _matrices = new NativeArray<Matrix4x4>(instanceCount, Allocator.Persistent);
|
|
//
|
|
// // Initial generation
|
|
// GenerateMatrices(0);
|
|
// _jobHandle.Complete();
|
|
// GPUICoreAPI.SetTransformBufferData(_rendererKey, _matrices);
|
|
//
|
|
// _isInitialized = true;
|
|
// Debug.Log($"[MassiveSpawner] Registered {instanceCount} instances with key {_rendererKey}");
|
|
// }
|
|
// else
|
|
// {
|
|
// Debug.LogError("[MassiveSpawner] Failed to register renderer!");
|
|
// }
|
|
// }
|
|
//
|
|
// private void Update()
|
|
// {
|
|
// if (!_isInitialized || _rendererKey == 0) return;
|
|
//
|
|
// if (runUpdate)
|
|
// {
|
|
// // Complete previous frame's work if any
|
|
// _jobHandle.Complete();
|
|
//
|
|
// // Apply updated matrices to GPUI
|
|
// GPUICoreAPI.SetTransformBufferData(_rendererKey, _matrices);
|
|
//
|
|
// // Schedule next frame's work
|
|
// GenerateMatrices(Time.time);
|
|
// }
|
|
// }
|
|
//
|
|
// private void GenerateMatrices(float time)
|
|
// {
|
|
// int side = Mathf.CeilToInt(Mathf.Sqrt(instanceCount));
|
|
//
|
|
// var job = new MatrixUpdateJob
|
|
// {
|
|
// matrices = _matrices,
|
|
// side = side,
|
|
// spacing = spacing,
|
|
// time = time,
|
|
// speed = movementSpeed,
|
|
// amplitude = amplitude,
|
|
// origin = transform.position
|
|
// };
|
|
//
|
|
// _jobHandle = job.Schedule(instanceCount, 64);
|
|
// }
|
|
//
|
|
// public void Dispose()
|
|
// {
|
|
// _jobHandle.Complete();
|
|
// if (_rendererKey != 0)
|
|
// {
|
|
// GPUICoreAPI.DisposeRenderer(_rendererKey);
|
|
// _rendererKey = 0;
|
|
// }
|
|
//
|
|
// if (_matrices.IsCreated)
|
|
// {
|
|
// _matrices.Dispose();
|
|
// }
|
|
// _isInitialized = false;
|
|
// }
|
|
//
|
|
// [BurstCompile]
|
|
// struct MatrixUpdateJob : IJobParallelFor
|
|
// {
|
|
// public NativeArray<Matrix4x4> matrices;
|
|
// public int side;
|
|
// public float spacing;
|
|
// public float time;
|
|
// public float speed;
|
|
// public float amplitude;
|
|
// public Vector3 origin;
|
|
//
|
|
// public void Execute(int index)
|
|
// {
|
|
// int x = index % side;
|
|
// int z = index / side;
|
|
//
|
|
// float xPos = x * spacing;
|
|
// float zPos = z * spacing;
|
|
//
|
|
// // Add some animation to prove it's updating
|
|
// float yPos = math.sin(time * speed + (xPos + zPos) * 0.1f) * amplitude;
|
|
//
|
|
// Vector3 pos = origin + new Vector3(xPos, yPos, zPos);
|
|
//
|
|
// // Simple rotation based on time
|
|
// float angle = (time * speed * 10f + index) % 360f;
|
|
// Quaternion rot = Quaternion.Euler(0, angle, 0);
|
|
//
|
|
// matrices[index] = Matrix4x4.TRS(pos, rot, Vector3.one);
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|