Custom Unity Distortion Shader Using Post Processing

preview_player
Показать описание

Custom Unity Distortion Shader Using Post Processing

In this video, we will use the simple post-processing we did last time. To create a masked-like post effect. We will be taking an object, sampling our post-effect texture, then manipulating it. Remember this is a post effect so you can do anything from distortion, to masking, and you can even have lighting info if needed.

Ben here, and I've been writing shaders and full systems of shaders in Unity from scratch for 6-7 years now. Shaders, especially HLSL or CG shaders seem to be less known in Unity, so I just wanted to do my part, share my experiences and really show others how to write shader code.

The goal is to teach others how to write optimal shader code that is production-ready. I also want to try to recreate the cool-looking effects we see in other games. It would be awesome to see more and more people write shaders from scratch, understand it at a deeper level and ultimately create their own shaders.

#unity3d #unitydeveloper #gamedev #shaders

For more in-depth access I do have a Udemy course that I will continue to update as I create more mini-lessons.

Рекомендации по теме
Комментарии
Автор

This is the best shader channel on YouTube.

slob
Автор

Very cool shader man, this helped me get my planetary height fog post process effect sorted out, thanks.

MK-lknc
Автор

I followed your previous video and got the tint working. However, I get to about 1:55 in this video and my sphere is just a grey sphere in scene and completely invisible (with no tint control capability) in game. I added it to the appropriate layer and changed the cull lists which renders it invisible, but that's it. The steps missing between the last video and this one would probably help clear that up.

MatthewConnolly-jn
Автор

How to make a post process distort shader for hdrp?

starbuck
Автор

Does anyone know if this shader works in URP?
I have been trying my hand at this with a few differences, I expect a tranparent or render of the postcam, but i just get a white texture on the sphere. ...I can't get it to work, can anyone help me debug?
Shader "Custom/DistortShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_ScreenTint ("_ScreenTint", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100

Pass
{
Blend One OneMinusSrcAlpha
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;
// Bring in Screen Position
float4 screenPostion : TEXCOORD1;
};

sampler2D _MainTex;
float4 _MainTex_ST;
float4 _ScreenTint;

v2f vert (appdata v)
{
v2f o;
o.vertex =
// o.uv = v.uv, not _MainTex sample
o.uv = v.uv;
// @1:25 Compute screen position
o.screenPostion = ComputeScreenPos(o.vertex);
return o;
}

fixed4 frag (v2f i) : SV_Target
{
float2 textureCoordinate = i.screenPostion.xy / i.screenPostion.w;
fixed4 col = tex2D(_MainTex, textureCoordinate);
col *= _ScreenTint;
return col;
}
ENDCG
}
}
}


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

// 12-25-23 Distortion Shader. This script is attached to the post cam, which is a child of the main cam.
public class DistortionPFXCtrl : MonoBehaviour
{
[SerializeField] private GameObject _postFXSphere;
private Material _mat;
private RenderTexture _renderTex;
[SerializeField] public Color ScreenTint;

private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (_mat == null)
{
_mat =
}

if (_renderTex == null)
{
_renderTex = new RenderTexture(source.width, source.height, 0, source.format);
}

_mat.SetColor("_ScreenTint", ScreenTint);

Graphics.Blit(source, _renderTex, _mat, 0);

_mat.mainTexture = _renderTex;

Graphics.Blit(source, destination);

_renderTex.Release();
}
}
Edit: It does not work in URP.

maxfun