Hi there!
I’ve never worked with shaders, but for my game i have to combine two shaders
Animal Crossing Curved World Shader | Alastair Aitchison
Second shader
Shader "Cartoon FX/Particles Additive Alpha8"
{
Properties
{
_MainTex ("Particle Texture (Alpha8)", 2D) = "white" {}
}
Category
{
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend SrcAlpha One
Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
BindChannels
{
Bind "Color", color
Bind "Vertex", vertex
Bind "TexCoord", texcoord
}
SubShader
{
Pass
{
SetTexture [_MainTex]
{
combine primary, texture * primary
}
}
}
}
}
The part of shader where “v.vertex += mul(unity_WorldToObject, vv);” (line 39) doesn’t work.
i.e. no curve
Merged shader
Shader "Cartoon FX/Particles Additive Alpha8"
{
Properties
{
_MainTex ("Particle Texture (Alpha8)", 2D) = "white" {}
_Curvature("Curvature", Float) = 0.002
}
SubShader
{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Blend SrcAlpha One
Cull Off Lighting Off ZWrite Off Fog{ Color(0,0,0,0) }
Pass
{
SetTexture [_MainTex]
{
combine primary, texture * primary
}
}
CGPROGRAM
#pragma surface surf Lambert vertex:vert
sampler2D _MainTex;
uniform float _Curvature;
struct Input {
float2 uv_MainTex;
float4 color : Color;
};
void vert(inout appdata_full v, out Input o) {
float4 vv = mul(unity_ObjectToWorld, v.vertex);
vv.xyz -= _WorldSpaceCameraPos.xyz;
vv = float4((vv.z * vv.z) * -_Curvature *5.5f, (vv.z * vv.z) * -_Curvature *5.5f, 0.0f, 0.0f);
v.vertex += mul(unity_WorldToObject, vv);
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color = v.color;
}
void surf(Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb * IN.color.rgb;
o.Alpha = c.a * IN.color.a;
}
ENDCG
}
}
Any ideas how to fix this ?
Thanks
bgolus
December 25, 2017, 4:51am
2
The first shader is a Surface Shader. This is a special type of shader that Unity has to make it easier to write shaders that support Unity’s lighting systems. Surface shaders are technically vertex fragment shader generators.
The second shader is a Fixed Function Shader. Unity dropped support for fixed function rendering, so Fixed Function Shaders are now also vertex fragment shader generators.
Copying both into one file isn’t going to produce anything useful. You’re going to need to write a custom vertex fragment shader that does a combination of both shaders. First step should be select the particle shader in the editor and click on “show generated code”, and copy that into the particle shader replacing the old fixed function version. After that you should be able to copy the appropriate parts of the vert function from the curved world shader into the vertex function of the particle shader.
Thanks a lot!!!
I did as you said and It works!
Shader
Shader "Cartoon FX/Particles Additive Alpha8" {
Properties{
_MainTex("Particle Texture (Alpha8)", 2D) = "white" { }
_Curvature("Curvature", Float) = 0.002
}
SubShader{
Tags{ "QUEUE" = "Transparent" "IGNOREPROJECTOR" = "true" "RenderType" = "Transparent" }
Pass{
Tags{ "QUEUE" = "Transparent" "IGNOREPROJECTOR" = "true" "RenderType" = "Transparent" }
ZWrite Off
Cull Off
Blend SrcAlpha One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#include "UnityCG.cginc"
#pragma multi_compile_fog
#define USING_FOG (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
// uniforms
float4 _MainTex_ST;
uniform float _Curvature;
// textures
sampler2D _MainTex;
// vertex shader input data
struct appdata {
float3 pos : POSITION;
half4 color : COLOR;
float3 uv0 : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
// vertex-to-fragment interpolators
struct v2f {
fixed4 color : COLOR0;
float2 uv0 : TEXCOORD0;
#if USING_FOG
fixed fog : TEXCOORD1;
#endif
float4 pos : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
// vertex shader
v2f vert(appdata IN) {
v2f o;
UNITY_SETUP_INSTANCE_ID(IN);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
half4 color = IN.color;
float3 eyePos = mul(UNITY_MATRIX_MV, float4(IN.pos,1)).xyz;
half3 viewDir = 0.0;
o.color = saturate(color);
// compute texture coordinates
o.uv0 = TRANSFORM_TEX(IN.uv0, _MainTex);
// fog
#if USING_FOG
float fogCoord = length(eyePos.xyz); // radial fog distance
UNITY_CALC_FOG_FACTOR_RAW(fogCoord);
o.fog = saturate(unityFogFactor);
#endif
// transform position
float4 vv = mul(unity_ObjectToWorld, IN.pos);
vv.xyz -= _WorldSpaceCameraPos.xyz;
vv = float4((vv.z * vv.z) * -_Curvature *5.5f, (vv.z * vv.z) * -_Curvature *5.5f, 0.0f, 0.0f);//set curvature
//vv = float4(0.0f, 0.0f, 0.0f, 0.0f);
IN.pos += mul(unity_WorldToObject, vv);
o.pos = UnityObjectToClipPos(IN.pos);
return o;
}
// fragment shader
fixed4 frag(v2f IN) : SV_Target{
fixed4 col;
fixed4 tex, tmp0, tmp1, tmp2;
// SetTexture #0
tex = tex2D(_MainTex, IN.uv0.xy);
col.rgb = IN.color;
col.a = tex.a * IN.color.a;
// fog
#if USING_FOG
col.rgb = lerp(unity_FogColor.rgb, col.rgb, IN.fog);
#endif
return col;
}
// texenvs
//! TexEnv0: 01030000 01050107 [_MainTex]
ENDCG
}
}
}