/// </Test> void Start() { posToCopy = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * 3); quatToCopy = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * 4); scaleToCopy = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * 3); if (standardMaterial == null) { standardMaterial = new Material(Shader.Find("Standard")); } rootNodeID = GlobalOsgInterface.requestNodeFile(Marshal.StringToHGlobalAnsi(filename)); if (rootNodeID > 0) { GameObject rootNodeObj = new GameObject("Root node"); rootNodeObj.transform.SetParent(transform, false); transform.localRotation = GlobalOsgInterface.sceneRotation; GlobalOsgInterface.beginReadNode(rootNodeID); IterateNodeData(rootNodeObj, 0); GlobalOsgInterface.endReadNode(eraseLowLevelGeometry); // Erase low-level gemetry and texture data } }
void Update() { // Find and add/remove paged node children int addedCount = 0, removedCount = 0; System.IntPtr sys = GlobalOsgInterface.takeNewlyAddedNodes(rootNodeID, ref addedCount); System.IntPtr sys2 = GlobalOsgInterface.takeNewlyRemovedNodes(rootNodeID, ref removedCount); GameObject parentObj = null; if (addedCount > 0) { int[] addedNodes = new int[addedCount * 2]; Marshal.Copy(sys, addedNodes, 0, addedCount * 2); for (int i = 0; i < addedNodes.Length; i += 2) { bool ok = GlobalOsgInterface.beginReadPagedNode(addedNodes[i], addedNodes[i + 1]); if (ok) { GameObject childObj = new GameObject("PagedLevel_" + addedNodes[i + 1]); if (pagedNodeMap.TryGetValue(addedNodes[i], out parentObj)) { childObj.transform.SetParent(parentObj.transform, false); } IterateNodeData(childObj, 0); GlobalOsgInterface.endReadNode(eraseLowLevelGeometry); PagedChildID pagedID = childObj.AddComponent <PagedChildID>(); pagedID.parentID = addedNodes[i]; pagedID.location = addedNodes[i + 1]; } else { Debug.LogWarning("[MeshLoader] Failed to read paged node data " + addedNodes[i] + ":" + addedNodes[i + 1]); } } } if (removedCount > 0) { int[] removedNodes = new int[removedCount * 2]; Marshal.Copy(sys2, removedNodes, 0, removedCount * 2); for (int i = 0; i < removedNodes.Length; i += 2) { if (pagedNodeMap.TryGetValue(removedNodes[i], out parentObj)) { foreach (Transform child in parentObj.transform) { PagedChildID pagedID = child.GetComponent <PagedChildID>(); if (pagedID != null && pagedID.location == removedNodes[i + 1]) { // Clear child pagedNodeMap DestroyChildPagedNodes(child, ref pagedNodeMap); // Remove me MeshFilter[] filters = child.GetComponentsInChildren <MeshFilter>(); if (filters.Length > 0) { foreach (MeshFilter mf in filters) { Destroy(mf.sharedMesh); } } Destroy(child.gameObject); } } } else { Debug.LogWarning("[MeshLoader] Failed to remove unregistered paged data: " + removedNodes[i] + ":" + removedNodes[i + 1]); } } } // Traverse all paged nodes and update their LOD stats List <int> nodeToRemove = new List <int>(); foreach (KeyValuePair <int, GameObject> entry in pagedNodeMap) { int locationCount = 0; GameObject pagedNode = entry.Value; System.IntPtr sysState = GlobalOsgInterface.updatePagedNodeState(entry.Key, ref locationCount); if (sysState == System.IntPtr.Zero || locationCount == 0) { continue; } else if (locationCount == -2) { nodeToRemove.Add(entry.Key); continue; } else if (locationCount == -1) { Debug.LogWarning("Unable to get paged information from " + pagedNode.name); continue; } int[] locArray = new int[locationCount]; Marshal.Copy(sysState, locArray, 0, locationCount); List <int> locations = new List <int>(locArray); foreach (Transform child in pagedNode.transform) { PagedChildID idObj = child.GetComponent <PagedChildID>(); if (idObj != null && locations.Contains(idObj.location)) { child.gameObject.SetActive(true); } else { child.gameObject.SetActive(false); } } } foreach (int key in nodeToRemove) { pagedNodeMap.Remove(key); } }