Пример #1
0
        public void AddMesh()
        {
            if (Mesh == null)
            {
                return;
            }

            int        nextId = 0;
            SharedMesh mesh;

            for (int i = 0; i < Meshes.Count; i++)
            {
                mesh = Meshes[i];
                if (mesh.Id > nextId)
                {
                    nextId = mesh.Id;
                }
            }
            nextId++;

            mesh = new SharedMesh {
                Id = nextId, Name = Name, Mesh = Mesh
            };
            Meshes.Add(mesh);
            Mesh = null;
            Name = null;

#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(this);
#endif
        }
Пример #2
0
        public static Mesh ToUnityMesh(this SharedMesh sharedMesh)
        {
            Mesh newMesh = new Mesh();

            sharedMesh.ToUnityMesh(newMesh);
            return(newMesh);
        }
Пример #3
0
        public void Save()
        {
            NativeArray <BlobAssetReference <MeshSourceData> > blobs = new NativeArray <BlobAssetReference <MeshSourceData> >(Meshes.Count, Allocator.Temp);

            for (int i = 0; i < Meshes.Count; i++)
            {
                SharedMesh sharedMesh = Meshes[i];
                var        blob       = MeshSourceData.Create(sharedMesh.Mesh);
                blobs[i] = blob;
            }
            NavMeshStoreSystem.Instance.SaveSources(blobs, -1);
        }
Пример #4
0
 void FindSharedMesh()
 {
     if (useSharedMaterial)
     {
         if (sharedMeshForThisPiece == null)
         {
             var meshGO = GameObject.Find("CombinedMesh: " + renderer.sharedMaterial.name.Replace("(Instance)", ""));
             if (meshGO == null)
             {
                 Debug.LogError("Could not find shared mesh for piece: " + gameObject.name);
                 return;
             }
             sharedMeshForThisPiece = meshGO.GetComponent <SharedMesh>();
             if (sharedMeshForThisPiece == null)
             {
                 sharedMeshForThisPiece = meshGO.AddComponent <SharedMesh>();
             }
         }
     }
 }
Пример #5
0
        public void AddPrimitives()
        {
            SharedMesh sm = new SharedMesh {
                Id = (int)PrimitiveType.Capsule + 1, Mesh = MeshSourceData.CreateMesh(PrimitiveType.Capsule), Name = PrimitiveType.Capsule.ToString()
            };

            Meshes.Add(sm);
            sm = new SharedMesh {
                Id = (int)PrimitiveType.Cube + 1, Mesh = MeshSourceData.CreateMesh(PrimitiveType.Cube), Name = PrimitiveType.Cube.ToString()
            };
            Meshes.Add(sm);
            sm = new SharedMesh {
                Id = (int)PrimitiveType.Cylinder + 1, Mesh = MeshSourceData.CreateMesh(PrimitiveType.Cylinder), Name = PrimitiveType.Cylinder.ToString()
            };
            Meshes.Add(sm);
            sm = new SharedMesh {
                Id = (int)PrimitiveType.Sphere + 1, Mesh = MeshSourceData.CreateMesh(PrimitiveType.Sphere), Name = PrimitiveType.Sphere.ToString()
            };
            Meshes.Add(sm);

#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(this);
#endif
        }
Пример #6
0
        public static SharedMesh ToSharedMesh(this Mesh mesh)
        {
            System.Console.WriteLine($"Vertex count:{mesh.vertexCount}");

            UVector3[] vertices = mesh.vertices;

            SharedMesh sharedMesh = new SharedMesh();

            sharedMesh.positions = new NVector3[vertices.Length];

            MetaAttributeList attributes = new EmptyMetaAttributeList(vertices.Length);

            for (int i = 0; i < vertices.Length; i++)
            {
                sharedMesh.positions[i] = new NVector3(vertices[i].x, vertices[i].y, vertices[i].z);
            }

            List <AttributeDefinition> attributeDefinitions = new List <AttributeDefinition>();

            UVector3[] normals = mesh.normals;
            if (normals != null && normals.Length > 0)
            {
                int k = attributeDefinitions.Count;
                attributeDefinitions.Add(new AttributeDefinition(AttributeType.Normals, 222.0194f));
                attributes = attributes.AddAttributeType <NVector3F>();
                for (int i = 0; i < normals.Length; i++)
                {
                    attributes[i] = attributes[i].Set(k, new NVector3F(normals[i].x, normals[i].y, normals[i].z));
                }
            }

            UVector2[] uvs1 = mesh.uv;
            if (uvs1 != null && uvs1.Length > 0)
            {
                int k = attributeDefinitions.Count;
                attributeDefinitions.Add(new AttributeDefinition(AttributeType.UVs, 90.21234f, 0));
                attributes = attributes.AddAttributeType <NVector2F>();
                for (int i = 0; i < uvs1.Length; i++)
                {
                    attributes[i] = attributes[i].Set(k, new NVector2F(uvs1[i].x, uvs1[i].y));
                }
            }

            UVector2[] uvs2 = mesh.uv2;
            if (uvs2 != null && uvs2.Length > 0)
            {
                int k = attributeDefinitions.Count;
                attributeDefinitions.Add(new AttributeDefinition(AttributeType.UVs, 9.021234f, 1));
                attributes = attributes.AddAttributeType <NVector2F>();
                for (int i = 0; i < uvs2.Length; i++)
                {
                    attributes[i] = attributes[i].Set(k, new NVector2F(uvs2[i].x, uvs2[i].y));
                }
            }

            /*
             * UVector2[] uvs3 = mesh.uv3;
             * if (uvs3 != null && uvs3.Length > 0)
             * {
             *  int k = attributeDefinitions.Count;
             *  attributeDefinitions.Add(new AttributeDefinition(AttributeType.UVs));
             *  attributes = attributes.AddAttributeType<NVector2F>();
             *  for (int i = 0; i < uvs3.Length; i++)
             *  {
             *      attributes[i] = attributes[i].Set(k, new NVector2F(uvs3[i].x, uvs3[i].y));
             *  }
             * }
             *
             * UVector2[] uvs4 = mesh.uv4;
             * if (uvs4 != null && uvs4.Length > 0)
             * {
             *  int k = attributeDefinitions.Count;
             *  attributeDefinitions.Add(new AttributeDefinition(AttributeType.UVs));
             *  attributes = attributes.AddAttributeType<NVector2F>();
             *  for (int i = 0; i < uvs4.Length; i++)
             *  {
             *      attributes[i] = attributes[i].Set(k, new NVector2F(uvs4[i].x, uvs4[i].y));
             *  }
             * }
             *
             * UVector2[] uvs5 = mesh.uv5;
             * if (uvs5 != null && uvs5.Length > 0)
             * {
             *  int k = attributeDefinitions.Count;
             *  attributeDefinitions.Add(new AttributeDefinition(AttributeType.UVs));
             *  attributes = attributes.AddAttributeType<NVector2F>();
             *  for (int i = 0; i < uvs5.Length; i++)
             *  {
             *      attributes[i] = attributes[i].Set(k, new NVector2F(uvs5[i].x, uvs5[i].y));
             *  }
             * }
             *
             * UVector2[] uvs6 = mesh.uv6;
             * if (uvs6 != null && uvs6.Length > 0)
             * {
             *  int k = attributeDefinitions.Count;
             *  attributeDefinitions.Add(new AttributeDefinition(AttributeType.UVs));
             *  attributes = attributes.AddAttributeType<NVector2F>();
             *  for (int i = 0; i < uvs6.Length; i++)
             *  {
             *      attributes[i] = attributes[i].Set(k, new NVector2F(uvs6[i].x, uvs6[i].y));
             *  }
             * }
             *
             * UVector2[] uvs7 = mesh.uv7;
             * if (uvs7 != null && uvs7.Length > 0)
             * {
             *  int k = attributeDefinitions.Count;
             *  attributeDefinitions.Add(new AttributeDefinition(AttributeType.UVs));
             *  attributes = attributes.AddAttributeType<NVector2F>();
             *  for (int i = 0; i < uvs7.Length; i++)
             *  {
             *      attributes[i] = attributes[i].Set(k, new NVector2F(uvs7[i].x, uvs7[i].y));
             *  }
             * }
             *
             * UVector2[] uvs8 = mesh.uv8;
             * if (uvs8 != null && uvs8.Length > 0)
             * {
             *  int k = attributeDefinitions.Count;
             *  attributeDefinitions.Add(new AttributeDefinition(AttributeType.UVs));
             *  attributes = attributes.AddAttributeType<NVector2F>();
             *  for (int i = 0; i < uvs8.Length; i++)
             *  {
             *      attributes[i] = attributes[i].Set(k, new NVector2F(uvs8[i].x, uvs8[i].y));
             *  }
             * }
             */

            UColor32[] colors = mesh.colors32;
            if (colors != null && colors.Length > 0)
            {
                int k = attributeDefinitions.Count;
                attributeDefinitions.Add(new AttributeDefinition(AttributeType.Colors, 0.0001));
                attributes = attributes.AddAttributeType <NColor32>();
                for (int i = 0; i < colors.Length; i++)
                {
                    attributes[i] = attributes[i].Set(k, new NColor32(colors[i].r, colors[i].g, colors[i].b, colors[i].a));
                }
            }

            UBoneWeight[] boneWeights = mesh.boneWeights;
            if (boneWeights != null && boneWeights.Length > 0)
            {
                int k = attributeDefinitions.Count;
                attributeDefinitions.Add(new AttributeDefinition(AttributeType.BoneWeights, 1f));
                attributes = attributes.AddAttributeType <NBoneWeight>();
                for (int i = 0; i < boneWeights.Length; i++)
                {
                    attributes[i] = attributes[i].Set(k, new NBoneWeight(
                                                          boneWeights[i].boneIndex0,
                                                          boneWeights[i].boneIndex1,
                                                          boneWeights[i].boneIndex2,
                                                          boneWeights[i].boneIndex3,
                                                          boneWeights[i].weight0,
                                                          boneWeights[i].weight1,
                                                          boneWeights[i].weight2,
                                                          boneWeights[i].weight3));
                }
            }

            sharedMesh.attributeDefinitions = attributeDefinitions.ToArray();
            sharedMesh.attributes           = attributes;

            sharedMesh.triangles = mesh.triangles;
            sharedMesh.groups    = new Group[mesh.subMeshCount];

            for (int i = 0; i < mesh.subMeshCount; i++)
            {
                UnityEngine.Rendering.SubMeshDescriptor submeshDesc = mesh.GetSubMesh(i);
                sharedMesh.groups[i] = new Group {
                    firstIndex = submeshDesc.indexStart, indexCount = submeshDesc.indexCount
                };
            }

            return(sharedMesh);
        }
Пример #7
0
        public static void ToUnityMesh(this SharedMesh sharedMesh, Mesh mesh)
        {
            mesh.Clear();

            mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;

            UVector3[] vertices = new UVector3[sharedMesh.positions.Length];
            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i] = new UVector3((float)sharedMesh.positions[i].x, (float)sharedMesh.positions[i].y, (float)sharedMesh.positions[i].z);
            }

            mesh.vertices = vertices;

            if (sharedMesh.attributes != null)
            {
                for (int i = 0; i < sharedMesh.attributeDefinitions.Length; i++)
                {
                    if (sharedMesh.attributeDefinitions[i].type == AttributeType.Normals)
                    {
                        UVector3[] normals = new UVector3[sharedMesh.positions.Length];
                        for (int j = 0; j < sharedMesh.attributes.Count; j++)
                        {
                            NVector3F normal = sharedMesh.attributes[j].Get <NVector3F>(i);
                            normals[j] = new UVector3(normal.x, normal.y, normal.z);
                        }
                        mesh.normals = normals;
                    }
                    else if (sharedMesh.attributeDefinitions[i].type == AttributeType.UVs)
                    {
                        UVector2[] uvs = new UVector2[sharedMesh.positions.Length];
                        for (int j = 0; j < sharedMesh.attributes.Count; j++)
                        {
                            NVector2F uv = sharedMesh.attributes[j].Get <NVector2F>(i);
                            uvs[j] = new UVector2(uv.x, uv.y);
                        }
                        mesh.SetUVs(sharedMesh.attributeDefinitions[i].id, uvs);
                    }
                    else if (sharedMesh.attributeDefinitions[i].type == AttributeType.BoneWeights)
                    {
                        UBoneWeight[] boneWeights = new UBoneWeight[sharedMesh.positions.Length];
                        for (int j = 0; j < sharedMesh.attributes.Count; j++)
                        {
                            NBoneWeight boneWeight = sharedMesh.attributes[j].Get <NBoneWeight>(i);
                            boneWeights[j] = new UBoneWeight
                            {
                                boneIndex0 = boneWeight.index0,
                                boneIndex1 = boneWeight.index1,
                                boneIndex2 = boneWeight.index2,
                                boneIndex3 = boneWeight.index3,
                                weight0    = boneWeight.weight0,
                                weight1    = boneWeight.weight1,
                                weight2    = boneWeight.weight2,
                                weight3    = boneWeight.weight3,
                            };
                        }
                        mesh.boneWeights = boneWeights;
                    }
                }
            }

            mesh.triangles    = sharedMesh.triangles;
            mesh.subMeshCount = sharedMesh.groups.Length;

            for (int i = 0; i < sharedMesh.groups.Length; i++)
            {
                mesh.SetSubMesh(i, new UnityEngine.Rendering.SubMeshDescriptor(sharedMesh.groups[i].firstIndex, sharedMesh.groups[i].indexCount));
            }

            mesh.RecalculateTangents();
        }