unity-hou-qi-chu-li-bao-zheng-bei-jing-yan-se - vss388 com vn đăng nhập
Trang web vss388 com vn đăng nhập tiện lợi
Đảm bảo màu nền trong xử lý hậu kỳ Unity
Do đó, tôi đã tạo ra một kịch bản để điền màu cụ thể dựa trên thông tin độ sâu của máy ảnh. Kịch bản này nên được gắn sau Color Grading để đảm bảo hiệu quả mong muốn.
namespace FinGameWorks.Scripts.PostProcess
{
using System;
using UnityEngine;
[RequireComponent(typeof(Camera))]
[ExecuteInEditMode]
public class MaskedFirstPass : MonoBehaviour
{
[Range(0f, 3f)] public float depthLevel = 0.5f; // Mức độ sâu cho phép
public Color backgroundColor = Color.white; // Màu nền mặc định
public Shader _shader; // Shader tùy chỉnh
private Shader shader
{
get { return _shader != null ? _shader : (_shader = Shader.Find("MaskedFirstPassShader")); }
}
private Material _material;
private Material material
{
get
{
if (_material == null)
{
_material = new Material(shader);
_material.hideFlags = HideFlags.HideAndDontSave;
}
return _material;
}
}
private void Start()
{
if (!SystemInfo.supportsImageEffects) // Kiểm tra hỗ trợ hiệu ứng hình ảnh
{
print("Hệ thống không hỗ trợ hiệu ứng hình ảnh");
enabled = false;
return;
}
if (shader == null || !shader.isSupported) // Kiểm tra tính khả dụng của Shader
{
enabled = false;
print("Shader " + shader.name + " không được hỗ trợ");
return;
}
GetComponent<Camera>().depthTextureMode = DepthTextureMode.Depth; // Bật chế độ texture độ sâu
}
private void OnDisable()
{
if (_material != null) // Xóa tài nguyên khi tắt component
DestroyImmediate(_material);
}
private void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (shader != null)
{
material.SetFloat("_DepthLevel", depthLevel); // Cập nhật mức độ sâu
material.SetColor("_BGColor", backgroundColor); // Thiết lập màu nền
Graphics.Blit(src, dest, material); // Áp dụng hiệu ứng lên ảnh
}
else
{
Graphics.Blit(src, dest); // Nếu không có Shader, chỉ render ảnh gốc
}
}
}
}
Shader tùy chỉnh
Shader dưới đây giúp kiểm soát việc hiển thị màu nền dựa trên thông tin độ sâu của máy ảnh:
Shader "MaskedFirstPassShader"
{
Properties
{
_BGColor ("Màu Nền", COLOR) = (1.0,1.0,1.0,1.0) // Màu nền tùy chỉnh
_DepthLevel ("Mức Độ Sâu", Range(1, 3)) = 1 // Phạm vi độ sâu
_MainTex ("Nguồn", 2D) = "white" {} // Texture nguồn
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _CameraDepthTexture; // Texture độ sâu
uniform half4 _MainTex_TexelSize; // Kích thước texel
uniform float4 _BGColor; // Màu nền
uniform fixed _DepthLevel; // Mức độ sâu
struct input
{
float4 pos : POSITION;
half2 uv : TEXCOORD0;
};
struct output
{
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
};
output vert(input i)
{
output o;
o.pos = UnityObjectToClipPos(i.pos); // Chuyển đổi [sicbo](/posts/21/) tọa độ từ object sang clip space
o.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, i.uv); // Tính toán UV cho texture
#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0) // Điều chỉnh hướng UV nếu cần
o.uv.y = 1 - o.uv.y;
#endif
return o;
}
sampler2D _MainTex; // Texture chính
float4 _MainTex_ST; // Scale and Offset cho texture
fixed4 frag(output o) : COLOR
{
float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, o.uv)); // Lấy giá trị độ sâu
depth = pow(Linear01Depth(depth), _DepthLevel); // Chuyển đổi và áp dụng mức độ sâu
if (depth < 0.5) // Kiểm tra điều kiện độ sâu
{
float4 color = tex2D(_MainTex,
UnityStereoScreenSpaceUVAdjust(
o.uv, _MainTex_ST)); // Lấy màu từ texture nguồn
return color;
}
else
{
return _BGColor; // Trả về màu nền nếu điều kiện không thỏa mãn
}
}
ENDCG
}
}
}
Bằng cách kết hợp hai đoạn mã trên, bạn có thể đảm bảo rằng màu nền của máy ảnh sẽ không bị thay đổi bởi các hiệu ứng hậu kỳ khác, đồng thời duy trì sự linh hoạt trong thiết kế giao diện ứng dụng.
Sửa đổi lần cuối vào 2025-04-06