private IEnumerator LoadTargetDataBuffer(Buffer buffer) { using (buffer) { Optional <ImageTarget> targetOptional = null; bool taskFinished = false; EasyARController.Instance.Worker.Run(() => { targetOptional = ImageTarget.createFromTargetData(buffer); taskFinished = true; }); while (!taskFinished) { yield return(0); } if (targetOptional.OnSome) { Target = targetOptional.Value; if (Target != null && TargetAvailable != null) { TargetAvailable(); } } else { throw new Exception("invalid buffer"); } } UpdateTargetInTracker(); }
private IEnumerator LoadImageTarget() { var path = TargetPath; var type = Type; WWW www; if (type == PathType.Absolute) { path = Utility.AddFileHeader(path); #if UNITY_ANDROID && !UNITY_EDITOR path = "file://" + path; #endif } else if (type == PathType.StreamingAssets) { path = Utility.AddFileHeader(Application.streamingAssetsPath + "/" + path); } Debug.Log("[EasyAR]:" + path); www = new WWW(path); while (!www.isDone) { yield return(0); } if (!string.IsNullOrEmpty(www.error)) { Debug.LogError(www.error); www.Dispose(); yield break; } var data = www.bytes; easyar.Buffer buffer = easyar.Buffer.create(data.Length); var ptr = buffer.data(); Marshal.Copy(data, 0, ptr, data.Length); Optional <easyar.ImageTarget> op_target; if (targetType == TargetType.LocalImage) { var image = ImageHelper.decode(buffer); if (!image.OnSome) { throw new System.Exception("decode image file data failed"); } var p = new ImageTargetParameters(); p.setImage(image.Value); p.setName(TargetName); p.setScale(TargetSize); p.setUid(""); p.setMeta(""); op_target = ImageTarget.createFromParameters(p); if (!op_target.OnSome) { throw new System.Exception("create image target failed from image target parameters"); } image.Value.Dispose(); buffer.Dispose(); p.Dispose(); } else { op_target = ImageTarget.createFromTargetData(buffer); if (!op_target.OnSome) { throw new System.Exception("create image target failed from image target target data"); } buffer.Dispose(); } target = op_target.Value; Destroy(www.texture); www.Dispose(); if (ImageTracker == null) { yield break; } ImageTracker.LoadImageTarget(this, (_target, status) => { targetImage = ((_target as ImageTarget).images())[0]; Debug.Log("[EasyAR] Targtet name: " + _target.name() + " target runtimeID: " + _target.runtimeID() + " load status: " + status); Debug.Log("[EasyAR] Target size" + targetImage.width() + " " + targetImage.height()); }); }
static void DrawGizmo(ImageTargetController scr, GizmoType gizmoType) { var signature = scr.SourceType.ToString(); switch (scr.SourceType) { case ImageTargetController.DataSource.ImageFile: if (!EasyARController.Settings.GizmoConfig.ImageTarget.EnableImageFile) { return; } signature += scr.ImageFileSource.PathType.ToString() + scr.ImageFileSource.Path; break; case ImageTargetController.DataSource.TargetDataFile: if (!EasyARController.Settings.GizmoConfig.ImageTarget.EnableTargetDataFile) { return; } signature += scr.TargetDataFileSource.PathType.ToString() + scr.TargetDataFileSource.Path; break; case ImageTargetController.DataSource.Target: if (!EasyARController.Settings.GizmoConfig.ImageTarget.EnableTarget) { return; } if (scr.Target != null) { signature += scr.Target.runtimeID().ToString(); } break; default: break; } if (scr.GizmoData.Material == null) { scr.GizmoData.Material = new Material(Shader.Find("EasyAR/ImageTargetGizmo")); } if (scr.GizmoData.Signature != signature) { if (scr.GizmoData.Texture != null) { UnityEngine.Object.DestroyImmediate(scr.GizmoData.Texture); scr.GizmoData.Texture = null; } string path; switch (scr.SourceType) { case ImageTargetController.DataSource.ImageFile: path = scr.ImageFileSource.Path; if (scr.ImageFileSource.PathType == PathType.StreamingAssets) { path = Application.streamingAssetsPath + "/" + scr.ImageFileSource.Path; } if (System.IO.File.Exists(path)) { var sourceData = System.IO.File.ReadAllBytes(path); scr.GizmoData.Texture = new Texture2D(2, 2); scr.GizmoData.Texture.LoadImage(sourceData); scr.GizmoData.Texture.Apply(); UpdateScale(scr, scr.ImageFileSource.Scale); if (SceneView.lastActiveSceneView) { SceneView.lastActiveSceneView.Repaint(); } } break; case ImageTargetController.DataSource.TargetDataFile: path = scr.TargetDataFileSource.Path; if (scr.TargetDataFileSource.PathType == PathType.StreamingAssets) { path = Application.streamingAssetsPath + "/" + scr.TargetDataFileSource.Path; } if (System.IO.File.Exists(path)) { if (!EasyARController.Initialized) { EasyARController.GlobalInitialization(); if (!EasyARController.Initialized) { Debug.LogWarning("EasyAR Sense target data gizmo enabled but license key validation failed, target data gizmo will not show"); } } var sourceData = System.IO.File.ReadAllBytes(path); using (Buffer buffer = Buffer.wrapByteArray(sourceData)) { var targetOptional = ImageTarget.createFromTargetData(buffer); if (targetOptional.OnSome) { using (ImageTarget target = targetOptional.Value) { var imageList = target.images(); if (imageList.Count > 0) { var image = imageList[0]; scr.GizmoData.Texture = new Texture2D(image.width(), image.height(), TextureFormat.R8, false); scr.GizmoData.Texture.LoadRawTextureData(image.buffer().data(), image.buffer().size()); scr.GizmoData.Texture.Apply(); } foreach (var image in imageList) { image.Dispose(); } UpdateScale(scr, target.scale()); if (SceneView.lastActiveSceneView) { SceneView.lastActiveSceneView.Repaint(); } } } } } break; case ImageTargetController.DataSource.Target: if (scr.Target != null) { var imageList = (scr.Target as ImageTarget).images(); if (imageList.Count > 0) { var image = imageList[0]; scr.GizmoData.Texture = new Texture2D(image.width(), image.height(), TextureFormat.R8, false); scr.GizmoData.Texture.LoadRawTextureData(image.buffer().data(), image.buffer().size()); scr.GizmoData.Texture.Apply(); } foreach (var image in imageList) { image.Dispose(); } UpdateScale(scr, (scr.Target as ImageTarget).scale()); if (SceneView.lastActiveSceneView) { SceneView.lastActiveSceneView.Repaint(); } } break; default: break; } if (scr.GizmoData.Texture == null) { scr.GizmoData.Texture = new Texture2D(2, 2); scr.GizmoData.Texture.LoadImage(new byte[0]); scr.GizmoData.Texture.Apply(); } scr.GizmoData.Signature = signature; } if (scr.GizmoData.Material && scr.GizmoData.Texture) { scr.GizmoData.Material.SetMatrix("_Transform", scr.transform.localToWorldMatrix); if (scr.GizmoData.Texture.format == TextureFormat.R8) { scr.GizmoData.Material.SetInt("_isRenderGrayTexture", 1); } else { scr.GizmoData.Material.SetInt("_isRenderGrayTexture", 0); } scr.GizmoData.Material.SetFloat("_Ratio", (float)scr.GizmoData.Texture.height / scr.GizmoData.Texture.width); Gizmos.DrawGUITexture(new Rect(0, 0, 1, 1), scr.GizmoData.Texture, scr.GizmoData.Material); } }