public void Uninitialize()
        {
            if (_dataSource == null)
            {
                return;
            }

            StopCoroutine("SequenceTrigger");

            //Releases sequence
            Bridge4DS.DestroySequence(_dataSource.FDVUUID);
            _dataSource = null;

            //Releases memory
            _newVerticesHandle.Free();
            _newUVsHandle.Free();
            _newTrianglesHandle.Free();
            _newTextureDataHandle.Free();
            if (_computeNormals)
            {
                _newNormalsHandle.Free();
            }

            if (!_preview)
            {
                for (int i = 0; i < _nbGeometryBuffers; i++)
                {
                    Destroy(_meshes[i]);
                }
                _meshes = null;
                for (int i = 0; i < _nbTextureBuffers; i++)
                {
                    Destroy(_textures[i]);
                }
                _textures = null;
            }

            _newVertices    = null;
            _newUVs         = null;
            _newTriangles   = null;
            _newNormals     = null;
            _newTextureData = null;

            _isSequenceTriggerON = false;
            _isInitialized       = false;

#if UNITY_EDITOR
#if UNITY_2017_3_OR_NEWER
            EditorApplication.playModeStateChanged -= HandleOnPlayModeChanged;
#else
            EditorApplication.playmodeStateChanged -= HandleOnPlayModeChanged;
#endif
#endif
        }
        //Static constructor: creates a data source or returns null when no source can be created
        static public DataSource4DS CreateNetworkSource(string serverip, int serverport)
        {
            bool success = false;

            DataSource4DS instance = new DataSource4DS(serverip, serverport, ref success);

            if (success)
            {
                return(instance);
            }
            else
            {
                Debug.LogError("FDV Error: cannot create network source");
                return(null);
            }
        }
Example #3
0
        void Uninitialize()
        {
            if (!_isInitialized)
            {
                return;
            }

            //Releases sequence
            if (_dataSource != null)
            {
                Bridge4DS.DestroySequence(_dataSource.FDVUUID);
            }

            //Releases memory
            if (_newVerticesHandle.IsAllocated)
            {
                _newVerticesHandle.Free();
            }
            if (_newUVsHandle.IsAllocated)
            {
                _newUVsHandle.Free();
            }
            if (_newTrianglesHandle.IsAllocated)
            {
                _newTrianglesHandle.Free();
            }
            if (_newTextureDataHandle.IsAllocated)
            {
                _newTextureDataHandle.Free();
            }
            if (_newNormalsHandle.IsAllocated)
            {
                _newNormalsHandle.Free();
            }

            if (_meshes != null)
            {
                for (int i = 0; i < _meshes.Length; i++)
                {
                    DestroyImmediate(_meshes[i]);
                }
                _meshes = null;
            }
            if (_textures != null)
            {
                for (int i = 0; i < _textures.Length; i++)
                {
                    DestroyImmediate(_textures[i]);
                }
                _textures = null;
            }

            _dataSource     = null;
            _newVertices    = null;
            _newUVs         = null;
            _newTriangles   = null;
            _newNormals     = null;
            _newTextureData = null;

            _isSequenceTriggerON = false;
            _isInitialized       = false;

#if UNITY_EDITOR
            EditorApplication.pauseStateChanged -= HandlePauseState;
#endif
        }
Example #4
0
        //-----------------------------//
        //- Class methods implement.  -//
        //-----------------------------//



        public void Initialize()
        {
            //Initialize already called successfully
            if (_isInitialized == true)
            {
                return;
            }

            if (_dataSource == null)
            {
                int key = 0;
                //if (_sourceType == SOURCE_TYPE.Network)
                //{
                //    key = Bridge4DS.CreateConnection(_connexionHost, _connexionPort);
                //}

                //Creates data source from the given path
                _dataSource = DataSource4DS.CreateDataSource(key, _sequenceName, _dataInStreamingAssets, _mainDataPath, (int)_activeRangeMin, (int)_activeRangeMax, _outRangeMode);
                if (_dataSource == null)
                {
                    OnModelNotFound?.Invoke();
                    return;
                }
            }

            _lastModelId = -1;

            _meshComponent     = GetComponent <MeshFilter>();
            _rendererComponent = GetComponent <Renderer>();
            _meshCollider      = GetComponent <MeshCollider>();

            _nbFrames = Bridge4DS.GetSequenceNbFrames(_dataSource.FDVUUID);

            Bridge4DS.SetSpeed(_dataSource.FDVUUID, _speedRatio);

            if (_sourceType == SOURCE_TYPE.Network)
            {
                Bridge4DS.SetHTTPDownloadSize(_dataSource.FDVUUID, _HTTPDownloadSize);
                Bridge4DS.SetHTTPKeepInCache(_dataSource.FDVUUID, _HTTPKeepInCache);
            }

            Bridge4DS.SetChunkBufferMaxSize(_dataSource.FDVUUID, _chunkBufferMaxSize);
            Bridge4DS.SetMeshBufferMaxSize(_dataSource.FDVUUID, _meshBufferMaxSize);


            //Allocates geometry buffers
            AllocateGeometryBuffers(ref _newVertices, ref _newUVs, ref _newNormals, ref _newTriangles, _dataSource.MaxVertices, _dataSource.MaxTriangles);

            //Allocates texture pixel buffer
            int pixelBufferSize = _dataSource.TextureSize * _dataSource.TextureSize / 2; //default is 4 bpp

            if (_dataSource.TextureFormat == TextureFormat.PVRTC_RGB2)                   //pvrtc2 is 2bpp
            {
                pixelBufferSize /= 2;
            }
            if (_dataSource.TextureFormat == TextureFormat.ASTC_RGBA_8x8)
            {
                int blockSize = 8;
                int xblocks   = (_dataSource.TextureSize + blockSize - 1) / blockSize;
                pixelBufferSize = xblocks * xblocks * 16;
            }
            _newTextureData = new byte[pixelBufferSize];

            //Gets pinned memory handle
            _newVerticesHandle    = GCHandle.Alloc(_newVertices, GCHandleType.Pinned);
            _newUVsHandle         = GCHandle.Alloc(_newUVs, GCHandleType.Pinned);
            _newTrianglesHandle   = GCHandle.Alloc(_newTriangles, GCHandleType.Pinned);
            _newTextureDataHandle = GCHandle.Alloc(_newTextureData, GCHandleType.Pinned);
            _newNormalsHandle     = GCHandle.Alloc(_newNormals, GCHandleType.Pinned);

            //Allocates objects buffers for double buffering
            _meshes   = new Mesh[_nbGeometryBuffers];
            _textures = new Texture2D[_nbTextureBuffers];

            for (int i = 0; i < _nbGeometryBuffers; i++)
            {
                //Mesh
                Mesh mesh = new Mesh();
                if (_dataSource.MaxVertices > MAX_SHORT)
                {
                    mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
                }
                mesh.MarkDynamic(); //Optimize mesh for frequent updates. Call this before assigning vertices.
                mesh.vertices  = _newVertices;
                mesh.uv        = _newUVs;
                mesh.triangles = _newTriangles;
                mesh.normals   = _newNormals;

                Bounds newBounds = mesh.bounds;
                newBounds.extents = new Vector3(4, 4, 4);
                mesh.bounds       = newBounds;
                _meshes[i]        = mesh;
            }

            for (int i = 0; i < _nbTextureBuffers; i++)
            {
                //Texture
#if UNITY_2019_1_OR_NEWER
                if (_dataSource.TextureFormat == TextureFormat.ASTC_RGBA_8x8)   //since unity 2019 ASTC RGBA is no more supported
                {
                    _dataSource.TextureFormat = TextureFormat.ASTC_8x8;
                }
#endif
                Texture2D texture = new Texture2D(_dataSource.TextureSize, _dataSource.TextureSize, _dataSource.TextureFormat, false)
                {
                    wrapMode   = TextureWrapMode.Clamp,
                    filterMode = FilterMode.Bilinear
                };
                texture.Apply(); //upload to GPU
                _textures[i] = texture;
            }

            _currentGeometryBuffer = _currentTextureBuffer = 0;

            _nbFrames      = Bridge4DS.GetSequenceNbFrames(_dataSource.FDVUUID);
            _isInitialized = true;
        }
Example #5
0
        //Static constructor: creates a data source or returns null when no source can be created
        static public DataSource4DS CreateDataSource(int key, string sequenceName, bool dataInStreamingAssets, string mainPath, int activeRangeBegin, int activeRangeLastFrame, OUT_RANGE_MODE outRangeMode)
        {
            bool   success = false;
            string rootpath;

            if (!sequenceName.StartsWith("http") && key == 0 && dataInStreamingAssets)
            {
                rootpath = Application.streamingAssetsPath + "/" + mainPath + sequenceName;

                //ANDROID STREAMING ASSETS => need to copy the data somewhere else on device to acces it, beacause it is currently in jar file
                if (rootpath.StartsWith("jar"))
                {
                    WWW www = new WWW(rootpath);
                    //yield return www; //can't do yield here, not really blocking beacause the data is local
                    while (!www.isDone)
                    {
                        ;
                    }

                    if (!string.IsNullOrEmpty(www.error))
                    {
                        Debug.LogError("PATH : " + rootpath);
                        Debug.LogError("Can't read data in streaming assets: " + www.error);
                    }
                    else
                    {
                        //copy data on device
                        rootpath = Application.persistentDataPath + "/" + sequenceName;
                        if (!System.IO.File.Exists(rootpath))
                        {
                            Debug.Log("4DVIEWS: NEW Roopath: " + rootpath);
                            System.IO.FileStream fs = System.IO.File.Create(rootpath);
                            fs.Write(www.bytes, 0, www.bytesDownloaded);
                            Debug.Log("4DVIEWS: data copied");
                            fs.Dispose();
                        }
                    }
                }

                Debug.Log("Create Instance");
                DataSource4DS instance = new DataSource4DS(key, rootpath, activeRangeBegin, activeRangeLastFrame, outRangeMode, ref success);
                if (success)
                {
                    Debug.Log("Instance Created ");
                    return(instance);
                }
                else
                {
                    Debug.LogError("FDV Error: cannot find data source at location " + rootpath);
                    return(null);
                }
            }
            else
            {
                if (sequenceName.StartsWith("http"))
                {
                    rootpath = sequenceName;
                }
                else
                {
                    rootpath = mainPath + sequenceName;
                }
                DataSource4DS instance = new DataSource4DS(key, rootpath, activeRangeBegin, activeRangeLastFrame, outRangeMode, ref success);
                if (success)
                {
                    return(instance);
                }
                else
                {
                    Debug.LogError("FDV Error: cannot find data source at " + rootpath);
                    return(null);
                }
            }
        }
        public void Initialize()
        {
            //Initialize already called successfully
            if (_isInitialized == true)
            {
                return;
            }

            if (_dataSource == null)
            {
                if (_sourceType == SOURCE_TYPE.Network)
                {
                    //Creates data source from server ip
                    _dataSource = DataSource4DS.CreateNetworkSource(_serverAddress, _serverPort);
                }
                else
                {
                    //Creates data source from the given path (directory or sequence.xml)
                    _dataSource = DataSource4DS.CreateDataSource(_sequenceName, _dataInStreamingAssets, _mainDataPath, (int)_activeRangeMin, (int)_activeRangeMax, _outRangeMode);
                }
                if (_dataSource == null)
                {
                    if (onModelNotFound != null)
                    {
                        onModelNotFound();
                    }
                    return;
                }
            }

            _lastModelId = -1;

            _meshComponent     = GetComponent <MeshFilter>();
            _rendererComponent = GetComponent <Renderer>();
            _meshCollider      = GetComponent <MeshCollider>();

            Bridge4DS.SetComputeNormals(_dataSource.FDVUUID, _computeNormals);

            //Allocates geometry buffers
            AllocateGeometryBuffers(ref _newVertices, ref _newUVs, ref _newNormals, ref _newTriangles, _dataSource.MaxVertices, _dataSource.MaxTriangles);

            //Allocates texture pixel buffer
            int pixelBufferSize = _dataSource.TextureSize * _dataSource.TextureSize / 2; //default is 4 bpp

            if (_dataSource.TextureFormat == TextureFormat.PVRTC_RGB2)                   //pvrtc2 is 2bpp
            {
                pixelBufferSize /= 2;
            }
            if (_dataSource.TextureFormat == TextureFormat.ASTC_RGBA_8x8)
            {
                int blockSize = 8;
                int xblocks   = (_dataSource.TextureSize + blockSize - 1) / blockSize;
                pixelBufferSize = xblocks * xblocks * 16;
            }
            _newTextureData = new byte[pixelBufferSize];

            //Gets pinned memory handle
#if USE_NATIVE_LIB
            _newVerticesHandle    = GCHandle.Alloc(_newVertices, GCHandleType.Pinned);
            _newUVsHandle         = GCHandle.Alloc(_newUVs, GCHandleType.Pinned);
            _newTrianglesHandle   = GCHandle.Alloc(_newTriangles, GCHandleType.Pinned);
            _newTextureDataHandle = GCHandle.Alloc(_newTextureData, GCHandleType.Pinned);
            if (_computeNormals)
            {
                _newNormalsHandle = GCHandle.Alloc(_newNormals, GCHandleType.Pinned);
            }
#endif

            //Allocates objects buffers for double buffering
            _meshes   = new Mesh[_nbGeometryBuffers];
            _textures = new Texture2D[_nbTextureBuffers];

            for (int i = 0; i < _nbGeometryBuffers; i++)
            {
                //Mesh
                Mesh mesh = new Mesh();
                mesh.MarkDynamic(); //Optimize mesh for frequent updates. Call this before assigning vertices.
                mesh.vertices  = _newVertices;
                mesh.uv        = _newUVs;
                mesh.triangles = _newTriangles;
                if (_computeNormals)
                {
                    mesh.normals = _newNormals;
                }

                Bounds newBounds = mesh.bounds;
                newBounds.extents = new Vector3(10, 10, 10);
                mesh.bounds       = newBounds;
                _meshes[i]        = mesh;
            }


            for (int i = 0; i < _nbTextureBuffers; i++)
            {
                //Texture
                Texture2D texture = new Texture2D(_dataSource.TextureSize, _dataSource.TextureSize, _dataSource.TextureFormat, false);
                texture.wrapMode   = TextureWrapMode.Clamp;
                texture.filterMode = FilterMode.Point;
                texture.Apply(); //upload to GPU
                _textures[i] = texture;
            }

            Bridge4DS.SetBuffering(_dataSource.FDVUUID, _bufferMode, _bufferSize);

            //Bridge4DS.SetCachingMode(_dataSource.FDVUUID, _cachingMode);
            _currentGeometryBuffer = _currentTextureBuffer = 0;

            if (_autoPlay)
            {
                Play(true);
            }

            _isInitialized = true;
        }