This commit is contained in:
CortexCore
2023-10-20 19:31:12 +08:00
parent 5cd094ed9a
commit a160813262
1878 changed files with 630581 additions and 4485 deletions

View File

@@ -0,0 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MeshCombineStudio
{
public class CombineRuntime : MonoBehaviour
{
public MeshCombiner meshCombiner;
public bool useSearchConditions = true;
public GameObject[] gos;
void Start()
{
Combine();
}
void Combine()
{
meshCombiner.searchOptions.parentGOs = gos;
meshCombiner.CombineAll(useSearchConditions);
}
}
}

View File

@@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MeshCombineStudio
{
// 1) Add a MeshCombineStudio GameObject to the root of the scene and configure it
// -- make sure that Combine In Runtime is checked
// -- make sure that Combine On Start is unchecked
// -- make sure that On/Off With Tab is unchecked
// 2) Attach this script to a GameObject in the root scene (the scene that loads and unloads other scenes)
// 3) Drag the MeshCombineStudio GameObject into the MCS Game Object field in the RootSceneCombiner component
public class RootSceneCombiner : MonoBehaviour
{
public MeshCombiner MCSGameObject;
}
}

View File

@@ -0,0 +1,43 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace MeshCombineStudio
{
// This script needs to be attached to a GameObject in the loaded scene, which is the parent of the children to be combined.
// This can be done using the RequireComponentAttribute - for example, with NatureManufacture's WorldStreamer,
// this can be added to the ObjectsInSceneParent scripts automatically as follows:
// [RequireComponent(typeof(StreamedSceneCombiner))]
// public class ObjectsInSceneParent : MonoBehaviour {
// // ... rest of code here
//
public class StreamedSceneCombiner : MonoBehaviour
{
void Start()
{
// The rootScene is the scene that loads and unloads other scenes (usually as the player moves)
var rootScene = SceneManager.GetActiveScene();
foreach (var gameObject in rootScene.GetRootGameObjects())
{
// find the RootSceneCombiner in the rootScene (there should be only one!)
if (gameObject.TryGetComponent<RootSceneCombiner>(out var rootSceneCombiner) &&
rootSceneCombiner.MCSGameObject != null &&
rootSceneCombiner.MCSGameObject.TryGetComponent<MeshCombiner>(out var _))
{
// clone the MCS GameObject from the rootScene and make it a child of this GameObject's parent
var mcsGameObject = Instantiate(rootSceneCombiner.MCSGameObject, this.transform.parent);
var meshCombiner = mcsGameObject.GetComponent<MeshCombiner>();
// set the parentGOs to this GameObject - this will cause the MeshCombiner to combine the children of this GameObject
meshCombiner.searchOptions.parentGOs = new GameObject[] { this.gameObject };
// finally, combine the children of this GameObject
meshCombiner.CombineAll();
break;
}
}
}
}
}

View File

@@ -0,0 +1,89 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MeshCombineStudio
{
public class SelectOriginal : MonoBehaviour
{
public MeshCombiner meshCombiner;
public Camera mainCamera;
public Material matSelect;
Material oldMat;
Vector3 oldPos;
MeshRenderer oldMr;
// This Example script will deactive the combined mesh renderers of the cell and activate the originals.
// When deselecting it will switch back to enabled combined mesh renderers and disabled originals.
void Update()
{
RaycastHit hit;
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (Input.GetKeyDown(KeyCode.Tab)) Deselect(); // Deselect because Tab switches MCS ON/OFF
if (Physics.Raycast(ray, out hit))
{
Transform hitT = hit.transform;
var mr = hitT.GetComponent<MeshRenderer>();
if (mr != null)
{
if (mr == oldMr) return;
Deselect();
oldMr = mr;
if (meshCombiner.searchOptions.objectCenter == MeshCombiner.ObjectCenter.BoundsCenter) oldPos = oldMr.bounds.center; else oldPos = hitT.position;
oldMat = oldMr.sharedMaterial;
SelectOrDeselect(oldPos, oldMr, matSelect, true);
}
}
else
{
Deselect();
}
}
void Deselect()
{
if (oldMr != null) SelectOrDeselect(oldPos, oldMr, oldMat, false);
}
void SelectOrDeselect(Vector3 position, MeshRenderer mr, Material mat, bool select)
{
var octree = meshCombiner.octree;
if (octree == null) return;
ObjectOctree.MaxCell cell = octree.GetCell(position);
if (cell == null) return;
mr.sharedMaterial = mat;
if (meshCombiner.activeOriginal) return;
ObjectOctree.LODParent[] lodParents = cell.lodParents;
for (int i = 0; i < lodParents.Length; i++)
{
ObjectOctree.LODParent lodParent = lodParents[i];
if (lodParent == null) continue;
ObjectOctree.LODLevel[] lodLevels = lodParent.lodLevels;
for (int j = 0; j < lodLevels.Length; j++)
{
ObjectOctree.LODLevel lodLevel = lodLevels[j];
if (lodLevel == null) continue;
Methods.SetMeshRenderersActive(lodLevel.newMeshRenderers, !select);
Methods.SetCachedGOSActive(lodLevel.cachedGOs, select);
}
}
}
}
}