- Home /
Additive shader visible in viewport but not in-game
Hi, I set up this shader for an afterburner effect. It all works fine and looks good in viewport, but not in the game viewport or at game run time.
It does however pop up in the frame debugger. Any ideas?
Shader "Afterburner/Flame" {
Properties {
_Color1 ("Main Color", Color) = (1,1,1,1)
_Color2 ("Secundary Color", Color) = (1,1,1,1)
_ColorBlendForce ("Color Blend", Float) = 1
_MainTex ("My tex", 2D) = "white" {}
_StartStrength("Base Strength", Range(0,20)) = 1
_VertexColorStrength("Vertex Color Strength", Range(0,1)) = 1
_Scroll_Mul("Base scroll mul", Range(0,10)) = 3
_Scroll_Mul_Rotation("Base scroll rotation mul", Range(-3,3)) = 0
_Noise_large_ClampMin ("Noise Large Min" , Range(0.1, 1)) = 0.1
_Noise_TileX ( "Noise Large X Tile", float ) = 0.5
_Noise_TileY ( "Noise Large Y Tile", float ) = 1.5
_Noise_small_amount ("Noise Small amount", Range(0,10)) = 4
_AlphaMax ( "Alpha Max", Range(0,1) ) = 1
}
SubShader {
Tags { "Queue"="Transparent+1" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend SrcAlpha One
Cull Off
Lighting Off
ZWrite Off
Fog {Mode Off}
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float4 color : COLOR;
};
struct v2f
{
float4 pos :SV_POSITION;
float2 texcoord : TEXCOORD0;
fixed4 color : COLOR;
};
fixed4 _Color1;
fixed4 _Color2;
float _ColorBlendForce;
sampler2D _MainTex;
float _Base_Scroll1;
float _Base_Scroll2;
float _Scroll_Mul;
float _Scroll_Mul_Rotation;
float _Noise_large_ClampMin;
float _Noise_TileX;
float _Noise_TileY;
float _Noise_small_amount;
float _StartStrength;
float _VertexColorStrength;
float _AlphaMax;
v2f vert (appdata IN)
{
v2f OUT;
OUT.pos = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color;
return OUT;
}
fixed4 frag (v2f IN) : COLOR
{
// BASE SCROLLING COORDS
fixed2 base1Coords = IN.texcoord;
fixed2 base2Coords = IN.texcoord;
base1Coords.y += _Time.y * _Base_Scroll1 * _Scroll_Mul;
base2Coords.y += _Time.y * _Base_Scroll2 * _Scroll_Mul;
// GET GREEN CHANNELS FOR BASE LAYER
fixed4 base1 = tex2D(_MainTex, base1Coords).g;
fixed4 base2 = tex2D(_MainTex, base2Coords).g;
// NOISE SCROLLING COORDS
fixed2 noiseBlendCoords = IN.texcoord;
noiseBlendCoords += _Time * fixed2(-15,-0.5) * _Scroll_Mul;
// GET BLUE CHANNEL FOR NOISE LAYER
fixed4 noise = tex2D(_MainTex, noiseBlendCoords).b;
// BASE LAYERS BLENDED
fixed4 finalColor = lerp(base1, base2, noise);
// ADD LARGE SCALE NOISE
fixed2 noiseLargeCoords = IN.texcoord * fixed2(_Noise_TileX,_Noise_TileY) + (_Time * fixed2(-10,-0.1*_Scroll_Mul_Rotation) * _Scroll_Mul );
fixed4 noiseLarge = tex2D(_MainTex, noiseLargeCoords).b;
noiseLarge = clamp(noiseLarge, _Noise_large_ClampMin,1) * _StartStrength;
// ADD LOW SCALE NOISE
fixed2 noiseSmallCoords = IN.texcoord * fixed2(0.8,2.8) + (_Time * fixed2(-20,3*_Scroll_Mul_Rotation) * _Scroll_Mul );
fixed4 noiseSmall = tex2D(_MainTex, noiseSmallCoords).b;
noiseSmall = pow(noiseSmall, 10) * (noiseLarge * _Noise_small_amount * IN.color * _VertexColorStrength);
// Combine
finalColor = mul(finalColor, noiseLarge) + noiseSmall;
finalColor = finalColor * (IN.color * _VertexColorStrength);
// Tint the mesh
fixed4 colorGradient = lerp(_Color1, _Color2, pow(IN.color, _ColorBlendForce));
finalColor = finalColor * colorGradient;
finalColor.a = _AlphaMax;
return finalColor;
}
ENDCG
}
}
FallBack "Diffuse"
}
in play mode only it show that effect are you sure you are in play mode ?
It does not show in the game viewport in play mode and when not in play mode. It always shows correctly in the "scene" viewport however. There are no C# scripts affecting the shader either.
Does changing the main camera's render mode make any difference? (i.e. Forward, Deferred, etc.)
The primary camera's setting should be reflected in the scene view as well, but there's no harm in checking just in case.
Does changing the clearflags on the camera help? maybe the skybox shader does some transparency magic which messes this up.
Yes that indeed affects it! Changing skybox to solid color reveals the mesh. Do you think there is a way to keep the skybox? I'm not seeing how this is much different than the default particle/additive shader.
i think you could rework your shader to be just additive and put it in the geometry queue
Answer by bramnic · May 23, 2016 at 04:08 PM
Finally got everything working, here's the solution. It basically had to do with the renderqueue, of which I barely found any info about. This script (from another forum source) can be attached to the object in question and solves every transparency sorting issue. Apparently not even all objects in my scene need it. A value of 2001 seems to work.
using UnityEngine;
[AddComponentMenu("Effects/SetRenderQueue")]
[RequireComponent(typeof(Renderer))]
public class SetRenderQueue : MonoBehaviour {
[Tooltip("Background=1000, Geometry=2000, AlphaTest=2450, Transparent=3000, Overlay=4000")]
public int queue = 1;
public int[] queues;
void Start() {
Renderer renderer = GetComponent<Renderer>();
if (!renderer || !renderer.sharedMaterial || queues == null)
return;
renderer.sharedMaterial.renderQueue = queue;
for (int i = 0; i < queues.Length && i < renderer.sharedMaterials.Length; i++)
renderer.sharedMaterials[i].renderQueue = queues[i];
}
void OnValidate() {
Start();
}
}
Answer by Alex-Tsurcan · Apr 13, 2016 at 11:27 AM
Did you check layer filter of camera and layer of gameobject ?
Answer by unic · Feb 16, 2018 at 07:30 PM
I have the same issue with my custom shader I've made using Shader Forge. I need a quad to be rendered on the backgroud, so I've set Render Queue to "1000". But in this case shader is visible in viewport but not in game.
After I've set Render Queue to "3000" the shader became visible.
Then I've noticed that it becomes visible having Render Queue value > 2500. I tried to use the script from the solution above, but it doesn't solve my problem.
Does anybody have an idea why the shader is visible in the viewport but not in game?
Your answer
Follow this Question
Related Questions
Surface Shader, run vertex multiple times 0 Answers
Is it possible to get back data for a specific vertex from a shader ? 0 Answers
Vertex shader breaks render to texture - camera preview and render texture not the same 1 Answer
How can I make shadows pixelated in fragment function? 1 Answer
Prevent ColorMask obscuring parts of an object's mesh 0 Answers