后处理之辉光效果

这是我在《游戏架构-核心技术与面试精粹》看的,记录一下~

泛光和辉光不一样么?
那是肯定的

辉光(Glow):是全屏泛光的升级版本
区别就是辉光要求场景内只有部分物体泛光,而不是全部泛光
所以需要区分出忽略那些物体


大体思路:
1.用一个额外的摄像机给,在这个摄像机下,所有的非泛光物体都是黑色的,泛光物体保持原样
2.再将这个结果渲染到一个 RT 上,对他进行模糊
3.最后将模糊后的反光图叠加到主摄像机的渲染图像上

创建一个 LightGeometry.shader
默认的surface shader,改了 RenderType
RenderType 可以使用自定义名称,并不会对改 shader 的使用和着色器产生影响
需要自己区别场景中不同渲染对象使用的 Shader 的 RenderType 的类型名称不同

Shader "Custom/LightGeometry"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Glow" } //只改了这里
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

在相机那边,可以通过该函数 动态替换摄像机下物体的 shader

 GetComponent<Camera>().RenderWithShader(replaceShader, "RenderType");

当调用 RenderWithShader 是,会检查摄像机下的每个物体的 RenderTag
(通常选取 Tag 的条件都是 RenderType)

替换的过程:
1.遍历所有要显示的物体
2.如果物体的 Shader 与传入的 replaceShader 有相同的 RenderType
3.则用 replaceShader 的响应 subshader 替换

下面的 shader,增加了一个 subshader
用来将 RenderType 为 Opaque 的物体替换成为黑色,而为 Glow 的内容保持不变
(不发光的物体要绘制呈黑色,避免遮挡关系会出错)

Shader "Custom/RenderGlowRT"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Glow" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }

    SubShader
    {
        Tags {"RenderType" = "Opaque"}
        LOD 200

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
			};

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
			};

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
			}

            fixed4 frag(v2f i) : SV_TARGET
            {
                fixed4 col = fixed4(0, 0, 0, 1);
                return col;
			}

            ENDCG
		}
	}

    FallBack "Diffuse"
}

再创建一个 CustomGlow.cs 脚本,负责:
1.在主摄像机下创建了参数相同的子摄像机
2.并把这个摄像机的渲染目标设置为内存中的一个 RenderTextrue
3.渲染时,通过 RenderImage 将其与主摄像机的内容进行混合
4.使用 RenderWithShader 实现特效时,应该将相机设为disable,防止影响原图的绘制

public class CustomGlow : MonoBehaviour
{
    public Shader renderGlowShader;
    public Material blurMaterial;
    public Material mixMaterial;

    private RenderTexture rt;


    private void Start()
    {
        Camera mainCam = GetComponent<Camera>();

        this.rt = new RenderTexture(Screen.width, Screen.height, (int)mainCam.depth);

        GameObject go = new GameObject("CameraRT");
        go.transform.SetParent(transform, false);

        Camera cam = go.AddComponent<Camera>();
        cam.enabled = false;
        cam.clearFlags = CameraClearFlags.SolidColor;
        cam.backgroundColor = Color.black;
        cam.orthographic = mainCam.orthographic;
        cam.orthographicSize = mainCam.orthographicSize;
        cam.nearClipPlane = mainCam.nearClipPlane;
        cam.farClipPlane = mainCam.farClipPlane;
        cam.fieldOfView = mainCam.fieldOfView;
        cam.fieldOfView = mainCam.fieldOfView;
        cam.targetTexture = rt;

        RenderPostEffect bloomTex = go.AddComponent<RenderPostEffect>();
        bloomTex.replaceShader = renderGlowShader;
        bloomTex.renderMaterial = blurMaterial;

        mixMaterial.SetTexture("_BlurTex", rt);
    }

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        Graphics.Blit(source, destination, mixMaterial);
    }
}

最后创建一个 mixTexture.shader 文件

Shader "Custom/MixTexture"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BlurTex ("Blur Textrue", 2D) = "White" {}
        _BlurFactor ("Blur Factor", Range(0, 1)) = 0.5
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            sampler2D _BlurTex;
            float4 _BlurTex_ST;

            float _BlurFactor;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                fixed4 blur = tex2D(_BlurTex, i.uv);
                fixed4 final = col + blur * _BlurFactor;
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, final);
                return final ;
            }
            ENDCG
        }
    }
}

最后把它挂到相机上,挂到相机上:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 4292-webgl threejs 是一个开源的 JavaScript 库,被广泛应用于 3D 图形的开发,其最大的特点是可以利用 WebGL 技术来让 3D 图形运行在 Web 上。在智慧城市建设中,道路流光效果和建筑模型辉光效果对于增强城市的科技感和时尚感是非常重要的。通过利用 4292-webgl threejs 技术,可以很方便地实现这些效果,让城市更加具有现代感和独特性。 在道路流光效果方面,可以通过使用 4292-webgl threejs 来模拟城市夜景的灯光效果,让道路上的流行车灯光流动起来,从而营造出繁华热闹的城市夜景。 在建筑模型辉光效果方面,通过使用 4292-webgl threejs,可以实现建筑物的多彩辉光效果,让城市更加鲜艳美丽,吸引游客和市民的目光。 此外,特效demo源码的使用可以让开发人员更加高效地完成上述效果的实现,加速智慧城市的建设和发展。 总之,利用 4292-webgl threejs 技术可以让城市更加现代化和美丽化,为智慧城市建设提供了强有力的技术支持。 ### 回答2: 智慧城市是一个新的城市发展模式,借助先进科技手段实现各类城市运行数据的收集、分析和处理,以促进城市治理水平的提升,提高城市居民生活质量。 在智慧城市建设中,道路流光效果和建筑模型辉光效果是比较常见的特效应用。通过对这些特效的实现,可以让城市在视觉上呈现出更具科技感和现代感的感觉。 其中,WebGL和threejs是比较常用的技术工具。WebGL是一种基于Web的3D图形库,通过它可以利用GPU的并行计算能力来实现高性能的图形渲染,包括模型渲染、纹理贴图、光照等。而threejs是基于WebGL的图形库,它提供了更高层次的封装,提供了一系列的3D渲染算法和工具类,可以方便地构建3D场景。 在实现道路流光效果时,我们可以利用threejs提供的ShaderMaterial来实现。ShaderMaterial是一种基于WebGL的材质类型,可以自定义渲染管线中的顶点、像素和片段着色器,并将其与threejs的对象进行关联。 而在实现建筑模型的辉光效果时,则可以利用threejs的BloomPass特效实现。BloomPass是threejs中的一种后处理特效,通过对明亮区域进行高斯模糊,增强高光和光晕效果,实现了辉光效果。 当然,在实现这些特效时,也需要掌握一定的HTML、CSS和JS编程技术,才能完整地展示出智慧城市的效果。此外,为了让广大开发人员更方便地掌握三维图形编程技术,我们还可以通过源码分享的方式来促进交流和共享。 ### 回答3: 现代城市的发展已经不单单是一个单纯的建筑和交通设施的堆砌,更加注重城市的文化和氛围。智慧城市在其中的作用逐渐凸显。在智慧城市的建设中,道路流光和建筑模型的辉光效果是一种可以提升城市氛围的创新做法。这种效果通常是通过webGL和threejs技术来实现的。 webGL是一种基于OpenGL的高性能图形渲染API,旨在为Web进行硬件加速的3D和2D图形渲染,是目前Web引擎标准中的一部分。而threejs是一个JavaScript 3D库。该库使您能够轻松地创建并显示动画化的 3D 图形。threejs作为webGL API的一个包装器,提供了更高层次的抽象,简化了3D绘图的代码实现。 大多数WebGL开发者都应该很熟悉three.js和它的许多出色的功能和演示。对于JavaScript开发人员和设计师,使用three.js代替WebGL API能简化应用程序的堆栈。 在道路流光效果中,通过threejs中的粒子系统,可以快捷地实现流光的效果。这种效果将道路上原本普通的灯光变成了流动的流光线,给持续的夜晚增添了一个梦幻般的氛围。 而在建筑模型中,可以通过三维建模工具,如SketchUp、3DS Max等制作城市建筑物的3D模型。然后通过threejs的灯光工具和材质特效工具,为建筑物添加光效,达到辉光效果。这种效果不仅仅美化了建筑,更可以为城市地标建筑物创造出一个高端、独特的形象。 当然,在threejs中,还有许多不同的特效demo可以使用。这些demo代码可以在github上找到,并在不同的场景中使用。比如裂纹特效、雾化特效、雨雪天特效等等。 总之,webGL和threejs提供的技术,为智慧城市的建设提供了新的思路和工具。道路流光和建筑模型的辉光效果可以提高城市的文化氛围,特效demo则可以让城市更加生动多彩。 我们期待未来这些特效技术和城市的智慧化建设可以越来越广泛的应用到各个领域。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值