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
3
Question by xCRKx TyPHooN · Mar 28, 2011 at 11:03 PM · shadersurfacecustom-shader

Passing variables from Script to a Shader

Hey guys,

Im having trouble passing variables from C# script to a shader. I had success with a separate vertex/fragment shader, however, I can't seem to pass variables to a surface shader. Is this the problem or is there something wrong with my code? I'm trying to pass in a position variable, as well as a delta time variable. Thanks, code below

Shader "SonarShader" { Properties { _PlayerPosition("Player Position", Vector) = (1,1,1,1) _TimePassed("Time", float) = 0 }

SubShader { Tags { "RenderType" = "Transparent" } Cull Off

CGPROGRAM #pragma surface surf Lambert #include "UnityCG.cginc"

struct Input { float2 uv_MainTex; float3 worldPos; };

sampler2D _MainTex; float4 _PlayerPosition; float _TimePassed;

void surf (Input IN, inout SurfaceOutput o) { IN.worldPos.x += _PlayerPosition.x; IN.worldPos.y += _PlayerPosition.y; IN.worldPos.z += _PlayerPosition.z; float radius = length(IN.worldPos); float scale = 0.5f; float time = _TimePassed; float speed = 1000 time scale; float min = (-30 + speed); float max = (0 + speed);

   if(radius > min && radius < max)
     clip( sin( (radius) - speed) );
   else
     clip(-1);

} 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

2 Replies

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

Answer by Justin Warner · Mar 28, 2011 at 11:49 PM

Nice question...

Check out google:

http://www.google.com/search?client=opera&rls=en&q=Passing+variables+from+Script+to+a+Shader+unity3d&sourceid=opera&ie=utf-8&oe=utf-8&channel=suggest

And this seems to be more what you're asking...

http://forum.unity3d.com/threads/62770-passing-custom-variables-into-surface-shader

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 xCRKx TyPHooN · Mar 28, 2011 at 11:52 PM 1
Share

Lol believe me, I have hit pretty much every link in that google search and have already been to that thread :) For whatever reason the variables I'm passing into this shader just aren't being read by the program. I used the same code for a vertex/fragment shader and it worked, so I'm reaching my wits end with this.

avatar image
0

Answer by taoa · Mar 29, 2011 at 04:43 PM

I'm not sure you're allowed to modify the vertex' world position like that. You're more likely to manage to do what you want by making a temporary float3 storing whatever value is in IN.worldPos, and then add your vector to that.

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 xCRKx TyPHooN · Mar 29, 2011 at 05:12 PM 0
Share

I guess you can because I got it working about five $$anonymous$$utes ago. I was making a mistake on the script side. I was trying to send parameters to a global material in the class. I switched it to iterate through my GameObjects and send in the variables for each of their individual shaders and it worked.

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

No one has followed this question yet.

Related Questions

How can I pass world position to a custom lighting function for a surface shader? 1 Answer

Creating a fogless version of a built-in shader 1 Answer

Custom shader dissappear on mobile 0 Answers

uv.xyz in a surface shader? 2 Answers

Surface Shader, run vertex multiple times 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