protected void ConstructRawMesh(string id, BinaryReader br, int size, Vector3 bbMin, Vector3 bbMax)
        {
            // TODO: solve the issue that OpenCTM does NOT support multi-threading
            lock (PCQueue.Current.LockerForUser)
                if (_rawMeshCache.ContainsKey(id) == false)
                {
                    OpenCTM.CtmFileReader reader = new OpenCTM.CtmFileReader(br.BaseStream);
                    OpenCTM.Mesh          mesh   = reader.decode();

                    RawMesh rawMesh = new RawMesh();

                    mesh.checkIntegrity();

                    {
                        List <Vector3> Vertices = new List <Vector3>();
                        for (int j = 0; j < mesh.getVertexCount(); j++)
                        {
                            Vertices.Add(new Vector3(mesh.vertices[(j * 3)], mesh.vertices[(j * 3) + 2], mesh.vertices[(j * 3) + 1]));
                        }
                        rawMesh.Vertices = Vertices.ToArray();
                    }

                    {
                        List <int> Triangles = new List <int>();
                        for (int j = 0; j < mesh.indices.Length / 3; j++)
                        {
                            Triangles.Add(mesh.indices[(j * 3)]);
                            Triangles.Add(mesh.indices[(j * 3) + 2]);
                            Triangles.Add(mesh.indices[(j * 3) + 1]);
                        }
                        rawMesh.Triangles = Triangles.ToArray();
                    }

                    if (mesh.getUVCount() > 0)
                    {
                        List <Vector2> UVList = new List <Vector2>();
                        for (int j = 0; j < mesh.texcoordinates[0].values.Length / 2; j++)
                        {
                            UVList.Add(new Vector2(mesh.texcoordinates[0].values[(j * 2)], 1 - mesh.texcoordinates[0].values[(j * 2) + 1]));
                        }
                        rawMesh.UVList = UVList.ToArray();
                    }

                    if (mesh.hasNormals())
                    {
                        List <Vector3> Normals = new List <Vector3>();
                        for (int j = 0; j < mesh.getVertexCount(); j++)
                        {
                            Normals.Add(new Vector3(mesh.normals[(j * 3)], mesh.normals[(j * 3) + 2], mesh.normals[(j * 3) + 1]));
                        }
                        rawMesh.Normals = Normals.ToArray();
                    }
                    rawMesh.BBMin = bbMin;
                    rawMesh.BBMax = bbMax;

                    _rawMeshCache.Add(id, rawMesh);
                }
        }
Beispiel #2
0
        //public override void CancelRequest () ;

        public override void ProcessResponse(AsyncCompletedEventArgs e)
        {
            //TimeSpan tm = DateTime.Now - emitted;
            //UnityEngine.Debug.Log ("Received: " + tm.TotalSeconds.ToString () + " / " + uri.ToString ());
            DownloadDataCompletedEventArgs args = e as DownloadDataCompletedEventArgs;

            try {
                byte [] bytes = args.Result;
                if (compression)
                {
                    bytes = RequestObjectInterface.Decompress(bytes);
                }

                List <Eppy.Tuple <int, int, OpenCTM.Mesh> > tmp = new List <Eppy.Tuple <int, int, OpenCTM.Mesh> > ();
                int len = BitConverter.ToInt32(bytes, 0);
                for (int i = 0; i < len; i++)
                {
                    int dbId   = BitConverter.ToInt32(bytes, ((i * 3) + 1) * sizeof(Int32));
                    int fragId = BitConverter.ToInt32(bytes, ((i * 3) + 2) * sizeof(Int32));
                    int offset = BitConverter.ToInt32(bytes, ((i * 3) + 3) * sizeof(Int32));
                    int end    = bytes.Length;
                    if (i < len - 1)
                    {
                        end = BitConverter.ToInt32(bytes, (((i + 1) * 3) + 3) * sizeof(Int32));
                    }

                    byte [] ctm = RequestObjectInterface.Decompress(bytes.Skip(offset).Take(end - offset).ToArray());

                    System.IO.Stream      readMemory = new System.IO.MemoryStream(ctm);                // (bytes, offset, end - offset);
                    OpenCTM.CtmFileReader reader     = new OpenCTM.CtmFileReader(readMemory);
                    //_openctm.Add (new Eppy.Tuple<int, int, OpenCTM.Mesh> (dbId, fragId, reader.decode ()));
                    Eppy.Tuple <int, int, OpenCTM.Mesh> mesh = _openctm.Single(x => x.Item1 == dbId && x.Item2 == fragId);
                    tmp.Add(new Eppy.Tuple <int, int, OpenCTM.Mesh> (mesh.Item1, mesh.Item2, reader.decode()));
                }
                _openctm.Clear();
                _openctm = tmp;

                state = SceneLoadingStatus.eReceived;
            } catch (Exception ex) {
                Debug.Log(ForgeLoader.GetCurrentMethod() + " " + ex.Message);
                state = SceneLoadingStatus.eError;
            } finally {
            }
        }
Beispiel #3
0
        protected IEnumerator ConstructMesh(string id, BinaryReader br, int size, Vector3 bbMin, Vector3 bbMax)
        {
            // NOTE: OpenCTM does NOT support multi-threading
            if (_MeshCache.ContainsKey(id) == false)
            {
                OpenCTM.CtmFileReader reader = new OpenCTM.CtmFileReader(br.BaseStream);
                yield return(null);

                OpenCTM.Mesh mesh = reader.decode();
                yield return(null);

                UnityEngine.Mesh um = new UnityEngine.Mesh();
                {
                    Vector3[] Vertices = new Vector3[mesh.getVertexCount()];
                    for (int j = 0; j < mesh.getVertexCount(); j++)
                    {
                        Vertices[j].x = mesh.vertices[(j * 3)];
                        Vertices[j].y = mesh.vertices[(j * 3) + 2];
                        Vertices[j].z = mesh.vertices[(j * 3) + 1];
                    }
                    um.vertices = Vertices;
                }
                yield return(null);

                {
                    int[] Triangles = new int[mesh.indices.Length];
                    for (int j = 0; j < mesh.indices.Length / 3; j++)
                    {
                        Triangles[(j * 3)]     = mesh.indices[(j * 3)];
                        Triangles[(j * 3) + 1] = mesh.indices[(j * 3) + 2];
                        Triangles[(j * 3) + 2] = mesh.indices[(j * 3) + 1];
                    }
                    um.triangles = Triangles;
                }
                yield return(null);

                if (mesh.getUVCount() > 0)
                {
                    Vector2[] UVList = new Vector2[mesh.texcoordinates[0].values.Length / 2];
                    for (int j = 0; j < mesh.texcoordinates[0].values.Length / 2; j++)
                    {
                        UVList[j].x = mesh.texcoordinates[0].values[(j * 2)];
                        UVList[j].y = mesh.texcoordinates[0].values[(j * 2) + 1];
                    }
                    um.uv = UVList;
                }
                yield return(null);

                if (mesh.hasNormals())
                {
                    Vector3[] Normals = new Vector3[mesh.getVertexCount()];
                    for (int j = 0; j < mesh.getVertexCount(); j++)
                    {
                        Normals[j].x = mesh.normals[(j * 3)];
                        Normals[j].y = mesh.normals[(j * 3) + 2];
                        Normals[j].z = mesh.normals[(j * 3) + 1];
                    }
                    um.normals = Normals;
                }
                else
                {
                    um.RecalculateNormals();
                }
                yield return(null);

                um.bounds.SetMinMax(bbMin, bbMax);

                _MeshCache.Add(id, um);
            }
        }