ObjImporter.createMeshStruct C# (CSharp) Method

createMeshStruct() private static method

private static createMeshStruct ( string filename ) : meshStruct
filename string
return meshStruct
    private static meshStruct createMeshStruct(string filename)
    {
        int triangles = 0;
        int vertices = 0;
        int vt = 0;
        int vn = 0;
        int face = 0;
        meshStruct mesh = new meshStruct();
        mesh.fileName = filename;
        StreamReader stream = File.OpenText(filename);
        string entireText = stream.ReadToEnd();
        stream.Close();
        using (StringReader reader = new StringReader(entireText))
        {
            string currentText = reader.ReadLine();
            char[] splitIdentifier = { ' ' };
            string[] brokenString;
            while (currentText != null)
            {
                if (!currentText.StartsWith("f ") && !currentText.StartsWith("v ") && !currentText.StartsWith("vt ")
                    && !currentText.StartsWith("vn "))
                {
                    currentText = reader.ReadLine();
                    if (currentText != null)
                    {
                        currentText = currentText.Replace("  ", " ");
                    }
                }
                else
                {
                    currentText = currentText.Trim();                           //Trim the current line
                    brokenString = currentText.Split(splitIdentifier, 50);      //Split the line into an array, separating the original line by blank spaces
                    switch (brokenString[0])
                    {
                        case "v":
                            vertices++;
                            break;
                        case "vt":
                            vt++;
                            break;
                        case "vn":
                            vn++;
                            break;
                        case "f":
                            face = face + brokenString.Length - 1;
                            triangles = triangles + 3 * (brokenString.Length - 2); /*brokenString.Length is 3 or greater since a face must have at least
                                                                                     3 vertices.  For each additional vertice, there is an additional
                                                                                     triangle in the mesh (hence this formula).*/
                            break;
                    }
                    currentText = reader.ReadLine();
                    if (currentText != null)
                    {
                        currentText = currentText.Replace("  ", " ");
                    }
                }
            }
        }
        mesh.triangles = new int[triangles];
        mesh.vertices = new Vector3[vertices];
        mesh.uv = new Vector2[vt];
        mesh.normals = new Vector3[vn];
        mesh.faceData = new Vector3[face];
        return mesh;
    }

Usage Example

Example #1
0
        void DrawBezierPathInspector()
        {
            using (var check = new EditorGUI.ChangeCheckScope()) {
                EditorGUILayout.HelpBox("Path calculation functions.", MessageType.Info);
                // Path distance to position
                data.pathDist = EditorGUILayout.FloatField("Path Distance", data.pathDist);
                if (GUILayout.Button("Get World Coordinates"))
                {
                    Vector3 pos  = creator.path.GetPointAtDistance(data.pathDist);
                    Vector3 rots = creator.path.GetRotationAtDistance(data.pathDist).eulerAngles;
                    Debug.Log($"World - Pos X: {pos.x:.000}, Pos Y: {pos.y:.000}, Pos Z: {pos.z:.000}, Rot X: {rots.x:.000}, Rot Y: {rots.y:.000}, Rot Z: {rots.z:.000}");
                }
                data.pathWorld = EditorGUILayout.Vector3Field("World Position", data.pathWorld);
                if (GUILayout.Button("Get Path Position"))
                {
                    float   pos  = creator.path.GetClosestDistanceAlongPath(data.pathWorld);
                    Vector3 rots = creator.path.GetRotationAtDistance(pos).eulerAngles;
                    Debug.Log($"Path - Path Distance: {pos:.000}, Rot X: {rots.x:.000}, Rot Y: {rots.y:.000}, Rot Z: {rots.z:.000}");
                }
                // Path options:
                data.showPathOptions = EditorGUILayout.Foldout(data.showPathOptions, new GUIContent("Bézier Path Options"), true, boldFoldoutStyle);
                if (data.showPathOptions)
                {
                    bezierPath.Space            = (PathSpace)EditorGUILayout.Popup("Space", (int)bezierPath.Space, spaceNames);
                    bezierPath.ControlPointMode = (BezierPath.ControlMode)EditorGUILayout.EnumPopup(new GUIContent("Control Mode"), bezierPath.ControlPointMode);
                    if (bezierPath.ControlPointMode == BezierPath.ControlMode.Automatic)
                    {
                        bezierPath.AutoControlLength = EditorGUILayout.Slider(new GUIContent("Control Spacing"), bezierPath.AutoControlLength, 0, 1);
                    }

                    bezierPath.IsClosed            = EditorGUILayout.Toggle("Closed Path", bezierPath.IsClosed);
                    data.pathTransformationEnabled = EditorGUILayout.Toggle(new GUIContent("Enable Transforms"), data.pathTransformationEnabled);

                    // If a point has been selected
                    if (handleIndexToDisplayAsTransform != -1)
                    {
                        EditorGUILayout.LabelField("Selected Point:");

                        using (new EditorGUI.IndentLevelScope()) {
                            var currentPosition = creator.bezierPath[handleIndexToDisplayAsTransform];
                            var newPosition     = EditorGUILayout.Vector3Field("Position", currentPosition);
                            if (newPosition != currentPosition)
                            {
                                Undo.RecordObject(creator, "Move point");
                                creator.bezierPath.MovePoint(handleIndexToDisplayAsTransform, newPosition);
                            }
                            // Don't draw the angle field if we aren't selecting an anchor point/not in 3d space
                            if (handleIndexToDisplayAsTransform % 3 == 0 && creator.bezierPath.Space == PathSpace.xyz)
                            {
                                var anchorIndex  = handleIndexToDisplayAsTransform / 3;
                                var currentAngle = creator.bezierPath.GetAnchorNormalAngle(anchorIndex);
                                var newAngle     = EditorGUILayout.FloatField("Angle", currentAngle);
                                if (newAngle != currentAngle)
                                {
                                    Undo.RecordObject(creator, "Set Angle");
                                    creator.bezierPath.SetAnchorNormalAngle(anchorIndex, newAngle);
                                }
                            }
                        }
                    }

                    if (GUILayout.Button("Reset Path"))
                    {
                        Undo.RecordObject(creator, "Reset Path");
                        bool in2DEditorMode = EditorSettings.defaultBehaviorMode == EditorBehaviorMode.Mode2D;
                        data.ResetBezierPath(creator.transform.position, in2DEditorMode);
                        EditorApplication.QueuePlayerLoopUpdate();
                    }

                    if (GUILayout.Button("Import OBJ"))
                    {
                        // Select file.
                        string path = EditorUtility.OpenFilePanel("Select obj file", "", "obj");
                        // Import.
                        ObjImporter.meshStruct holderMesh = ObjImporter.createMeshStruct(path);
                        ObjImporter.populateMeshStruct(ref holderMesh);
                        //Assign
                        data.bezierPath = new BezierPath(holderMesh.vertices, false, PathSpace.xyz);
                    }

                    GUILayout.Space(inspectorSectionSpacing);
                }

                data.showNormals = EditorGUILayout.Foldout(data.showNormals, new GUIContent("Normals Options"), true, boldFoldoutStyle);
                if (data.showNormals)
                {
                    bezierPath.FlipNormals = EditorGUILayout.Toggle(new GUIContent("Flip Normals"), bezierPath.FlipNormals);
                    if (bezierPath.Space == PathSpace.xyz)
                    {
                        bezierPath.GlobalNormalsAngle = EditorGUILayout.Slider(new GUIContent("Global Angle"), bezierPath.GlobalNormalsAngle, 0, 360);

                        if (GUILayout.Button("Reset Normals"))
                        {
                            Undo.RecordObject(creator, "Reset Normals");
                            bezierPath.FlipNormals = false;
                            bezierPath.ResetNormalAngles();
                        }
                    }
                    GUILayout.Space(inspectorSectionSpacing);
                }

                // Editor display options
                data.showDisplayOptions = EditorGUILayout.Foldout(data.showDisplayOptions, new GUIContent("Display Options"), true, boldFoldoutStyle);
                if (data.showDisplayOptions)
                {
                    data.showPathBounds       = GUILayout.Toggle(data.showPathBounds, new GUIContent("Show Path Bounds"));
                    data.showPerSegmentBounds = GUILayout.Toggle(data.showPerSegmentBounds, new GUIContent("Show Segment Bounds"));
                    data.displayAnchorPoints  = GUILayout.Toggle(data.displayAnchorPoints, new GUIContent("Show Anchor Points"));
                    if (!(bezierPath.ControlPointMode == BezierPath.ControlMode.Automatic && globalDisplaySettings.hideAutoControls))
                    {
                        data.displayControlPoints = GUILayout.Toggle(data.displayControlPoints, new GUIContent("Show Control Points"));
                    }
                    data.keepConstantHandleSize = GUILayout.Toggle(data.keepConstantHandleSize, new GUIContent("Constant Point Size", constantSizeTooltip));
                    data.bezierHandleScale      = Mathf.Max(0, EditorGUILayout.FloatField(new GUIContent("Handle Scale"), data.bezierHandleScale));
                    DrawGlobalDisplaySettingsInspector();
                }

                if (check.changed)
                {
                    SceneView.RepaintAll();
                    EditorApplication.QueuePlayerLoopUpdate();
                }
            }
        }