Hi!
I’m going crazy… the wpos (worldPos) of this shader fors from 0 to 1 in the object, instead of showing the real world pos. Why is this? Thanks ![]()
Shader "EffectBakers/FogBaker"
{
Properties
{
[PerRendererData] _MainTex ("Color (RGB) Alpha (A)", 2D) = "black" {}
[MaterialToggle] PixelSnap( "Pixel snap", Float) = 0
[HideInInspector]_FogTexture( "Fog Ramp Texture", 2D) = "black" { }
[HideInInspector]_01FogDepthVerticals( "Linear Depth 01 And Verticals", Vector) = (0.0, 0.0, 0.0, 0.0)
[HideInInspector]_IsCold( "Warm texture or Cold texture", Range(0,1)) = 0
}
SubShader
{
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
LOD 100
ZWrite Off
Cull Off
Fog {Mode Off}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#pragma multi_compile _ PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv_MainTex : TEXCOORD0;
half4 screenpos : TEXCOORD1;
float3 wpos : TEXCOORD2;
};
fixed4 _Color;
half4 _MainTex_ST;
v2f vert(appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
#ifdef PIXELSNAP_ON
o.vertex = UnityPixelSnap (o.vertex);
#endif
o.screenpos = ComputeScreenPos( o.vertex );
o.wpos = mul(_Object2World, v.vertex);
return o;
}
sampler2D _FogTexture;
fixed4 _01FogDepthVerticals;
int _IsCold;
float4 FogColor(float2 worldPos)
{
float fog_bottom_column = 0.05 + ( _IsCold * 0.5);
float fog_top_column = 0.15 + ( _IsCold * 0.5);
float fog_depth = _01FogDepthVerticals.x;
float front = ( abs( fog_depth ) - fog_depth ) / ( abs( fog_depth ) * 2 );
fog_bottom_column += front * 0.2;
fog_top_column += front * 0.2;
fog_depth = abs(fog_depth);
float2 fog_position_bottom = float2(fog_bottom_column, 1 - fog_depth);
float4 bottom_FogTexture = tex2D(_FogTexture, fog_position_bottom);
float2 fog_position_top = float2(fog_top_column, 1 - fog_depth);
float4 top_FogTexture = tex2D(_FogTexture, fog_position_top);
float vertical_pos = clamp((worldPos.y - _01FogDepthVerticals.y) / (_01FogDepthVerticals.z - _01FogDepthVerticals.y), 0, 1);
return lerp(bottom_FogTexture, top_FogTexture, vertical_pos);
}
sampler2D _MainTex;
fixed4 frag(v2f inp) : COLOR
{
fixed4 color = tex2D( _MainTex, inp.uv_MainTex );
fixed4 color_fog_tex = FogColor( inp.wpos );
float color_factor = color_fog_tex.a;
color_fog_tex.a = color.a;
color = lerp( color, color_fog_tex, color_factor );
color.rgb *= color.a;
return inp.screenpos.y;
}
ENDCG
}
}
}
EDIT: This shader is used with a blit command, outisde run, to bake a texture. Is it possible that if the game is not running, I cannot know the position in the world?
EDIT2: The answer is yes ![]()
So now I need help to know how to get the world position outside of runtime… Thanks!
EDIT3: Of course, blit doesn’t know about the object position because there is no object. I will open another thread asking that. This can be used for reference if someone finds same problem.