BeginPreview() public method

public BeginPreview ( Rect r, GUIStyle previewBackground ) : void
r UnityEngine.Rect
previewBackground UnityEngine.GUIStyle
return void
Esempio n. 1
0
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                if (Event.current.type == EventType.Repaint)
                {
                    EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40), "Preview requires\nrender texture support");
                }
                return;
            }

            InitPreview();

            m_Settings.previewDir = PreviewGUI.Drag2D(m_Settings.previewDir, r);

            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            m_PreviewUtility.BeginPreview(r, background);

            DoRenderPreview(m_PreviewShaded);

            m_PreviewUtility.EndAndDrawPreview(r);
        }
Esempio n. 2
0
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture || !SystemInfo.supports3DTextures)
            {
                if (Event.current.type == EventType.Repaint)
                {
                    EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40), "3D texture preview not supported");
                }
                return;
            }

            InitPreviewUtility();
            Event e = Event.current;

            m_PreviewDir = PreviewGUI.Drag2D(m_PreviewDir, r);

            if (e.type == EventType.ScrollWheel)
            {
                m_ViewDistance = Mathf.Clamp(m_ViewDistance + e.delta.y * (0.01f + Mathf.Sqrt(m_ViewDistance) / 20), s_MinViewDistance, s_MaxViewDistance);
                e.Use();
                Repaint();
            }

            if (e.type != EventType.Repaint)
            {
                return;
            }

            m_PreviewUtility.BeginPreview(r, background);
            DrawPreview();
            m_PreviewUtility.EndAndDrawPreview(r);
        }
Esempio n. 3
0
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            var evt = Event.current;

            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                if (evt.type == EventType.Repaint)
                {
                    EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40),
                                              "Mesh preview requires\nrender texture support");
                }
                return;
            }

            Init();

            Assembly   editorAssembly = Assembly.GetAssembly(typeof(EditorGUI));
            Type       guiPreview     = editorAssembly.GetType("PreviewGUI");
            MethodInfo drag2D         = guiPreview.GetMethod("Drag2D");

            if ((evt.type == EventType.ValidateCommand || evt.type == EventType.ExecuteCommand) && evt.commandName == "FrameSelected") // compare against EventCommandNames.FrameSelected when on trunk
            {
                FrameObject();
                evt.Use();
            }

            if (evt.button <= 0 && m_Settings.displayMode != DisplayMode.UVLayout)
            {
                m_Settings.previewDir = (Vector2)drag2D?.Invoke(null, new object[] { m_Settings.previewDir, r });
            }

            if (evt.button == 1 && m_Settings.displayMode != DisplayMode.UVLayout)
            {
                m_Settings.lightDir = (Vector2)drag2D?.Invoke(null, new object[] { m_Settings.lightDir, r });
            }

            if (evt.type == EventType.ScrollWheel)
            {
                MeshPreviewZoom(r, evt);
            }

            if (evt.type == EventType.MouseDrag && (m_Settings.displayMode == DisplayMode.UVLayout || evt.button == 2))
            {
                MeshPreviewPan(r, evt);
            }

            if (evt.type != EventType.Repaint)
            {
                return;
            }

            m_PreviewUtility.BeginPreview(r, background);

            DoRenderPreview();

            m_PreviewUtility.EndAndDrawPreview(r);
        }
Esempio n. 4
0
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            var evt = Event.current;

            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                if (evt.type == EventType.Repaint)
                {
                    EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40),
                                              "Mesh preview requires\nrender texture support");
                }
                return;
            }

            Init();

            if ((evt.type == EventType.ValidateCommand || evt.type == EventType.ExecuteCommand) && evt.commandName == EventCommandNames.FrameSelected)
            {
                FrameObject();
                evt.Use();
            }

            if (evt.button <= 0 && m_Settings.displayMode != DisplayMode.UVLayout)
            {
                m_Settings.previewDir = PreviewGUI.Drag2D(m_Settings.previewDir, r);
            }

            if (evt.button == 1 && m_Settings.displayMode != DisplayMode.UVLayout)
            {
                m_Settings.lightDir = PreviewGUI.Drag2D(m_Settings.lightDir, r);
            }

            if (evt.type == EventType.ScrollWheel)
            {
                MeshPreviewZoom(r, evt);
            }

            if (evt.type == EventType.MouseDrag && (m_Settings.displayMode == DisplayMode.UVLayout || evt.button == 2))
            {
                MeshPreviewPan(r, evt);
            }

            if (evt.type != EventType.Repaint)
            {
                return;
            }

            m_PreviewUtility.BeginPreview(r, background);

            DoRenderPreview();

            m_PreviewUtility.EndAndDrawPreview(r);
        }
Esempio n. 5
0
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            m_mouseDelta = CheckMouseMovement(m_mouseDelta, r);

            if (Event.current.type == EventType.Repaint)
            {
                m_previewRenderUtility.BeginPreview(r, background);

                Texture resultRender = m_previewRenderUtility.EndPreview();
                m_previewRenderUtility.DrawMesh(m_previewMesh, Matrix4x4.identity, m_material, 0);
                m_cameraTransform.rotation = Quaternion.Euler(new Vector3(-m_mouseDelta.y, -m_mouseDelta.x, 0));
                m_cameraTransform.position = m_cameraTransform.forward * -8f;
                m_previewRenderUtility.m_Camera.Render();

                GUI.DrawTexture(r, resultRender, ScaleMode.StretchToFill, false);
            }
        }
Esempio n. 6
0
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                if (Event.current.type == EventType.Repaint)
                {
                    EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40),
                                              "Mesh preview requires\nrender texture support");
                }
                return;
            }

            Init();

            Assembly   editorAssembly = Assembly.GetAssembly(typeof(EditorGUI));
            Type       guiPreview     = editorAssembly.GetType("PreviewGUI");
            MethodInfo drag2D         = guiPreview.GetMethod("Drag2D");

            if (m_Settings.displayMode != DisplayMode.UVLayout)
            {
                m_Settings.previewDir = (Vector2)drag2D?.Invoke(null, new object[] { m_Settings.previewDir, r });
            }
            //previewDir = PreviewGUI.Drag2D(previewDir, r);

            if (Event.current.type == EventType.ScrollWheel && m_Settings.displayMode == DisplayMode.UVLayout)
            {
                MeshPreviewZoom(r, Event.current);
            }

            if (Event.current.type == EventType.MouseDrag && m_Settings.displayMode == DisplayMode.UVLayout)
            {
                MeshPreviewPan(r, Event.current);
            }

            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            m_PreviewUtility.BeginPreview(r, background);

            DoRenderPreview();

            m_PreviewUtility.EndAndDrawPreview(r);
        }
Esempio n. 7
0
        /// <summary>
        /// Draws the preview GUI for the RuleTile
        /// </summary>
        /// <param name="rect">Rect to draw the preview GUI</param>
        /// <param name="background">The GUIStyle of the background for the preview</param>
        public override void OnPreviewGUI(Rect rect, GUIStyle background)
        {
            if (m_PreviewUtility == null)
            {
                CreatePreview();
            }

            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            m_PreviewUtility.BeginPreview(rect, background);
            m_PreviewUtility.camera.orthographicSize = 2;
            if (rect.height > rect.width)
            {
                m_PreviewUtility.camera.orthographicSize *= (float)rect.height / rect.width;
            }
            m_PreviewUtility.camera.Render();
            m_PreviewUtility.EndAndDrawPreview(rect);
        }
 public override void OnPreviewGUI(Rect r, GUIStyle background)
 {
     if (!ShaderUtil.hardwareSupportsRectRenderTexture)
     {
         if (Event.current.type == EventType.Repaint)
         {
             EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40f), "Preview requires\nrender texture support");
         }
     }
     else
     {
         this.previewDir = PreviewGUI.Drag2D(this.previewDir, r);
         if (Event.current.type == EventType.Repaint)
         {
             PreviewRenderUtility renderUtility = this.GetPreviewData().renderUtility;
             renderUtility.BeginPreview(r, background);
             this.DoRenderPreview();
             renderUtility.EndAndDrawPreview(r);
         }
     }
 }
Esempio n. 9
0
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                if (Event.current.type == EventType.Repaint)
                {
                    EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40),
                                              "Mesh preview requires\nrender texture support");
                }
                return;
            }

            Init();

            if (m_Settings.displayMode != DisplayMode.UVLayout)
            {
                m_Settings.previewDir = PreviewGUI.Drag2D(m_Settings.previewDir, r);
            }

            if (Event.current.type == EventType.ScrollWheel && m_Settings.displayMode == DisplayMode.UVLayout)
            {
                MeshPreviewZoom(r, Event.current);
            }

            if (Event.current.type == EventType.MouseDrag && m_Settings.displayMode == DisplayMode.UVLayout)
            {
                MeshPreviewPan(r, Event.current);
            }

            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            m_PreviewUtility.BeginPreview(r, background);

            DoRenderPreview();

            m_PreviewUtility.EndAndDrawPreview(r);
        }
Esempio n. 10
0
        public void OnPreviewGUI(Texture t, Rect r, GUIStyle background, float exposure)
        {
            if (t == null)
            {
                return;
            }

            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                if (Event.current.type == EventType.Repaint)
                {
                    EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40), "Cubemap preview requires\nrender texture support");
                }
                return;
            }

            m_PreviewDir = PreviewGUI.Drag2D(m_PreviewDir, r);

            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            InitPreview();
            m_PreviewUtility.BeginPreview(r, background);
            const float previewDistance = 6.0f;

            RenderCubemap(t, m_PreviewDir, previewDistance, exposure);

            Texture renderedTexture = m_PreviewUtility.EndPreview();

            GUI.DrawTexture(r, renderedTexture, ScaleMode.StretchToFill, false);

            if (mipLevel != 0)
            {
                EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 20), "Mip " + mipLevel);
            }
        }
        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture || !SystemInfo.supports3DTextures)
            {
                if (Event.current.type == EventType.Repaint)
                {
                    EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40), "3D texture preview not supported");
                }
                return;
            }

            m_PreviewDir = PreviewGUI.Drag2D(m_PreviewDir, r);

            if (Event.current.type != EventType.Repaint)
            {
                return;
            }

            InitPreview();
            m_Material.mainTexture = target as Texture;

            m_PreviewUtility.BeginPreview(r, background);
            bool oldFog = RenderSettings.fog;

            Unsupported.SetRenderSettingsUseFogNoDirty(false);

            m_PreviewUtility.camera.transform.position = -Vector3.forward * 3.0f;
            m_PreviewUtility.camera.transform.rotation = Quaternion.identity;
            Quaternion rot = Quaternion.Euler(m_PreviewDir.y, 0, 0) * Quaternion.Euler(0, m_PreviewDir.x, 0);

            m_PreviewUtility.DrawMesh(m_Mesh, Vector3.zero, rot, m_Material, 0);
            m_PreviewUtility.Render();

            Unsupported.SetRenderSettingsUseFogNoDirty(oldFog);
            m_PreviewUtility.EndAndDrawPreview(r);
        }
Esempio n. 12
0
        public void DoRenderPreview(Rect previewRect, GUIStyle background)
        {
            var probe = RenderSettings.ambientProbe;

            previewUtility.BeginPreview(previewRect, background);

            Quaternion bodyRot;
            Quaternion rootRot;
            Vector3    rootPos;
            Vector3    bodyPos = rootPosition;
            Vector3    pivotPos;

            if (Animator && Animator.isHuman)
            {
                rootRot = Animator.rootRotation;
                rootPos = Animator.rootPosition;

                bodyRot = Animator.bodyRotation;

                pivotPos = Animator.pivotPosition;
            }
            else if (Animator && Animator.hasRootMotion)
            {
                rootRot = Animator.rootRotation;
                rootPos = Animator.rootPosition;

                bodyRot = Quaternion.identity;

                pivotPos = Vector3.zero;
            }
            else
            {
                rootRot = Quaternion.identity;
                rootPos = Vector3.zero;

                bodyRot = Quaternion.identity;

                pivotPos = Vector3.zero;
            }

            SetupPreviewLightingAndFx(probe);

            Vector3 direction = bodyRot * Vector3.forward;

            direction[1] = 0;
            Quaternion directionRot = Quaternion.LookRotation(direction);
            Vector3    directionPos = rootPos;

            Quaternion pivotRot = rootRot;

            // Scale all Preview Objects to fit avatar size.
            PositionPreviewObjects(pivotRot, pivotPos, bodyRot, bodyPosition, directionRot, rootRot, rootPos, directionPos, m_AvatarScale);

            bool dynamicFloorHeight = is2D ? false : Mathf.Abs(m_NextFloorHeight - m_PrevFloorHeight) > m_ZoomFactor * 0.01f;

            // Calculate floor height and alpha
            float mainFloorHeight, mainFloorAlpha;

            if (dynamicFloorHeight)
            {
                float fadeMoment = m_NextFloorHeight < m_PrevFloorHeight ? kFloorFadeDuration : (1 - kFloorFadeDuration);
                mainFloorHeight = timeControl.normalizedTime < fadeMoment ? m_PrevFloorHeight : m_NextFloorHeight;
                mainFloorAlpha  = Mathf.Clamp01(Mathf.Abs(timeControl.normalizedTime - fadeMoment) / kFloorFadeDuration);
            }
            else
            {
                mainFloorHeight = m_PrevFloorHeight;
                mainFloorAlpha  = is2D ? 0.5f : 1;
            }

            Quaternion floorRot = is2D ? Quaternion.Euler(-90, 0, 0) : Quaternion.identity;
            Vector3    floorPos = m_ReferenceInstance.transform.position;

            floorPos.y = mainFloorHeight;

            // Render shadow map
            Matrix4x4     shadowMatrix;
            RenderTexture shadowMap = RenderPreviewShadowmap(previewUtility.lights[0], m_BoundingVolumeScale / 2, bodyPosition, floorPos, out shadowMatrix);

            float tempZoomFactor = (is2D ? 1.0f : m_ZoomFactor);

            // Position camera
            previewUtility.camera.orthographic = is2D;
            if (is2D)
            {
                previewUtility.camera.orthographicSize = 2.0f * m_ZoomFactor;
            }
            previewUtility.camera.nearClipPlane = 0.5f * tempZoomFactor;
            previewUtility.camera.farClipPlane  = 100.0f * m_AvatarScale;
            Quaternion camRot = Quaternion.Euler(-m_PreviewDir.y, -m_PreviewDir.x, 0);

            // Add panning offset
            Vector3 camPos = camRot * (Vector3.forward * -5.5f * tempZoomFactor) + bodyPos + m_PivotPositionOffset;

            previewUtility.camera.transform.position = camPos;
            previewUtility.camera.transform.rotation = camRot;


            SetPreviewCharacterEnabled(true, m_ShowReference);
            foreach (var previewable in m_Previewables)
            {
                previewable.OnPreviewUpdate();
            }
            previewUtility.Render(option != PreviewPopupOptions.DefaultModel);
            SetPreviewCharacterEnabled(false, false);

            // Texture offset - negative in order to compensate the floor movement.
            Vector2 textureOffset = -new Vector2(floorPos.x, is2D ? floorPos.y : floorPos.z);

            // Render main floor
            {
                Material  mat    = m_FloorMaterial;
                Matrix4x4 matrix = Matrix4x4.TRS(floorPos, floorRot, Vector3.one * kFloorScale * m_AvatarScale);

                mat.mainTextureOffset = textureOffset * kFloorScale * 0.08f * (1.0f / m_AvatarScale);
                mat.SetTexture("_ShadowTexture", shadowMap);
                mat.SetMatrix("_ShadowTextureMatrix", shadowMatrix);
                mat.SetVector("_Alphas", new Vector4(kFloorAlpha * mainFloorAlpha, kFloorShadowAlpha * mainFloorAlpha, 0, 0));
                mat.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Background;

                Graphics.DrawMesh(m_FloorPlane, matrix, mat, kDefaultLayer, previewUtility.camera, 0);
            }

            // Render small floor
            if (dynamicFloorHeight)
            {
                bool  topIsNext        = m_NextFloorHeight > m_PrevFloorHeight;
                float floorHeight      = topIsNext ? m_NextFloorHeight : m_PrevFloorHeight;
                float otherFloorHeight = topIsNext ? m_PrevFloorHeight : m_NextFloorHeight;
                float floorAlpha       = (floorHeight == mainFloorHeight ? 1 - mainFloorAlpha : 1) * Mathf.InverseLerp(otherFloorHeight, floorHeight, rootPos.y);
                floorPos.y = floorHeight;

                Material mat = m_FloorMaterialSmall;
                mat.mainTextureOffset = textureOffset * kFloorScaleSmall * 0.08f;
                mat.SetTexture("_ShadowTexture", shadowMap);
                mat.SetMatrix("_ShadowTextureMatrix", shadowMatrix);
                mat.SetVector("_Alphas", new Vector4(kFloorAlpha * floorAlpha, 0, 0, 0));
                Matrix4x4 matrix = Matrix4x4.TRS(floorPos, floorRot, Vector3.one * kFloorScaleSmall * m_AvatarScale);
                Graphics.DrawMesh(m_FloorPlane, matrix, mat, kDefaultLayer, previewUtility.camera, 0);
            }

            var clearMode = previewUtility.camera.clearFlags;

            previewUtility.camera.clearFlags = CameraClearFlags.Nothing;
            previewUtility.Render(false);
            previewUtility.camera.clearFlags = clearMode;
            RenderTexture.ReleaseTemporary(shadowMap);
        }