Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Question How to achieve a photoshop-like perspective effect in screen space- overlay

Discussion in 'UGUI & TextMesh Pro' started by ShenKang4181, Jan 13, 2023.

  1. ShenKang4181

    ShenKang4181

    Joined:
    Aug 13, 2019
    Posts:
    13
    like this: upload_2023-1-13_18-17-1.png
    I seem to have implemented this effect in my code, but it doesn't look quite the same, here's my code and what I achieved
    upload_2023-1-13_18-20-22.png
    public class aa : RawImage
    {
    /*
    * 1 2
    * 0 3
    */

    public Vector3 offset0;
    public Vector3 offset1;
    public Vector3 offset2;
    public Vector3 offset3;

    private float xProportion0, xProportion1, xProportion2, xProportion3;
    private float yProportion0, yProportion1, yProportion2, yProportion3;

    protected override void OnPopulateMesh( VertexHelper vh )
    {
    base.OnPopulateMesh( vh );

    UIVertex vertex = new UIVertex( );
    vh.PopulateUIVertex( ref vertex , 0 );
    vertex.position += offset0;
    vh.SetUIVertex( vertex , 0 );

    vertex = new UIVertex( );
    vh.PopulateUIVertex( ref vertex , 1 );
    vertex.position += offset1;
    vh.SetUIVertex( vertex , 1 );

    vertex = new UIVertex( );
    vh.PopulateUIVertex( ref vertex , 2 );
    vertex.position += offset2;
    vh.SetUIVertex( vertex , 2 );

    vertex = new UIVertex( );
    vh.PopulateUIVertex( ref vertex , 3 );
    vertex.position += offset3;
    vh.SetUIVertex( vertex , 3 );

    //// L B R T
    //raycastPadding = new Vector4( Mathf.Min( offset0.x , offset1.x ) ,
    // Mathf.Min( offset0.y , offset3.y ) ,
    // -Mathf.Max( offset2.x , offset3.x ) ,
    // -Mathf.Max( offset1.y , offset2.y ) );

    if ( offset0.x != default )
    {
    xProportion0 = rectTransform.rect.size.x / offset0.x;
    }
    if ( offset1.x != default )
    {
    xProportion1 = rectTransform.rect.size.x / offset1.x;
    }
    if ( offset2.x != default )
    {
    xProportion2 = rectTransform.rect.size.x / offset2.x;
    }
    if ( offset3.x != default )
    {
    xProportion3 = rectTransform.rect.size.x / offset3.x;
    }
    if ( offset0.y != default )
    {
    yProportion0 = rectTransform.rect.size.y / offset0.y;
    }
    if ( offset1.y != default )
    {
    yProportion1 = rectTransform.rect.size.y / offset1.y;
    }
    if ( offset2.y != default )
    {
    yProportion2 = rectTransform.rect.size.y / offset2.y;
    }
    if ( offset3.y != default )
    {
    yProportion3 = rectTransform.rect.size.y / offset3.y;
    }
    }

    //protected override void OnRectTransformDimensionsChange( )
    //{
    // if ( xProportion0 != default )
    // {
    // offset0.x = rectTransform.rect.size.x / xProportion0;
    // }
    // if ( yProportion0 != default )
    // {
    // offset0.y = rectTransform.rect.size.y / yProportion0;
    // }
    // if ( xProportion1 != default )
    // {
    // offset1.x = rectTransform.rect.size.x / xProportion1;
    // }
    // if ( yProportion1 != default )
    // {
    // offset1.y = rectTransform.rect.size.y / yProportion1;
    // }
    // if ( xProportion2 != default )
    // {
    // offset2.x = rectTransform.rect.size.x / xProportion2;
    // }
    // if ( yProportion2 != default )
    // {
    // offset2.y = rectTransform.rect.size.y / yProportion2;
    // }
    // if ( xProportion3 != default )
    // {
    // offset3.x = rectTransform.rect.size.x / xProportion3;
    // }
    // if ( yProportion3 != default )
    // {
    // offset3.y = rectTransform.rect.size.y / yProportion3;
    // }
    // base.OnRectTransformDimensionsChange( );
    //}

    //public override bool Raycast( Vector2 sp , Camera eventCamera )
    //{
    // RectTransformUtility.ScreenPointToLocalPointInRectangle( transform.parent as RectTransform , sp , eventCamera , out var localPoint );
    // return Utility.ContainsPoint( this.GetLocalCornersPosition( ) , localPoint );
    //}
    }

    [CustomEditor( typeof( aa ) , true )]
    public class aaeditor : RawImageEditor
    {
    public override void OnInspectorGUI( )
    {
    base.OnInspectorGUI( );

    //EditorRawImage image = ( EditorRawImage ) target;
    EditorGUILayout.Space( );
    EditorGUILayout.PropertyField( serializedObject.FindProperty( "offset0" ) , new GUIContent( "offset0" ) );
    EditorGUILayout.PropertyField( serializedObject.FindProperty( "offset1" ) , new GUIContent( "offset1" ) );
    EditorGUILayout.PropertyField( serializedObject.FindProperty( "offset2" ) , new GUIContent( "offset2" ) );
    EditorGUILayout.PropertyField( serializedObject.FindProperty( "offset3" ) , new GUIContent( "offset3" ) );
    serializedObject.ApplyModifiedProperties( );
    }
    }
     
  2. ShenKang4181

    ShenKang4181

    Joined:
    Aug 13, 2019
    Posts:
    13
    any body ?