void _setMBValues(MB3_MeshCombinerSingle targ)
        {
            targ.validationLevel    = _validationLevel;
            targ.textureBakeResults = textureBakeResults;

            // Even though the MultiMeshBaker supports baking into prefabs, the sub-combiners don't do bake into prefab when
            // this is happening. They do bake into sceneObject, then the MultiMeshBaker takes their output and combines it
            // into a prefab.
            targ.outputOption = MB2_OutputOptions.bakeIntoSceneObject;
            if (settingsHolder != null)
            {
                targ.settingsHolder = settingsHolder;
            }
            else
            {
                targ.renderType                    = renderType;
                targ.lightmapOption                = lightmapOption;
                targ.doNorm                        = doNorm;
                targ.doTan                         = doTan;
                targ.doCol                         = doCol;
                targ.doUV                          = doUV;
                targ.doUV3                         = doUV3;
                targ.doUV4                         = doUV4;
                targ.doBlendShapes                 = doBlendShapes;
                targ.optimizeAfterBake             = optimizeAfterBake;
                targ.recenterVertsToBoundsCenter   = recenterVertsToBoundsCenter;
                targ.uv2UnwrappingParamsHardAngle  = uv2UnwrappingParamsHardAngle;
                targ.uv2UnwrappingParamsPackMargin = uv2UnwrappingParamsPackMargin;
            }
        }
        public static bool BakeMeshesInPlace(MB3_MeshCombinerSingle mom, List <GameObject> objsToMesh, string saveFolder, bool clearBuffersAfterBake, ProgressUpdateDelegate updateProgressBar)
        {
            if (MB3_MeshCombiner.EVAL_VERSION)
            {
                return(false);
            }
            if (saveFolder.Length < 6)
            {
                Debug.LogError("Please select a folder for meshes.");
                return(false);
            }
            if (!Directory.Exists(Application.dataPath + saveFolder.Substring(6)))
            {
                Debug.Log((Application.dataPath + saveFolder.Substring(6)));
                Debug.Log(Path.GetFullPath(Application.dataPath + saveFolder.Substring(6)));
                Debug.LogError("The selected Folder For Meshes does not exist or is not inside the projects Assets folder. Please 'Choose Folder For Bake In Place Meshes' that is inside the project's assets folder.");
                return(false);
            }

            MB3_EditorMethods editorMethods = new MB3_EditorMethods();

            mom.DestroyMeshEditor(editorMethods);

            MB_RenderType originalRenderType = mom.renderType;
            bool          success            = false;

            string[] objNames = GenerateNames(objsToMesh);
            for (int i = 0; i < objsToMesh.Count; i++)
            {
                if (objsToMesh[i] == null)
                {
                    Debug.LogError("The " + i + "th object on the list of objects to combine is 'None'. Use Command-Delete on Mac OS X; Delete or Shift-Delete on Windows to remove this one element.");
                    return(false);
                }

                Mesh m = new Mesh();
                success = BakeOneMesh(mom, m, objsToMesh[i]);
                if (success)
                {
                    string newMeshFilePath = saveFolder + "/" + objNames[i];
                    Debug.Log("Creating mesh asset at " + newMeshFilePath + " for mesh " + m + " numVerts " + m.vertexCount);
                    AssetDatabase.CreateAsset(mom.GetMesh(), newMeshFilePath);
                }
                if (updateProgressBar != null)
                {
                    updateProgressBar("Created mesh saving mesh on " + objsToMesh[i].name + " to asset " + objNames[i], .6f);
                }
            }
            mom.renderType = originalRenderType;
            MB_Utility.Destroy(mom.resultSceneObject);
            if (clearBuffersAfterBake)
            {
                mom.ClearBuffers();
            }
            return(success);
        }
Exemplo n.º 3
0
 internal MBBlendShape[] GetBlendShapes(Mesh m, int gameObjectID, GameObject gameObject)
 {
     if (MBVersion.GetMajorVersion() > 5 ||
         (MBVersion.GetMajorVersion() == 5 && MBVersion.GetMinorVersion() >= 3))
     {
         MeshChannels mc;
         if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
         {
             mc = new MeshChannels();
             meshID2MeshChannels.Add(m.GetInstanceID(), mc);
         }
         if (mc.blendShapes == null)
         {
             MBBlendShape[] shapes   = new MBBlendShape[m.blendShapeCount];
             int            arrayLen = m.vertexCount;
             for (int i = 0; i < shapes.Length; i++)
             {
                 MBBlendShape shape = shapes[i] = new MBBlendShape();
                 shape.frames        = new MBBlendShapeFrame[MBVersion.GetBlendShapeFrameCount(m, i)];
                 shape.name          = m.GetBlendShapeName(i);
                 shape.indexInSource = i;
                 shape.gameObjectID  = gameObjectID;
                 shape.gameObject    = gameObject;
                 for (int j = 0; j < shape.frames.Length; j++)
                 {
                     MBBlendShapeFrame frame = shape.frames[j] = new MBBlendShapeFrame();
                     frame.frameWeight = MBVersion.GetBlendShapeFrameWeight(m, i, j);
                     frame.vertices    = new Vector3[arrayLen];
                     frame.normals     = new Vector3[arrayLen];
                     frame.tangents    = new Vector3[arrayLen];
                     MBVersion.GetBlendShapeFrameVertices(m, i, j, frame.vertices, frame.normals, frame.tangents);
                 }
             }
             mc.blendShapes = shapes;
             return(mc.blendShapes);
         }
         else
         { //copy cached blend shapes assigning a different gameObjectID
             MBBlendShape[] shapes = new MBBlendShape[mc.blendShapes.Length];
             for (int i = 0; i < shapes.Length; i++)
             {
                 shapes[i]               = new MBBlendShape();
                 shapes[i].name          = mc.blendShapes[i].name;
                 shapes[i].indexInSource = mc.blendShapes[i].indexInSource;
                 shapes[i].frames        = mc.blendShapes[i].frames;
                 shapes[i].gameObjectID  = gameObjectID;
                 shapes[i].gameObject    = gameObject;
             }
             return(shapes);
         }
     }
     else
     {
         return(new MBBlendShape[0]);
     }
 }
            public bool isDirty = false;     //needs apply

            public CombinedMesh(int maxNumVertsInMesh, GameObject resultSceneObject, MB2_LogLevel ll)
            {
                combinedMesh = new MB3_MeshCombinerSingle();
                combinedMesh.resultSceneObject = resultSceneObject;
                combinedMesh.LOG_LEVEL         = ll;
                extraSpace             = maxNumVertsInMesh;
                numVertsInListToDelete = 0;
                numVertsInListToAdd    = 0;
                gosToAdd    = new List <GameObject>();
                gosToDelete = new List <int>();
                gosToUpdate = new List <GameObject>();
            }
 void _setMBValues(MB3_MeshCombinerSingle targ)
 {
     targ.validationLevel    = _validationLevel;
     targ.renderType         = renderType;
     targ.outputOption       = MB2_OutputOptions.bakeIntoSceneObject;
     targ.lightmapOption     = lightmapOption;
     targ.textureBakeResults = textureBakeResults;
     targ.doNorm             = doNorm;
     targ.doTan = doTan;
     targ.doCol = doCol;
     targ.doUV  = doUV;
     targ.doUV1 = doUV1;
 }
            internal int[] GetTriangles(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.triangles == null)
                {
                    mc.triangles = m.triangles;
                }
                return(mc.triangles);
            }
            internal Vector2[] GetUv4(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.uv4 == null)
                {
                    mc.uv4 = MBVersion.GetMeshUV3orUV4(m, false, this.mc.LOG_LEVEL);
                }
                return(mc.uv4);
            }
            internal Vector3[] GetVertices(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.vertices == null)
                {
                    mc.vertices = m.vertices;
                }
                return(mc.vertices);
            }
            internal Vector2[] GetUv2(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.uv2 == null)
                {
                    mc.uv2 = _getMeshUV2s(m);
                }
                return(mc.uv2);
            }
            internal Color[] GetColors(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.colors == null)
                {
                    mc.colors = _getMeshColors(m);
                }
                return(mc.colors);
            }
            internal Vector4[] GetTangents(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.tangents == null)
                {
                    mc.tangents = _getMeshTangents(m);
                }
                return(mc.tangents);
            }
            internal Vector3[] GetNormals(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.normals == null)
                {
                    mc.normals = _getMeshNormals(m);
                }
                return(mc.normals);
            }
Exemplo n.º 13
0
 private void _setMBValues(MB3_MeshCombinerSingle targ)
 {
     targ.validationLevel    = this._validationLevel;
     targ.renderType         = this.renderType;
     targ.outputOption       = MB2_OutputOptions.bakeIntoSceneObject;
     targ.lightmapOption     = this.lightmapOption;
     targ.textureBakeResults = this.textureBakeResults;
     targ.doNorm             = this.doNorm;
     targ.doTan = this.doTan;
     targ.doCol = this.doCol;
     targ.doUV  = this.doUV;
     targ.doUV3 = this.doUV3;
     targ.doUV4 = this.doUV4;
     targ.uv2UnwrappingParamsHardAngle  = this.uv2UnwrappingParamsHardAngle;
     targ.uv2UnwrappingParamsPackMargin = this.uv2UnwrappingParamsPackMargin;
 }
            internal Vector2[] GetUv0Modified(Mesh m)
            {
                MeshChannels mc;

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.uv0modified == null)
                {
                    //todo
                    mc.uv0modified = null;
                }
                return(mc.uv0modified);
            }
            internal Matrix4x4[] GetBindposes(Renderer r)
            {
                MeshChannels mc;
                Mesh         m = MB_Utility.GetMesh(r.gameObject);

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.bindPoses == null)
                {
                    mc.bindPoses = _getBindPoses(r);
                }
                return(mc.bindPoses);
            }
            internal BoneWeight[] GetBoneWeights(Renderer r, int numVertsInMeshBeingAdded)
            {
                MeshChannels mc;
                Mesh         m = MB_Utility.GetMesh(r.gameObject);

                if (!meshID2MeshChannels.TryGetValue(m.GetInstanceID(), out mc))
                {
                    mc = new MeshChannels();
                    meshID2MeshChannels.Add(m.GetInstanceID(), mc);
                }
                if (mc.boneWeights == null)
                {
                    mc.boneWeights = _getBoneWeights(r, numVertsInMeshBeingAdded);
                }
                return(mc.boneWeights);
            }
Exemplo n.º 17
0
        static public Mesh BakeOneMesh(MB3_MeshCombinerSingle mom, string newMeshFilePath, GameObject objToBake)
        {
            Mesh outMesh = null;

            if (objToBake == null)
            {
                Debug.LogError("An object on the list of objects to combine is 'None'. Use Command-Delete on Mac OS X; Delete or Shift-Delete on Windows to remove this one element.");
                return(null);
            }

            MB3_EditorMethods editorMethods = new MB3_EditorMethods();

            GameObject[] objs = new GameObject[] { objToBake };
            Renderer     r    = MB_Utility.GetRenderer(objToBake);

            if (r is SkinnedMeshRenderer)
            {
                mom.renderType = MB_RenderType.skinnedMeshRenderer;
            }
            else if (r is MeshRenderer)
            {
                mom.renderType = MB_RenderType.meshRenderer;
            }
            else
            {
                Debug.LogError("Unsupported Renderer type on object. Must be SkinnedMesh or MeshFilter.");
                return(null);
            }
            if (newMeshFilePath == null && newMeshFilePath.Length != 0)              //todo check directory exists
            {
                Debug.LogError("File path was not in assets folder.");
                return(null);
            }
            if (mom.AddDeleteGameObjects(objs, null, false))
            {
                mom.Apply(MB3_MeshBakerEditorFunctions.UnwrapUV2);
                Mesh mf = MB_Utility.GetMesh(objToBake);
                if (mf != null)
                {
                    Debug.Log("Creating mesh for " + objToBake.name + " with adjusted UVs at: " + newMeshFilePath);
                    AssetDatabase.CreateAsset(mom.GetMesh(), newMeshFilePath);
                    outMesh = (Mesh)AssetDatabase.LoadAssetAtPath(newMeshFilePath, typeof(Mesh));
                }
            }
            mom.DestroyMeshEditor(editorMethods);
            return(outMesh);
        }
Exemplo n.º 18
0
        static public bool BakeOneMesh(MB3_MeshCombinerSingle mom, Mesh targMesh, GameObject objToBake)
        {
            if (objToBake == null)
            {
                Debug.LogError("An object on the list of objects to combine is 'None'. Use Command-Delete on Mac OS X; Delete or Shift-Delete on Windows to remove this one element.");
                return(false);
            }
            if (targMesh == null)
            {
                Debug.LogError("No mesh was provided.");
                return(false);
            }

            mom.SetMesh(targMesh);
            mom.ClearMesh();
            GameObject[] objs = new GameObject[] { objToBake };
            Renderer     r    = MB_Utility.GetRenderer(objToBake);

            if (r is SkinnedMeshRenderer)
            {
                mom.renderType = MB_RenderType.skinnedMeshRenderer;
            }
            else if (r is MeshRenderer)
            {
                mom.renderType = MB_RenderType.meshRenderer;
            }
            else
            {
                Debug.LogError("Unsupported Renderer type on object. Must be SkinnedMesh or MeshFilter.");
                return(false);
            }
            if (mom.AddDeleteGameObjects(objs, null, false))
            {
                mom.Apply(MB3_MeshBakerEditorFunctions.UnwrapUV2);
                Mesh mf = MB_Utility.GetMesh(objToBake);
                if (mf == null)
                {
                    Debug.LogError("Failed to create mesh for " + objToBake.name);
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 19
0
        public static Mesh BakeMeshesInPlace(MB3_MeshCombinerSingle mom, List <GameObject> objsToMesh, string saveFolder, ProgressUpdateDelegate updateProgressBar)
        {
            if (MB3_MeshCombiner.EVAL_VERSION)
            {
                return(null);
            }
            if (saveFolder.Length < 6)
            {
                Debug.LogError("Please select a folder for meshes.");
                return(null);
            }
            if (!Directory.Exists(Application.dataPath + saveFolder.Substring(6)))
            {
                Debug.Log((Application.dataPath + saveFolder.Substring(6)));
                Debug.Log(Path.GetFullPath(Application.dataPath + saveFolder.Substring(6)));
                Debug.LogError("The selected Folder For Meshes does not exist or is not inside the projects Assets folder. Please 'Choose Folder For Bake In Place Meshes' that is inside the project's assets folder.");
                return(null);
            }

            MB3_EditorMethods editorMethods = new MB3_EditorMethods();

            mom.DestroyMeshEditor(editorMethods);

            MB_RenderType originalRenderType = mom.renderType;
            Mesh          outMesh            = null;

            for (int i = 0; i < objsToMesh.Count; i++)
            {
                if (objsToMesh[i] == null)
                {
                    Debug.LogError("The " + i + "th object on the list of objects to combine is 'None'. Use Command-Delete on Mac OS X; Delete or Shift-Delete on Windows to remove this one element.");
                    return(null);
                }
                string[] objNames = GenerateNames(objsToMesh);
                outMesh = BakeOneMesh(mom, saveFolder + "/" + objNames[i], objsToMesh[i]);
                if (updateProgressBar != null)
                {
                    updateProgressBar("Created mesh saving mesh on " + objsToMesh[i].name + " to asset " + objNames[i], .6f);
                }
            }
            mom.renderType = originalRenderType;
            return(outMesh);
        }
Exemplo n.º 20
0
 void _setMBValues(MB3_MeshCombinerSingle targ)
 {
     targ.validationLevel    = _validationLevel;
     targ.renderType         = renderType;
     targ.outputOption       = MB2_OutputOptions.bakeIntoSceneObject;
     targ.lightmapOption     = lightmapOption;
     targ.textureBakeResults = textureBakeResults;
     targ.doNorm             = doNorm;
     targ.doTan                         = doTan;
     targ.doCol                         = doCol;
     targ.doUV                          = doUV;
     targ.doUV3                         = doUV3;
     targ.doUV4                         = doUV4;
     targ.doBlendShapes                 = doBlendShapes;
     targ.optimizeAfterBake             = optimizeAfterBake;
     targ.recenterVertsToBoundsCenter   = recenterVertsToBoundsCenter;
     targ.uv2UnwrappingParamsHardAngle  = uv2UnwrappingParamsHardAngle;
     targ.uv2UnwrappingParamsPackMargin = uv2UnwrappingParamsPackMargin;
 }
 public MB3_MeshCombinerSimpleBones(MB3_MeshCombinerSingle cm)
 {
     combiner = cm;
 }
 internal MBBlendShape[] GetBlendShapes(Mesh m, int gameObjectID, GameObject gameObject)
 {
     return(MB3_MeshCombinerSingle.GetBlendShapes(m, gameObjectID, gameObject, meshID2MeshChannels));
 }
            public static void AddBonesToNewBonesArrayAndAdjustBWIndexes(MB3_MeshCombinerSingle combiner, MB_DynamicGameObject dgo, Renderer r, int vertsIdx,
                                                                         Transform[] nbones, BoneWeight[] nboneWeights, MeshChannelsCache meshChannelCache)
            {
                Transform[]  dgoBones                  = dgo._tmpSMR_CachedBones;
                Matrix4x4[]  dgoBindPoses              = dgo._tmpSMR_CachedBindposes;
                BoneWeight[] dgoBoneWeights            = dgo._tmpSMR_CachedBoneWeights;
                int[]        srcIndex2combinedIndexMap = new int[dgoBones.Length];
                for (int j = 0; j < dgo._tmpSMRIndexesOfSourceBonesUsed.Length; j++)
                {
                    int dgoBoneIdx = dgo._tmpSMRIndexesOfSourceBonesUsed[j];

                    for (int k = 0; k < nbones.Length; k++)
                    {
                        if (dgoBones[dgoBoneIdx] == nbones[k])
                        {
                            if (dgoBindPoses[dgoBoneIdx] == combiner.bindPoses[k])
                            {
                                srcIndex2combinedIndexMap[dgoBoneIdx] = k;
                                break;
                            }
                        }
                    }
                }

                //remap the bone weights for this dgo
                //build a list of usedBones, can't trust dgoBones because it contains all bones in the rig
                for (int j = 0; j < dgoBoneWeights.Length; j++)
                {
                    int newVertIdx = vertsIdx + j;
                    nboneWeights[newVertIdx].boneIndex0 = srcIndex2combinedIndexMap[dgoBoneWeights[j].boneIndex0];
                    nboneWeights[newVertIdx].boneIndex1 = srcIndex2combinedIndexMap[dgoBoneWeights[j].boneIndex1];
                    nboneWeights[newVertIdx].boneIndex2 = srcIndex2combinedIndexMap[dgoBoneWeights[j].boneIndex2];
                    nboneWeights[newVertIdx].boneIndex3 = srcIndex2combinedIndexMap[dgoBoneWeights[j].boneIndex3];
                    nboneWeights[newVertIdx].weight0    = dgoBoneWeights[j].weight0;
                    nboneWeights[newVertIdx].weight1    = dgoBoneWeights[j].weight1;
                    nboneWeights[newVertIdx].weight2    = dgoBoneWeights[j].weight2;
                    nboneWeights[newVertIdx].weight3    = dgoBoneWeights[j].weight3;
                }

                // repurposing the _tmpIndexesOfSourceBonesUsed since
                //we don't need it anymore and this saves a memory allocation . remap the indexes that point to source bones to combined bones.
                for (int j = 0; j < dgo._tmpSMRIndexesOfSourceBonesUsed.Length; j++)
                {
                    dgo._tmpSMRIndexesOfSourceBonesUsed[j] = srcIndex2combinedIndexMap[dgo._tmpSMRIndexesOfSourceBonesUsed[j]];
                }
                dgo.indexesOfBonesUsed = dgo._tmpSMRIndexesOfSourceBonesUsed;
                dgo._tmpSMRIndexesOfSourceBonesUsed = null;
                dgo._tmpSMR_CachedBones             = null;
                dgo._tmpSMR_CachedBindposes         = null;
                dgo._tmpSMR_CachedBoneWeights       = null;

                //check original bones and bindPoses

                /*
                 * for (int j = 0; j < dgo.indexesOfBonesUsed.Length; j++) {
                 *  Transform bone = bones[dgo.indexesOfBonesUsed[j]];
                 *  Matrix4x4 bindpose = bindPoses[dgo.indexesOfBonesUsed[j]];
                 *  bool found = false;
                 *  for (int k = 0; k < dgo._originalBones.Length; k++) {
                 *      if (dgo._originalBones[k] == bone && dgo._originalBindPoses[k] == bindpose) {
                 *          found = true;
                 *      }
                 *  }
                 *  if (!found) Debug.LogError("A Mismatch between original bones and bones array. " + dgo.name);
                 * }
                 */
            }
 public MeshChannelsCache(MB3_MeshCombinerSingle mcs)
 {
     mc = mcs;
 }