Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Reflection;
- using System.Collections.Generic;
- using UnityEngine;
- public class GpuBuildingz : MonoBehaviour
- {
- public int layerID = 1;
- Transform playerTrans;
- Camera camera1;
- public int maxMeshTypes = 200;
- public int maxObjects = 5000;
- public MeshGpuObject[] mesh_Instances;
- public int meshObjectCounter;
- public void Start()
- {
- DontDestroyOnLoad(transform.gameObject);
- meshObjectCounter = 0;
- mesh_Instances = new MeshGpuObject[maxMeshTypes];
- //REMOVE! - I THINK
- for (int i = 0; i < maxMeshTypes; i++) //Initialize Classes & Positions
- {
- mesh_Instances[i] = new MeshGpuObject(); //Move These 2 Lines
- mesh_Instances[i].positions = new Matrix4x4[maxObjects];
- mesh_Instances[i].GobjectIDs = new int[maxObjects];
- //for (int x = 0; x < maxObjects; x++)
- //{
- //mesh_Instances[i].positions[x] = Matrix4x4.TRS(new Vector3(0f, 0f, 0f), new Quaternion(0f, 0f, 0f, 0f), new Vector3(1f, 1f, 1f));
- //}
- }
- GetCamera();
- }
- void Update()
- {
- if (camera1 == null)
- GetCamera();
- UpdateLODs();
- if (bl_Utilz.IsStandAlone == false)
- DrawInstanceMeshes(0);
- }
- public void UpdateLODs()
- {
- //Flush LOD Arrays
- //Add new LOD_Positions based on which LOD they are
- }
- public void DrawInstanceMeshes(int lod2)
- {
- if (lod2 == 0)
- {
- for (int i = 0; i < meshObjectCounter; i++)
- {
- if (mesh_Instances[i] != null) //This Line might not be needed
- for (int x = 0; x < mesh_Instances[i].loD1.LOD0_mats.Length; x++)
- {
- 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);
- }
- }
- }
- }
- public void AddMesh(GameObject obj1, int GobjectID)
- {
- if (bl_Utilz.IsStandAlone == true)
- {
- GpuLODObject currLOD2;
- currLOD2 = obj1.GetComponent<GpuLODObject>();
- currLOD2.LOD0_mats = null;
- currLOD2.LOD1_mats = null;
- currLOD2.LOD2_mats = null;
- return;
- }
- bool alreadyExists = false;
- int meshIndex = 0;
- string prefabName = obj1.transform.name.Replace("(Clone)", "");
- //Check if Object already is added:
- for (int i = 0; i < meshObjectCounter; i++)
- {
- if (mesh_Instances[i].prefabName == prefabName)
- {
- meshIndex = i;
- alreadyExists = true;
- break;
- }
- }
- //Add To List
- if (alreadyExists == true)
- {
- mesh_Instances[meshIndex].objectsCounter++;
- int newID = mesh_Instances[meshIndex].objectsCounter - 1;
- mesh_Instances[meshIndex].GobjectIDs[newID] = GobjectID;
- Quaternion rot = Quaternion.Euler(obj1.transform.eulerAngles.x, obj1.transform.eulerAngles.y, obj1.transform.eulerAngles.z);
- mesh_Instances[meshIndex].positions[newID] = Matrix4x4.TRS(obj1.transform.position, rot, new Vector3(1f, 1f, 1f));
- }
- else //Create New Instance Mesh
- {
- Debug.Log("Creating new meshInstance. ID: " + meshObjectCounter + ", Name: " + prefabName);
- GpuLODObject currLOD;
- currLOD = obj1.GetComponent<GpuLODObject>();
- if (currLOD.LOD0_mats == null)
- {
- Debug.Log("Materials are null, could not add: " + prefabName);
- return;
- }
- else
- {
- for (int x = 0; x < currLOD.LOD0_mats.Length; x++) //
- {
- if (currLOD.LOD0_mats[x] == null)
- {
- Debug.Log("Null material, could not add: " + prefabName);
- return;
- }
- currLOD.LOD0_mats[x].enableInstancing = true;
- }
- }
- //Initialize & Setup New Prefab Mesh
- mesh_Instances[meshObjectCounter].prefabName = prefabName;
- mesh_Instances[meshObjectCounter].GobjectIDs[0] = GobjectID;
- mesh_Instances[meshObjectCounter].meshRender = obj1.GetComponent<MeshRenderer>();
- mesh_Instances[meshObjectCounter].loD1 = currLOD;
- mesh_Instances[meshObjectCounter].objectsCounter = 1;
- Quaternion rot = Quaternion.Euler(obj1.transform.eulerAngles.x, obj1.transform.eulerAngles.y, obj1.transform.eulerAngles.z);
- mesh_Instances[meshObjectCounter].positions[0] = Matrix4x4.TRS(obj1.transform.position, rot, new Vector3(1f, 1f, 1f));
- //Debug.Log("Created new meshInstance: " + prefabName);
- meshObjectCounter++;
- }
- //Disable MeshRenderer
- obj1.GetComponent<MeshRenderer>().enabled = false;
- }
- public void RemoveMesh(string prefabName1, int GobjID)
- {
- try
- {
- if (bl_Utilz.IsStandAlone == true)
- return;
- //Find the Mesh Prefab
- prefabName1 = prefabName1.Replace("(Clone)", "");
- int meshIndex = -1;
- for (int i = 0; i < meshObjectCounter; i++)
- {
- if (mesh_Instances[i].prefabName == prefabName1)
- {
- meshIndex = i;
- break;
- }
- }
- if (meshIndex > -1) //Find Gobject
- {
- Quaternion rot = Quaternion.Euler(1f, 1f, 1f);
- for (int x = 0; x < mesh_Instances[meshIndex].GobjectIDs.Length; x++)
- {
- if (mesh_Instances[meshIndex].GobjectIDs[x] == GobjID)
- {
- //Set the Position & Scale to 0 & ID to -2
- mesh_Instances[meshIndex].GobjectIDs[x] = -2;
- mesh_Instances[meshIndex].positions[x] = Matrix4x4.TRS(new Vector3(0.1f, 0.1f, 0.1f), rot, new Vector3(0.1f, 0.1f, 0.1f));
- break;
- }
- }
- }
- }
- catch (Exception e)
- {
- //Debug.Log("Couldnt Remove Instanced Mesh (Ignored)" + e.Message);
- }
- }
- public void GetCamera()
- {
- try
- {
- camera1 = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
- //foundCamera = true;
- //Debug.Log("Found Camera!");
- }
- catch (Exception e)
- { }
- }
- }
- public class MeshGpuObject
- {
- public string prefabName = "";
- public GpuLODObject loD1;
- public Matrix4x4[] positions; //(Vector3 pos, Quaternion q, Vector3 s)
- public int[] GobjectIDs;
- public int objectsCounter = 0;
- public Renderer meshRender;
- }
- //Object Class
- using UnityEngine;
- using System;
- using System.Collections;
- public class GpuLODObject : MonoBehaviour
- {
- public Mesh LOD0;
- public Mesh LOD1;
- public Mesh LOD2;
- public Material[] LOD0_mats;
- public Material[] LOD1_mats;
- public Material[] LOD2_mats;
- public float lod0Dist = 10f;
- public float lod1Dist = 20f;
- public float lod2Dist = 30f;
- GpuBuildingz gpuBuilding;
- int GobjID;
- string prefabName1;
- bool isAdded;
- private float currTimer;
- private float addTimer = 0.5f;
- void Start()
- {
- isAdded = false;
- AddToGPUBuildingz();
- }
- void AddToGPUBuildingz()
- {
- //Dont update if CharSelect Scene
- if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == ServerSettings.ChrCreate)
- return;
- //Add To GPU Buildingz
- /*gpuBuilding = GameObject.Find("GpuBuildings").GetComponent<GpuBuildingz>();
- if (gpuBuilding != null)
- {
- if (this.gameObject.GetComponent<ObjectID>() != null)
- {
- GobjID = this.gameObject.GetComponent<ObjectID>().ObjID;
- prefabName1 = transform.name;
- gpuBuilding.AddMesh(this.gameObject, GobjID);
- }
- else
- {
- //Debug.Log("ObjectID script not getting added on client!!!!");
- }
- }*/
- }
- void OnDestroy()
- {
- if (gpuBuilding != null)
- gpuBuilding.RemoveMesh(prefabName1, GobjID);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement