/// <summary> /// Load an OBJ file from a stream. No materials will be loaded, and will instead be supplemented by a blank white material. /// </summary> /// <param name="input">Input OBJ stream</param> /// <returns>Returns a GameObject represeting the OBJ file, with each imported object as a child.</returns> public GameObject Load(Stream input) { var inputReader = new StreamReader(input); var reader = new StringReader(inputReader.ReadToEnd()); Dictionary <string, OBJObjectBuilder> builderDict = new Dictionary <string, OBJObjectBuilder>(); OBJObjectBuilder currentBuilder = null; string currentMaterial = "default"; //lists for face data //prevents excess GC List <int> vertexIndices = new List <int>(); List <int> normalIndices = new List <int>(); List <int> uvIndices = new List <int>(); //helper func Action <string> setCurrentObjectFunc = (string objectName) => { if (!builderDict.TryGetValue(objectName, out currentBuilder)) { currentBuilder = new OBJObjectBuilder(objectName, this); builderDict[objectName] = currentBuilder; } }; //create default object setCurrentObjectFunc.Invoke("default"); //do the reading for (string line = reader.ReadLine(); line != null; line = reader.ReadLine()) { if (StringExtension.IsNullOrWhiteSpace(line)) { continue; } string processedLine = line.Clean(); string[] splitLine = processedLine.Split(' '); //comment or blank if (processedLine[0] == '#' || splitLine.Length < 2) { continue; } //load material library if (splitLine[0] == "mtllib" && Materials == null) { string mtlLibPath = processedLine.Substring(7); LoadMaterialLibrary(mtlLibPath); } //vtx if (splitLine[0] == "v") { Vertices.Add(OBJLoaderHelper.VectorFromStrArray(splitLine)); continue; } //normal if (splitLine[0] == "vn") { Normals.Add(OBJLoaderHelper.VectorFromStrArray(splitLine)); continue; } //uv if (splitLine[0] == "vt") { UVs.Add(OBJLoaderHelper.VectorFromStrArray(splitLine)); continue; } //new material if (splitLine[0] == "usemtl") { string materialName = processedLine.Substring(7); currentMaterial = materialName; if (SplitMode == SplitMode.Material) { setCurrentObjectFunc.Invoke(materialName); } continue; } //new object if ((splitLine[0] == "o" || splitLine[0] == "g") && SplitMode == SplitMode.Object) { string objectName = processedLine.Substring(2); setCurrentObjectFunc.Invoke(objectName); continue; } //face data (the fun part) if (splitLine[0] == "f") { //loop through indices for (int i = 1; i < splitLine.Length; i++) { string faceLoop = splitLine[i]; int vertexIndex = int.MinValue; int normalIndex = int.MinValue; int uvIndex = int.MinValue; //parse face loop if (faceLoop.Contains("//")) { //vertex and normal string[] slashSplits = faceLoop.Split('/'); vertexIndex = OBJLoaderHelper.FastIntParse(slashSplits[0]); normalIndex = OBJLoaderHelper.FastIntParse(slashSplits[2]); } else if (faceLoop.Contains("/")) { //get slash splits string[] slashSplits = faceLoop.Split('/'); if (slashSplits.Length > 2) { //vertex, uv, and normal vertexIndex = OBJLoaderHelper.FastIntParse(slashSplits[0]); uvIndex = OBJLoaderHelper.FastIntParse(slashSplits[1]); normalIndex = OBJLoaderHelper.FastIntParse(slashSplits[2]); } else { //vertex, and uv vertexIndex = OBJLoaderHelper.FastIntParse(slashSplits[0]); uvIndex = OBJLoaderHelper.FastIntParse(slashSplits[1]); } } else { //just vertex index vertexIndex = OBJLoaderHelper.FastIntParse(faceLoop); } //"postprocess" indices if (vertexIndex > int.MinValue) { if (vertexIndex < 0) { vertexIndex = Vertices.Count - vertexIndex; } vertexIndex--; } if (normalIndex > int.MinValue) { if (normalIndex < 0) { normalIndex = Normals.Count - normalIndex; } normalIndex--; } if (uvIndex > int.MinValue) { if (uvIndex < 0) { uvIndex = UVs.Count - uvIndex; } uvIndex--; } //set array values vertexIndices.Add(vertexIndex); normalIndices.Add(normalIndex); uvIndices.Add(uvIndex); } //push to builder currentBuilder.PushFace(currentMaterial, vertexIndices, normalIndices, uvIndices); //clear lists vertexIndices.Clear(); normalIndices.Clear(); uvIndices.Clear(); } } //finally, put it all together GameObject obj = new GameObject(_objInfo != null ? Path.GetFileNameWithoutExtension(_objInfo.Name) : "WavefrontObject"); obj.transform.localScale = new Vector3(-1f, 1f, 1f); foreach (var builder in builderDict) { //empty object if (builder.Value.PushedFaceCount == 0) { continue; } var builtObj = builder.Value.Build(); builtObj.transform.SetParent(obj.transform, false); } return(obj); }
/// <summary> /// Load an OBJ file from a stream. No materials will be loaded, and will instead be supplemented by a blank white material. /// </summary> /// <param name="input">Input OBJ stream</param> /// <returns>Returns a GameObject represeting the OBJ file, with each imported object as a child.</returns> private GameObject Load(Stream input) { var reader = new StreamReader(input); Dictionary <string, OBJObjectBuilder> builderDict = new Dictionary <string, OBJObjectBuilder>(); OBJObjectBuilder currentBuilder = null; string currentMaterial = "default"; //lists for face data //prevents excess GC List <int> vertexIndices = new List <int>(); List <int> normalIndices = new List <int>(); List <int> uvIndices = new List <int>(); //helper func Action <string> setCurrentObjectFunc = ( string objectName ) => { if (!builderDict.TryGetValue(objectName, out currentBuilder)) { currentBuilder = new OBJObjectBuilder(objectName, this); builderDict[objectName] = currentBuilder; } }; //create default object setCurrentObjectFunc.Invoke("default"); var buffer = new CharWordReader(reader, 4 * 1024); //do the reading while (true) { buffer.SkipWhitespaces(); if (buffer.endReached == true) { break; } buffer.ReadUntilWhiteSpace(); //comment or blank if (buffer.Is("#")) { buffer.SkipUntilNewLine(); continue; } if (Materials == null && buffer.Is("mtllib")) { buffer.SkipWhitespaces(); buffer.ReadUntilNewLine(); string mtlLibPath = buffer.GetString(); LoadMaterialLibrary(mtlLibPath); continue; } if (buffer.Is("v")) { Vertices.Add(buffer.ReadVector()); continue; } //normal if (buffer.Is("vn")) { Normals.Add(buffer.ReadVector()); continue; } //uv if (buffer.Is("vt")) { UVs.Add(buffer.ReadVector()); continue; } //new material if (buffer.Is("usemtl")) { buffer.SkipWhitespaces(); buffer.ReadUntilNewLine(); string materialName = buffer.GetString(); currentMaterial = materialName; continue; } //new object if (buffer.Is("o") || buffer.Is("g")) { buffer.ReadUntilNewLine(); string objectName = buffer.GetString(1); setCurrentObjectFunc.Invoke(objectName); continue; } //face data (the fun part) if (buffer.Is("f")) { //loop through indices while (true) { bool newLinePassed; buffer.SkipWhitespaces(out newLinePassed); if (newLinePassed == true) { break; } int vertexIndex = int.MinValue; int normalIndex = int.MinValue; int uvIndex = int.MinValue; vertexIndex = buffer.ReadInt(); if (buffer.currentChar == '/') { buffer.MoveNext(); if (buffer.currentChar != '/') { uvIndex = buffer.ReadInt(); } if (buffer.currentChar == '/') { buffer.MoveNext(); normalIndex = buffer.ReadInt(); } } //"postprocess" indices if (vertexIndex > int.MinValue) { if (vertexIndex < 0) { vertexIndex = Vertices.Count - vertexIndex; } vertexIndex--; } if (normalIndex > int.MinValue) { if (normalIndex < 0) { normalIndex = Normals.Count - normalIndex; } normalIndex--; } if (uvIndex > int.MinValue) { if (uvIndex < 0) { uvIndex = UVs.Count - uvIndex; } uvIndex--; } //set array values vertexIndices.Add(vertexIndex); normalIndices.Add(normalIndex); uvIndices.Add(uvIndex); } //push to builder currentBuilder.PushFace(currentMaterial, vertexIndices, normalIndices, uvIndices); //clear lists vertexIndices.Clear(); normalIndices.Clear(); uvIndices.Clear(); continue; } buffer.SkipUntilNewLine(); } //finally, put it all together GameObject obj = new GameObject(_objInfo != null ? Path.GetFileNameWithoutExtension(_objInfo.Name) : "WavefrontObject"); obj.transform.localScale = new Vector3(-1f, 1f, 1f); foreach (var builder in builderDict) { //empty object if (builder.Value.PushedFaceCount == 0) { continue; } var builtObj = builder.Value.Build(); builtObj.transform.SetParent(obj.transform, false); } return(obj); }
private Dictionary <string, OBJObjectBuilder> CustomLoad(StreamReader reader) { Dictionary <string, OBJObjectBuilder> builderDict = new Dictionary <string, OBJObjectBuilder>(); OBJObjectBuilder currentBuilder = null; string currentMaterial = "default"; //lists for face data //prevents excess GC List <int> vertexIndices = new List <int>(); List <int> normalIndices = new List <int>(); List <int> uvIndices = new List <int>(); //helper func Action <string> setCurrentObjectFunc = (string objectName) => { if (!builderDict.TryGetValue(objectName, out currentBuilder)) { currentBuilder = new OBJObjectBuilder(objectName, this); builderDict[objectName] = currentBuilder; } }; //create default object setCurrentObjectFunc.Invoke("default"); //var buffer = new DoubleBuffer(reader, 256 * 1024); var buffer = new CharWordReader(reader, 4 * 1024); //do the reading while (true) { buffer.SkipWhitespaces(); if (buffer.endReached == true) { break; } buffer.ReadUntilWhiteSpace(); //comment or blank if (buffer.Is("#")) { buffer.SkipUntilNewLine(); continue; } if (Materials == null && buffer.Is("mtllib")) { buffer.SkipWhitespaces(); buffer.ReadUntilNewLine(); string mtlLibPath = buffer.GetString(); LoadMaterialLibrary(mtlLibPath); continue; } if (buffer.Is("v")) { Vertices.Add(buffer.ReadVector()); continue; } //normal if (buffer.Is("vn")) { Normals.Add(buffer.ReadVector()); continue; } //uv if (buffer.Is("vt")) { UVs.Add(buffer.ReadVector()); continue; } //new material if (buffer.Is("usemtl")) { buffer.SkipWhitespaces(); buffer.ReadUntilNewLine(); string materialName = buffer.GetString(); currentMaterial = materialName; if (SplitMode == SplitMode.Material) { setCurrentObjectFunc.Invoke(materialName); } continue; } //new object if ((buffer.Is("o") || buffer.Is("g")) && SplitMode == SplitMode.Object) { buffer.ReadUntilNewLine(); string objectName = buffer.GetString(1); setCurrentObjectFunc.Invoke(objectName); continue; } //face data (the fun part) if (buffer.Is("f")) { //loop through indices while (true) { bool newLinePassed; buffer.SkipWhitespaces(out newLinePassed); if (newLinePassed == true) { break; } int vertexIndex = int.MinValue; int normalIndex = int.MinValue; int uvIndex = int.MinValue; vertexIndex = buffer.ReadInt(); if (buffer.currentChar == '/') { buffer.MoveNext(); if (buffer.currentChar != '/') { uvIndex = buffer.ReadInt(); } if (buffer.currentChar == '/') { buffer.MoveNext(); normalIndex = buffer.ReadInt(); } } //"postprocess" indices if (vertexIndex > int.MinValue) { if (vertexIndex < 0) { vertexIndex = Vertices.Count - vertexIndex; } vertexIndex--; } if (normalIndex > int.MinValue) { if (normalIndex < 0) { normalIndex = Normals.Count - normalIndex; } normalIndex--; } if (uvIndex > int.MinValue) { if (uvIndex < 0) { uvIndex = UVs.Count - uvIndex; } uvIndex--; } //set array values vertexIndices.Add(vertexIndex); normalIndices.Add(normalIndex); uvIndices.Add(uvIndex); } //push to builder currentBuilder.PushFace(currentMaterial, vertexIndices, normalIndices, uvIndices); //clear lists vertexIndices.Clear(); normalIndices.Clear(); uvIndices.Clear(); continue; } buffer.SkipUntilNewLine(); } return(builderDict); }
/// <summary> /// Load an OBJ file from a stream. No materials will be loaded, and will instead be supplemented by a blank white material. /// </summary> /// <param name="input">Input OBJ stream</param> /// <returns>Returns a GameObject represeting the OBJ file, with each imported object as a child.</returns> public GameObject Load(Stream input1, Stream input2, Stream input3, Stream input4) { var reader1 = new StreamReader(input1); var reader2 = new StreamReader(input2); var reader3 = new StreamReader(input3); var reader4 = new StreamReader(input4); //var reader = new StringReader(inputReader.ReadToEnd()); Dictionary <string, OBJObjectBuilder> builderDict = new Dictionary <string, OBJObjectBuilder>(); OBJObjectBuilder currentBuilder = null; string currentMaterial = "default"; //lists for face data //prevents excess GC List <int> vertexIndices = new List <int>(); List <int> normalIndices = new List <int>(); List <int> uvIndices = new List <int>(); //helper func Action <string> setCurrentObjectFunc = (string objectName) => { if (!builderDict.TryGetValue(objectName, out currentBuilder)) { currentBuilder = new OBJObjectBuilder(objectName, this); builderDict[objectName] = currentBuilder; } }; //create default object setCurrentObjectFunc.Invoke("object"); //var buffer = new DoubleBuffer(reader, 256 * 1024); var buffer1 = new CharWordReader(reader1, 4 * 1024); var buffer2 = new CharWordReader(reader2, 4 * 1024); var buffer3 = new CharWordReader(reader3, 4 * 1024); var buffer4 = new CharWordReader(reader4, 4 * 1024); //reading 'v' and others while (true) { buffer1.SkipWhitespaces(); if (buffer1.endReached == true) { break; } buffer1.ReadUntilWhiteSpace(); //comment or blank if (buffer1.Is("#")) { buffer1.SkipUntilNewLine(); continue; } if (buffer1.Is("v")) { Vertices.Add(buffer1.ReadVector()); continue; } buffer1.SkipUntilNewLine(); } // reading 'vt' while (!isUvRead) { buffer3.SkipWhitespaces(); if (buffer3.endReached == true) { isUvRead = true; break; } buffer3.ReadUntilWhiteSpace(); //comment or blank if (buffer3.Is("#")) { buffer3.SkipUntilNewLine(); continue; } //uv if (buffer3.Is("vt")) { UVs.Add(buffer3.ReadVector()); continue; } buffer3.SkipUntilNewLine(); } //reading 'f' while (!isFaceRead) { buffer2.SkipWhitespaces(); if (buffer2.endReached == true) { isFaceRead = true; break; } buffer2.ReadUntilWhiteSpace(); //comment or blank if (buffer2.Is("#")) { buffer2.SkipUntilNewLine(); continue; } //face data (the fun part) if (buffer2.Is("f")) { //loop through indices while (true) { bool newLinePassed; buffer2.SkipWhitespaces(out newLinePassed); if (newLinePassed == true) { break; } int vertexIndex = int.MinValue; int normalIndex = int.MinValue; int uvIndex = int.MinValue; vertexIndex = buffer2.ReadInt(); normalIndex = uvIndex = vertexIndex; /*if (buffer2.currentChar == '/') * { * buffer2.MoveNext(); * if (buffer2.currentChar != '/') * { * uvIndex = buffer2.ReadInt(); * } * if (buffer2.currentChar == '/') * { * buffer2.MoveNext(); * normalIndex = buffer2.ReadInt(); * } * }*/ //"postprocess" indices if (vertexIndex > int.MinValue) { if (vertexIndex < 0) { vertexIndex = Vertices.Count - vertexIndex; } vertexIndex--; } if (normalIndex > int.MinValue) { if (normalIndex < 0) { normalIndex = Normals.Count - normalIndex; } normalIndex--; } if (uvIndex > int.MinValue) { if (uvIndex < 0) { uvIndex = UVs.Count - uvIndex; } uvIndex--; } //set array values vertexIndices.Add(vertexIndex); normalIndices.Add(normalIndex); uvIndices.Add(uvIndex); } //push to builder currentBuilder.PushFace(currentMaterial, vertexIndices, normalIndices, uvIndices); //clear lists vertexIndices.Clear(); normalIndices.Clear(); uvIndices.Clear(); continue; } buffer2.SkipUntilNewLine(); } // reading 'pi" while (!isPartRead) { buffer4.SkipWhitespaces(); if (buffer4.endReached == true) { isPartRead = true; break; } buffer4.ReadUntilWhiteSpace(); //comment or blank if (buffer4.Is("#")) { buffer4.SkipUntilNewLine(); continue; } //uv if (buffer4.Is("pi")) { Parts.Add(buffer4.ReadVector()); continue; } buffer4.SkipUntilNewLine(); } //finally, put it all together GameObject obj = new GameObject("person"); obj.transform.localScale = new Vector3(-1f, 1f, 1f); foreach (var builder in builderDict) { //empty object if (builder.Value.PushedFaceCount == 0) { continue; } var builtObj = builder.Value.Build(); builtObj.transform.SetParent(obj.transform, false); } tf = obj.transform.GetChild(0); mesh = tf.GetComponent <MeshFilter>().mesh; mesh.RecalculateNormals(); //foreach (var vertex in Vertices) //{ // mesh.vertices[num].x = vertex.x; // mesh.vertices[num].y = vertex.y; // mesh.vertices[num].z = vertex.z; // num++; //} //if (!isFaceMapped) //{ // num = 0; // foreach (var index in vertexIndices) // { // mesh.triangles[num] = index; // num++; // } // isFaceMapped = true; //} //if (!isUvMapped) //{ // num = 0; // foreach (var uv in UVs) // { // mesh.uv[num].x = uv.x; // mesh.uv[num].y = uv.y; // num++; // } // isUvMapped = true; //} reader1.Close(); reader2.Close(); reader3.Close(); reader4.Close(); Find_Hand(obj); Find_Center(obj); return(obj); }
/// <summary> /// Load an OBJ file from a stream. No materials will be loaded, and will instead be supplemented by a blank white material. /// </summary> /// <param name="input">Input OBJ stream</param> /// <returns>Returns a GameObject represeting the OBJ file, with each imported object as a child.</returns> public GameObject Load(Stream input) { var reader = new StreamReader(input); //var reader = new StringReader(inputReader.ReadToEnd()); Dictionary <string, OBJObjectBuilder> builderDict = new Dictionary <string, OBJObjectBuilder>(); OBJObjectBuilder currentBuilder = null; string currentMaterial = "default"; //lists for face data //prevents excess GC List <int> vertexIndices = new List <int>(); List <int> normalIndices = new List <int>(); List <int> uvIndices = new List <int>(); //helper func Action <string> setCurrentObjectFunc = (string objectName) => { if (!builderDict.TryGetValue(objectName, out currentBuilder)) { currentBuilder = new OBJObjectBuilder(objectName, this); builderDict[objectName] = currentBuilder; } }; //create default object setCurrentObjectFunc.Invoke("default"); //var buffer = new DoubleBuffer(reader, 256 * 1024); var buffer = new CharWordReader(reader, 4 * 1024); //do the reading while (true) { buffer.SkipWhitespaces(); if (buffer.endReached == true) { break; } buffer.ReadUntilWhiteSpace(); //comment or blank if (buffer.Is("#")) { buffer.SkipUntilNewLine(); continue; } if (Materials == null && buffer.Is("mtllib")) { buffer.SkipWhitespaces(); buffer.ReadUntilNewLine(); string mtlLibPath = buffer.GetString(); LoadMaterialLibrary(mtlLibPath); continue; } if (buffer.Is("v")) { Vertices.Add(buffer.ReadVector()); continue; } //normal if (buffer.Is("vn")) { Normals.Add(buffer.ReadVector()); continue; } //uv if (buffer.Is("vt")) { UVs.Add(buffer.ReadVector()); continue; } //new material if (buffer.Is("usemtl")) { buffer.SkipWhitespaces(); buffer.ReadUntilNewLine(); string materialName = buffer.GetString(); currentMaterial = materialName; if (SplitMode == SplitMode.Material) { setCurrentObjectFunc.Invoke(materialName); } continue; } //new object if ((buffer.Is("o") || buffer.Is("g")) && SplitMode == SplitMode.Object) { buffer.ReadUntilNewLine(); string objectName = buffer.GetString(1); setCurrentObjectFunc.Invoke(objectName); continue; } //face data (the fun part) if (buffer.Is("f")) { //loop through indices while (true) { bool newLinePassed; buffer.SkipWhitespaces(out newLinePassed); if (newLinePassed == true) { break; } int vertexIndex = int.MinValue; int normalIndex = int.MinValue; int uvIndex = int.MinValue; vertexIndex = buffer.ReadInt(); if (buffer.currentChar == '/') { buffer.MoveNext(); if (buffer.currentChar != '/') { uvIndex = buffer.ReadInt(); } if (buffer.currentChar == '/') { buffer.MoveNext(); normalIndex = buffer.ReadInt(); } } //"postprocess" indices if (vertexIndex > int.MinValue) { if (vertexIndex < 0) { vertexIndex = Vertices.Count - vertexIndex; } vertexIndex--; } if (normalIndex > int.MinValue) { if (normalIndex < 0) { normalIndex = Normals.Count - normalIndex; } normalIndex--; } if (uvIndex > int.MinValue) { if (uvIndex < 0) { uvIndex = UVs.Count - uvIndex; } uvIndex--; } //set array values vertexIndices.Add(vertexIndex); normalIndices.Add(normalIndex); uvIndices.Add(uvIndex); } //push to builder currentBuilder.PushFace(currentMaterial, vertexIndices, normalIndices, uvIndices); //clear lists vertexIndices.Clear(); normalIndices.Clear(); uvIndices.Clear(); continue; } buffer.SkipUntilNewLine(); } //finally, put it all together GameObject obj = new GameObject(_objInfo != null ? Path.GetFileNameWithoutExtension(_objInfo.Name) : "WavefrontObject"); //ADICIONADO obj.gameObject.name = "Objeto"; obj.transform.localScale = new Vector3(1f, 1f, 1f); //ADICIONADO obj.gameObject.AddComponent <AddBoxCollider>(); obj.gameObject.AddComponent <RotateObjectWithMouse>(); obj.gameObject.AddComponent <ResetSelfRotation>(); //ADICIONADO var center = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, Camera.main.nearClipPlane * 10)); Camera.main.GetComponent <CenterCamera>().SetTarget(obj); obj.transform.position = new Vector3(center.x, center.y, Camera.main.nearClipPlane); Camera.main.GetComponent <FrameObject>().CallCoroutine(Camera.main, obj); foreach (var builder in builderDict) { //empty object if (builder.Value.PushedFaceCount == 0) { continue; } var builtObj = builder.Value.Build(); builtObj.transform.SetParent(obj.transform, false); } Debug.Log("the obj: " + obj.name); return(obj); }