85 lines
2.6 KiB
C#
85 lines
2.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BITKit;
|
|
using MemoryPack;
|
|
using Net.BITKit.Quadtree;
|
|
using Unity.Mathematics;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
#if UNITY_5_3_OR_NEWER
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace Net.Project.B.WorldNode
|
|
{
|
|
public class UnityVegetationNode : MonoBehaviour
|
|
{
|
|
[SerializeField] private float3[] positions;
|
|
[SerializeField] private float2 sizeFactor;
|
|
|
|
[SerializeField] private GameObject searchPrefab;
|
|
|
|
private Mesh _mesh;
|
|
private Mesh[] _subMeshes;
|
|
private Material[] _materials;
|
|
private List<Matrix4x4> _matrix4;
|
|
|
|
private readonly Quadtree _quadtree=new(default,new float2(2048,2048));
|
|
private void Start()
|
|
{
|
|
_mesh = searchPrefab.GetComponentInChildren<MeshFilter>().sharedMesh;
|
|
_materials = searchPrefab.GetComponentInChildren<MeshRenderer>().sharedMaterials;
|
|
|
|
_matrix4 = new List<Matrix4x4>();
|
|
for (var i = 0; i < positions.Length; i++)
|
|
{
|
|
var position = positions[i];
|
|
var matrix = Matrix4x4.TRS(position, Quaternion.identity, Vector3.one);
|
|
_matrix4.Add(matrix);
|
|
|
|
_quadtree.Insert(i,position.xz);
|
|
}
|
|
}
|
|
private void Update()
|
|
{
|
|
|
|
for (var i = 0; i < _mesh.subMeshCount; i++)
|
|
{
|
|
Graphics.DrawMeshInstanced(_mesh,i,_materials[i],_matrix4);
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
private void OnDrawGizmosSelected()
|
|
{
|
|
foreach (var position in positions)
|
|
{
|
|
Gizmos.DrawCube(position,Vector3.one);
|
|
}
|
|
}
|
|
[ContextMenu(nameof(Search))]
|
|
private void Search()
|
|
{
|
|
var positionList = new List<float3>();
|
|
|
|
var allObjects = GameObject.FindObjectsOfType<GameObject>();
|
|
|
|
foreach (var obj in allObjects)
|
|
{
|
|
if (PrefabUtility.GetPrefabAssetType(obj) is not (PrefabAssetType.Regular or PrefabAssetType.Variant)) continue;
|
|
if (PrefabUtility.IsAnyPrefabInstanceRoot(obj) is false) continue;
|
|
var source = PrefabUtility.GetCorrespondingObjectFromOriginalSource(obj);
|
|
if(source!=searchPrefab)continue;
|
|
positionList.Add(obj.transform.position);
|
|
}
|
|
|
|
Debug.Log($"已获取到{positionList.Count}个预制体");
|
|
positions = positionList.ToArray();
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
|
|
#endif |