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.
Related
so I am trying to make a material which causes all rendered objects behind it to be blurred. It is important for me to not be a global or screen space effect. I need a kind of mask, which is why I chose this approach.
I found some blur shader which works perfectly and just as I need it, but only in non XR rendering. As soon as I enabled the Single Pass Instanced rendering (or testing on the AR device) this does not work anymore.
Since I am not that good in shader-stuff, I quickly found out that there are some special things to be considered when doing single pass instanced shaders, or multi view shaders in general. What I did so far is changing the return-type of frags to SV_Target, change sample2d to UNITY_DECLARE_SCREENSPACE_TEXTURE and added all these UNITY_X macros to the functions, as described in the doc.
But for some reason, the effect does not work at all. Depending on my changes, I see nothing, and sometimes I see some weird artifacts going on.
Is there something else I forgot to make this shader work as I described? My biggest fear is that the vert-part of the shader does not work for multiview rendering, since the calculations might be off and require a more complicated approach?! But I am not good when it comes to such kind of math. (For now I removed the distort part of the shader, since this did not work at all.)
Shader "Custom/MRTKBlur"
{
Properties{
_Color("Main Color", Color) = (1,1,1,1)
_BumpAmt("Distortion", Range(0,128)) = 10
_MainTex("Tint Color (RGB)", 2D) = "white" {}
_BumpMap("Normalmap", 2D) = "bump" {}
_Size("Size", Range(0, 20)) = 1
}
Category{
// We must be transparent, so other objects are drawn before this one.
Tags { "Queue" = "Transparent+1" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout" }
LOD 200
SubShader {
// Horizontal blur
GrabPass {
Tags { "LightMode" = "Always" }
}
Pass {
Blend SrcAlpha OneMinusSrcAlpha
ZWrite On
Lighting Off
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f {
float4 vertex : SV_POSITION;
float4 uvgrab : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert(appdata_t v) {
v2f o;
UNITY_SETUP_INSTANCE_ID(v); //Insert
UNITY_TRANSFER_INSTANCE_ID(v, o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); //Insert
o.vertex = UnityObjectToClipPos(v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y * scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
return o;
}
UNITY_DECLARE_SCREENSPACE_TEXTURE(_GrabTexture);
float4 _GrabTexture_TexelSize;
float _Size;
half4 frag(v2f i) : SV_TARGET {
UNITY_SETUP_INSTANCE_ID(i);
half4 sum = half4(0,0,0,0);
#if UNITY_SINGLE_PASS
#define GRABPIXEL(weight,kernelx) UNITY_SAMPLE_SCREENSPACE_TEXTURE( _GrabTexture, UNITY_PROJ_COORD(UnityStereoScreenSpaceUVAdjust(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w), _GrabTexture_TexelSize))) * weight
#else
#define GRABPIXEL(weight,kernelx) UNITY_SAMPLE_SCREENSPACE_TEXTURE( _GrabTexture, ((float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w), _GrabTexture_TexelSize))) * weight
#endif
sum += GRABPIXEL(0.05, -4.0);
sum += GRABPIXEL(0.09, -3.0);
sum += GRABPIXEL(0.12, -2.0);
sum += GRABPIXEL(0.15, -1.0);
sum += GRABPIXEL(0.18, 0.0);
sum += GRABPIXEL(0.15, +1.0);
sum += GRABPIXEL(0.12, +2.0);
sum += GRABPIXEL(0.09, +3.0);
sum += GRABPIXEL(0.05, +4.0);
return sum;
}
ENDCG
}
// Vertical blur
GrabPass {
Tags { "LightMode" = "Always" }
}
Pass {
Tags { "LightMode" = "Always" }
Blend SrcAlpha OneMinusSrcAlpha
ZWrite On
Lighting Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f {
float4 vertex : SV_POSITION;
float4 uvgrab : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert(appdata_t v) {
v2f o;
UNITY_SETUP_INSTANCE_ID(v); //Insert
UNITY_TRANSFER_INSTANCE_ID(v, o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); //Insert
o.vertex = UnityObjectToClipPos(v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y * scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
return o;
}
UNITY_DECLARE_SCREENSPACE_TEXTURE(_GrabTexture);
float4 _GrabTexture_TexelSize;
float _Size;
half4 frag(v2f i) : SV_Target {
UNITY_SETUP_INSTANCE_ID(i);
half4 sum = half4(0,0,0,0);
#if UNITY_SINGLE_PASS
#define GRABPIXEL(weight,kernelx) UNITY_SAMPLE_SCREENSPACE_TEXTURE( _GrabTexture, UNITY_PROJ_COORD(UnityStereoScreenSpaceUVAdjust(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w), _GrabTexture_TexelSize))) * weight
#else
#define GRABPIXEL(weight,kernelx) UNITY_SAMPLE_SCREENSPACE_TEXTURE( _GrabTexture, ((float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w), _GrabTexture_TexelSize))) * weight
#endif
//G(X) = (1/(sqrt(2*PI*deviation*deviation))) * exp(-(x*x / (2*deviation*deviation)))
sum += GRABPIXEL(0.05, -4.0);
sum += GRABPIXEL(0.09, -3.0);
sum += GRABPIXEL(0.12, -2.0);
sum += GRABPIXEL(0.15, -1.0);
sum += GRABPIXEL(0.18, 0.0);
sum += GRABPIXEL(0.15, +1.0);
sum += GRABPIXEL(0.12, +2.0);
sum += GRABPIXEL(0.09, +3.0);
sum += GRABPIXEL(0.05, +4.0);
return sum;
}
ENDCG
}
/*
// Distortion
GrabPass {
Tags { "LightMode" = "Always" }
}
Pass {
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f {
float4 vertex : SV_POSITION;
float4 uvgrab : TEXCOORD0;
float2 uvbump : TEXCOORD1;
float2 uvmain : TEXCOORD2;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
float _BumpAmt;
float4 _BumpMap_ST;
float4 _MainTex_ST;
v2f vert(appdata_t v) {
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.vertex = UnityObjectToClipPos(v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y * scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
o.uvbump = TRANSFORM_TEX(v.texcoord, _BumpMap);
o.uvmain = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 _Color;
UNITY_DECLARE_SCREENSPACE_TEXTURE(_GrabTexture);
float4 _GrabTexture_TexelSize;
sampler2D _BumpMap;
sampler2D _MainTex;
half4 frag(v2f i) : SV_Target {
UNITY_SETUP_INSTANCE_ID(i);
// calculate perturbed coordinates
half2 bump = UnpackNormal(tex2D(_BumpMap, i.uvbump)).rg; // we could optimize this by just reading the x y without reconstructing the Z
float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
half4 col = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture, i.uvgrab.xy / i.uvgrab.w);
half4 tint = tex2D(_MainTex, i.uvmain) * _Color;
return col * tint;
}
ENDCG
}
*/
}
}
}
I wish to have alpha transparency on my shader that enables characters to be seen behind foreground layers (without completely removing them). However, it is either fully on or fully off! I've been tweaking with a shader without much luck! Here is the shader:
Shader "Universal Render Pipeline/2D/EntityForegroundTrancparency"
{
Properties
{
_MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
_MaskTex("Mask", 2D) = "white" {}
_NormalMap("Normal Map", 2D) = "bump" {}
_Transparency("Transparency", Range(0.0,0.5)) = 0.25
// Legacy properties. They're here so that materials using this shader can gracefully fallback to the legacy sprite shader.
[HideInInspector] _Color("Tint", Color) = (1,1,1,1)
[HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
[HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
[HideInInspector] _AlphaTex("External Alpha", 2D) = "white" {}
[HideInInspector] _EnableExternalAlpha("Enable External Alpha", Float) = 0
}
SubShader
{
Tags {"Queue" = "Transparent" "IgnoreProjector"="True" "RenderType" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
Cull Off
ZWrite Off
Stencil
{
Ref 1
Comp Always
Pass Replace
}
Pass
{
Tags { "LightMode" = "Universal2D" }
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#pragma vertex CombinedShapeLightVertex
#pragma fragment CombinedShapeLightFragment
#pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
#pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
#pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
#pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __
#pragma multi_compile _ DEBUG_DISPLAY
struct Attributes
{
float3 positionOS : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 positionCS : SV_POSITION;
half4 color : COLOR;
float2 uv : TEXCOORD0;
half2 lightingUV : TEXCOORD1;
#if defined(DEBUG_DISPLAY)
float3 positionWS : TEXCOORD2;
#endif
UNITY_VERTEX_OUTPUT_STEREO
};
#include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
TEXTURE2D(_MaskTex);
SAMPLER(sampler_MaskTex);
half4 _MainTex_ST;
float _Transparency;
#if USE_SHAPE_LIGHT_TYPE_0
SHAPE_LIGHT(0)
#endif
#if USE_SHAPE_LIGHT_TYPE_1
SHAPE_LIGHT(1)
#endif
#if USE_SHAPE_LIGHT_TYPE_2
SHAPE_LIGHT(2)
#endif
#if USE_SHAPE_LIGHT_TYPE_3
SHAPE_LIGHT(3)
#endif
Varyings CombinedShapeLightVertex(Attributes v)
{
Varyings o = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.positionCS = TransformObjectToHClip(v.positionOS);
#if defined(DEBUG_DISPLAY)
o.positionWS = TransformObjectToWorld(v.positionOS);
#endif
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.lightingUV = half2(ComputeScreenPos(o.positionCS / o.positionCS.w).xy);
o.color = v.color;
o.color.a = _Transparency;
return o;
}
#include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"
half4 CombinedShapeLightFragment(Varyings i) : SV_Target
{
i.color.a = _Transparency;
half4 main = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
half4 mask = i.color * SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv);
SurfaceData2D surfaceData;
InputData2D inputData;
InitializeSurfaceData(main.rgb, _Transparency, mask, surfaceData);
InitializeInputData(i.uv, i.lightingUV, inputData);
return CombinedShapeLightShared(surfaceData, inputData);
}
ENDHLSL
}
Pass
{
Tags { "LightMode" = "NormalsRendering"}
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#pragma vertex NormalsRenderingVertex
#pragma fragment NormalsRenderingFragment
struct Attributes
{
float3 positionOS : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
float4 tangent : TANGENT;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 positionCS : SV_POSITION;
half4 color : COLOR;
float2 uv : TEXCOORD0;
half3 normalWS : TEXCOORD1;
half3 tangentWS : TEXCOORD2;
half3 bitangentWS : TEXCOORD3;
UNITY_VERTEX_OUTPUT_STEREO
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
TEXTURE2D(_NormalMap);
SAMPLER(sampler_NormalMap);
half4 _NormalMap_ST; // Is this the right way to do this?
float _Transparency;
Varyings NormalsRenderingVertex(Attributes attributes)
{
Varyings o = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(attributes);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.positionCS = TransformObjectToHClip(attributes.positionOS);
o.uv = TRANSFORM_TEX(attributes.uv, _NormalMap);
o.color = attributes.color;
o.color.a = _Transparency;
o.normalWS = -GetViewForwardDir();
o.tangentWS = TransformObjectToWorldDir(attributes.tangent.xyz);
o.bitangentWS = cross(o.normalWS, o.tangentWS) * attributes.tangent.w;
return o;
}
#include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/NormalsRenderingShared.hlsl"
half4 NormalsRenderingFragment(Varyings i) : SV_Target
{
i.color.a = _Transparency;
const half4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
const half3 normalTS = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, i.uv));
return NormalsRenderingShared(mainTex, normalTS, i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz);
}
ENDHLSL
}
Pass
{
Tags { "LightMode" = "UniversalForward" "Queue"="Transparent" "RenderType"="Transparent"}
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#pragma vertex UnlitVertex
#pragma fragment UnlitFragment
struct Attributes
{
float3 positionOS : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
#if defined(DEBUG_DISPLAY)
float3 positionWS : TEXCOORD2;
#endif
UNITY_VERTEX_OUTPUT_STEREO
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
float4 _MainTex_ST;
float _Transparency;
Varyings UnlitVertex(Attributes attributes)
{
Varyings o = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(attributes);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.positionCS = TransformObjectToHClip(attributes.positionOS);
#if defined(DEBUG_DISPLAY)
o.positionWS = TransformObjectToWorld(v.positionOS);
#endif
o.uv = TRANSFORM_TEX(attributes.uv, _MainTex);
o.color = attributes.color;
o.color.a = _Transparency;
return o;
}
float4 UnlitFragment(Varyings i) : SV_Target
{
i.color.a = _Transparency;
float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
#if defined(DEBUG_DISPLAY)
SurfaceData2D surfaceData;
InputData2D inputData;
half4 debugColor = 0;
InitializeSurfaceData(mainTex.rgb, _Transparency, surfaceData);
InitializeInputData(i.uv, inputData);
SETUP_DEBUG_DATA_2D(inputData, i.positionWS);
if(CanDebugOverrideOutputColor(surfaceData, inputData, debugColor))
{
return debugColor;
}
#endif
return mainTex;
}
ENDHLSL
}
}
Fallback "Sprites/Default"
}
As you can see from the following video, alpha seems to be completely ignored:
https://www.youtube.com/watch?v=zUTKF-bIdD8
Thanks in advance.
My fog Shader works fine on the Windows platform but it stopped when I switch to Android platform.
this is a screenshot of scene in windows platform , on android it will be gone
it's my shader:
Shader "Custom/VerticalFogIntersection"
{
Properties
{
_Color("Main Color", Color) = (1, 1, 1, .5)
_IntersectionThresholdMax("Intersection Threshold Max", float) = 1
}
SubShader
{
Tags { "Queue" = "Transparent" "RenderType"="Transparent" }
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 scrPos : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _CameraDepthTexture;
float4 _Color;
float4 _IntersectionColor;
float _IntersectionThresholdMax;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.scrPos = ComputeScreenPos(o.vertex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
half4 frag(v2f i) : SV_TARGET
{
float depth = LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)));
float diff = saturate(_IntersectionThresholdMax * (depth - i.scrPos.w));
fixed4 col = lerp(fixed4(_Color.rgb, 0.0), _Color, diff * diff * diff * (diff * (6 * diff - 15) + 10));
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Auto Graphics API : unchecked.
Unity version: 2019.3.4f.
there is not any error or log.
I also tested builded APK on physical android device.
I was implementing a lightmap into my shader and then I got this error and I can't figure out what it means. Does any one know how I can fix this? It started happening since I added the light map in.
Shader error in 'Custom/HauntedHouseShader': incorrect number of arguments to numeric-type constructor at line 84 (on d3d11)
This is my code:
Shader "Custom/HauntedHouseShader" {
Properties {
_Texture ("Diffuse", 2D) = "white"{}
_Color ("Color", Color) = (0,0,0,1)
_TilingX ("TilingX", float) = 1
_TilingZ ("TilingZ", float) = 1
}
SubShader {
Pass{
Tags { "LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#include "UnityCG.cginc"
float4 _Color;
sampler2D _Texture;
float _TilingX;
float _TilingZ;
sampler2D unity_Lightmap;
float4 unity_LightmapST;
float4 _LightColor0;
struct VertexOutput
{
float4 pos : SV_POSITION;
float4 posWorld : TEXCOORD1;
float4 tex : TEXCOORD0;
float3 normalDir : TEXCOORD2;
float2 tex2 : TEXCOORD3;
};
struct VertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct FragmentOutput
{
float4 color : COLOR;
};
VertexOutput vert (VertexInput i)
{
VertexOutput VOUT;
VOUT.tex = i.texcoord;
VOUT.pos = mul(UNITY_MATRIX_MVP,i.vertex);
VOUT.posWorld = mul(_Object2World,i.vertex);
VOUT.normalDir = normalize( mul(float4(i.normal,0.0),_World2Object).xyz);
VOUT.tex2 = i.texcoord.xy * unity_LightmapST.xy + unity_LightmapST.zw;
return VOUT;
}
FragmentOutput frag(VertexOutput v)
{
FragmentOutput FOUT;
float3 lightDir;
float atten;
if(_WorldSpaceLightPos0.w == 0.0){
atten = 1;
lightDir = normalize(_WorldSpaceLightPos0.xyz);
}else
{
float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - v.posWorld.xyz;
float dist = length(fragmentToLightSource);
atten = 1.0/dist;
lightDir = normalize(fragmentToLightSource);
}
float3 lightMap = float3(DecodeLightmap(tex2D(unity_Lightmap, v.tex2.xy)),0.0);
float3 normalDir = v.normalDir;
float4 tex = tex2D(_Texture, float4(v.posWorld.x * _TilingX,v.posWorld.z * _TilingZ,0.0,0.0));
float3 diffuseReflection = atten * _LightColor0.xyz * saturate(dot(normalDir, lightDir));
float3 lightFinal = UNITY_LIGHTMODEL_AMBIENT.xyz + diffuseReflection;
float4 col = float4(tex.xyz * lightFinal * _Color + lightMap,0.0);
FOUT.color = col;
return FOUT;
}
ENDCG
}
Pass{
Tags { "LightMode" = "ForwardAdd"}
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
float4 _Color;
sampler2D _Texture;
float _TilingX;
float _TilingZ;
float4 _LightColor0;
struct VertexOutput
{
float4 pos : SV_POSITION;
float4 posWorld : TEXCOORD1;
float4 tex : TEXCOORD0;
float3 normalDir : TEXCOORD2;
};
struct VertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 tangent : TANGENT;
};
struct FragmentOutput
{
float4 color : COLOR;
};
VertexOutput vert (VertexInput i)
{
VertexOutput VOUT;
VOUT.tex = i.texcoord;
VOUT.pos = mul(UNITY_MATRIX_MVP,i.vertex);
VOUT.posWorld = mul(_Object2World,i.vertex);
VOUT.normalDir = normalize( mul(float4(i.normal,0.0),_World2Object).xyz);
return VOUT;
}
FragmentOutput frag(VertexOutput v)
{
FragmentOutput FOUT;
float3 lightDir;
float atten;
if(_WorldSpaceLightPos0.w == 0.0){
atten = 1;
lightDir = normalize(_WorldSpaceLightPos0.xyz);
}else
{
float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - v.posWorld.xyz;
float dist = length(fragmentToLightSource);
atten = 1.0/dist;
lightDir = normalize(fragmentToLightSource);
}
float3 normalDir = v.normalDir;
float4 tex = tex2D(_Texture, float4(v.posWorld.x * _TilingX,v.posWorld.z * _TilingZ,0.0,0.0));
float3 diffuseReflection = atten * _LightColor0.xyz * saturate(dot(normalDir, lightDir));
float3 lightFinal = UNITY_LIGHTMODEL_AMBIENT.xyz + diffuseReflection;
float4 col = float4(tex.xyz * lightFinal * _Color ,0.0);
FOUT.color = col;
return FOUT;
}
ENDCG
}
}
FallBack "Diffuse"
}
Well the error is from the line
float3 lightMap = float3(DecodeLightmap(tex2D(unity_Lightmap, v.tex2.xy)),0.0);
which is a clearly a typo, since here you try to construct float3 from 2 arguments float3 and float, which is meaningless. Maybe you meant:
float3 lightMap = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, v.tex2.xy));
or
float3 lightMap = float3(DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, v.tex2.xy)).xy, 0.0);
I assume that what makes the confusion here is that error points to a wrong line number, the reason is that Unity 5 compilation makes some automatic converions of older APIs in your shader source (quite confusing), try reloading the file and see how it actually looks.
This is what I’m trying to accomplish
Texture geometry with primary shader
Layer a second texture over the first one by a projected amount following the normal.
Here is my current shader code:
Shader "Custom/FurShader"
{
Properties
{
_MainTex( "Main Texture", 2D ) = "white" {}
_MaxHairLength( "Max Hair Length", Float ) = 0.5
_NoOfPasses( "Number of Passes", Float ) = 2.0
}
CGINCLUDE
//includes
#include "UnityCG.cginc"
//structures
struct vertexInput
{
float4 vertex : POSITION;
float4 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct fragmentInput
{
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
};
//uniforms
uniform float _MaxHairLength;
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform sampler2D _SecondTex;
uniform float4 _SecondTex_ST;
uniform float _NoOfPasses;
//function
inline fragmentInput LevelFragmentShader( vertexInput i, int level )
{
fragmentInput o;
float movementDist = ( _MaxHairLength / _NoOfPasses ) * level;
float4 pos = ( i.vertex + ( i.normal * movementDist ) );
o.pos = mul( UNITY_MATRIX_MVP, pos );
o.uv = TRANSFORM_TEX( i.texcoord, _SecondTex );
return o;
}
half4 frag( fragmentInput i ) : COLOR
{
return tex2D( _SecondTex, i.uv );
}
ENDCG
SubShader {
Tags { "Queue" = "Transparent"}
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
#pragma exclude_renderers gles
#pragma vertex vert
#pragma fragment frag_unique
fragmentInput vert( vertexInput i )
{
fragmentInput o;
o.pos = mul( UNITY_MATRIX_MVP, i.vertex );
o.uv = TRANSFORM_TEX( i.texcoord, _MainTex );
return o;
}
half4 frag_unique( fragmentInput i ) : COLOR
{
return tex2D( _MainTex, i.uv );
}
ENDCG
}
Pass
{
CGPROGRAM
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
#pragma exclude_renderers gles
#pragma vertex vert
#pragma fragment frag
fragmentInput vert( vertexInput i )
{
fragmentInput o = LevelFragmentShader( i, 1 );
return o;
}
ENDCG
}
}
FallBack "Diffuse"
}
But as you can see the result second texture is not projecting perpendicularly edge to edge. Any suggestions would be great, I'm sure my maths is correct vertxPos + ( Normal * projectionDistance). Could it be something to do with how I'm using unitys ModelViewProjection Matrix?
Image showing result
http://i1265.photobucket.com/albums/jj508/maxfire1/Capture_zpsc8db2b1f.png