- Home /
Screen shaders that alter geometry at runtime or through camera
Hello people,
I am new to shaders and have read a few tutorials covering basic concepts but I found it really hard to find information about shaders that alter geometry through the camera or at runtime.
I want something like an image effect that doesn't actually change any textures but distorts the environment. For example I would like every object in the scene that the camera sees to change direction by a certain amount. Down the road I would like the distortion to change as the player moves in the environment.
I was able to make the following shader that moves individual objects along the xyz line.
Shader "Vertex Modifier" {
Properties{
_MainTex("Texture", 2D) = "white" {}
_Amount("Height", Float) = 1.0
}
SubShader{
Tags{ "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert vertex:vert
struct Input {
float2 uv_MainTex;
};
float _Amount;
void vert(inout appdata_full v) {
v.vertex.xyz += 0.1*_Amount;
}
sampler2D _MainTex;
void surf(Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
Fallback "Diffuse"
}
My first problem is that I don't know how to pass this as a screen shader/image effect.
Answer by Remy_Unity · Apr 17, 2018 at 11:38 AM
First, an image effect won't do what your want. It's ment to be applied after rendering, so the closest you could do with it is distortion.
To do what you're talking about, you should make a shader that affect all the objects you want, and use the camera position or the view space to alter the geometry.
Do some research on matrix math in shaders, and look at this doc page https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html. You'll find some interesting UNITY_MATRIX_MV : Current model * view matrix that will allow you to calculate the vertex position in view space.
Thank you for your answer. From what I understand I have to make a shader and put it on each object? What if I have hundreds of objects? Isn't there a way to attach the shader to the camera or trigger it with the start function?
Well, if you really don't want to change the shader used by your objects you could still use replacement shader : https://docs.unity3d.com/$$anonymous$$anual/SL-ShaderReplacement.html
But if it's the only thing that you're rendering, and are not doing something fancy on your objects, it's probably best to simply assign them a material using this shader.
Thank you. Shader replacement seems like a good idea. I lost you on the last sentence. What do you mean assign them a material using this shader. Which shader are you referring to?