Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by ootsby · Feb 03, 2015 at 12:13 AM · shadercgsurfaceshader

Shader Help: "Cannot implicitly convert 'float3' to 'float4'"

Hi,

I'm trying to experiment with using a shader to alter rendering based on the player position and a texture. I've taken a sample shader from the Unity docs and added a position vector input, as below:

   Shader "Custom/Test" {
     Properties {
       _MainTex ("Texture", 2D) = "white" {}
       _Detail ("Detail", 2D) = "gray" {}
       _PlayerPos ("PlayerPos", Vector ) = (0,0,0,1)
     }
     SubShader {
       Tags { "RenderType" = "Opaque" }
       CGPROGRAM
       #pragma debug
       #pragma surface surf Lambert
       struct Input {
           float2 uv_MainTex;
           float4 screenPos;
           float4 worldPos;
       };
       sampler2D _MainTex;
       sampler2D _Detail;
       float4 _PlayerPos;
       
       void surf (Input IN, inout SurfaceOutput o) {
           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
           float2 shadowUV = float2( IN.worldPos.x - _PlayerPos.x, IN.worldPos.z - _PlayerPos.z );
           shadowUV /= 32.0;
           shadowUV += float2(0.5,0.5);
 
           o.Albedo *= tex2D (_Detail, shadowUV).r;
           
       }
       ENDCG
     } 
     Fallback "Diffuse"
   }

The problem is that this fails to compile with the "Cannot implicitly convert 'float3' to 'float4'" error in the title being flagged against the last "o.Albedo *=" line although obviously there's generated code involved that I can't see.

As there are no float3 variables involved, and I'm expressly building a float2, I'm a bit lost as to what the issue is. Can anyone shed some light?

Additional: The issue seems to be to do with the shadowUV value as simply setting it to "shadowUV = float2(0.5,0.5);" allows the shader to compile and run (although not do anything of interest of course). I've tried passing the player position as two floats instead of via a Vector4 and it does the same thing.

Some more experimenting:

 float2 shadowUV = _PlayerPos.xz; //compiles
 float2 shadowUV = worldPos.xz; //throws the conversion compile error

Comment
Add comment · Show 3
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
avatar image carrollh · Feb 03, 2015 at 01:25 AM 0
Share

You don't have an o.Albedo += line. Do you mean the *= line?

avatar image ootsby · Feb 03, 2015 at 01:44 AM 0
Share

Yes. Edited.

avatar image carrollh · Feb 03, 2015 at 01:46 AM 0
Share

ok, give me a $$anonymous$$ute.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by ootsby · Feb 03, 2015 at 04:16 AM

I've worked it out, so I may as well add the answer to where I messed up.

I defined worldPos as a float4 in the Input struct when it's a float3. The fact that the error is thrown at the end of the chain of use rather than declaration made it a bit difficult to work out where the issue was.

Comment
Add comment · Show 1 · 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
avatar image carrollh · Feb 03, 2015 at 10:32 PM 0
Share

Glad you sorted. Yeah, I hate shader program$$anonymous$$g for that very reason. Well, that and "backwards compatibility" isn't really a thing :P

avatar image
0

Answer by carrollh · Feb 03, 2015 at 01:58 AM

Nutshell: You're in the CGProgram part, which is HLSL code. tex2D returns a float4 color value based on the things you pass in. Normally that's a sampler2D object and a tecture coordinate (or a float2). So you're good there. I'm not used to seeing tex2(..., ...). r anywhere. Normally it's .rgb or .a, but I'm not an HLSL wizard by any means. SurfaceOutput.Albedo is a half3 (float3?). So try changing .r to .rgb maybe.

Comment
Add comment · Show 1 · 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
avatar image ootsby · Feb 03, 2015 at 02:13 AM 0
Share

Thanks but unfortunately .r or .rgb makes no difference to the compile issue. Presumably r does a scalar multiply while rgb would do a vector multiply.

The issue seems to be to do with the shadowUV value as simply setting it to "shadowUV = float2(0.5,0.5);" allows the shader to compile and run (although not do anything of interest of course). I've tried passing the player position as two floats ins$$anonymous$$d of via a Vector4 and it does the same thing.

I'll add this extra info to the question.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Order for Surface Shader / CG frag passes? 1 Answer

Shader with zwrite, shadows and alpha (special alpha) 2 Answers

Mobile Additive surface shader 0 Answers

How to force the compilation of a shader in Unity? 5 Answers

Multiple tex2D commands in fragment shader needed, is there a workaround? 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