Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 trapazza · Apr 19, 2020 at 06:02 PM · surface shadervertex shader

Surface shader + Instancing -> Invalid subscript 'texcoord' in generated code

So I'm writing a really simple surface shader to be used with indirect instancing. All this shader does is drawing a textured quad. I use a ComputeBuffer to provide per-instance data and then I just pass uv_MainTex in the Input structure to access the uv coords.

So everything compiles ok until i write the vertex shader where I access the per-instance compute buffer, then I get "invalid subscript 'texcoord' at line 97, 108 and 153 (lines that don't exist in my shader).

Can anyone tell me what I'm doing wrong? Does the "automatic" uv_MainTex get "deactivated" and I should pass the uvs manually?

Thanks

 Shader "Custom/instanced_indirect_tex"
 {
     Properties
     {
         _Color ("Color", Color) = (1,1,1,1)
         _MainTex ("Albedo (RGB)", 2D) = "white" {}
         _Glossiness ("Smoothness", Range(0,1)) = 0.5
         _Metallic ("Metallic", Range(0,1)) = 0.0
     }
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         LOD 200
 
         CGPROGRAM
         //#pragma vertex vert
         #pragma surface surf Standard fullforwardshadows vertex:pre_vert
         //#pragma target 5
 
         #include "UnityCG.cginc"
 
         sampler2D _MainTex;
 
         struct VertexShaderInput
         {
             float4 vertex     : POSITION;
             float3 normal     : NORMAL;
             uint   instanceId : SV_InstanceID;
         };
         
         // Surface shader input struct
         struct Input
         {
             float2 uv_MainTex;
         };
 
         half _Glossiness;
         half _Metallic;
         fixed4 _Color;
 
 
         struct MeshProperties
         {
             float4x4 mat;
             float4 color;
         };
 
         #ifdef SHADER_API_D3D11 
         StructuredBuffer<MeshProperties> _Properties;
         #endif
 
         void pre_vert( inout VertexShaderInput v, out Input o )
         {
             UNITY_INITIALIZE_OUTPUT( Input, o );
         #ifdef SHADER_API_D3D11
             v.vertex = mul( _Properties[ v.instanceId ].mat, v.vertex );
             //o.colorin = _Properties[ v.instanceId ].color;
         #endif
         }
 
         void surf( Input IN, inout SurfaceOutputStandard o )
         {
           // Albedo comes from a texture tinted by colors
             o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
         #ifdef SHADER_API_D3D11
             //o.Albedo = IN.colorin;
         #endif
             // Metallic and smoothness come from slider variables
             o.Metallic = .61;
             o.Smoothness = .61;
         }
         ENDCG
     }
     //FallBack "Diffuse"
 }

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
2

Answer by Namey5 · Apr 21, 2020 at 04:27 AM

In order for the custom vertex function to be compatible with the compiled internal vertex function, they will both use whatever input struct you pass to the custom vertex function (hence the 'inout' keyword). The compiled surface shader generally assumes you are using 'appdata_full' (as it contains all 'required' inputs), and because surface shaders do support instancing, the instance ID is actually defined in that struct, it's just abstracted to the point that it's difficult to access manually. Luckily, there is a macro for accessing the abstracted version;

 void pre_vert( inout appdata_full v, out Input o )
 {
     UNITY_INITIALIZE_OUTPUT( Input, o );
 #ifdef SHADER_API_D3D11
     uint instanceID = UNITY_GET_INSTANCE_ID (v);
     v.vertex = mul( _Properties[ instanceID ].mat, v.vertex );
     //o.colorin = _Properties[ instanceID ].color;
 #endif
 }

This may not work in your case (I'm not sure when Unity decides you are using instancing or not), in which case you would need to add some things to your struct;

 struct VertexShaderInput
 {
     float4 vertex : POSITION;
     float4 tangent : TANGENT;
     float3 normal : NORMAL;
     float4 texcoord : TEXCOORD0;
     float4 texcoord1 : TEXCOORD1;
     float4 texcoord2 : TEXCOORD2;
     float4 texcoord3 : TEXCOORD3;
     fixed4 color : COLOR;
     uint instanceId : SV_InstanceID;
 };
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.

Follow this Question

Answers Answers and Comments

126 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

Related Questions

'vert': output parameter 'o' not completely initialized 2 Answers

How to get pure black shadow on gradient shader 0 Answers

Can I unpack normal / tangent / uv information in the vertex function in a surface shader? 1 Answer

Passing an array to the Vertex shader 0 Answers

How to access array elements in Surface Vertex shaders? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges