vert: function return value missing semantics - unity3d

I am just trying to running this code from unity site
Shader "UnityExample/Vertex and Fragment/3-Behind Bars" {
SubShader{
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct vertOut{
float4 pos: SV_POSITION;
float4 scrPos;
};
vertOut vert(appdata_base v){
vertOut o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.scrPos = ComputeScreenPos(o.pos);
return o;
}
fixed4 frag(vertOut i) : SV_Target{
float2 wcoord = (i.scrPos.xy/i.scrPos.w);
fixed4 color;
if(fmod(20.0 * wcoord.x,2.0)<1.0){
color = fixed4(wcoord.xy,0.0,1.0);
} else{
color = fixed4(0.3,0.3,0.3,1.0);
}
return color;
}
ENDCG
}
}
}
This code is showing me an error on this line
vertOut vert(appdata_base v)
vert: function return value missing semantics
I am using unity 4.6

Semantics is missing for scrPos.
float4 scrPos : TEXCOORD0;

Related

Why is my custom shader code saying : Unexpected token 'uv'?

I watched b3agz's making minecraft in unity tutorial on episode 15 there was an error when i coded the shade saying unexpected token 'uv' please help.
Here is my code for it:
Shader "Minecraft/Blocks"
{
Properties
{
_MainTex("Block Texture Atlas", 2D) = "white" {}
}
SubShader
{
Tags{"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
LOD 100
Lighting Off
Pass
{
CGPROGRAM
#pragma vertex vertFunction
#pragma fragment fragFunction
#pragma target 2.0
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION:
float2 uv : TEXCOORD0;
float4 color : COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION:
float2 uv : TEXCOORD0;
float4 color : COLOR;
};
sampler2D _MainTex;
float GlobalLightLevel;
v2f vertFunction(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.color = v.color;
return o;
}
fixed4 fragFunction(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
float localLightLevel = clamp(GlobalLightLevel + i.color.a,0,1);
clip(col.a - 1);
col = lerp(col, float4(0,0,0,1), localLightLevel);
return col;
}
ENDCG
}
}
}
Both of your struct definitions, appdata and v2f have a colon in place of a semi-colon for the float4 vertex.
The correct syntax would be:
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
};

Unity Shader Error when Building to WebGL

When trying to build my game to Webgl I receive the following errors:
Shader error in 'Hidden/Universal/CoreBlit': invalid subscript 'positionCS' at /PathToProject/Library/PackageCache/com.unity.render-pipelines.core#12.1.2/Runtime/Utilities/Blit.hlsl(92) (on gles)
Shader error in 'Hidden/kMotion/CameraMotionVectors': SV_VertexID semantic is not supported on GLES 2.0 at line 11 (on gles)
The Shader I am using has the following code:
Shader "Skybox Gradient"
{
Properties
{
_Top("Top", Color) = (1,1,1,0)
_Bottom("Bottom", Color) = (0,0,0,0)
_mult("mult", Float) = 1
_pwer("pwer", Float) = 1
[Toggle(_SCREENSPACE_ON)] _Screenspace("Screen space", Float) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
CGINCLUDE
#pragma target 3.0
ENDCG
Blend Off
Cull Back
ColorMask RGBA
ZWrite On
ZTest LEqual
Offset 0 , 0
Pass
{
Name "Unlit"
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#ifndef UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX
//only defining to not throw compilation error over Unity 5.5
#define UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input)
#endif
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "UnityCG.cginc"
#pragma shader_feature_local _SCREENSPACE_ON
struct appdata
{
float4 vertex : POSITION;
float4 color : COLOR;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
#ifdef ASE_NEEDS_FRAG_WORLD_POSITION
float3 worldPos : TEXCOORD0;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
float4 ase_texcoord1 : TEXCOORD1;
float4 ase_texcoord2 : TEXCOORD2;
};
uniform float4 _Bottom;
uniform float4 _Top;
uniform float _mult;
uniform float _pwer;
v2f vert ( appdata v )
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
UNITY_TRANSFER_INSTANCE_ID(v, o);
float4 ase_clipPos = UnityObjectToClipPos(v.vertex);
float4 screenPos = ComputeScreenPos(ase_clipPos);
o.ase_texcoord2 = screenPos;
o.ase_texcoord1 = v.vertex;
float3 vertexValue = float3(0, 0, 0);
#if ASE_ABSOLUTE_VERTEX_POS
vertexValue = v.vertex.xyz;
#endif
vertexValue = vertexValue;
#if ASE_ABSOLUTE_VERTEX_POS
v.vertex.xyz = vertexValue;
#else
v.vertex.xyz += vertexValue;
#endif
o.vertex = UnityObjectToClipPos(v.vertex);
#ifdef ASE_NEEDS_FRAG_WORLD_POSITION
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
#endif
return o;
}
fixed4 frag (v2f i ) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
fixed4 finalColor;
#ifdef ASE_NEEDS_FRAG_WORLD_POSITION
float3 WorldPosition = i.worldPos;
#endif
float4 screenPos = i.ase_texcoord2;
float4 ase_screenPosNorm = screenPos / screenPos.w;
ase_screenPosNorm.z = ( UNITY_NEAR_CLIP_VALUE >= 0 ) ? ase_screenPosNorm.z : ase_screenPosNorm.z * 0.5 + 0.5;
#ifdef _SCREENSPACE_ON
float staticSwitch13 = ase_screenPosNorm.y;
#else
float staticSwitch13 = i.ase_texcoord1.xyz.y;
#endif
float4 lerpResult3 = lerp( _Bottom , _Top , pow( saturate( ( staticSwitch13 * _mult ) ) , _pwer ));
finalColor = lerpResult3;
return finalColor;
}
ENDCG
}
}
CustomEditor "ASEMaterialInspector"
}
The error only occurs every second or third time I try to locally build the game.
However, when I am building with Unity Cloud Build it always appears.

Invalid output semantic 'SV_POSITION': Legal indices are in [0,0]

I just start to learn Unity3d Shader, I followed the tutorial of a book, all my code is the same as the tutorial, however, there is an error I can't figure out.
Here is the code:
Shader "Unity Shaders Book/Chapter5/Simple Shader"{
SubShader{
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct a2v {
float4 vertex:POSITION;
float3 normal:NORMAL;
float4 texcoord:TEXCOORD0;
};
struct v2f {
float4 pos: SV_POSITION;
fixed3 color : COLOR0;
};
v2f vert(a2v v) : SV_POSITION {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.color = v.normal *0.5 + fixed3(0.5, 0.5, 0.5);
return o;
}
fixed4 frag(v2f i) : SV_Target{
return fixed4(i.color,1.0);
}
ENDCG
}
}
}
The error
Change this line:
v2f vert(a2v v) : SV_POSITION {
Into this:
v2f vert(a2v v) {

Unity shader pixel manipulation

I'm studying shaders in unity and I'm trying to achieve the below image. But I can't seem to make my code produce my target output. My end goal is to divide the whole image depending on the size of my array. Any idea how to correct my code?
Target output:
Code:Shader "Custom/PassArray"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags {"Queue"="Transparent"}
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform float4 _Test [3];
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col;
_Test[0] = fixed4(0,0,1,1);
_Test[1] = fixed4(1,0,0,1);
_Test[2] = fixed4(0,1,0,1);
//Test1: This code block produces output1 image.
int index = round(i.uv.x*_Test.Length);
col = _Test[index];
//Test2: This codeblocks outputs pure green.
//if(i.uv.x < 1/3){
// col = _Test[0];
//}
//else if(i.uv.x < 2/3){
// col = _Test[1];
//}
//else if(i.uv.x < 3/3){
// col = _Test[2];
//}
return col;
}
ENDCG
}
}
}
Actual output:
If I understood it correctly, it's giving me black pixels because of the max i.uv.x is 1. Thus making the index 3 (based on the computation i.uv.x/_Test.Length). But I don't have index 3 in my array though.
Change round to floor. Because you're rounding, values greater than 2.5 get rounded to 3. As you probably know, an array of 3 elements has no index 3, hence the black bar.
I've figured it out. Here's the updated code for anyone who may find it useful. I changed the formula from int index = round(i.uv.x_Test.Length); to int index = ceil(i.uv.x/1_Test.Length)-1;
Shader "Custom/PassArray"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags {"Queue"="Transparent"}
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform float4 _Test [3];
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 color : COLOR;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col;
_Test[0] = fixed4(0,0,1,1);
_Test[1] = fixed4(1,0,0,1);
_Test[2] = fixed4(0,1,0,1);
//Test1:
int index = ceil(i.uv.x/1*_Test.Length)-1;
col = _Test[index];
return col;
}
ENDCG
}
}
}

Error on scroll texture shader based on Alan Zucconi's book

Using unity 5.5 and Alan Zucconi's book I'm getting " incorrect number of arguments to numeric-type constructor"
shader source added as reccommended:
Shader "book/scroller"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_ScrollXSpeed ("x scroll speed", Range(0,10)) = 2
_ScrollYSpeed("Y scroll speed", Range(0,10)) = 2
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _ScrollXSpeed;
fixed _ScrollYSpeed;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed2 scrolledUV = i.uv;
fixed2 xScrolledValue = _ScrollXSpeed * _Time;
fixed2 yScrolledValue = _ScrollYSpeed * _Time;
scrolledUV += fixed2 (xScrolledValue, yScrolledValue);
fixed4 col = tex2D(_MainTex, scrolledUV);
return col;
}
ENDCG
}
}
}
Error shows at following line:
scrolledUV += fixed2 (xSCrolledValue, ySCrolledValue);
What is wrong with it?
Reason for the error:
You are creating fixed2-variable and giving it two fixed2 values as parameters, should be floats instead.
Here is a fixed version:
fixed xScrolledValue = _ScrollXSpeed * _Time;
fixed yScrolledValue = _ScrollYSpeed * _Time;
scrolledUV += fixed2 (xScrolledValue, yScrolledValue);