private void SetOutRangeMode(OUT_RANGE_MODE mode) { if (_dataSource != null) { Bridge4DS.ChangeOutRangeMode(_dataSource.FDVUUID, mode); } _outRangeMode = mode; }
public void GotoFrame(int frame) { bool wasPlaying = _isPlaying; Play(false); Bridge4DS.GotoFrame(_dataSource.FDVUUID, frame); Play(wasPlaying); UpdateMesh(); }
public void Stop() { if (_isSequenceTriggerON == true) { Bridge4DS.Stop(_dataSource.FDVUUID); StopCoroutine("SequenceTrigger"); _isSequenceTriggerON = false; } _isPlaying = false; }
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 }
private int GetSequenceNbFrames() { if (_dataSource != null) { return(Bridge4DS.GetSequenceNbFrames(_dataSource.FDVUUID)); } else { return(_nbFrames); } }
private void UpdateMesh() { if (_dataSource == null) { return; } //Get the new model int modelId = Bridge4DS.UpdateModel(_dataSource.FDVUUID, _newVerticesHandle.AddrOfPinnedObject(), _newUVsHandle.AddrOfPinnedObject(), _newTrianglesHandle.AddrOfPinnedObject(), _newTextureDataHandle.AddrOfPinnedObject(), _newNormalsHandle.AddrOfPinnedObject(), _lastModelId, ref _nbVertices, ref _nbTriangles); //Check if there is model if (!_newMeshAvailable) { _newMeshAvailable = (modelId != -1 && modelId != _lastModelId); } if (modelId == -1) { modelId = _lastModelId; } else { _lastModelId = modelId; } if (_debugInfo) { double timeInMSeconds = System.DateTime.Now.Subtract(_lastDecodingTime).TotalMilliseconds; if (_lastDecodingId == 0 || timeInMSeconds > 500f) { _decodingFPS = (float)((double)(Mathf.Abs((float)(modelId - _lastDecodingId))) / timeInMSeconds) * 1000f; _lastDecodingTime = System.DateTime.Now; _lastDecodingId = modelId; } } }
void PlayOnFocus(bool on) { if (on && _isPlaying) { if (_isSequenceTriggerON == false) { Bridge4DS.Play(_dataSource.FDVUUID, on); StartCoroutine("SequenceTrigger"); _isSequenceTriggerON = true; } } else { if (_isSequenceTriggerON == true) { Bridge4DS.Play(_dataSource.FDVUUID, on); StopCoroutine("SequenceTrigger"); _isSequenceTriggerON = false; } } }
private void InitAudio() { //setup audio if there is one inside the 4ds file int audioSize = Bridge4DS.GetAudioBufferSize(_dataSource.FDVUUID); if (audioSize > 0) { GameObject audioNode; AudioSource audioSource; //check if audio node already exists Sync4DS sync = GetComponent <Sync4DS>(); if (sync._audioSources.Length > 0 && sync._audioSources[0].audioSource) { audioSource = sync._audioSources[0].audioSource; audioNode = sync._audioSources[0].audioSource.gameObject; } else { audioNode = new GameObject("Audio4DS"); audioNode.transform.parent = this.transform.parent; audioSource = audioNode.AddComponent <AudioSource>(); } int nbSamples = Bridge4DS.GetAudioNbSamples(_dataSource.FDVUUID); samples = new float[nbSamples * Bridge4DS.GetAudioNbChannels(_dataSource.FDVUUID)]; audioBufferHandle = GCHandle.Alloc(samples, GCHandleType.Pinned); Bridge4DS.GetAudioBuffer(_dataSource.FDVUUID, audioBufferHandle.AddrOfPinnedObject()); audioSource.clip = AudioClip.Create("audioInside4ds", nbSamples, Bridge4DS.GetAudioNbChannels(_dataSource.FDVUUID), Bridge4DS.GetAudioSampleRate(_dataSource.FDVUUID), false); audioSource.clip.SetData(samples, 0); Sync4DS synchro = GetComponent <Sync4DS>(); synchro._audioSources[0].audioSource = audioSource; synchro.enabled = true; } }
//Public functions public void Play(bool on) { if (on) { if (_isSequenceTriggerON == false) { Bridge4DS.Play(_dataSource.FDVUUID, on); StartCoroutine("SequenceTrigger"); _isSequenceTriggerON = true; _totalFramesPlayed = 0; _playDate = System.DateTime.Now; } } else { if (_isSequenceTriggerON == true) { Bridge4DS.Play(_dataSource.FDVUUID, on); StopCoroutine("SequenceTrigger"); _isSequenceTriggerON = false; } } _isPlaying = on; }
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 }
//-----------------------------// //- 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; }
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; }
//private constructor private DataSource4DS(int key, string rootpath, int activeRangeBegin, int activeRangeEnd, OUT_RANGE_MODE outRangeMode, ref bool success, ref string errorMsg) { this.FDVUUID = 0; success = true; //Create sequence with native plugin GCHandle errorMsgHandle = GCHandle.Alloc(errorMsg, GCHandleType.Pinned); this.FDVUUID = Bridge4DS.CreateSequence(key, rootpath, activeRangeBegin, activeRangeEnd, outRangeMode, errorMsgHandle.AddrOfPinnedObject()); errorMsg = Marshal.PtrToStringAnsi(errorMsgHandle.AddrOfPinnedObject()); if (this.FDVUUID == 0) { success = false; } //Get sequence info if (success) { #if (UNITY_ANDROID || UNITY_IOS || UNITY_LUMIN) && !UNITY_EDITOR Bridge4DS.AddASTCSupport(this.FDVUUID); #else Bridge4DS.AddDXTSupport(this.FDVUUID); #endif this.TextureSize = Bridge4DS.GetTextureSize(this.FDVUUID); if (this.TextureSize == 0) { this.TextureSize = 1024; //put 1024 by default => will crash if we have 2048 texture and it's not written in xml fi } int textureEncoding = Bridge4DS.GetTextureEncoding(this.FDVUUID); switch (textureEncoding) { case 5: case 120: this.TextureFormat = TextureFormat.ETC_RGB4; break; case 6: case 130: this.TextureFormat = TextureFormat.PVRTC_RGB4; break; case 4: case 131: this.TextureFormat = TextureFormat.PVRTC_RGB2; break; case 1: case 100: #if (UNITY_ANDROID || UNITY_IOS || UNITY_LUMIN) && !UNITY_EDITOR this.TextureFormat = TextureFormat.RGBA32; #else this.TextureFormat = TextureFormat.DXT1; #endif break; case 8: case 164: #if (UNITY_ANDROID || UNITY_IOS || UNITY_LUMIN) && !UNITY_EDITOR this.TextureFormat = TextureFormat.ASTC_8x8; #else this.TextureFormat = TextureFormat.RGBA32; #endif break; default: #if UNITY_IOS this.TextureFormat = TextureFormat.PVRTC_RGB4; #elif UNITY_ANDROID this.TextureFormat = TextureFormat.ETC_RGB4; #else this.TextureFormat = TextureFormat.DXT1; #endif break; } this.MaxVertices = Bridge4DS.GetSequenceMaxVertices(this.FDVUUID); if (this.MaxVertices == 0) { this.MaxVertices = 65535; } this.MaxTriangles = Bridge4DS.GetSequenceMaxTriangles(this.FDVUUID); if (this.MaxTriangles == 0) { this.MaxTriangles = 65535; } this.FrameRate = (float)Bridge4DS.GetSequenceFramerate(this.FDVUUID); } }
public void GotoFrame(int frame) { Bridge4DS.GotoFrame(_dataSource.FDVUUID, frame); _isPlaying = false; UpdateMesh(); }
private void UpdateMesh() { if (_dataSource == null) { return; } //Get the new model #if USE_NATIVE_LIB System.IntPtr normalAddr = System.IntPtr.Zero; if (_computeNormals) { if (_newNormals == null) { _newNormals = new Vector3[_dataSource.MaxVertices]; _newNormalsHandle = GCHandle.Alloc(_newNormals, GCHandleType.Pinned); } normalAddr = _newNormalsHandle.AddrOfPinnedObject(); } int modelId = Bridge4DS.UpdateModel(_dataSource.FDVUUID, _newVerticesHandle.AddrOfPinnedObject(), _newUVsHandle.AddrOfPinnedObject(), _newTrianglesHandle.AddrOfPinnedObject(), _newTextureDataHandle.AddrOfPinnedObject(), normalAddr, _lastModelId, ref _nbVertices, ref _nbTriangles); #endif //look for end of range event if (Bridge4DS.OutOfRangeEvent(_dataSource.FDVUUID)) { //Send event if (onOutOfRange != null) { onOutOfRange(); } if (_outRangeMode == OUT_RANGE_MODE.Stop) { Play(false); } if (_outRangeMode == OUT_RANGE_MODE.Hide) { Play(false); _meshComponent.sharedMesh.Clear(); } } //Check if there is model if (!_newMeshAvailable) { _newMeshAvailable = (modelId != -1 && modelId != _lastModelId); } if (modelId == -1) { modelId = _lastModelId; } else { _lastModelId = modelId; } if (_debugInfo) { double timeInMSeconds = System.DateTime.Now.Subtract(_lastDecodingTime).TotalMilliseconds; if (_lastDecodingId == 0 || timeInMSeconds > 500f) { _decodingFPS = (float)((double)(Mathf.Abs((float)(modelId - _lastDecodingId))) / timeInMSeconds) * 1000f; _lastDecodingTime = System.DateTime.Now; _lastDecodingId = modelId; } } }
//private constructor private DataSource4DS(int key, string rootpath, int activeRangeBegin, int activeRangeEnd, OUT_RANGE_MODE outRangeMode, ref bool success) { this.FDVUUID = 0; success = true; //Create sequence with native plugin this.FDVUUID = Bridge4DS.CreateSequence(key, rootpath, activeRangeBegin, activeRangeEnd, outRangeMode); if (this.FDVUUID == 0) { success = false; } //Get sequence info if (success) { this.TextureSize = Bridge4DS.GetTextureSize(this.FDVUUID); if (this.TextureSize == 0) { this.TextureSize = 1024; //put 1024 by default => will crash if we have 2048 texture and it's not written in xml fi } int textureEncoding = Bridge4DS.GetTextureEncoding(this.FDVUUID); switch (textureEncoding) { case 5: case 120: this.TextureFormat = TextureFormat.ETC_RGB4; break; case 6: case 130: this.TextureFormat = TextureFormat.PVRTC_RGB4; break; case 4: case 131: this.TextureFormat = TextureFormat.PVRTC_RGB2; break; case 1: case 100: this.TextureFormat = TextureFormat.DXT1; break; case 8: case 164: this.TextureFormat = TextureFormat.ASTC_RGBA_8x8; break; default: #if UNITY_IPHONE this.TextureFormat = TextureFormat.PVRTC_RGB4; #elif UNITY_ANDROID this.TextureFormat = TextureFormat.ETC_RGB4; #else this.TextureFormat = TextureFormat.DXT1; #endif break; } this.MaxVertices = Bridge4DS.GetSequenceMaxVertices(this.FDVUUID); if (this.MaxVertices == 0) { this.MaxVertices = 65535; } this.MaxTriangles = Bridge4DS.GetSequenceMaxTriangles(this.FDVUUID); if (this.MaxTriangles == 0) { this.MaxTriangles = 65535; } this.FrameRate = (float)Bridge4DS.GetSequenceFramerate(this.FDVUUID); } }
//private constructor private DataSource4DS(string serverip, int serverport, ref bool success) { this.FDVUUID = 0; success = true; //Create network client with native plugin this.FDVUUID = Bridge4DS.CreateConnection(serverip, serverport); if (this.FDVUUID == 0) { success = false; } //Get sequence info if (success) { this.TextureSize = Bridge4DS.GetTextureSize(this.FDVUUID); if (this.TextureSize == 0) { this.TextureSize = 1024; //put 1024 by default => will crash if we have 2048 texture and it's not written in xml fi } int textureEncoding = Bridge4DS.GetTextureEncoding(this.FDVUUID); switch (textureEncoding) { case 5: this.TextureFormat = TextureFormat.ETC_RGB4; break; case 6: this.TextureFormat = TextureFormat.PVRTC_RGB4; break; case 4: this.TextureFormat = TextureFormat.PVRTC_RGB2; break; case 1: this.TextureFormat = TextureFormat.DXT1; break; case 8: this.TextureFormat = TextureFormat.ASTC_RGBA_8x8; break; default: #if UNITY_IPHONE this.TextureFormat = TextureFormat.PVRTC_RGB4; #elif UNITY_ANDROID this.TextureFormat = TextureFormat.ETC_RGB4; #else this.TextureFormat = TextureFormat.DXT1; #endif break; } this.MaxVertices = Bridge4DS.GetSequenceMaxVertices(this.FDVUUID); if (this.MaxVertices == 0) { this.MaxVertices = 65535; } this.MaxTriangles = Bridge4DS.GetSequenceMaxTriangles(this.FDVUUID); if (this.MaxTriangles == 0) { this.MaxTriangles = 65535; } this.FrameRate = (float)Bridge4DS.GetSequenceFramerate(this.FDVUUID); } }