Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read .obj and converted to ctm but failed to load in three.js #12

Open
mayurdp313 opened this issue Jul 11, 2019 · 0 comments
Open

Read .obj and converted to ctm but failed to load in three.js #12

mayurdp313 opened this issue Jul 11, 2019 · 0 comments

Comments

@mayurdp313
Copy link

When I read the .obj file and convert to Mesh
to generate ctm File
it was created but failed to load in three.js

it show following message

Uncaught Error in data stream
*** sample .obj file is uploaded

`using OpenCTM;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{

class Program
{
    private static float[] vert = new float[]{  0,0,0,
                                                1,0,0,
                                                1,1,0,
                                                0,1,0};//v
    private static float[] normals = new float[]{1,1,1,
                                                1,1,1,
                                                1,1,1,
                                                1,1,1};//vn
    private static AttributeData[] uvad = { new AttributeData ("uv1", "test",
                                                                AttributeData.STANDARD_UV_PRECISION,
                                                                new float[]{0.0034f, 0.1f,
                                                                            0.8f, 1.0f,
                                                                            0.351f, 0.612f,
                                                                            0.1229f, 0.91224f}) };//vt
    private static int[] ind = new int[] { 0, 1, 2, 0, 2, 3 };//f
    private static Mesh quad = new Mesh(vert, normals, ind, uvad, new AttributeData[0]);
    
    
    public void rawTest()
    {
        testEncoder(new RawEncoder());
    }

    public void mg1Test()
    {
        testEncoder(new MG1Encoder());
    }

    public void mg2Test()
    {
        testEncoder(new MG2Encoder(MG2Encoder.STANDARD_VERTEX_PRECISION, MG2Encoder.STANDARD_NORMAL_PRECISION));
    }

    private void testEncoder(MeshEncoder encoder)
    {
        MemoryStream memory = new MemoryStream();
        CtmFileWriter writer = new CtmFileWriter(memory, encoder);
        writer.encode(quad, null);

        memory.Seek(0, SeekOrigin.Begin);
        using (FileStream file = new FileStream("mayu.ctm", FileMode.Create, System.IO.FileAccess.Write))
            memory.CopyTo(file);
        Stream readMemory = new MemoryStream(memory.ToArray());
        CtmFileReader reader = new CtmFileReader(readMemory);
        Mesh m = reader.decode();

        m.checkIntegrity();

        //MG2Encoder mg2 = encoder as MG2Encoder;

        //Chech file integirty
        //MemoryStream ms = new MemoryStream();
        //using (FileStream file = new FileStream("col.ctm", FileMode.Open, FileAccess.Read))
        //    file.CopyTo(ms);

        //Stream readMemory1 = new MemoryStream(ms.ToArray());
        //CtmFileReader reader1 = new CtmFileReader(readMemory1);
        //Mesh m2 = reader1.decode();

        //m2.checkIntegrity();
        //float[] v = m2.vertices;
        //float[] n = m2.normals;
        //AttributeData []ad = m2.texcoordinates;
        //int[] index = m2.indices;
        //var t= m2.getVertexCount();
        //var c = m2.getTriangleCount();

        
    }

    static void Main(string[] args)
    {
        Program p = new ConsoleApplication1.Program();

        var filrread = File.ReadAllLines(@"D:\\collar.obj");
       //var filrread = File.ReadAllLines(@"D:\\Box.obj");
       // var filrread = File.ReadAllLines(@"D:\\boxxxx.obj");

        var ctmName = filrread.Where(x => x.Contains("object")).Select(x => x.Split(' ')[2]).FirstOrDefault();

        var vcount = Convert.ToInt32(filrread.Where(x => x.Contains("vertices")).Select(x => x.Split(' ')[1]).FirstOrDefault())*3;
        var vncount = Convert.ToInt32(filrread.Where(x => x.Contains("normals")).Select(x => x.Split(' ')[1]).FirstOrDefault())*3;
        var vtcount = Convert.ToInt32(filrread.Where(x => x.Contains("texture")).Select(x => x.Split(' ')[1]).FirstOrDefault())*3;

        var fcount = Convert.ToInt32(filrread.Where(x => x.Contains("triangles")).Select(x => x.Split(' ')[4]).FirstOrDefault());

        var v = filrread.Where(x => x.StartsWith("v ")).Select(x => x.Replace("v  ", string.Empty).Split(' ').ToList()).ToList();
        var vn = filrread.Where(x => x.StartsWith("vn ")).Select(x => x.Replace("vn ", string.Empty).Split(' ').ToList()).ToList();
        var vt = filrread.Where(x => x.StartsWith("vt ")).Select(x => x.Replace("vt ", string.Empty).Split(' ').ToList()).ToList();

        var f = filrread.Where(x => x.StartsWith("f ")).Select(x => x.Replace("f ", string.Empty).TrimEnd().Split(' ').ToList().Select(ac => ac.Split('/')[0])).ToList();

        List<float> vertex = new List<float>();
        List<float> normal = new List<float>();
        List<float> uv = new List<float>();
        List<int> index = new List<int>();

        int cnt = 0;
        foreach (var item in v)
        {
            foreach (var itemList in item)
            {
                vertex.Add(Convert.ToSingle(itemList));
                //normal.Add(cnt);
                //uv.Add(cnt);
                //cnt++;
            }
        }

        foreach (var item in vn)
        {
            foreach (var itemList in item)
            {
                normal.Add(Convert.ToSingle(itemList));
            }
        }

        foreach (var item in vt)
        {
            //foreach (var itemList in item)
            for (int i = 0; i < item.Count - 1; i++)
            {
                //uv.Add(Convert.ToSingle(itemList[i]));
                uv.Add(Convert.ToSingle(item[i]));
            }
        }

        foreach (var item in f)
        {
            foreach (var itemList in item)
            {
                index.Add(Convert.ToInt32(itemList)-1);  // remove -1 For Face adjustment
            }
        }

        //if (vertex.Count == vcount && normal.Count == vncount && uv.Count == (vtcount-vt.Count) && f.Count == fcount)
        {

            vert = vertex.ToArray();
            normals = normal.ToArray();
            AttributeData ad = new AttributeData(ctmName, "1", AttributeData.STANDARD_UV_PRECISION, uv.ToArray());
            List<AttributeData> adlist = new List<AttributeData>();
            adlist.Add(ad);
            ind = index.ToArray();

            quad = new Mesh(vert, normals, ind, adlist.ToArray(), new AttributeData[0]);
        }


        p.mg2Test();
    }
}

}
collar.obj.txt
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant