Advertisement
HolyFot

OLD GPU Instancing

Jul 17th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.69 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class GpuBuildingz : MonoBehaviour
  7. {
  8. public int layerID = 1;
  9. Transform playerTrans;
  10. Camera camera1;
  11.  
  12. public int maxMeshTypes = 200;
  13. public int maxObjects = 5000;
  14.  
  15. public MeshGpuObject[] mesh_Instances;
  16. public int meshObjectCounter;
  17.  
  18. public void Start()
  19. {
  20. DontDestroyOnLoad(transform.gameObject);
  21.  
  22. meshObjectCounter = 0;
  23. mesh_Instances = new MeshGpuObject[maxMeshTypes];
  24.  
  25.  
  26. //REMOVE! - I THINK
  27. for (int i = 0; i < maxMeshTypes; i++) //Initialize Classes & Positions
  28. {
  29. mesh_Instances[i] = new MeshGpuObject(); //Move These 2 Lines
  30. mesh_Instances[i].positions = new Matrix4x4[maxObjects];
  31. mesh_Instances[i].GobjectIDs = new int[maxObjects];
  32. //for (int x = 0; x < maxObjects; x++)
  33. //{
  34. //mesh_Instances[i].positions[x] = Matrix4x4.TRS(new Vector3(0f, 0f, 0f), new Quaternion(0f, 0f, 0f, 0f), new Vector3(1f, 1f, 1f));
  35. //}
  36. }
  37.  
  38. GetCamera();
  39. }
  40.  
  41. void Update()
  42. {
  43. if (camera1 == null)
  44. GetCamera();
  45.  
  46. UpdateLODs();
  47. if (bl_Utilz.IsStandAlone == false)
  48. DrawInstanceMeshes(0);
  49. }
  50.  
  51.  
  52. public void UpdateLODs()
  53. {
  54. //Flush LOD Arrays
  55.  
  56. //Add new LOD_Positions based on which LOD they are
  57.  
  58. }
  59.  
  60. public void DrawInstanceMeshes(int lod2)
  61. {
  62. if (lod2 == 0)
  63. {
  64. for (int i = 0; i < meshObjectCounter; i++)
  65. {
  66. if (mesh_Instances[i] != null) //This Line might not be needed
  67. for (int x = 0; x < mesh_Instances[i].loD1.LOD0_mats.Length; x++)
  68. {
  69. Graphics.DrawMeshInstanced(mesh_Instances[i].loD1.LOD0, x, mesh_Instances[i].loD1.LOD0_mats[x], mesh_Instances[i].positions, mesh_Instances[i].objectsCounter, null, UnityEngine.Rendering.ShadowCastingMode.TwoSided, true, layerID, null);
  70. }
  71. }
  72. }
  73. }
  74.  
  75. public void AddMesh(GameObject obj1, int GobjectID)
  76. {
  77. if (bl_Utilz.IsStandAlone == true)
  78. {
  79. GpuLODObject currLOD2;
  80. currLOD2 = obj1.GetComponent<GpuLODObject>();
  81. currLOD2.LOD0_mats = null;
  82. currLOD2.LOD1_mats = null;
  83. currLOD2.LOD2_mats = null;
  84. return;
  85. }
  86.  
  87. bool alreadyExists = false;
  88. int meshIndex = 0;
  89. string prefabName = obj1.transform.name.Replace("(Clone)", "");
  90.  
  91. //Check if Object already is added:
  92. for (int i = 0; i < meshObjectCounter; i++)
  93. {
  94. if (mesh_Instances[i].prefabName == prefabName)
  95. {
  96. meshIndex = i;
  97. alreadyExists = true;
  98. break;
  99. }
  100. }
  101.  
  102. //Add To List
  103. if (alreadyExists == true)
  104. {
  105. mesh_Instances[meshIndex].objectsCounter++;
  106. int newID = mesh_Instances[meshIndex].objectsCounter - 1;
  107. mesh_Instances[meshIndex].GobjectIDs[newID] = GobjectID;
  108.  
  109. Quaternion rot = Quaternion.Euler(obj1.transform.eulerAngles.x, obj1.transform.eulerAngles.y, obj1.transform.eulerAngles.z);
  110. mesh_Instances[meshIndex].positions[newID] = Matrix4x4.TRS(obj1.transform.position, rot, new Vector3(1f, 1f, 1f));
  111.  
  112. }
  113. else //Create New Instance Mesh
  114. {
  115. Debug.Log("Creating new meshInstance. ID: " + meshObjectCounter + ", Name: " + prefabName);
  116.  
  117. GpuLODObject currLOD;
  118. currLOD = obj1.GetComponent<GpuLODObject>();
  119.  
  120. if (currLOD.LOD0_mats == null)
  121. {
  122. Debug.Log("Materials are null, could not add: " + prefabName);
  123. return;
  124. }
  125. else
  126. {
  127. for (int x = 0; x < currLOD.LOD0_mats.Length; x++) //
  128. {
  129. if (currLOD.LOD0_mats[x] == null)
  130. {
  131. Debug.Log("Null material, could not add: " + prefabName);
  132. return;
  133. }
  134. currLOD.LOD0_mats[x].enableInstancing = true;
  135. }
  136. }
  137.  
  138. //Initialize & Setup New Prefab Mesh
  139. mesh_Instances[meshObjectCounter].prefabName = prefabName;
  140. mesh_Instances[meshObjectCounter].GobjectIDs[0] = GobjectID;
  141. mesh_Instances[meshObjectCounter].meshRender = obj1.GetComponent<MeshRenderer>();
  142. mesh_Instances[meshObjectCounter].loD1 = currLOD;
  143.  
  144.  
  145.  
  146. mesh_Instances[meshObjectCounter].objectsCounter = 1;
  147. Quaternion rot = Quaternion.Euler(obj1.transform.eulerAngles.x, obj1.transform.eulerAngles.y, obj1.transform.eulerAngles.z);
  148. mesh_Instances[meshObjectCounter].positions[0] = Matrix4x4.TRS(obj1.transform.position, rot, new Vector3(1f, 1f, 1f));
  149.  
  150. //Debug.Log("Created new meshInstance: " + prefabName);
  151. meshObjectCounter++;
  152. }
  153.  
  154. //Disable MeshRenderer
  155. obj1.GetComponent<MeshRenderer>().enabled = false;
  156. }
  157.  
  158.  
  159. public void RemoveMesh(string prefabName1, int GobjID)
  160. {
  161. try
  162. {
  163. if (bl_Utilz.IsStandAlone == true)
  164. return;
  165.  
  166. //Find the Mesh Prefab
  167. prefabName1 = prefabName1.Replace("(Clone)", "");
  168. int meshIndex = -1;
  169. for (int i = 0; i < meshObjectCounter; i++)
  170. {
  171. if (mesh_Instances[i].prefabName == prefabName1)
  172. {
  173. meshIndex = i;
  174. break;
  175. }
  176. }
  177.  
  178. if (meshIndex > -1) //Find Gobject
  179. {
  180. Quaternion rot = Quaternion.Euler(1f, 1f, 1f);
  181. for (int x = 0; x < mesh_Instances[meshIndex].GobjectIDs.Length; x++)
  182. {
  183. if (mesh_Instances[meshIndex].GobjectIDs[x] == GobjID)
  184. {
  185. //Set the Position & Scale to 0 & ID to -2
  186. mesh_Instances[meshIndex].GobjectIDs[x] = -2;
  187. mesh_Instances[meshIndex].positions[x] = Matrix4x4.TRS(new Vector3(0.1f, 0.1f, 0.1f), rot, new Vector3(0.1f, 0.1f, 0.1f));
  188. break;
  189. }
  190. }
  191. }
  192. }
  193. catch (Exception e)
  194. {
  195. //Debug.Log("Couldnt Remove Instanced Mesh (Ignored)" + e.Message);
  196. }
  197. }
  198.  
  199. public void GetCamera()
  200. {
  201. try
  202. {
  203. camera1 = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
  204. //foundCamera = true;
  205. //Debug.Log("Found Camera!");
  206. }
  207. catch (Exception e)
  208. { }
  209. }
  210. }
  211.  
  212. public class MeshGpuObject
  213. {
  214. public string prefabName = "";
  215. public GpuLODObject loD1;
  216.  
  217.  
  218. public Matrix4x4[] positions; //(Vector3 pos, Quaternion q, Vector3 s)
  219. public int[] GobjectIDs;
  220. public int objectsCounter = 0;
  221.  
  222. public Renderer meshRender;
  223. }
  224.  
  225.  
  226. //Object Class
  227. using UnityEngine;
  228. using System;
  229. using System.Collections;
  230.  
  231. public class GpuLODObject : MonoBehaviour
  232. {
  233. public Mesh LOD0;
  234. public Mesh LOD1;
  235. public Mesh LOD2;
  236.  
  237. public Material[] LOD0_mats;
  238. public Material[] LOD1_mats;
  239. public Material[] LOD2_mats;
  240.  
  241. public float lod0Dist = 10f;
  242. public float lod1Dist = 20f;
  243. public float lod2Dist = 30f;
  244.  
  245. GpuBuildingz gpuBuilding;
  246. int GobjID;
  247. string prefabName1;
  248. bool isAdded;
  249. private float currTimer;
  250. private float addTimer = 0.5f;
  251.  
  252. void Start()
  253. {
  254. isAdded = false;
  255. AddToGPUBuildingz();
  256. }
  257.  
  258. void AddToGPUBuildingz()
  259. {
  260. //Dont update if CharSelect Scene
  261. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == ServerSettings.ChrCreate)
  262. return;
  263.  
  264. //Add To GPU Buildingz
  265. /*gpuBuilding = GameObject.Find("GpuBuildings").GetComponent<GpuBuildingz>();
  266. if (gpuBuilding != null)
  267. {
  268. if (this.gameObject.GetComponent<ObjectID>() != null)
  269. {
  270. GobjID = this.gameObject.GetComponent<ObjectID>().ObjID;
  271. prefabName1 = transform.name;
  272. gpuBuilding.AddMesh(this.gameObject, GobjID);
  273. }
  274. else
  275. {
  276. //Debug.Log("ObjectID script not getting added on client!!!!");
  277. }
  278. }*/
  279. }
  280.  
  281. void OnDestroy()
  282. {
  283. if (gpuBuilding != null)
  284. gpuBuilding.RemoveMesh(prefabName1, GobjID);
  285. }
  286.  
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement