private void Reset() { if (m_OwnerClipper == null) { return; } GameObject.Destroy(m_OwnerClipper); m_OwnerClipper = null; }
void Start() { if (m_UseChilds) { Renderer[] rs = GetComponentsInChildren <Renderer> (true); if (m_GPUInstancingList == null) { m_GPUInstancingList = new List <Renderer> (rs.Length); } else { m_GPUInstancingList.Clear(); } m_GPUInstancingList.AddRange(rs); } if (m_GPUInstancingList != null && m_GPUInstancingList.Count > 0) { Camera cam = m_TargetCamera; if (cam == null) { cam = Camera.main; } if (cam == null) { return; } KdTreeCameraClipper clipper = cam.GetComponent <KdTreeCameraClipper> (); if (clipper == null) { clipper = cam.gameObject.AddComponent <KdTreeCameraClipper> (); clipper.AutoSetVisible = m_GroupType == GPUInstancingGroupType.GroupAutoVisible; clipper.OpenGPUInstancing = m_GroupType == GPUInstancingGroupType.GroupGPUInstancing; clipper.m_AspectScale = 2.0f; clipper.PerMaxVisbileCount = 500; clipper.m_UseThread = true; m_OwnerClipper = clipper; } else { clipper.Clear(); } clipper.ReBuild(m_GPUInstancingList); } }
// 进入场景只Build一次,一个场景Build一次,频率还好, 内部代码有GC public void Build(bool isUseThread = true) { Reset(); if (m_Clipper != null) { int cnt = m_Clipper.ObjectsCount; if (cnt <= 0) { return; } Action <Vector3[]> action = (Vector3[] arr) => { int idx = 0; bool isSet = false; if (m_KdTreeObjList == null) { m_KdTreeObjList = new List <KdTreeObj>(m_Clipper.ObjectsCount); } else { m_KdTreeObjList.Clear(); m_KdTreeObjList.Capacity = m_Clipper.ObjectsCount; } m_Clipper.ForEachObjects( (Renderer render) => { Vector3 pos = render.transform.position; arr[idx] = pos; ++idx; isSet = true; BoundingSphere boundingSpere = KdTreeCameraClipper.GetBoundingSphere(render); KdTreeObj obj = new KdTreeObj(); obj.InstanceId = render.GetInstanceID(); obj.boundSphere = boundingSpere; m_KdTreeObjList.Add(obj); } ); if (isSet) { m_KdTree = new KDTree.KDTree(arr, 32, false); m_KdTreeStatus = KdTreeStatus.WaitRebuild; m_UseThread = isUseThread; UpdateClipper(); CreateThread(); } }; if (m_VecArr == null) { m_VecArr = new Vector3[cnt]; action(m_VecArr); } else if (cnt > m_VecArr.Length) { Array.Resize(ref m_VecArr, cnt); action(m_VecArr); } else { UnsafeUtil.Vector3HackArraySizeCall(m_VecArr, cnt, action); } } }