Convert Depth to Position - c#

I would like to convert a DepthInformation into a position.
Somehow I can't do it even after several attempts.
I try to get the position of objects fragment from the DepthInformation that i get from an Depthpeeling shader.
This is the code writing the information in a RenderTexture
Shader "Hidden/OIT/Depth Peeling/Depth Peeling" {
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_MainTex ("Main Tex", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "bump" {}
}
SubShader {
Tags {"Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="Transparent"}
Pass {
Tags { "LightMode"="ForwardBase" }
ZWrite On
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
sampler2D _PrevDepthTex;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float3 lightDir: TEXCOORD0;
float3 viewDir : TEXCOORD1;
float2 uv : TEXCOORD2;
float4 screenPos: TEXCOORD3;
float depth : TEXCOORD4;
};
struct PixelOutput {
fixed4 col : COLOR0;
fixed4 depth : COLOR1;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
TANGENT_SPACE_ROTATION;
// Transform the light direction from object space to tangent space
o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
// Transform the view direction from object space to tangent space
o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
o.screenPos = ComputeScreenPos(o.pos);
o.depth = COMPUTE_DEPTH_01;
return o;
}
PixelOutput frag(v2f i) : SV_Target {
float depth = i.depth;
float prevDepth = DecodeFloatRGBA(tex2Dproj(_PrevDepthTex, UNITY_PROJ_COORD(i.screenPos)));
clip(depth - (prevDepth + 0.00001));
fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);
fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uv));
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal, tangentLightDir));
PixelOutput o;
o.col = fixed4(ambient + diffuse, _Color.a);
o.depth = EncodeFloatRGBA(i.depth);
return o;
}
ENDCG
}
}
FallBack "Diffuse/VertexLit"
}
and this is the function where i try to decode it again to a world position:
float4 DepthTextureToPos(sampler2D depthSampler,float2 uvCoord,float4 screenPos) {
float depth = DecodeFloatRGBA(tex2Dproj(depthSampler, UNITY_PROJ_COORD(screenPos)));
// H is the viewport position at this pixel in the range -1 to 1.
float4 H = float4((uvCoord.x) * 2 - 1, (uvCoord.y) * 2 - 1, depth, 1.0);
float4 D = mul(_ViewProjectInverse, H);
//return D / D.w;
float4 objPos= mul(unity_WorldToObject, (D / D.w));
return objPos;
}
the uv Coords have this as input:
float2 texc = i.screenPosition.xy / i.screenPosition.w;
the screenPos has this as Input:
ComputeScreenPos(o.vertex);

Related

Unity distance fade shader

I'm working on a URP shader in unity that is supposed to make an object transparent the further away it is from the camera.
The shader I wrote does not work and I can't find a solution.
Shader "Custom/DistanceFade" {
Properties{
_Color("Color", Color) = (1,1,1,1)
_Threshold("Threshold", float) = 5
}
SubShader{
Tags { "Queue" = "Transparent" }
LOD 200
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _Color;
float _Threshold;
v2f vert(appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord;
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed4 col = tex2D(_MainTex, i.texcoord);
float _distance = distance(_WorldSpaceCameraPos, i.vertex);
col.a = saturate((_Threshold - _distance) / _Threshold);
return col * _Color;
}
ENDCG
}
}
FallBack "Diffuse"
}
Does anyone have an idea?
Basically you just need to enable blending assuming you have meaningful distance values. Here is a variant which does not depend on a depth texture for the distance and gives some control over the fading interval:
Shader "Custom/DistanceFade" {
Properties{
_Color("Color", Color) = (1,1,1,1)
_Threshold("Threshold", float) = 5
}
SubShader{
Tags { "Queue" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha // <-- enable blending
LOD 200
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
// make float3 for an extra value (can be done in other ways)
float3 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _Color;
float _Threshold;
v2f vert(appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord.xy = v.texcoord.xy;
// get the distance from the camera to the vertex
// (we do it in world space)
float dist = distance(_WorldSpaceCameraPos, mul(unity_ObjectToWorld, v.vertex));
// map the distance to an fade interval
float beginfade = 500;
float endfade = 600;
float alpha = min(max(dist, beginfade), endfade) - beginfade;
alpha = 1 - alpha / (endfade - beginfade);
// put alpha somewhere unused to deliver it to the fragment shader
o.texcoord.z = alpha ;
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed4 col = tex2D(_MainTex, i.texcoord);
// use our fade value
col.a = i.texcoord.z;
return col * _Color;
}
ENDCG
}
}
FallBack "Diffuse"
}

Shader error unrecognized identifier 'Blit'

I'm making a volumetric lightning from the tutorial on https://www.kodeco.com/22027819-volumetric-light-scattering-as-a-custom-renderer-feature-in-urp.
and I'm getting an error from unity: [Worker0] Shader error in 'Hidden/RW/RadialBlur': unrecognized identifier 'Blit' at line 26 (on d3d11)
I tried changing the variables sorting but didn't worked.
Full code :
Shader "Hidden/RW/RadialBlur"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_BlurWidth("Blur Width", Range(0,1)) = 0.85
_Intensity("Intensity", Range(0,1)) = 1
_Center("Center", Vector) = (0.5,0.5,0,0)
}
SubShader
{
// No culling or depth
Blend One One
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define NUM_SAMPLES 100
float _BlurWidth;
float _Intensity;
float4 _Center;
// HERER HERER HERER
Blit(cmd, occluders.Identifier(), cameraColorTargetIdent,
radialBlurMaterial);
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
radialBlurMaterial.SetVector("_Center", new Vector4(
sunPositionViewportSpace.x, sunPositionViewportSpace.y, 0, 0));
radialBlurMaterial.SetFloat("_Intensity", intensity);
radialBlurMaterial.SetFloat("_BlurWidth", blurWidth);
fixed4 frag(v2f i) : SV_Target
{
//1
fixed4 color = fixed4(0.0f, 0.0f, 0.0f, 1.0f);
//2
float2 ray = i.uv - _Center.xy;
//3
for (int i = 0; i < NUM_SAMPLES; i++)
{
float scale = 1.0f - _BlurWidth * (float(i) /
float(NUM_SAMPLES - 1));
color.xyz += tex2D(_MainTex, (ray * scale) +
_Center.xy).xyz / float(NUM_SAMPLES);
}
//4
return color * _Intensity;
}
ENDCG
}
}
}
I'm a beginner to shader programming please help.

How To Use Custom Shader With URP

I found this BLUR Shader and it works beautifully which I apply to a material.
Shader "Custom/BLUR" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_BumpAmt ("Distortion", Range (0,128)) = 10
_MainTex ("Tint Color (RGB)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_Size ("Size", Range(0, 20)) = 1
}
Category {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" }
SubShader {
GrabPass {
Tags { "LightMode" = "Always" }
}
Pass {
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float4 uvgrab : TEXCOORD0;
};
v2f vert (appdata_t v) {
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
return o;
}
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float _Size;
half4 frag( v2f i ) : COLOR {
half4 sum = half4(0,0,0,0);
#define GRABPIXEL(weight,kernelx) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
sum += GRABPIXEL(0.05, -4.0);
sum += GRABPIXEL(0.09, -3.0);
sum += GRABPIXEL(0.12, -2.0);
sum += GRABPIXEL(0.15, -1.0);
sum += GRABPIXEL(0.18, 0.0);
sum += GRABPIXEL(0.15, +1.0);
sum += GRABPIXEL(0.12, +2.0);
sum += GRABPIXEL(0.09, +3.0);
sum += GRABPIXEL(0.05, +4.0);
return sum;
}
ENDCG
}
GrabPass {
Tags { "LightMode" = "Always" }
}
Pass {
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float4 uvgrab : TEXCOORD0;
};
v2f vert (appdata_t v) {
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
return o;
}
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
float _Size;
half4 frag( v2f i ) : COLOR {
half4 sum = half4(0,0,0,0);
#define GRABPIXEL(weight,kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely*_Size, i.uvgrab.z, i.uvgrab.w))) * weight
sum += GRABPIXEL(0.05, -4.0);
sum += GRABPIXEL(0.09, -3.0);
sum += GRABPIXEL(0.12, -2.0);
sum += GRABPIXEL(0.15, -1.0);
sum += GRABPIXEL(0.18, 0.0);
sum += GRABPIXEL(0.15, +1.0);
sum += GRABPIXEL(0.12, +2.0);
sum += GRABPIXEL(0.09, +3.0);
sum += GRABPIXEL(0.05, +4.0);
return sum;
}
ENDCG
}
GrabPass {
Tags { "LightMode" = "Always" }
}
Pass {
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float4 uvgrab : TEXCOORD0;
float2 uvbump : TEXCOORD1;
float2 uvmain : TEXCOORD2;
};
float _BumpAmt;
float4 _BumpMap_ST;
float4 _MainTex_ST;
v2f vert (appdata_t v) {
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
return o;
}
fixed4 _Color;
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
sampler2D _BumpMap;
sampler2D _MainTex;
half4 frag( v2f i ) : COLOR {
half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg;
float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
half4 tint = tex2D( _MainTex, i.uvmain ) * _Color;
return col * tint;
}
ENDCG
}
}
}
}
But when using with URP Asset (Universal Render Pipeline), the BLUR looks just like a solid color. It works without using URP. Is it possible to use Custom Shader while still using URP?
I tried everything to fix and make it work together but no progress so far.
I highly value any reply.

Temporal filtering on depth image

I'm reading the following paper and I'm trying to implement it, but it seems I'm not understanding fully the paper.
https://vcg.informatik.uni-rostock.de/~malub/publications/2016_depthenhance/16_ARLS_depthenhancement.pdf
I have calculated optical flow and some history of the depth images, and took the a linear average over the history samples as shown in the shader code.
What I get is an image that has holes however the original one doesn't have any holes.
Current filtered image
Shader Implementation:
Shader "Unlit/DepthFilter"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_PrevTex("_PrevTex", 2D) = "white" {}
_CurrentDepth("Current Depth", 2D) = "white" {}
_HistoryA("History A", 2D) = "white" {}
_HistoryB("History B", 2D) = "white" {}
_HistoryC("History C", 2D) = "white" {}
_dataDelta("Data Delta", range(0, 256)) = 256
_Lambda("Lambda", Range(0.0, 0.1)) = 0.015
_Threshold("Threshold", Range(0.0, 10)) = 0.046
_Scale("_Scale", Range(0.0, 10)) = 1.33
}
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 100
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;
sampler2D _PrevTex;
float4 _MainTex_TexelSize;
sampler2D _CurrentDepth;
sampler2D _HistoryA;
sampler2D _HistoryB;
sampler2D _HistoryC;
float _dataDelta;
float _Scale, _Lambda, _Threshold;
float4 gradient(sampler2D tex, float2 uv, float2 offset)
{
return (tex2D(tex, uv + offset)) - (tex2D(tex, uv - offset));
}
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float2 uv = i.uv;
float4 current = tex2D(_MainTex, uv);
float4 prev = tex2D(_PrevTex, uv);
float2 dx = float2(_MainTex_TexelSize.x, 0);
float2 dy = float2(0, _MainTex_TexelSize.y);
float4 diff = current - prev;
float4 gx = gradient(_PrevTex, uv, dx) + gradient(_MainTex, uv, dx);
float4 gy = gradient(_PrevTex, uv, dy) + gradient(_MainTex, uv, dy);
float4 gmag = sqrt(gx * gx + gy * gy + float4(_Lambda, _Lambda, _Lambda, _Lambda));
float4 invGmag = 1.0 / gmag;
float4 vx = diff * (gx * invGmag);
float4 vy = diff * (gy * invGmag);
float2 flow = float2(0, 0);
const float inv3 = 0.33333;
flow.x = -(vx.x + vx.y + vx.z) * inv3;
flow.y = -(vy.x + vy.y + vy.z) * inv3;
float w = length(flow);
float nw = (w - _Threshold) / (1.0 - _Threshold);
flow = lerp(float2(0, 0), normalize(flow) * nw * _Scale, step(_Threshold, w));
float2 curr = tex2D(_CurrentDepth, i.uv).xy;
float2 last_depth_frame_1 = tex2D(_HistoryA, i.uv).xy;
float2 last_depth_frame_2 = tex2D(_HistoryB, i.uv).xy;
float2 last_depth_frame_3 = tex2D(_HistoryC, i.uv).xy;
float2 last_depth_avg = (last_depth_frame_1 + last_depth_frame_2 + last_depth_frame_3) / 3.0;
float2 average_at_pixel;
average_at_pixel.x = curr.x + last_depth_avg.x - flow.x;
average_at_pixel.y = curr.y + last_depth_avg.y - flow.y;
fixed c = average_at_pixel;
//float4 velocity = float4(flow, 0,1);
//return float4(abs(velocity.xy), 0, 1);
return c;
}
ENDCG
}
}
}

Black hole distortion shader in Unity

I found shader code that has the effect of warping a space around a certain point. It's a cool effect, but it's missing some animation, so I've added something to it:
Shader "Marek/BlackHoleDistortion"
{
Properties {
_DistortionStrength ("Distortion Strength", Range(0, 10)) = 0
_Timer("Timer", Range(0, 10)) = 0
_HoleSize ("Hole Size", Range(0, 1)) = 0.1736101
_HoleEdgeSmoothness ("Hole Edge Smoothness", Range(1, 4)) = 4
_ObjectEdgeArtifactFix ("Object Edge Artifact Fix", Range(1, 10)) = 1
}
SubShader {
Tags {
"IgnoreProjector"="True"
"Queue"="Transparent"
"RenderType"="Transparent"
}
GrabPass{ }
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
ZWrite Off
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 3.0
uniform sampler2D _GrabTexture;
uniform float _DistortionStrength;
uniform float _HoleSize;
uniform float _HoleEdgeSmoothness;
uniform float _ObjectEdgeArtifactFix;
uniform float _Timer;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float4 posWorld : TEXCOORD0;
float3 normalDir : TEXCOORD1;
float4 projPos : TEXCOORD2;
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.normalDir = UnityObjectToWorldNormal(v.normal);
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
o.pos = UnityObjectToClipPos(v.vertex);
o.projPos = ComputeScreenPos(o.pos);
COMPUTE_EYEDEPTH(o.projPos.z);
return o;
}
float4 frag(VertexOutput i) : COLOR {
i.normalDir = normalize(i.normalDir);
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 normalDirection = i.normalDir;
float2 sceneUVs = (i.projPos.xy / i.projPos.w);
float node_9892 = (_HoleSize * -1.0 + 1.0);
float node_3969 = (1.0 - pow(1.0 - max(0, dot(normalDirection, viewDirection)), clamp(_DistortionStrength - _Timer, 0, _DistortionStrength)));
float node_9136 = (length(float2(ddx(node_3969), ddy(node_3969))) * _HoleEdgeSmoothness);
float node_4918 = pow(node_3969, 6.0);
float node_1920 = (1.0 - smoothstep((node_9892 - node_9136), (node_9892 + node_9136), node_4918));
float3 finalColor = (
lerp(
float4(node_1920, node_1920, node_1920, node_1920),
float4(1, 1, 1, 1),
pow(
pow(1.0 - max(0, dot(normalDirection, viewDirection)), 1.0),
_ObjectEdgeArtifactFix
)
) * tex2D(_GrabTexture, ((node_4918 * (sceneUVs.rg * _Time * -2.0 + 1.0)) + sceneUVs.rg)).rgb).rgb;
return fixed4(finalColor, 1);
}
ENDCG
}
}
FallBack "Diffuse"
}
Now, the problem is that in order to make the distortion disappear after certain time, I need to include some variable into the equation - here I'm calling it _Timer. I'm not using the _Time built in because of obvious reasons - it's an ever growing value and I need something that starts from 0 each time the object using this shader is made active. C# code handling passing that parameter looks as follows:
public void Update() {
_timeElapsed += Time.deltaTime;
_renderer.material.SetFloat("_Timer", _timeElapsed);
}
The question is - can I do it better? I would like this shader's code to be more of a self-contained thing - without the need to pass parameters from cs script to it.
Can I do it better?
In-short, yes and no. If you want the shader to behave differently per material you simply cannot avoid passing a property from C#. You can however avoid doing this in Update by passing a start time and computing the elapse time in the shader.
C#
void OnEnable ()
{
_renderer.material.SetFloat("_StartTime", Time.timeSinceLevelLoad);
}
Shader
uniform float _StartTime;
float4 frag(VertexOutput i) : COLOR
{
float elapse = _Time.y - _StartTime;
}
Now, although this will tie directly into the setup you are currently using, it should be noted that accessing the .material property will clone the material (which can break batching, among other things).
This can be avoided with the more recent introduction of MaterialPropertyBlocks.
Unity provides a handful of built-in values for your shaders: things like current object’s transformation matrices, time etc.
You just use them in ShaderLab like you’d use any other property, the only difference is that you don’t have to declare it somewhere - they are “built in”.
https://docs.unity3d.com/455/Documentation/Manual/SL-BuiltinValues.html
there is a clever way of giving you 4 variations of the value, potentially saving you a multiply operation by re-using the pre-multiplied value for every pixel being rendered. There are 4 values available.
_Time.x = time / 20
_Time.y = time
_Time.z = time * 2
_Time.w = time * 3
this is a simple example that show you how it works:
Shader "Example/Circle"
{
Properties
{
}
SubShader
{
Cull Off
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;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float circle(in float2 _st, in float _radius){
float2 dist = distance(_st,float2(0.5,0.5));
float result = step(dist,_radius);
return result;
}
fixed4 frag (v2f i) : SV_Target
{
float WaveTime = sin(_Time.z);
float3 color = float3(1,1,1)*circle(i.uv,WaveTime);
return float4( color, 1.0 );
}
ENDCG
}
}
}
In comments you mentioned that you want reset time value when you enable it , so here you need to Initialize Time value with script.
so you should use your own Time In shader:
Properties
{
_Timer("Timer",Float) = 0
}
float WaveTime = sin(_Timer);
using System.Collections;
using UnityEngine;
public class Circle : MonoBehaviour {
public float _timeElapsed;
void OnEnable(){
_timeElapsed = 0;
}
public void Update() {
_timeElapsed += Time.deltaTime;
var _renderer = GetComponent<MeshRenderer>();
_renderer.material.SetFloat("_Timer", _timeElapsed);
}
}

Categories