Storing data from previous shader pass
Hi! I'm relatively new to shaders, and am working on a shader that distorts a mesh given its distance from the 'player'. At the moment it works fine, but I'd like to be able to lerp between a vertex's previous and new 'worldPos' values to limit the rate of change, mimicking some sort of plasticity.
How would I go about storing the 'worldPos' value of each vertice externally and then accessing it in the subsequent pass?
I'll provide any extra info needed, thanks in advance!
The shader in action (couldn't upload directly): https://imgur.com/w322gyB
The code:
Shader "Custom/Displacement Shader"
{
Properties
{
_Tint("Tint", Color) = (1, 1, 1, 1)
[HideInInspector]
_PlayerPos("Player Position", Vector) = (0, 0, 0)
_EffectRange("Effect Range", Float) = 1
_Stretchiness("Stretchiness", Range(0, 1)) = 0.5
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
float3 normal : NORMAL;
};
float4 _Tint;
float3 _PlayerPos;
float _EffectRange, _Stretchiness;
v2f vert (appdata v)
{
v2f o;
float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
float4 worldNormal = mul(unity_ObjectToWorld, v.normal);
float3 dir = normalize(_PlayerPos - worldPos.xyz);
float dist = distance(worldPos, _PlayerPos);
if (dist < _EffectRange ) {
worldPos += float4(dir * (dist - _EffectRange) * _Stretchiness, 1);
}
o.vertex = UnityWorldToClipPos(worldPos);
o.normal = UnityObjectToWorldNormal(mul(unity_WorldToObject, worldNormal));
return o;
}
fixed4 frag (v2f i) : SV_Target
{
i.normal = normalize(i.normal);
float NdotL = max(0, dot(i.normal, _WorldSpaceLightPos0));
fixed4 col = _Tint * NdotL;
return col;
}
ENDCG
}
}
}
Your answer
Follow this Question
Related Questions
Intercept last pass on a surface shader to change RGB of that pixel 0 Answers
Unity5 PBR - Single Pass possible? 0 Answers
Why is this point light square? 1 Answer
Vertex Lit Transparent Shader problem with Z 0 Answers
Very basic shader problem 0 Answers