關於反射探頭的實時反射

用反射探頭實現鏡面反射

以前看到過一個日本人寫的文章,關於如何使用Unity的反射探頭來實現地板的鏡面反射效果。git

文章地址:nn-hokuson.hatenablog.com/entry/2016/…github

核心代碼很短,主要是根據下圖所示實時更新反射探頭的位置:性能優化

先貼一下位置計算的代碼:bash

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProbeReflection : MonoBehaviour
{
    public GameObject reflectionPlane;

    private ReflectionProbe probe;

    void Start()
    {
        this.probe = GetComponent<ReflectionProbe>();
    }

    void Update()
    {
        if(reflectionPlane == null)
        {
            return;
        }

        Camera camera = Camera.main;

        if(camera == null)
        {
            return;
        }

        float probeY = reflectionPlane.transform.position.y - camera.transform.position.y;

        this.probe.transform.position = new Vector3(
            camera.transform.position.x,
            reflectionPlane.transform.position.y + probeY,
            camera.transform.position.z
        );
    }
}
複製代碼

這裏反射探頭設置成實時(Realtime),Cubemap分辨率設置爲2048:app

再把地板的金屬度和光滑度都調到1,效果仍是很好的,以下圖:性能

性能問題

上圖的鏡面反射效果很好,不過須要關注一下性能問題。測試

這裏作一個測試,仍是剛纔那個場景,把反射探頭的 Time Slicing 設置爲 No time slicing,即每幀完整的繪製一次環境,咱們對比一下drawcall:優化

能夠看到,如此一個簡單場景,打開探頭實時渲染後,drawcall從125翻到了540。ui

Unity的官方文檔對此有以下說明:this

Each of the six cubemap faces must be rendered separately using a 「camera 」 at the probe’s origin.

The probes will need to be rendered a separate time for each reflection bounce level (see documentation on Advanced Reflection Probes for further details).

The various mipmap levels of the cubemap must undergo a blurring process to allow for glossy reflections.

首先,環境會被渲染至cubemap,而cubemap的6個面都須要各自渲染,以下圖:

其次,反射探頭的環境繪製還會考慮到反彈,反彈的次數由Lighting面板全局控制,以下圖:

最後,爲了正確的計算間接高光,生成各級mipmap的時候還有一個blur操做。

性能優化

對於實時反射探頭的性能優化,Unity官方列了以下幾點:

  1. 下降cubemap的分辨率。

  2. 利用Cull Mask只渲染對環境影響較大的部分,好比天空盒。

  3. 限制繪製環境的頻率,避免每幀繪製。

  4. 分幀完成環境繪製。

這裏的優化方案屬於常規方式。

雙拋物面映射

此外,若是要求不是那麼高,咱們也不必定須要cubemap。

前項目在作環境的實時繪製時,用了另一個方式:雙拋物面映射。

關於雙拋物面映射,知乎有一篇推導寫得很好:詳解雙拋物面環境映射

利用雙拋物面映射,咱們能夠把cubemap的6次場景繪製下降爲下圖的2次:

截圖源自 Dual Paraboloid Environment Mapping Whitepaper

另外,前項目只生成mipmap,並不作blur操做,代碼細節留到下篇再寫。

關於粗糙度

以前爲了測試鏡面反射,我把金屬度和光滑度都設置成了1。

如今給地板一點粗糙度,這裏我把光滑度下降爲0.8,效果以下:

反射變得模糊了。

Unity會根據粗糙度來計算mipmap level,粗糙度越高mipmap level就越高,反射也就越模糊,代碼以下:

#ifndef UNITY_SPECCUBE_LOD_STEPS
#define UNITY_SPECCUBE_LOD_STEPS (6)
#endif

half perceptualRoughnessToMipmapLevel(half perceptualRoughness)
{
    return perceptualRoughness * UNITY_SPECCUBE_LOD_STEPS; 
}

half3 Unity_GlossyEnvironment (UNITY_ARGS_TEXCUBE(tex), half4 hdr, Unity_GlossyEnvironmentData glossIn)
{
    half perceptualRoughness = glossIn.roughness /* perceptualRoughness */ ;
    perceptualRoughness = perceptualRoughness*(1.7 - 0.7*perceptualRoughness); 

    half mip = perceptualRoughnessToMipmapLevel(perceptualRoughness);
    half3 R = glossIn.reflUVW;
    half4 rgbm = UNITY_SAMPLE_TEXCUBE_LOD(tex, R, mip);

    return DecodeHDR(rgbm, hdr);
}
複製代碼

若是咱們把光滑度再下降到0.7,反射的人物顏色幾乎已經看不見了,以下圖:

以前寫過一個 屏幕空間反射的材質插件,一樣是0.7的光滑度,經過多條反射射線採樣平均,反射顏色的細節能夠獲得更多保留,以下圖:

不過以上純屬廣告,與本文無關。

我的主頁

本文的我的主頁連接:baddogzz.github.io/2020/04/22/…

好了,拜拜!

相關文章
相關標籤/搜索