- Home /
How can I add the diffuse property to a vertex Shader?
Hi there!
I've been messing around with a shader that distorts the world by modifiying the vertices in the scene. Thing is, every object that I apply this shader to remains unlit. Is there a way to give it a diffuse in the script? Is that even possible with vertex shaders? How would I do it?
Here's my current shader:
Shader "Custom/Curved" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_QOffset ("Offset", Vector) = (0,0,0,0)
_Dist ("Distance", Float) = 100.0
_Sin ("Sin", Float) = 1
}
SubShader {
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _QOffset;
float _Dist;
float _Sin;
struct v2f {
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
};
v2f vert (appdata_base v)
{
v2f o;
float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
float zOff = vPos.z/_Dist;
_Sin = sin(zOff*1 - 3.1) * 6 + sin(zOff) * 2;
//_Sin = 0;
vPos += _QOffset*_Sin;
o.pos = mul (UNITY_MATRIX_P, vPos);
o.uv = v.texcoord;
return o;
}
half4 frag (v2f i) : COLOR
{
half4 col = tex2D(_MainTex, i.uv.xy);
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
I haven't written everything by myself, found it some time ago and modified it.
Thanks in advance!
Yes, of course. But seeing as you've not posted your shader code it's impossible to tell you what you're doing wrong. Perhaps start with reading http://docs.unity3d.com/$$anonymous$$anual/SL-VertexFragmentShaderExamples.html
I've posted the shader code now, do you know where I could do a slight change to get diffuse ins$$anonymous$$d of unlit? Shaders are really really confusing to me...
Answer by tanoshimi · Apr 24, 2015 at 08:44 PM
Well, you haven't really included any lighting calculations in your shader at all - you're just directly applying the _MainTex
texture in the fragment shader which is why it's unaffected by lighting. If you're not familiar with shaders you'll probably find it a lot easier to use a surface shader rather than a vert/frag - it greatly simplifies the lighting calculations required.
You appear to be trying to adjust vertex position to falloff with distance from camera? If so, I'dn recommend you start off with a surface shader template that achieves the same effect and adjust it to your needs, e.g.: https://alastaira.wordpress.com/2013/10/25/animal-crossing-curved-world-shader/
Thanks a lot man! So basically, I can recreate the same effect with a surface shader, and still give it a good-looking shader that is as good as the standard shader in unity 5?
Your answer
Follow this Question
Related Questions
Curved + Diffuse Shader??? 0 Answers
Cubemap Diffuse / Heightmap Shader Issues. 1 Answer
Problem of color in custom vertex shader 0 Answers
Silhouette Toon Shader 0 Answers
Add new vertex attributes for a shader,Create vertex attributes 0 Answers