• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rizzutp · Jan 21, 2020 at 12:18 PM · shaderrendertexturedepthhlsl

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. alt text

Any ideas?

sampler2derror.jpg (95.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

185 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Builtin Shader UI/Unlit/Transparent Yellow Pixels Read As White 0 Answers

Complex Depth Shader 0 Answers

Multiple RenderTexture and depth buffer 1 Answer

Render scene depth to a texture 4 Answers

Normals from Depth texture distorted 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges