Esempio n. 1
0
        public override void UpdateRenderer(ObiActor actor)
        {
            base.UpdateRenderer(actor);

            if (Application.isPlaying && clothMesh != null && cloth.isLoaded)
            {
                // since the skinned mesh renderer won't accept a mesh with no bone weights, we need to render the mesh ourselves:
                skin.sharedMesh = null;

                skin.GetSharedMaterials(rendererMaterials);

                // Render all submeshes and materials:
                Matrix4x4 matrix       = skin.rootBone.transform.localToWorldMatrix;
                int       subMeshCount = clothMesh.subMeshCount;
                int       drawcalls    = Mathf.Max(subMeshCount, rendererMaterials.Count);

                for (int j = 0; j < drawcalls; ++j)
                {
                    if (j < rendererMaterials.Count)
                    {
                        Graphics.DrawMesh(clothMesh, matrix, rendererMaterials[j], gameObject.layer, null, Mathf.Min(j, subMeshCount - 1), null, skin.shadowCastingMode, skin.receiveShadows);
                    }
                }
            }
        }
        public override void UpdateRenderer(ObiActor actor)
        {
            base.UpdateRenderer(actor);

            if (Application.isPlaying && clothMesh != null && cloth.isLoaded)
            {
                // since the skinned mesh renderer won't accept a mesh with no bone weights, we need to render the mesh ourselves:
                skin.sharedMesh = null;

                skin.GetSharedMaterials(rendererMaterials);
                foreach (Material m in rendererMaterials)
                {
                    Graphics.DrawMesh(clothMesh, skin.rootBone.transform.localToWorldMatrix, m, gameObject.layer);
                }
            }
        }
        private static void BuildNewMesh(SkinnedMeshRenderer skinnedMeshRenderer, Dictionary <int, int> newIndexLookUpDict,
                                         Dictionary <int, int[]> subMeshes, BlendShapeLogic blendShapeLabel)
        {
            // get original mesh data
            var materialList = new List <Material>();

            skinnedMeshRenderer.GetSharedMaterials(materialList);
            var mesh            = skinnedMeshRenderer.sharedMesh;
            var meshVertices    = mesh.vertices;
            var meshNormals     = mesh.normals;
            var meshTangents    = mesh.tangents;
            var meshColors      = mesh.colors;
            var meshBoneWeights = mesh.boneWeights;
            var meshUVs         = mesh.uv;

            // build new mesh
            var materialListNew = new List <Material>();
            var newMesh         = new Mesh();

            if (mesh.vertexCount > ushort.MaxValue)
            {
#if UNITY_2017_3_OR_NEWER
                Debug.LogFormat("exceed 65535 vertices: {0}", mesh.vertexCount);
                newMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
#else
                throw new NotImplementedException(String.Format("exceed 65535 vertices: {0}", integrator.Positions.Count.ToString()));
#endif
            }

            var newDataLength  = newIndexLookUpDict.Count;
            var newIndexLookUp = newIndexLookUpDict.Keys.ToArray();

            newMesh.vertices = newIndexLookUp.Select(x => meshVertices[x]).ToArray();
            if (meshNormals.Length > 0)
            {
                newMesh.normals = newIndexLookUp.Select(x => meshNormals[x]).ToArray();
            }
            if (meshTangents.Length > 0)
            {
                newMesh.tangents = newIndexLookUp.Select(x => meshTangents[x]).ToArray();
            }
            if (meshColors.Length > 0)
            {
                newMesh.colors = newIndexLookUp.Select(x => meshColors[x]).ToArray();
            }
            if (meshBoneWeights.Length > 0)
            {
                newMesh.boneWeights = newIndexLookUp.Select(x => meshBoneWeights[x]).ToArray();
            }
            if (meshUVs.Length > 0)
            {
                newMesh.uv = newIndexLookUp.Select(x => meshUVs[x]).ToArray();
            }
            newMesh.bindposes = mesh.bindposes;

            // add BlendShape data
            if (blendShapeLabel == BlendShapeLogic.WithBlendShape)
            {
                for (int i = 0; i < mesh.blendShapeCount; i++)
                {
                    // get original BlendShape data
                    var srcVertices = new Vector3[mesh.vertexCount];
                    var srcNormals  = new Vector3[mesh.vertexCount];
                    var srcTangents = new Vector3[mesh.vertexCount];
                    mesh.GetBlendShapeFrameVertices(i, 0, srcVertices, srcNormals, srcTangents);

                    // declare the size for the destination array
                    var dstVertices = new Vector3[newDataLength];
                    var dstNormals  = new Vector3[newDataLength];
                    var dstTangents = new Vector3[newDataLength];

                    dstVertices = newIndexLookUp.Select(x => srcVertices[x]).ToArray();
                    dstNormals  = newIndexLookUp.Select(x => srcNormals[x]).ToArray();
                    dstTangents = newIndexLookUp.Select(x => srcTangents[x]).ToArray();
                    newMesh.AddBlendShapeFrame(mesh.GetBlendShapeName(i), mesh.GetBlendShapeFrameWeight(i, 0),
                                               dstVertices, dstNormals, dstTangents);
                }
            }

            newMesh.subMeshCount = subMeshes.Count;
            var cosMaterialIndex = subMeshes.Keys.ToArray();

            // build material list
            for (int i = 0; i < subMeshes.Count; i++)
            {
                newMesh.SetTriangles(subMeshes[cosMaterialIndex[i]], i);
                materialListNew.Add(materialList[cosMaterialIndex[i]]);
            }
            skinnedMeshRenderer.sharedMaterials = materialListNew.ToArray();
            skinnedMeshRenderer.sharedMesh      = newMesh;
        }
Esempio n. 4
0
        private static void BuildNewMesh(SkinnedMeshRenderer skinnedMeshRenderer, Dictionary <int, int> newIndexLookUpDict,
                                         Dictionary <int, int[]> subMeshes, BlendShapeLogic blendShapeLabel)
        {
            // get original mesh data
            var materialList = new List <Material>();

            skinnedMeshRenderer.GetSharedMaterials(materialList);
            var mesh            = skinnedMeshRenderer.sharedMesh;
            var meshVertices    = mesh.vertices;
            var meshNormals     = mesh.normals;
            var meshTangents    = mesh.tangents;
            var meshColors      = mesh.colors;
            var meshBoneWeights = mesh.boneWeights;
            var meshUVs         = mesh.uv;

            // build new mesh
            var materialListNew = new List <Material>();
            var newMesh         = new Mesh();

            if (mesh.vertexCount > ushort.MaxValue)
            {
#if UNITY_2017_3_OR_NEWER
                Debug.LogFormat("exceed 65535 vertices: {0}", mesh.vertexCount);
                newMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
#else
                throw new NotImplementedException(String.Format("exceed 65535 vertices: {0}", integrator.Positions.Count.ToString()));
#endif
            }

            var newDataLength  = newIndexLookUpDict.Count;
            var newIndexLookUp = newIndexLookUpDict.Keys.ToArray();

            newMesh.vertices = newIndexLookUp.Select(x => meshVertices[x]).ToArray();
            if (meshNormals.Length > 0)
            {
                newMesh.normals = newIndexLookUp.Select(x => meshNormals[x]).ToArray();
            }
            if (meshTangents.Length > 0)
            {
                newMesh.tangents = newIndexLookUp.Select(x => meshTangents[x]).ToArray();
            }
            if (meshColors.Length > 0)
            {
                newMesh.colors = newIndexLookUp.Select(x => meshColors[x]).ToArray();
            }
            if (meshBoneWeights.Length > 0)
            {
                newMesh.boneWeights = newIndexLookUp.Select(x => meshBoneWeights[x]).ToArray();
            }
            if (meshUVs.Length > 0)
            {
                newMesh.uv = newIndexLookUp.Select(x => meshUVs[x]).ToArray();
            }
            newMesh.bindposes = mesh.bindposes;

            // add BlendShape data
            if (blendShapeLabel == BlendShapeLogic.WithBlendShape)
            {
                for (int i = 0; i < mesh.blendShapeCount; i++)
                {
                    // get original BlendShape data
                    var srcVertices = new Vector3[mesh.vertexCount];
                    var srcNormals  = new Vector3[mesh.vertexCount];
                    var srcTangents = new Vector3[mesh.vertexCount];
                    mesh.GetBlendShapeFrameVertices(i, 0, srcVertices, srcNormals, srcTangents);

                    // declare the size for the destination array
                    var dstVertices = new Vector3[newDataLength];
                    var dstNormals  = new Vector3[newDataLength];
                    var dstTangents = new Vector3[newDataLength];

                    dstVertices = newIndexLookUp.Select(x => srcVertices[x]).ToArray();
                    dstNormals  = newIndexLookUp.Select(x => srcNormals[x]).ToArray();
                    dstTangents = newIndexLookUp.Select(x => srcTangents[x]).ToArray();
                    newMesh.AddBlendShapeFrame(mesh.GetBlendShapeName(i), mesh.GetBlendShapeFrameWeight(i, 0),
                                               dstVertices, dstNormals, dstTangents);
                }
            }

            newMesh.subMeshCount = subMeshes.Count;
            var cosMaterialIndex = subMeshes.Keys.ToArray();

            // build material list
            for (int i = 0; i < subMeshes.Count; i++)
            {
                newMesh.SetTriangles(subMeshes[cosMaterialIndex[i]], i);
                materialListNew.Add(materialList[cosMaterialIndex[i]]);
            }
            skinnedMeshRenderer.sharedMaterials = materialListNew.ToArray();
            skinnedMeshRenderer.sharedMesh      = newMesh;

            // save mesh as asset
            var assetPath = string.Format("{0}{1}", Path.GetFileNameWithoutExtension(mesh.name), ASSET_SUFFIX);
            Debug.Log(assetPath);
            if (!string.IsNullOrEmpty((AssetDatabase.GetAssetPath(mesh))))
            {
                var directory = Path.GetDirectoryName(AssetDatabase.GetAssetPath(mesh)).Replace("\\", "/");
                assetPath = string.Format("{0}/{1}{2}", directory, Path.GetFileNameWithoutExtension(mesh.name) + "_" + blendShapeLabel.ToString(), ASSET_SUFFIX);
            }
            else
            {
                assetPath = string.Format("Assets/{0}{1}", Path.GetFileNameWithoutExtension(mesh.name) + "_" + blendShapeLabel.ToString(), ASSET_SUFFIX);
            }
            Debug.LogFormat("CreateAsset: {0}", assetPath);
            AssetDatabase.CreateAsset(newMesh, assetPath);
        }