Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Graphics.BlitMultiTap only taps once (Unity 5.2, DX 11)

Discussion in 'General Graphics' started by Wompipomp, Sep 18, 2015.

  1. Wompipomp

    Wompipomp

    Joined:
    Aug 31, 2014
    Posts:
    24
    Hi,

    either I still don't understand the working of BlitMultiTap or I do something wrong.
    I want to blend an object many times with different offsets for testing with the following setup:

    ImageEffect code file:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class LensDistortion : MonoBehaviour
    6. {
    7.     private Material _material;
    8.     public Shader ImageShader;
    9.     public Texture2D RampTex;
    10.  
    11.  
    12.     public Material Material
    13.     {
    14.         get
    15.         {
    16.             if (_material == null)
    17.             {
    18.                 _material = new Material(ImageShader);
    19.                 _material.hideFlags = HideFlags.HideAndDontSave;
    20.             }
    21.             return _material;
    22.         }
    23.     }
    24.  
    25.     public void OnRenderImage(RenderTexture source, RenderTexture destination)
    26.     {
    27.         Graphics.BlitMultiTap(source, destination, Material,
    28.         new Vector2(-50, -50),
    29.         new Vector2(50, -50),
    30.         new Vector2(-50, 50),
    31.         new Vector2(50, 50),
    32.         new Vector2(-100, -100),
    33.         new Vector2(-100, 100),
    34.         new Vector2(100, -100)
    35.         );
    36.  
    37.     }
    38.  
    39.     public void OnDisable()
    40.     {
    41.         if (_material != null)
    42.         {
    43.             DestroyImmediate(_material);
    44.         }
    45.     }
    46. }
    47.  
    Shader:
    Code (CSharp):
    1. Shader "Hidden/MultiTap" {
    2.     Properties{ _MainTex("", any) = "" {} }
    3.    
    4.         CGINCLUDE
    5. #include "UnityCG.cginc"
    6.     struct v2f {
    7.         float4 pos : POSITION;
    8.         half2 uv : TEXCOORD0;
    9.         half2 taps[4] : TEXCOORD1;
    10.     };
    11.     sampler2D _MainTex;
    12.     half4 _MainTex_TexelSize;
    13.     half4 _BlurOffsets;
    14.     v2f vert(appdata_img v) {
    15.         v2f o;
    16.         o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    17.         o.uv = v.texcoord;
    18.         return o;
    19.     }
    20.     half4 frag(v2f i) : COLOR{
    21.         return tex2D(_MainTex, i.uv);
    22.     }
    23.     ENDCG
    24.    
    25.         SubShader {
    26.         Pass{
    27.             ZTest Always Cull Off ZWrite Off
    28.             Fog{ Mode off }
    29.  
    30.             CGPROGRAM
    31.         #pragma fragmentoption ARB_precision_hint_fastest
    32.         #pragma vertex vert
    33.         #pragma fragment frag
    34.         ENDCG
    35.         }
    36.     }
    37.     Fallback off
    38. }
    Basically I took the BlurMultiConeTap shader and modified it.

    Camera setup:
    Multitap.PNG

    Result:
    MultiTapSphere.PNG

    But for my understanding it should be more like:


    It seems the first offset vector is already added to the v.texcoord so the first tap is already at the correct position but I cannot get the other taps to process or am I missing something?
    Do I still have to blend or activate anything?

    I use Unity Free 5.2.

    Thank you
    Best regards
    Mark
     
  2. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    210
    Pretty sure this was broken in Unity 5.x

    Our blur effect we've had for years stopped working when we made the switch.
     
  3. Wompipomp

    Wompipomp

    Joined:
    Aug 31, 2014
    Posts:
    24
    Thanks for the info :)
     
  4. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    335
    So any update on this?
    Has this been reported to Unity? Will it be fixed?
     
  5. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    210
    We were only using it for the one thing, so when it appeared to break it was easier to just do a quick workaround, rather than do a full bug report. I suggest you file one if you're running into the same issue :)
     
  6. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    335
    Yeah I made a workaround as well, by just writing a fragments shader that does the same thing.
    If I find some time I'll report it to Unity.
    (The annoying thing is that when submitting a bug report, they always want a detailed description + project demonstrating the problem, which costs a lot of time on my end.)
     
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    6,195
    Reported this also, got confirmation:
    #820987

    If you use array for the params, it only uses the first item..
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    7,082
    That bug report on the issue tracker says "By design". ( https://issuetracker.unity3d.com/is...does-not-work-when-given-array-as-a-parameter )

    So can someone help me understand what BlitMultiTap is supposed to do?

    The manual says

    Okay, so how do access those values? It seems my v.texcoord is offset by the first vector2 I pass to it and... that's it?

    Also, why use it in the first place? What is the point? (why not do the taps and combine them in the shader? Is there a performance improvement somehow? is it for convenience?)
     
  9. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    472
    +1... I just don't understand BlitMultiTap, haven't seen a single example of what it does and why!
     
  10. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    7,082
    I haven't tried it recently, but I think it's supposed to work like this:

    Instead of doing something like

    Code (csharp):
    1.             v2f_tap vertTap(appdata_img v)
    2.             {
    3.                 v2f_tap o;
    4.                 o.pos = UnityObjectToClipPos(v.vertex);
    5.                 half off = _OffS;
    6.                 o.uv = v.texcoord;
    7.                 o.uv20 = v.texcoord + _MainTex_TexelSize * half2(off, 0);
    8.                 o.uv21 = v.texcoord + _MainTex_TexelSize * half2(-off, 0);
    9.                 return o;
    10.             }
    where you're calculating your offsets in a vertex shader, you would use v.texcoord1 and v.texcoord2 and not have to do any math etc.

    So I guess it precalculates the offsets so you have them ready in your shader.

    (I'm guessing it's a macro of sorts that uses this : https://docs.unity3d.com/ScriptReference/GL.MultiTexCoord.html ).

    My best guess (super emphasis on guess), is that there isn't really a performance benefit, it's just a convenient way to be able to control the offsets by script instead of doing a bunch of Material.SetFloat and Material.SetVector to control your offsets.

    An official response would be kinda nice though.
     
  11. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    472
    While reading your answer I was like "well, this seems like quite a specific usecase to deserve it's own API", until:
    ... "ooooh, cubemaps!"
    So I guess yeah maybe :) ?
     
  12. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    7,082
    Yeah, dunno, maybe, please note that my post is more or less me guessing stuff, so... (which is why a dev clarifying things would be great)
     
unityunity