Firstborn/Assets/MeshBaker/scripts/MB3_MeshBakerCommon.cs
Schaken-Mods 959e80cf72 assets upload
assets upload description.
2023-03-28 12:16:30 -05:00

300 lines
11 KiB
C#

//----------------------------------------------
// MeshBaker
// Copyright © 2011-2012 Ian Deane
//----------------------------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Specialized;
using System;
using System.Collections.Generic;
using System.Text;
using DigitalOpus.MB.Core;
/// <summary>
/// Maps a list of source materials to a combined material. Included in MB2_TextureBakeResults
/// </summary>
/// <summary>
/// Abstract root of the mesh combining classes
/// </summary>
public abstract class MB3_MeshBakerCommon : MB3_MeshBakerRoot {
//todo should be list of <Renderer>
public List<GameObject> objsToMesh;
public abstract MB3_MeshCombiner meshCombiner{
get;
}
public bool useObjsToMeshFromTexBaker = true;
public bool clearBuffersAfterBake = true;
//t0do put this in the batch baker
public string bakeAssetsInPlaceFolderPath;
[HideInInspector] public GameObject resultPrefab;
#if UNITY_EDITOR
[ContextMenu("Create Mesh Baker Settings Asset")]
public void CreateMeshBakerSettingsAsset()
{
string newFilePath = UnityEditor.EditorUtility.SaveFilePanelInProject("New Mesh Baker Settings", "MeshBakerSettings", "asset", "Create a new Mesh Baker Settings Asset");
if (newFilePath != null)
{
MB3_MeshCombinerSettings asset = ScriptableObject.CreateInstance<MB3_MeshCombinerSettings>();
UnityEditor.AssetDatabase.CreateAsset(asset, newFilePath);
}
}
[ContextMenu("Copy settings from Shared Settings")]
public void CopyMySettingsToAssignedSettingsAsset()
{
if (meshCombiner.settingsHolder == null)
{
Debug.LogError("No Shared Settings Asset Assigned.");
return;
}
UnityEditor.Undo.RecordObject(this, "Undo copy settings");
_CopySettings(meshCombiner.settingsHolder.GetMeshBakerSettings(), meshCombiner);
Debug.Log("Copied settings from assigned Shared Settings to this Mesh Baker.");
UnityEditor.EditorUtility.SetDirty(this);
}
[ContextMenu("Copy settings to Shared Settings")]
public void CopyAssignedSettingsAssetToMySettings()
{
if (meshCombiner.settingsHolder == null)
{
Debug.LogError("No Shared Settings Asset Assigned.");
return;
}
if (meshCombiner.settingsHolder is UnityEngine.Object) UnityEditor.Undo.RecordObject((UnityEngine.Object) meshCombiner.settingsHolder, "Undo copy settings");
_CopySettings(meshCombiner, meshCombiner.settingsHolder.GetMeshBakerSettings());
Debug.Log("Copied settings from this Mesh Baker to the assigned Shared Settings asset.");
if (meshCombiner.settingsHolder is UnityEngine.Object) UnityEditor.EditorUtility.SetDirty((UnityEngine.Object)meshCombiner.settingsHolder);
}
void _CopySettings(MB_IMeshBakerSettings src, MB_IMeshBakerSettings targ)
{
targ.clearBuffersAfterBake = src.clearBuffersAfterBake;
targ.doBlendShapes = src.doBlendShapes;
targ.doCol = src.doCol;
targ.doNorm = src.doNorm;
targ.doTan = src.doTan;
targ.doUV = src.doUV;
targ.doUV3 = src.doUV3;
targ.doUV4 = src.doUV4;
targ.optimizeAfterBake = src.optimizeAfterBake;
targ.recenterVertsToBoundsCenter = src.recenterVertsToBoundsCenter;
targ.lightmapOption = src.lightmapOption;
targ.renderType = src.renderType;
targ.uv2UnwrappingParamsHardAngle = src.uv2UnwrappingParamsHardAngle;
targ.uv2UnwrappingParamsPackMargin = src.uv2UnwrappingParamsPackMargin;
}
#endif
public override MB2_TextureBakeResults textureBakeResults{
get {return meshCombiner.textureBakeResults; }
set {meshCombiner.textureBakeResults = value; }
}
public override List<GameObject> GetObjectsToCombine(){
if (useObjsToMeshFromTexBaker){
MB3_TextureBaker tb = gameObject.GetComponent<MB3_TextureBaker>();
if (tb == null) tb = gameObject.transform.parent.GetComponent<MB3_TextureBaker>();
if (tb != null) {
return tb.GetObjectsToCombine();
} else {
Debug.LogWarning("Use Objects To Mesh From Texture Baker was checked but no texture baker");
return new List<GameObject>();
}
} else {
if (objsToMesh == null) objsToMesh = new List<GameObject>();
return objsToMesh;
}
}
public void EnableDisableSourceObjectRenderers(bool show){
for (int i = 0; i < GetObjectsToCombine().Count; i++){
GameObject go = GetObjectsToCombine()[i];
if (go != null){
Renderer mr = MB_Utility.GetRenderer(go);
if (mr != null){
mr.enabled = show;
}
LODGroup lodG = mr.GetComponentInParent<LODGroup>();
if (lodG != null)
{
bool isOnlyInGroup = true;
LOD[] lods = lodG.GetLODs();
for (int j = 0; j < lods.Length; j++)
{
for (int k = 0; k < lods[j].renderers.Length; k++)
{
if (lods[j].renderers[k] != mr)
{
isOnlyInGroup = false;
break;
}
}
}
if (isOnlyInGroup)
{
lodG.enabled = show;
}
}
}
}
}
/// <summary>
/// Clears the meshs and mesh related data but does not destroy it.
/// </summary>
public virtual void ClearMesh(){
meshCombiner.ClearMesh();
}
/// <summary>
/// Clears and desroys the mesh. Clears mesh related data.
/// </summary>
public virtual void DestroyMesh(){
meshCombiner.DestroyMesh();
}
public virtual void DestroyMeshEditor(MB2_EditorMethodsInterface editorMethods){
meshCombiner.DestroyMeshEditor(editorMethods);
}
public virtual int GetNumObjectsInCombined(){
return meshCombiner.GetNumObjectsInCombined();
}
public virtual int GetNumVerticesFor(GameObject go){
return meshCombiner.GetNumVerticesFor(go);
}
/// <summary>
/// Gets the texture baker on this component or its parent if it exists
/// </summary>
/// <returns>The texture baker.</returns>
public MB3_TextureBaker GetTextureBaker(){
MB3_TextureBaker tb = GetComponent<MB3_TextureBaker>();
if (tb != null) return tb;
if (transform.parent != null) return transform.parent.GetComponent<MB3_TextureBaker>();
return null;
}
/// <summary>
/// Adds and deletes objects from the combined mesh. gos and deleteGOs can be null.
/// You need to call Apply or ApplyAll to see the changes.
/// objects in gos must not include objects already in the combined mesh.
/// objects in gos and deleteGOs must be the game objects with a Renderer component
/// This method is slow, so should be called as infrequently as possible.
/// </summary>
/// <returns>
/// The first generated combined mesh
/// </returns>
/// <param name='gos'>
/// gos. Array of objects to add to the combined mesh. Array can be null. Must not include objects
/// already in the combined mesh. Array must contain game objects with a render component.
/// </param>
/// <param name='deleteGOs'>
/// deleteGOs. Array of objects to delete from the combined mesh. Array can be null.
/// </param>
/// <param name='disableRendererInSource'>
/// Disable renderer component on objects in gos after they have been added to the combined mesh.
/// </param>
/// <param name='fixOutOfBoundUVs'>
/// Whether to fix out of bounds UVs in meshes as they are being added. This paramater should be set to the same as the combined material.
/// </param>
/// </summary>
public abstract bool AddDeleteGameObjects(GameObject[] gos, GameObject[] deleteGOs, bool disableRendererInSource = true);
/// <summary>
/// This is the best version to use for deleting game objects since the source GameObjects may have been destroyed
/// Internaly Mesh Baker only stores the instanceID for Game Objects, so objects can be removed after they have been destroyed
/// </summary>
public abstract bool AddDeleteGameObjectsByID(GameObject[] gos, int[] deleteGOinstanceIDs, bool disableRendererInSource = true);
/// <summary>
/// Apply changes to the mesh. All channels set in this instance will be set in the combined mesh.
/// </summary>
public virtual void Apply(MB3_MeshCombiner.GenerateUV2Delegate uv2GenerationMethod=null){
meshCombiner.name = name + "-mesh";
meshCombiner.Apply(uv2GenerationMethod);
}
/// <summary>
/// Applys the changes to flagged properties of the mesh. This method is slow, and should only be called once per frame. The speed is directly proportional to the number of flags that are true. Only apply necessary properties.
/// </summary>
public virtual void Apply(bool triangles,
bool vertices,
bool normals,
bool tangents,
bool uvs,
bool uv2,
bool uv3,
bool uv4,
bool colors,
bool bones=false,
bool blendShapesFlag=false,
MB3_MeshCombiner.GenerateUV2Delegate uv2GenerationMethod=null){
meshCombiner.name = name + "-mesh";
meshCombiner.Apply(triangles,vertices,normals,tangents,uvs,uv2,uv3,uv4,colors,bones, blendShapesFlag,uv2GenerationMethod);
}
public virtual bool CombinedMeshContains(GameObject go){
return meshCombiner.CombinedMeshContains(go);
}
public virtual void UpdateGameObjects(GameObject[] gos, bool recalcBounds = true, bool updateVertices = true, bool updateNormals = true, bool updateTangents = true,
bool updateUV = false, bool updateUV1 = false, bool updateUV2 = false,
bool updateColors = false, bool updateSkinningInfo = false){
meshCombiner.name = name + "-mesh";
meshCombiner.UpdateGameObjects(gos,recalcBounds, updateVertices, updateNormals, updateTangents, updateUV, updateUV1, updateUV2, updateColors, updateSkinningInfo);
}
public virtual void UpdateSkinnedMeshApproximateBounds(){
if (_ValidateForUpdateSkinnedMeshBounds()){
meshCombiner.UpdateSkinnedMeshApproximateBounds();
}
}
public virtual void UpdateSkinnedMeshApproximateBoundsFromBones(){
if (_ValidateForUpdateSkinnedMeshBounds()){
meshCombiner.UpdateSkinnedMeshApproximateBoundsFromBones();
}
}
public virtual void UpdateSkinnedMeshApproximateBoundsFromBounds(){
if (_ValidateForUpdateSkinnedMeshBounds()){
meshCombiner.UpdateSkinnedMeshApproximateBoundsFromBounds();
}
}
protected virtual bool _ValidateForUpdateSkinnedMeshBounds(){
if (meshCombiner.outputOption == MB2_OutputOptions.bakeMeshAssetsInPlace){
Debug.LogWarning("Can't UpdateSkinnedMeshApproximateBounds when output type is bakeMeshAssetsInPlace");
return false;
}
if (meshCombiner.resultSceneObject == null){
Debug.LogWarning("Result Scene Object does not exist. No point in calling UpdateSkinnedMeshApproximateBounds.");
return false;
}
SkinnedMeshRenderer smr = meshCombiner.resultSceneObject.GetComponentInChildren<SkinnedMeshRenderer>();
if (smr == null){
Debug.LogWarning("No SkinnedMeshRenderer on result scene object.");
return false;
}
return true;
}
}