• 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
2
Question by kamil.slawicki · Jun 21, 2011 at 01:49 PM · shaderfloat

passing a float from script to shader

Hi guys. I'm trying to pass a float into my custom skybox shader from a script. I looked everywhere and I still have no idea how to do it. Any help is much appreciated. Here is my shader:

     Shader "RenderFX/CustomSky" {
 Properties {
     _Tex0 ("Cubemap0", Cube) = "white" {}
     _Tex1 ("Cubemap1", Cube) = "white" {}
     _BlendFactor ("BlendFactor", float) = 0.5
 }
 
 SubShader {
     Tags { "Queue"="Background" "RenderType"="Background" }
     Cull Off ZWrite Off Fog { Mode Off }
 
     Pass {
         
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #pragma fragmentoption ARB_precision_hint_fastest
 
         #include "UnityCG.cginc"
 
         samplerCUBE _Tex0;
         samplerCUBE _Tex1;
         uniform float _BlendFactor;
 
         
         struct appdata_t {
             float4 vertex : POSITION;
             float3 texcoord : TEXCOORD0;
         };
 
         struct v2f {
             float4 vertex : POSITION;
             float3 texcoord : TEXCOORD0;
         };
 
         v2f vert (appdata_t v)
         {
             v2f o;
             o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
             o.texcoord = v.texcoord;
             return o;
         }
 
         fixed4 frag (v2f i) : COLOR
         {
             fixed4 tex0 = texCUBE (_Tex0, i.texcoord);
             fixed4 tex1 = texCUBE (_Tex1, i.texcoord);
             
             fixed4 col = (tex0 *_BlendFactor)+( tex1 * (1.0 -_BlendFactor));
             return col;
         }
         ENDCG 
     }
 }     


Fallback Off

Here is my script:

 var globalLight : GameObject;
 static var localGameTime : float;
 static var maxTime : float = 20; //amount of time passed in seconds before the timer resets
 
 function Start()
 {
     localGameTime = 0;
 }
 
 function FixedUpdate()
 {
     localGameTime = Time.fixedTime%maxTime;
     CycleLightLocal();
     Debug.Log(localGameTime);
     //**Here I want to pass localGameTime/maxTime into the shader as an Uniform**
 }
 
 function CycleLightLocal()
 {
     globalLight.transform.Rotate((360/maxTime) * 0.02, 0, 0, Space.Self);    
 }

I apologize for bad formatting. Cheers. Kamil

Comment
Add comment · Show 1
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 almo · Jun 21, 2011 at 01:52 PM 0
Share

Formatting fixed. just use the 010101 button.

2 Replies

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

Answer by equalsequals · Jun 21, 2011 at 02:22 PM

Any time you need to communicate with a shader, you can do it through the Material class.

Essentially, you would just get reference to the skybox, perhaps through RenderSettings.skybox:

 Skybox _skybox = RenderSettings.skybox;

Skybox only has one property, material. So if you want to communicate a float to the material's shader you can use the methods Material.GetFloat("_PropertyName") and Material.SetFloat("_PropertyName", float)

So with your shader it would be:

 _skybox.material.SetFloat("_BlendFactor", .5F); //C#
 _skybox.material.SetFloat("_BlendFactor", 0.5); //JS

Hope that helps.

==

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 Ben-BearFish · May 23, 2015 at 12:45 AM 0
Share

@equalsequals Do you know how to set the cubemap or cubemap faces from a script on a skybox material?

avatar image
0

Answer by kamil.slawicki · Jun 21, 2011 at 02:39 PM

thanks! I sort of got it to work the way I wanted. Here's the code I used:

 var _skybox : Material;

var globalLight : GameObject;

static var localGameTime : float; static var maxTime : float = 20; //amount of time passed in seconds before the timer resets

function Start() { localGameTime = 0; _skybox = RenderSettings.skybox;

}

function FixedUpdate() { localGameTime = Time.fixedTime%maxTime;

 CycleLightLocal();

 
 Debug.Log(localGameTime);
 _skybox.SetFloat("_BlendFactor", localGameTime/maxTime); 
 
 


}

function CycleLightLocal() { globalLight.transform.Rotate((360/maxTime) * 0.02, 0, 0, Space.Self);
}

Kamil

Comment
Add comment · Show 4 · 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 equalsequals · Jun 21, 2011 at 02:49 PM 0
Share

If that helped, please upvote or accept the answer so that others having the same problem will be able to find it easier.

avatar image equalsequals · Jun 21, 2011 at 03:04 PM 0
Share

Also, don't post a comment as an answer, ins$$anonymous$$d leave it as a comment or update your question.

avatar image kamil.slawicki · Jun 21, 2011 at 03:39 PM 0
Share

sure, sorry about that, I'm new. Also started working with Unity yesterday.

avatar image equalsequals · Jun 21, 2011 at 03:57 PM 0
Share

that's alright, just trying to convey good practices so that the site is legible. glad I was able to help out - enjoy working with Unity!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Shader problems! GLSL Questions! 0 Answers

how to make special effects by joint or other ways ???!!!!! 0 Answers

How transfer data from shader to script? 0 Answers

What's the syntax for creating a skybox with a script? 2 Answers

getting a sum from two scripts 1 Answer


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