UnityShader 는 속도 맵 을 사용 하여 운동 의 모호 함 을 실현 합 니 다.
6704 단어 UnityShader운동 이 모호 하 다
원리:
픽 셀 의 현재 프레임 의 NDC 좌표(x,y 값 은 uv 에서 매 핑 되 고 z 값 은 깊이 값 에서 매 핑 됩 니 다)―(사용CurrentView Projection InverseMartix 변환 및 w 분량 나 누 기)―픽 셀 의 세계 좌표―(사용Previous View ProjectionMatrix 변환,w 분량 으로 나 누 기)―픽 셀 의 앞 프레임 NDC 좌표―(현재 프레임 NDC-앞 프레임 NDC)―속도
1.이 코드 는 카메라 에 걸 어 카 메 라 를 움 직 이게 한다.
using UnityEngine;
using System.Collections;
public class Translating : MonoBehaviour {
public float speed = 10.0f;
public Vector3 startPoint = Vector3.zero;
public Vector3 endPoint = Vector3.zero;
public Vector3 lookAt = Vector3.zero;
public bool pingpong = true;
private Vector3 curEndPoint = Vector3.zero;
// Use this for initialization
void Start () {
transform.position = startPoint;
curEndPoint = endPoint;
}
// Update is called once per frame
void Update () {
transform.position = Vector3.Slerp(transform.position, curEndPoint, Time.deltaTime * speed);
transform.LookAt(lookAt);
if (pingpong) {
if (Vector3.Distance(transform.position, curEndPoint) < 0.001f) {
curEndPoint = Vector3.Distance(curEndPoint, endPoint) < Vector3.Distance(curEndPoint, startPoint) ? startPoint : endPoint;
}
}
}
}
2.이 코드 는 카메라 에 걸 려 있다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MotionBlurWithDepthTexture : PostEffectsBase
{
public Shader MotionBlurShader;
private Material _motionBlurMaterial = null;
public Material Material
{
get
{
_motionBlurMaterial = CheckShaderAndCreateMaterial(MotionBlurShader, _motionBlurMaterial);
return _motionBlurMaterial;
}
}
//
[Range(0.0f, 1.0f)] public float BlurSize = 0.5f;
// Camera , ,
private Camera _myCamera;
public Camera Camera
{
get
{
if (_myCamera == null)
{
_myCamera = GetComponent<Camera>();
}
return _myCamera;
}
}
// *
private Matrix4x4 _previousViewProjectionMatrix;
// OnEnable ,
void OnEnable()
{
Camera.depthTextureMode = DepthTextureMode.Depth;
}
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (Material != null)
{
// Shader
Material.SetFloat("_BlurSize", BlurSize);
// * NDC( ) ,
// ,
// * Shader
Material.SetMatrix("_PreviousViewProjectionMatrix", _previousViewProjectionMatrix);
// *
//Camera.projectionMatrix
//Camera.worldToCameraMatrix
Matrix4x4 currentViewProjectionMartix = Camera.projectionMatrix * Camera.worldToCameraMatrix;
// *
Matrix4x4 currentViewProjectionInverseMartix = currentViewProjectionMartix.inverse;
// * Shader
Material.SetMatrix("_CurrentViewProjectionInverseMartix", currentViewProjectionInverseMartix);
// * *
_previousViewProjectionMatrix = currentViewProjectionMartix;
Graphics.Blit(src, dest, Material);
}
else
{
Graphics.Blit(src, dest);
}
}
}
3.이 Shader 는 코드 2 에 값 을 부여 합 니 다.
Shader "Unity Shaders Book/Chapter 13/MotionBlurWithDepthTexture"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
//
_BlurSize ("Blur Size", Float) = 1.0
// _PreviousViewProjectionMatrix _CurrentViewProjectionInverseMartix
// Unity , CG ,
}
SubShader
{
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
// _MainTex_TexelSize
half4 _MainTex_TexelSize;
//Unity
sampler2D _CameraDepthTexture;
// _PreviousViewProjectionMatrix _CurrentViewProjectionInverseMartix
float4x4 _PreviousViewProjectionMatrix;
float4x4 _CurrentViewProjectionInverseMartix;
half _BlurSize;
//
struct v2f {
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
//
half2 uv_depth : TEXCOORD1;
};
v2f vert(appdata_img v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
o.uv_depth = v.texcoord;
//
#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0) {
o.uv_depth.y = 1 - o.uv_depth.y;
}
#endif
return o;
}
//
fixed4 frag(v2f i) : SV_Target{
// ,
float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv_depth);
// NDC ,xy ,z d
float4 H = float4(i.uv.x * 2 - 1, i.uv.y * 2 - 1, d * 2 - 1, 1);
// * H
float4 D = mul(_CurrentViewProjectionInverseMartix, H);
// w ,
float4 worldPos = D / D.w;
// NDC
float4 currentPos = H;
// * worldPos, w , NDC
float4 previousPos = mul(_PreviousViewProjectionMatrix, worldPos);
previousPos /= previousPos.w;
// ,
float2 velocity = (currentPos.xy - previousPos.xy) / 2.0f;
// , , _BlurSize
float2 uv = i.uv;
float4 c = tex2D(_MainTex, uv);
uv += velocity * _BlurSize;
for (int it = 1; it < 3; it++, uv += velocity * _BlurSize) {
float4 currentColor = tex2D(_MainTex, uv);
c += currentColor;
}
c /= 3;
return fixed4(c.rgb, 1.0);
}
ENDCG
Pass
{
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
Fallback Off
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
UnityShader 는 이미지 중첩 을 사용 하여 운동 의 모호 함 을 실현 합 니 다.본 논문 의 사례 는 유 니 티 Shader 가 운동 의 모호 함 을 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다. 1.이 코드 는...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.