Пример #1
0
    public bool AddSubMeshGroup(string name)
    {
        if (currentObjectData.SubMeshGroups.TryGetValue(name, out currentSubMeshGroup))
        {
            return(true);
        }
        var newGroup = new SubMeshGroupData();

        if (name == null)
        {
            name = "Unnamed-" + UnnamedGroupIndex;
            UnnamedGroupIndex++;
        }
        newGroup.Name = name;
        currentObjectData.SubMeshGroups.Add(newGroup.Name, newGroup);
        currentSubMeshGroup = newGroup;

        return(false);
    }
Пример #2
0
    public void PopulateMeshes(GameObject[] gameObjects, Dictionary <string, Material> materialDictionary, Material defaultMaterial)
    {
        // Check is valid file.
        if (gameObjects.Length != NumberOfObjects)
        {
            Debug.LogError("Failed - OBJ File may be corrupt");
            return;
        }

        for (int i = 0; i < gameObjects.Length; i++)
        {
            ObjectData objectData       = Objects[i];
            bool       objectHasNormals = (HasNormals && objectData.NormalCount > 0);

            if (objectData.Name != "default")
            {
                gameObjects[i].name = objectData.Name;
            }

            //Some OBJ's will reuse verts, uvs or normals.
            //Our faces are in the lead. Lets make copies and rewrite ID's so our
            //Unity arrays can all be the same length.
            var allVertices = new Vector3[objectData.AllFacesIndices.Count];
            var allUVs      = new Vector3[objectData.AllFacesIndices.Count];
            var allNormals  = new Vector3[objectData.AllFacesIndices.Count];

            for (int j = 0; j < objectData.AllFacesIndices.Count; j++)
            {
                allVertices[j] = Vertices[objectData.AllFacesIndices[j].vertexIndex];

                if (HasUVs)
                {
                    allUVs[j] = Uvs[objectData.AllFacesIndices[j].vertexUV];
                }

                if (HasNormals)
                {
                    allNormals[j] = Normals[objectData.AllFacesIndices[j].vertexNormal];
                }

                //set new unique id's
                var faceIndices = objectData.AllFacesIndices[j];
                faceIndices.vertexIndex  = j;
                faceIndices.vertexUV     = j;
                faceIndices.vertexNormal = j;
            }

            Mesh mesh = gameObjects[i].GetComponent <MeshFilter>().mesh;
            mesh.indexFormat = (allVertices.Length > 65536) ? IndexFormat.UInt32 : IndexFormat.UInt32;             //Supports 4 billion verts for larger models
            mesh.vertices    = allVertices;
            if (HasUVs)
            {
                mesh.SetUVs(0, allUVs);
            }
            if (HasNormals)
            {
                mesh.SetNormals(allNormals);
            }

            if (objectData.SubMeshGroups.Count == 1)
            {
                SubMeshGroupData firstSubMeshGroup = objectData.SubMeshGroups.Values.First();
                string           matName           = firstSubMeshGroup.Name;
                if (materialDictionary.ContainsKey(matName))
                {
                    gameObjects[i].GetComponent <Renderer>().material = materialDictionary[matName];
                }
                else
                {
                    gameObjects[i].GetComponent <Renderer>().material      = new Material(defaultMaterial);
                    gameObjects[i].GetComponent <Renderer>().material.name = firstSubMeshGroup.Name;
                }

                var triangles = new int[firstSubMeshGroup.FaceIndices.Count];
                for (int j = 0; j < triangles.Length; j++)
                {
                    triangles[j] = firstSubMeshGroup.FaceIndices[j].vertexIndex;
                }
                mesh.triangles = triangles;
            }
            else
            {
                int subMeshCount = objectData.SubMeshGroups.Count;
                var materials    = new Material[subMeshCount];
                mesh.subMeshCount = subMeshCount;

                int submeshIndex = 0;
                Debug.Log("PopulateMeshes group count: " + subMeshCount);
                foreach (KeyValuePair <string, SubMeshGroupData> subMesh in objectData.SubMeshGroups)
                {
                    string matName = subMesh.Value.Name;
                    if (materialDictionary.ContainsKey(matName))
                    {
                        materials[submeshIndex] = materialDictionary[matName];
                        Debug.Log("PopulateMeshes mat: " + matName + " set.");
                    }
                    else
                    {
                        materials[submeshIndex]      = new Material(defaultMaterial);
                        materials[submeshIndex].name = matName;
                        Debug.LogWarning("PopulateMeshes mat: " + matName + " not found.");
                    }

                    var triangles = new int[subMesh.Value.FaceIndices.Count];
                    for (int v = 0; v < subMesh.Value.FaceIndices.Count; v++)
                    {
                        triangles[v] = subMesh.Value.FaceIndices[v].vertexIndex;
                    }

                    mesh.SetTriangles(triangles, submeshIndex);
                    submeshIndex++;
                }

                gameObjects[i].GetComponent <Renderer>().materials = materials;
            }

            if (!objectHasNormals)
            {
                mesh.RecalculateNormals();
            }
        }
    }