如何使Unity玻璃着色器仅折射它背后的物体?

Mil*_*les 3 shader hlsl unity-game-engine fragment-shader

我正在寻找Unity的玻璃着色器,它只能折射它背后的物体,或者想要如何修改现有的玻璃着色器来做到这一点.

此屏幕截图显示了在曲面网格上使用FX/Glass/Stained BumpDistort时会发生什么.

在此输入图像描述

如您所见,玻璃着色器折射网格前面的球体和网格后面的地面.我正在寻找一个只能折射它背后的物体的着色器.

以下是该着色器的代码,供参考:

// Per pixel bumped refraction.
// Uses a normal map to distort the image behind, and
// an additional texture to tint the color.

Shader "FX/Glass/Stained BumpDistort" {
Properties {
    _BumpAmt  ("Distortion", range (0,128)) = 10
    _MainTex ("Tint Color (RGB)", 2D) = "white" {}
    _BumpMap ("Normalmap", 2D) = "bump" {}
}

Category {

    // We must be transparent, so other objects are drawn before this one.
    Tags { "Queue"="Transparent" "RenderType"="Opaque" }


    SubShader {

        // This pass grabs the screen behind the object into a texture.
        // We can access the result in the next pass as _GrabTexture
        GrabPass {
            Name "BASE"
            Tags { "LightMode" = "Always" }
        }

        // Main pass: Take the texture grabbed above and use the bumpmap to perturb it
        // on to the screen
        Pass {
            Name "BASE"
            Tags { "LightMode" = "Always" }

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"

struct appdata_t {
    float4 vertex : POSITION;
    float2 texcoord: TEXCOORD0;
};

struct v2f {
    float4 vertex : SV_POSITION;
    float4 uvgrab : TEXCOORD0;
    float2 uvbump : TEXCOORD1;
    float2 uvmain : TEXCOORD2;
    UNITY_FOG_COORDS(3)
};

float _BumpAmt;
float4 _BumpMap_ST;
float4 _MainTex_ST;

v2f vert (appdata_t v)
{
    v2f o;
    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    #if UNITY_UV_STARTS_AT_TOP
    float scale = -1.0;
    #else
    float scale = 1.0;
    #endif
    o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    o.uvgrab.zw = o.vertex.zw;
    o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
    o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
    UNITY_TRANSFER_FOG(o,o.vertex);
    return o;
}

sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
sampler2D _BumpMap;
sampler2D _MainTex;

half4 frag (v2f i) : SV_Target
{
    // calculate perturbed coordinates
    half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg; // we could optimize this by just reading the x & y without reconstructing the Z
    float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
    i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;

    half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    half4 tint = tex2D(_MainTex, i.uvmain);
    col *= tint;
    UNITY_APPLY_FOG(i.fogCoord, col);
    return col;
}
ENDCG
        }
    }

    // ------------------------------------------------------------------
    // Fallback for older cards and Unity non-Pro

    SubShader {
        Blend DstColor Zero
        Pass {
            Name "BASE"
            SetTexture [_MainTex] { combine texture }
        }
    }
}

}
Run Code Online (Sandbox Code Playgroud)

我的直觉是它与_GrabTexture被捕获的方式有关,但我并不完全确定.我很感激任何建议.谢谢!

Emi*_*nez 5

对此没有简单的答案.如果不以某种方式考虑上下文,你就无法考虑折射,所以让我们看看:

基本上,定义一个对象何时"落后"另一个对象并不容易.有一些方法甚至可以确保一个点到摄像机的距离,更不用说考虑整个几何.有许多奇怪的情况,几何相交,中心和边界可以在任何地方.

光线追踪算法中通常很容易考虑折射(你只需要射线并计算它如何反弹/折射以获得颜色).但是在光栅图形(用于99%的实时图形)中,对象作为一个整体呈现,并轮流呈现.

该图像的作用是首先渲染背景和球,然后再渲染玻璃.玻璃不会"折射"任何东西,它只会将自身描绘为对渲染缓冲区中所写内容的扭曲.

"之前"是关键.你不会在光栅图形中获得"落后",一切都是通过意识到渲染顺序来完成的.让我们看看如何创建一些折射:

  • 手动为着色器设置渲染队列标记,以便您知道它们在管道中的哪个点被绘制
  • 手动设置每个材质的渲染队列
  • 创建一个不断编组场景的脚本,每个帧根据您想要的位置或任何方法计算玻璃之前或之后应绘制的内容,并在材质中设置渲染队列
  • 创建一个脚本,渲染场景(通过各种方法)过滤掉不应被折射的对象,并将其用作要折射的纹理(取决于场景的复杂性,这有时是必要的)

这些只是我头脑中的一些选项,一切都取决于你的场景

我的建议:

  • 选择球的材料
  • 右键单击Inspector窗口 - >勾选"Debug"模式
  • 将自定义渲染队列设置为2200(绘制常规几何体后)
  • 选择玻璃材料
  • 将自定义渲染队列设置为2100(在大多数几何体之后,但在球之前)