コード例 #1
0
ファイル: Receiver.cs プロジェクト: neon-izm/StreamingMesh
        IEnumerator CreateInitialData()
        {
            HttpWrapper wrapper = new HttpWrapper();

            //Get ChannelInfo
            ChannelInfo channelInfo = null;

            wrapper.RequestInfo <ChannelInfo>(GetAbsoluteURL(m_PlaylistName), info => { channelInfo = info; });
            yield return(new WaitUntil(wrapper.RequestFinished));

            if (channelInfo != null)
            {
                //Get PlayLists and others
                m_StreamPlayListName = channelInfo.stream_info;
                m_AudioPlayListName  = channelInfo.audio_info;
                m_PollingInterval    = channelInfo.frame_interval * channelInfo.combined_frames;

                //Get CombinedData
                byte[] combinedData = null;
                wrapper.RequestBinary(GetAbsoluteURL(channelInfo.data), bin => {
                    if (bin != null)
                    {
                        combinedData = ExternalTools.Decompress(bin);
                    }
                });
                yield return(new WaitUntil(wrapper.RequestFinished));

                if (combinedData == null)
                {
                    yield break;
                }

                StreamingMeshRenderer meshRenderer = new StreamingMeshRenderer
                {
                    ContainerSize  = channelInfo.container_size,
                    PackageSize    = channelInfo.package_size,
                    FrameInterval  = channelInfo.frame_interval,
                    CombinedFrames = channelInfo.combined_frames
                };

                StreamingAudioRenderer audioRenderer = new StreamingAudioRenderer
                {
                    FrameInterval  = channelInfo.frame_interval,
                    CombinedFrames = channelInfo.combined_frames
                };
#if !UNITY_WEBGL
                if (!string.IsNullOrEmpty(channelInfo.audio_info))
                {
                    m_AudioRenderer = audioRenderer;
                }
#else
                if (!string.IsNullOrEmpty(channelInfo.audio_clip))
                {
                    m_AudioRenderer = audioRenderer;
                }
#endif

                //Split Textures and Materials and Mesh from CombinedData
                int offsetBytes = 0;

                //Split Textures
                List <string> textureNames = channelInfo.textures;
                List <int>    textureSizes = channelInfo.textureSizes;
                for (int i = 0; i < textureSizes.Count; i++)
                {
                    int       size    = textureSizes[i];
                    Texture2D texture = TextureConverter.DeserializeFromBinary(combinedData, offsetBytes, size);
                    string    name    = textureNames[i];
                    if (texture != null)
                    {
                        meshRenderer.AddTexture(name, texture);
                    }
                    offsetBytes += size;
                    yield return(null);
                }

                //Split Materials
                List <string> materialNames = channelInfo.materials;
                List <int>    materialSizes = channelInfo.materialSizes;
                for (int i = 0; i < materialSizes.Count; i++)
                {
                    int      size     = materialSizes[i];
                    Material material = MaterialConverter.DeserializeFromBinary(
                        combinedData, offsetBytes, size, m_CustomShaders, m_DefaultShader, meshRenderer.TextureDictionary);
                    string name = material.name;
                    meshRenderer.AddMaterial(name, material);
                    offsetBytes += size;
                    yield return(null);
                }

                GameObject rootGameObject = new GameObject("RootGameObject");
                rootGameObject.transform.SetParent(transform, false);

                //Split Meshes
                List <string> meshNames = channelInfo.meshes;
                List <int>    meshSizes = channelInfo.meshSizes;
                for (int i = 0; i < meshSizes.Count; i++)
                {
                    string        name         = meshNames[i];
                    int           size         = meshSizes[i];
                    List <string> refMaterials = null;
                    Mesh          mesh         = MeshConverter.DeserializeFromBinary(
                        combinedData, offsetBytes, size, channelInfo.container_size, out refMaterials);

                    List <Material> materials = new List <Material>();
                    for (int j = 0; j < refMaterials.Count; j++)
                    {
                        Material material = null;
                        if (meshRenderer.MaterialDictionary.TryGetValue(refMaterials[i], out material))
                        {
                            materials.Add(material);
                            yield return(null);
                        }
                    }

                    GameObject obj = new GameObject("Mesh_" + name);
                    obj.transform.SetParent(rootGameObject.transform, false);
                    MeshFilter   meshFilter = obj.AddComponent <MeshFilter>();
                    MeshRenderer renderer   = obj.AddComponent <MeshRenderer>();

                    meshFilter.mesh    = mesh;
                    renderer.materials = materials.ToArray();
                    meshRenderer.AddMesh(name, mesh);
                    offsetBytes += size;
                    yield return(null);
                }

                //CreateVertexBuffer;
                meshRenderer.CreateVertexBuffer();
                meshRenderer.CreateVertexContainer(channelInfo.package_size, channelInfo.container_size);
                meshRenderer.RootGameObject = rootGameObject;
                m_MeshRenderer = meshRenderer;
            }
        }