Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question The effect is normal on the editor, but becomes strange on the physical phone

Discussion in 'Universal Render Pipeline' started by DavidWeng, Jun 1, 2023.

  1. DavidWeng

    DavidWeng

    Joined:
    Oct 15, 2022
    Posts:
    7
    Hi, I'd like to ask everyone, I've recently encountered some problems with an image effect.
    I'm using Unity 2021.3.20f1 and my rendering mode is URP.

    I dynamically create two RenderTextures with the program.
    SourceRenderTexture => This will be set to Camera.targetTexture
    OutputRenderTexture => This will be set to RawImage.texture

    I used Shader Graph to create a shader as follows
    Shader Graph.png

    Then create a material to use this shader, and then use this material in CommandBuffer.
    Like this
    Code (CSharp):
    1. void Update()
    2. {
    3.     _material.SetFloat("_Progress", _progress);
    4.  
    5.     CommandBuffer commandBuffer = new CommandBuffer();
    6.     commandBuffer.Blit(SourceRenderTexture, OutputRenderTexture, _material);
    7.     Graphics.ExecuteCommandBuffer(commandBuffer);
    8.     commandBuffer.Release();
    9. }
    At the bottom of the screen I generate a Slider to control _progress.
    _progress = 0, you can see the full character
    _progress = 1, you can't see the character at all

    In the editor I can get the right effect I want
    截圖 2023-06-01 下午9.40.08.png
    This is the URL for the video
    https://youtube.com/shorts/hVaVk2WaeWM?feature=share

    However, when exported to iOS, the effect becomes very strange
    IMG_0711.PNG
    This is the URL for the video
    https://youtube.com/shorts/QxOFgbs5-NU?feature=share

    The screen looks as if the transparent places become very strange, I would like to ask which side of my usage is incorrect?

    I also put up my test project below.
    https://forum.unity.com/attachments...2/?temp_hash=8991431e38d1017237d0bd4d4f3569dd

    Here is the complete program code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Rendering;
    6. using UnityEngine.Rendering.Universal;
    7.  
    8. public class EffectCamera : MonoBehaviour
    9. {
    10.     static int WIDTH = 1080;
    11.     static int HEIGHT = 1920;
    12.  
    13.     public Camera Camera { get; private set; } = null;
    14.     public RenderTexture SourceRenderTexture { get; private set; } = null;
    15.     public RenderTexture OutputRenderTexture { get; private set; } = null;
    16.  
    17.     [SerializeField]
    18.     private Material _material = null;
    19.  
    20.     [SerializeField]
    21.     private RawImage _rawImage = null;
    22.  
    23.     [SerializeField, Range(0f, 1f)]
    24.     private float _progress = 0f;
    25.  
    26.     void OnDestroy()
    27.     {
    28.         if (SourceRenderTexture != null)
    29.         {
    30.             SourceRenderTexture.Release();
    31.             SourceRenderTexture = null;
    32.         }
    33.  
    34.         if (OutputRenderTexture != null)
    35.         {
    36.             OutputRenderTexture.Release();
    37.             OutputRenderTexture = null;
    38.         }
    39.     }
    40.  
    41.     void Awake()
    42.     {
    43.         SetupRenderTexture();
    44.         SetupCamera();
    45.     }
    46.  
    47.     private void SetupRenderTexture()
    48.     {
    49.         SourceRenderTexture = new RenderTexture(WIDTH, HEIGHT, 24, RenderTextureFormat.ARGB32);
    50.         SourceRenderTexture.name = "SourceRenderTexture";
    51.         SourceRenderTexture.Create();
    52.         OutputRenderTexture = new RenderTexture(WIDTH, HEIGHT, 24, RenderTextureFormat.ARGB32);
    53.         OutputRenderTexture.name = "OutputRenderTexture";
    54.         OutputRenderTexture.Create();
    55.         _rawImage.texture = OutputRenderTexture;
    56.     }
    57.  
    58.     private void SetupCamera()
    59.     {
    60.         Camera = GetComponent<Camera>();
    61.         Camera.backgroundColor = Color.clear;
    62.         Camera.targetTexture = SourceRenderTexture;
    63.         UniversalAdditionalCameraData data = Camera.GetUniversalAdditionalCameraData();
    64.         data.renderShadows = false;
    65.     }
    66.  
    67.     void Update()
    68.     {
    69.         _material.SetFloat("_Progress", _progress);
    70.         CommandBuffer commandBuffer = new CommandBuffer();
    71.         commandBuffer.Blit(SourceRenderTexture, OutputRenderTexture, _material);
    72.         Graphics.ExecuteCommandBuffer(commandBuffer);
    73.         commandBuffer.Release();
    74.     }
    75.  
    76.     public void OnSetProgress(float progress)
    77.     {
    78.         _progress = progress;
    79.     }
    80. }
     

    Attached Files:

    Last edited: Jun 3, 2023