- Home /
sampler2d object has no methods
Hello there. I'm trying to write my shader, in order to get a depth texture that has to be assigned to the camera, in order to get a black-and-white description of the distance of the objects within the scene from the rendering camera.
This is the code ( from https://forum.unity.com/threads/camera-depth-texture-sampling-with-2018-3-and-hdrp-4-x-mip-map-issue.594160/ ):
Shader "Unlit/DepthShade"
{
Properties
{
_MainTex ("Decal Texture", 2D) = "white" {}
}
HLSLINCLUDE
#include "UnityCG.cginc"
#include "HLSLSupport.cginc"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/API/D3D11.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/API/GLCore.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Macros.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
ENDHLSL
CGINCLUDE
#include "UnityCG.cginc"
#include "HLSLSupport.cginc"
ENDCG
SubShader
{
Tags{ "Queue"="Geometry+1" }
Pass
{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
HLSLPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
struct v2f
{
float4 pos : SV_POSITION;
float4 screenUV : TEXCOORD0;
float3 ray : TEXCOORD1;
};
v2f vert (appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos (v.vertex);
o.screenUV = ComputeScreenPos (o.pos);
o.ray = UnityObjectToViewPos(v.vertex).xyz * float3(-1,-1,1);
return o;
}
sampler2D _MainTex;
sampler2D _CameraDepthTexture;
float4 frag(v2f i) : SV_Target
{
i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
float2 uv = i.screenUV.xy / i.screenUV.w;
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, uv);
depth = Linear01Depth (depth);
float4 vpos = float4(i.ray * depth,1);
float3 wpos = mul (unity_CameraToWorld, vpos).xyz;
float3 opos = mul (unity_WorldToObject, float4(wpos,1)).xyz;
clip (float3(0.5,0.5,0.5) - abs(opos.xyz));
float2 texUV = opos.xz + 0.5;
float4 col = tex2D (_MainTex, texUV);
return col;
}
ENDHLSL
}
}
Fallback Off
}
However, the compiler gives the following error: sampler2d object has no methods
.
Any ideas?
Answer by AkilliMum · Sep 22, 2020 at 11:28 AM
Hi, not sure about the problem but i suppose LOAD_TEXTURE2D wants a Texture2D not a sampler2D, because you defined the depth texture like "sampler2D _CameraDepthTexture;" the LOAD_TEXTURE2D related error happens. Maybe defining the depth like "Texture2D (_CameraDepthTexture);" may resolve your problem. Not an hdrp guru though :)
Your answer
Follow this Question
Related Questions
Render scene depth to a texture 4 Answers
Multiple RenderTexture and depth buffer 1 Answer
Compositing two RenderTextures based on depth 1 Answer
How to get the depth values of the camera view? 1 Answer
Normals from Depth texture distorted 2 Answers