Exemple #1
0
 void CreateRenderer()
 {
     previewRenderUtility = new PreviewRenderUtility(true);
     previewRenderUtility.cameraFieldOfView    = 30f;
     previewRenderUtility.camera.farClipPlane  = 1000;
     previewRenderUtility.camera.nearClipPlane = 0.3f;
     previewRenderUtility.camera.clearFlags    = CameraClearFlags.Skybox;
     previewObject = previewRenderUtility.InstantiatePrefabInScene(graph.Prefab);
     previewCamera = previewRenderUtility.camera;
 }
Exemple #2
0
        public override void OnInteractivePreviewGUI(Rect rect, GUIStyle background)
        {
            if (!graph || !graph.Prefab)
            {
                return;
            }
            if (graph.isPlaying)
            {
                deltaTime = (float)EditorApplication.timeSinceStartup;
            }
            if (previewObject == null || lastPreviewHash != graph.Prefab.GetHashCode())
            {
                Cleanup();
                CreateRenderer();
                lastPreviewHash = graph.Prefab.GetHashCode();
                previewObject   = previewRenderUtility.InstantiatePrefabInScene(graph.Prefab);
                goHeight        = GetHeight(previewObject.transform);
            }
            DrawRuntimeFields();

            if (graph.isPlaying)
            {
                var clip = graph.Evaluate(deltaTime);
                if (clip != null)
                {
                    clip.SampleAnimation(graph.Prefab, graph.internalCounter);
                    RepaintGraph();
                }
            }

            previewRenderUtility.BeginPreview(rect, background);

            foreach (var light in previewRenderUtility.lights)
            {
                light.transform.rotation = previewCamera.transform.rotation;
            }

            previewRenderUtility.Render();

            previewCamera.Render();

            previewRenderUtility.EndAndDrawPreview(rect);

            if (Event.current.type == EventType.Repaint)
            {
                previewRect = rect;
            }

            // http://anchan828.github.io/editor-manual/web/customeditor.html
            // http://anchan828.github.io/editor-manual/web/spriteanimationpreview2.html
            UpdatePosition();
            PreviewEventListeners();
        }
    private void Init()
    {
        if (_preview == null)
        {
            _preview = new PreviewRenderUtility();
            _preview.camera.clearFlags = CameraClearFlags.Nothing;
        }

        if (_proxy == null)
        {
            _proxy = _preview.InstantiatePrefabInScene(
                AssetDatabase.LoadAssetAtPath <GameObject>("Assets/Editor/VirtualGyro/Proxies/PhoneLandscape.prefab"));
            _proxyFilter = _proxy.GetComponent <MeshFilter>();
        }
    }
Exemple #4
0
    private void RenderIcon(PreviewRenderUtility previewRenderer, Target target)
    {
        const int w    = 1024;
        const int h    = 1024;
        var       rect = new Rect(0f, 0f, w, h);

        previewRenderer.BeginPreview(rect, null);

        previewRenderer.InstantiatePrefabInScene(_prefab);

        previewRenderer.camera.clearFlags         = CameraClearFlags.SolidColor;
        previewRenderer.camera.backgroundColor    = target.Background;
        previewRenderer.camera.transform.position = _cameraSettings.Position;
        previewRenderer.camera.transform.rotation = Quaternion.Euler(_cameraSettings.Rotation);
        previewRenderer.camera.nearClipPlane      = _cameraSettings.Near;
        previewRenderer.camera.farClipPlane       = _cameraSettings.Far;
        previewRenderer.Render(true);

        var rt = previewRenderer.EndPreview();

        var temporary = RenderTexture.GetTemporary(w, h, 0, GraphicsFormat.R8G8B8A8_UNorm);

        Graphics.Blit(rt, temporary, _tex2rgbMaterial);
        RenderTexture.active = temporary;
        var texture = new Texture2D(w, h, TextureFormat.RGBA32, false, false);

        texture.ReadPixels(rect, 0, 0);
        texture.Apply();

        RenderTexture.ReleaseTemporary(temporary);

        try
        {
            var path = AssetDatabase.GetAssetPath(target.TargetTexture);
            File.WriteAllBytes(path, texture.EncodeToPNG());
            AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
        }
        catch (Exception e)
        {
            Debug.LogError($"Unable to write to icon: {e.Message}");
        }
    }
Exemple #5
0
        public override int OnGUI(PreviewRenderUtility context, BaseParam[] param)
        {
            int refreshCount = 0;

            OnPUI("Mesh", false, () =>
            {
                var meshTypeValue = (MeshType)EditorGUILayout.EnumPopup("Type", meshType);
                if (meshType.Equals(meshTypeValue) == false)
                {
                    Record("Change Mesh Type");

                    if (param[(int)PreParamType.kAnimation] is AnimationParam animationParam)
                    {
                        animationParam.Dispose();
                    }
                    if (gameObject != null)
                    {
                        GameObject.DestroyImmediate(gameObject);
                        gameObject = null;
                    }
                    if (boundsObject != null)
                    {
                        GameObject.DestroyImmediate(boundsObject);
                        boundsObject = null;
                    }
                    meshType = meshTypeValue;
                }
                if (meshType == MeshType.kAssets)
                {
                    var assetMeshValue = EditorGUILayout.ObjectField("Mesh", assetMesh, typeof(Mesh), false) as Mesh;
                    if (assetMesh != assetMeshValue)
                    {
                        Record("Change Mesh Assets");
                        assetMesh = assetMeshValue;
                    }
                }
                else if (meshType == MeshType.kDynamic)
                {
                    Color vertexColorValue = EditorGUILayout.ColorField("Vertex Color", vertexColor);
                    if (vertexColor.Equals(vertexColorValue) == false)
                    {
                        Record("Change Vertex Color");
                        dynamicMesh.colors = new Color[]
                        {
                            vertexColorValue,
                            vertexColorValue,
                            vertexColorValue,
                            vertexColorValue
                        };
                        vertexColor = vertexColorValue;
                    }
                    List <Vector4> texcoord;
                    Vector4 uv;

                    Vector2 texcoordZW = new Vector2(texcoords0.z, texcoords0.w);
                    Vector2 uvZW       = EditorGUILayout.Vector2Field("TexCoord0.zw", texcoordZW);
                    if (texcoordZW.Equals(uvZW) == false)
                    {
                        Record("Change TexCoord0");
                        texcoord = new List <Vector4>();
                        texcoord.Add(new Vector4(0, 0, uvZW.x, uvZW.y));
                        texcoord.Add(new Vector4(0, 1, uvZW.x, uvZW.y));
                        texcoord.Add(new Vector4(1, 0, uvZW.x, uvZW.y));
                        texcoord.Add(new Vector4(1, 1, uvZW.x, uvZW.y));
                        dynamicMesh.SetUVs(0, texcoord);
                        texcoords0 = new Vector4(0, 0, uvZW.x, uvZW.y);
                    }
                    uv = EditorGUILayout.Vector4Field("TexCoord1", texcoords1);
                    if (texcoords1.Equals(uv) == false)
                    {
                        Record("Change TexCoord1");
                        texcoord = new List <Vector4>();
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        dynamicMesh.SetUVs(1, texcoord);
                        texcoords1 = uv;
                    }
                    uv = EditorGUILayout.Vector4Field("TexCoord2", texcoords2);
                    if (texcoords2.Equals(uv) == false)
                    {
                        Record("Change TexCoord2");
                        texcoord = new List <Vector4>();
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        dynamicMesh.SetUVs(2, texcoord);
                        texcoords2 = uv;
                    }
                    uv = EditorGUILayout.Vector4Field("TexCoord3", texcoords3);
                    if (texcoords3.Equals(uv) == false)
                    {
                        Record("Change TexCoord3");
                        texcoord = new List <Vector4>();
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        dynamicMesh.SetUVs(3, texcoord);
                        texcoords3 = uv;
                    }
                    uv = EditorGUILayout.Vector4Field("TexCoord4", texcoords4);
                    if (texcoords4.Equals(uv) == false)
                    {
                        Record("Change TexCoord4");
                        texcoord = new List <Vector4>();
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        dynamicMesh.SetUVs(4, texcoord);
                        texcoords4 = uv;
                    }
                    uv = EditorGUILayout.Vector4Field("TexCoord5", texcoords5);
                    if (texcoords5.Equals(uv) == false)
                    {
                        Record("Change TexCoord5");
                        texcoord = new List <Vector4>();
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        dynamicMesh.SetUVs(5, texcoord);
                        texcoords5 = uv;
                    }
                    uv = EditorGUILayout.Vector4Field("TexCoord6", texcoords6);
                    if (texcoords6.Equals(uv) == false)
                    {
                        Record("Change TexCoord6");
                        texcoord = new List <Vector4>();
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        dynamicMesh.SetUVs(6, texcoord);
                        texcoords6 = uv;
                    }
                    uv = EditorGUILayout.Vector4Field("TexCoord7", texcoords7);
                    if (texcoords7.Equals(uv) == false)
                    {
                        Record("Change TexCoord7");
                        texcoord = new List <Vector4>();
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        texcoord.Add(uv);
                        dynamicMesh.SetUVs(7, texcoord);
                        texcoords7 = uv;
                    }
                }
                else if (meshType == MeshType.kPrefab)
                {
                    var prefabValue = EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false) as GameObject;
                    if (requestPrefab != null)
                    {
                        prefabValue   = requestPrefab;
                        requestPrefab = null;
                        GUI.changed   = true;
                    }
                    if (prefab != prefabValue)
                    {
                        Record("Change Prefab");

                        if (param[(int)PreParamType.kAnimation] is AnimationParam animationParam)
                        {
                            animationParam.Dispose();
                        }
                        if (gameObject != null)
                        {
                            GameObject.DestroyImmediate(gameObject);
                            gameObject = null;
                        }
                        if (boundsObject != null)
                        {
                            GameObject.DestroyImmediate(boundsObject);
                            boundsObject = null;
                        }
                        prefab = prefabValue;
                    }
                    bool autoViewFitValue = EditorGUILayout.Toggle("Auto view fit", autoViewFit);
                    if (autoViewFit.Equals(autoViewFitValue) == false)
                    {
                        autoViewFit = autoViewFitValue;
                    }
                    bool showBoundsValue = EditorGUILayout.Toggle("Show BoundingBox", showBounds);
                    if (showBounds.Equals(showBoundsValue) == false)
                    {
                        boundsObject.SetActive(showBoundsValue);
                        showBounds = showBoundsValue;
                    }
                }
                if (prefab != null)
                {
                    if (gameObject == null)
                    {
                        gameObject = context.InstantiatePrefabInScene(prefab);

                        if (param[(int)PreParamType.kCamera] is CameraParam cameraParam)
                        {
                            if (param[(int)PreParamType.kTexture] is TextureParam textureParam)
                            {
                                if (param[(int)PreParamType.kTransform] is TransformParam transformParam)
                                {
                                    if (autoViewFit != false)
                                    {
                                        cameraParam.ToPreset(CameraPreset.kFit, textureParam, transformParam, this);
                                    }
                                }
                            }
                        }
                    }
                    if (boundsObject == null)
                    {
                        boundsObject = new GameObject("Bounds", typeof(MeshFilter), typeof(MeshRenderer));
                        context.AddSingleGO(boundsObject);
                        boundsObject.GetComponent <MeshFilter>().sharedMesh       = boundsMesh;
                        boundsObject.GetComponent <MeshRenderer>().sharedMaterial = boundsMaterial;
                        boundsObject.transform.SetParent(gameObject.transform, false);
                        boundsObject.SetActive(showBounds);
                    }
                }
            });
            return(refreshCount);
        }