This question was
closed Nov 18, 2020 at 12:59 PM by
xxmariofer for the following reason:
The question is answered, right answer was accepted
Question by
Rich-Ter · Nov 18, 2020 at 10:55 AM ·
errorshadershader programminggraphcustom-shader
[SOLVED] undeclared identifier X in custom function in shader graph
Hey so I'm trying to convert the sobel effect from shader to custom function, but I still get an "Undeclared identifier on the method I type, I'm not sure what I'm doing wrong, here is the source (the shader)
Shader "Unlit/SobelFilter"
{
Properties
{
[HideInInspector]_MainTex ("Base (RGB)", 2D) = "white" {}
_Delta ("Line Thickness", Range(0.0005, 0.0025)) = 0.001
[Toggle(RAW_OUTLINE)]_Raw ("Outline Only", Float) = 0
[Toggle(POSTERIZE)]_Poseterize ("Posterize", Float) = 0
_PosterizationCount ("Count", int) = 8
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
Pass
{
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#pragma shader_feature RAW_OUTLINE
#pragma shader_feature POSTERIZE
TEXTURE2D(_CameraDepthTexture);
SAMPLER(sampler_CameraDepthTexture);
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
float _Delta;
int _PosterizationCount;
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
float SampleDepth(float2 uv)
{
#if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
return SAMPLE_TEXTURE2D_ARRAY(_CameraDepthTexture, sampler_CameraDepthTexture, uv, unity_StereoEyeIndex).r;
#else
return SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, uv);
#endif
}
float sobel (float2 uv)
{
float2 delta = float2(_Delta, _Delta);
float hr = 0;
float vt = 0;
hr += SampleDepth(uv + float2(-1.0, -1.0) * delta) * 1.0;
hr += SampleDepth(uv + float2( 1.0, -1.0) * delta) * -1.0;
hr += SampleDepth(uv + float2(-1.0, 0.0) * delta) * 2.0;
hr += SampleDepth(uv + float2( 1.0, 0.0) * delta) * -2.0;
hr += SampleDepth(uv + float2(-1.0, 1.0) * delta) * 1.0;
hr += SampleDepth(uv + float2( 1.0, 1.0) * delta) * -1.0;
vt += SampleDepth(uv + float2(-1.0, -1.0) * delta) * 1.0;
vt += SampleDepth(uv + float2( 0.0, -1.0) * delta) * 2.0;
vt += SampleDepth(uv + float2( 1.0, -1.0) * delta) * 1.0;
vt += SampleDepth(uv + float2(-1.0, 1.0) * delta) * -1.0;
vt += SampleDepth(uv + float2( 0.0, 1.0) * delta) * -2.0;
vt += SampleDepth(uv + float2( 1.0, 1.0) * delta) * -1.0;
return sqrt(hr * hr + vt * vt);
}
Varyings vert(Attributes input)
{
Varyings output = (Varyings)0;
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
output.vertex = vertexInput.positionCS;
output.uv = input.uv;
return output;
}
half4 frag (Varyings input) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float s = pow(1 - saturate(sobel(input.uv)), 50);
#ifdef RAW_OUTLINE
return half4(s.xxx, 1);
#else
half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
#ifdef POSTERIZE
col = pow(col, 0.4545);
float3 c = RgbToHsv(col);
c.z = round(c.z * _PosterizationCount) / _PosterizationCount;
col = float4(HsvToRgb(c), col.a);
col = pow(col, 2.2);
#endif
return col * s;
#endif
}
#pragma vertex vert
#pragma fragment frag
ENDHLSL
}
}
FallBack "Diffuse"
}
And here is the custom function I created: from the vert and frag functions:
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
//#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
TEXTURE2D(_MainTex_ST);
SAMPLER(sampler_MainTex_ST);
struct VertexPositionInputs {
float3 positionWS; // World space position
float3 positionVS; // View space position
float4 positionCS; // Homogeneous clip space position
float4 positionNDC;// Homogeneous normalized device coordinates
};
struct Varyings
{
float2 uv;
float4 vertex;
UNITY_VERTEX_OUTPUT_STEREO
};
VertexPositionInputs GetVertexPositionInputs(float3 positionOS) {
VertexPositionInputs input;
input.positionWS = TransformObjectToWorld(positionOS);
input.positionVS = TransformWorldToView(input.positionWS);
input.positionCS = TransformWorldToHClip(input.positionWS);
float4 ndc = input.positionCS * 0.5f;
input.positionNDC.xy = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
input.positionNDC.zw = input.positionCS.zw;
return input;
}
Varyings GetVariyng(float4 pos, float2 uv)
{
Varyings output = (Varyings)0;
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
VertexPositionInputs vertexInput = GetVertexPositionInputs(pos.xyz);
output.vertex = vertexInput.positionCS;
output.uv = uv;
return output;
}
void toon_half4 (float2 uv, float4 vertex, float posterizationCount , out float4 Out)
{
Varyings input = GetVariyng(vertex, uv);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
half4 col = SAMPLE_TEXTURE2D(_MainTex_ST, sampler_MainTex_ST, input.uv);
col = pow(col, 0.4545);
float3 c = RgbToHsv(col);
c.z = round(c.z * posterizationCount) / posterizationCount;
col = float4(HsvToRgb(c), col.a);
Out = pow(col, 2.2);
}
The error I recieve is in the image!
please help, thank you!
screenshot-2020-11-18-121917.png
(28.5 kB)
Comment
Found the problem just needed to rename the function to the undeclared.... SOOOO STUPID!
in the hlsl convert from "toon_float4" to "toon_float"