• 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 minute.

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 programming 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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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? 4 Answers

Order for Surface Shader / CG frag passes? 1 Answer

How can I make my own shader include file (.cginc or .glslinc)? 2 Answers

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