-
-
Save friuns2/2c24e697b1f6d7bc21d35c394f64d8f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using GPUInstancer; | |
using SunTemple; | |
using UnityEditor; | |
using UnityEngine; | |
public class OptimizeScene : MonoBehaviour | |
{ | |
public int maxCnt=50; | |
public GPUInstancerPrefabManager prefabManager; | |
Dictionary<string, Proto> prototypes = new Dictionary<string, Proto>(); | |
public void Start() | |
{ | |
var trs = FindObjectsOfType<Transform>(); | |
foreach (var tr in trs) | |
{ | |
if (tr.GetComponent<DisableRenderer>()) | |
{ | |
DestroyImmediate(tr.GetComponent<MeshRenderer>()); | |
DestroyImmediate(tr.GetComponent<MeshFilter>()); | |
DestroyImmediate(tr.GetComponent<DisableRenderer>()); | |
} | |
var mf = tr.GetComponent<MeshFilter>(); | |
var lodGroup = tr.GetComponent<LODGroup>(); | |
var meshRenderer = tr.GetComponent<MeshRenderer>(); | |
if (lodGroup || mf && mf.sharedMesh && meshRenderer) | |
{ | |
//if (!lodGroup) | |
// //if (meshRenderer.bounds.size.magnitude < 10) | |
// { | |
// var lg = tr.gameObject.AddComponent<LODGroup>(); | |
// lg.SetLODs(new LOD[1] { new LOD(.1f, new[] { meshRenderer }) }); | |
// } | |
string key = lodGroup ? lodGroup.size.ToString() : mf.sharedMesh.name; | |
if (!tr.parent || !tr.parent.GetComponentInParent<LODGroup>() || lodGroup) | |
{ | |
Proto val; | |
if (!prototypes.TryGetValue(key, out val)) | |
{ | |
//var pr = GPUInstancerAPI.DefineGameObjectAsPrefabPrototypeAtRuntime(prefabManager, tr.gameObject); | |
//if (pr) | |
// pr.isFrustumCulling= pr.isOcclusionCulling = false; | |
//pr.maxDistance = 10; | |
val = prototypes[key] = new Proto() { /*a = pr */}; | |
} | |
val.instances.Add(tr.gameObject); | |
} | |
//if (meshRenderer) | |
// meshRenderer.enabled = false; | |
//if (lodGroup) | |
// lodGroup.enabled = false; | |
//tr.gameObject.SetActive(false); | |
//tr.name = key; | |
} | |
} | |
foreach (var a in prototypes) | |
{ | |
print(a.Key + " " + a.Value.instances.Count); | |
if (a.Value.instances.Count > maxCnt) | |
{ | |
var pr = GPUInstancerAPI.DefineGameObjectAsPrefabPrototypeAtRuntime(prefabManager, a.Value.instances[0].gameObject); | |
if (pr != null) | |
GPUInstancerAPI.AddInstancesToPrefabPrototypeAtRuntime(prefabManager, pr, a.Value.instances); | |
} | |
//else | |
// foreach (var g in a.Value.instances) | |
// { | |
// var mr = g.GetComponent<MeshRenderer>(); | |
// if (mr && mr.bounds.size.magnitude < 10) | |
// { | |
// var lg = g.AddComponent<LODGroup>(); | |
// lg.SetLODs(new LOD[1] { new LOD(.5f, new[] { mr }) }); | |
// } | |
// } | |
} | |
} | |
} | |
public class Proto | |
{ | |
//public GPUInstancerPrefabPrototype a; | |
public List<GameObject> instances = new List<GameObject>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment