Ejemplo n.º 1
0
        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);
            }
        }