Question by
CloudChing · Sep 17, 2015 at 10:44 AM ·
shadershadersshader programmingshaderlabshader writing
Fog not working in my own Vertext Shader
Fog not working in my own Vertext Shader,i use default surface shader is working.
I see the documentation:
Fog handling was changed
Unity 5.0 makes built-in Fog work on WP8 and consoles, but in order to achieve that we’ve changed how Fog is done a bit. For surface shaders and fixed function shaders, nothing extra needs to be done - fog will be added automatically (you can add “nofog” to surface shader #pragma line to explicitly make it not support fog).
For manually written vertex/fragment shaders, fog does not happen automagically now. You need to add
#pragma multi_compile_fog and fog handling macros to your shader code. Check out built-in shader source, for example Unlit-Normal how to do it.
i add the #pragma multi_compile_fog , but still not working...
there is my code:
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_QOffset ("Offset", Vector) = (0,0,0,0)
_Dist ("Distance", Float) = 100.0
}
SubShader {
Tags { "RenderType"="Opaque" }
Fog {Mode Linear }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile_fog
sampler2D _MainTex;
float4 _QOffset;
float _Dist;
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;
vPos += _QOffset*zOff*zOff;
o.pos = mul (UNITY_MATRIX_P, vPos);
o.uv = mul( UNITY_MATRIX_TEXTURE0, v.texcoord );
return o;
}
half4 frag (v2f i) : COLOR
{
half4 col = tex2D(_MainTex, i.uv.xy);
return col;
}
ENDCG
}
}
Comment
Best Answer
Answer by cod · Sep 18, 2015 at 08:43 AM
From the documentation :
// - no lighting
// - no lightmap support
// - no per-material color
Shader "Unlit/Texture" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 100
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
UNITY_FOG_COORDS(1)
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.texcoord);
UNITY_APPLY_FOG(i.fogCoord, col);
UNITY_OPAQUE_ALPHA(col.a);
return col;
}
ENDCG
}
}
}
Hope it works
PS if you want to access fog's data look here under the section fog http://docs.unity3d.com/Manual/SL-UnityShaderVariables.html unity_FogColor and unity_FogParams
how would you do the same thing but WITH lighting and transparency?
Answer by CloudChing · Sep 18, 2015 at 01:53 AM
some one help me?
Avoid posting a new reply, edit your question ins$$anonymous$$d
Your answer
