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
0
Question by Charles_Gams · Mar 23, 2014 at 03:36 AM · shaderslider

Change value of a shader propertie using a Slider

Hi I've looked at other answers similar to what I'm asking but i still need clarity on it.

How would you use a GUI slider to control say specular amount of a shader in game.

using UnityEngine; using System.Collections;

public class ClickObject : MonoBehaviour {

 Rect windowRect = new Rect(10, 20, 500,300 );
 public bool canClick;
 public GameObject cont;

 //Toggle Booleans
 bool toggleWindow;
 bool toggleWindowTwo;
 bool toggleWindowThree;
 bool toggleWindowFour;
 bool toggleWindowFive;

 // Use this for initialization
 void Start () {
     canClick = false;

     toggleWindow = false;
     toggleWindowTwo = false;
 }

 void OnGUI ()
 {
     if(canClick)
     {

         ToggleWindows();

         if(Input.GetMouseButtonUp(0))
         {
             Ray ray = camera.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;

             if(Physics.Raycast(ray,out hit) && hit.transform.tag == "Object")
             {
                 Debug.Log("Open Lambert Shader Window");
                 toggleWindow = true;
             }
             else if(Physics.Raycast(ray,out hit) && hit.transform.tag == "Object2")
             {
                 Debug.Log("Open Blinn Shader Window");
                 toggleWindowTwo = true;
             }

         }

     }
     else
     {
         toggleWindow = false;
         toggleWindowTwo = false;
     }


     if(Input.GetKeyUp(KeyCode.Escape))
     {
         toggleWindow = false;
         toggleWindowTwo = false;
     }
 }

 void ToggleWindows ()
 {
     if(toggleWindow)
     {
         GUI.Window(0, windowRect, DoMyWindow, "My Window");
         toggleWindowTwo = false;
     }
     else if(toggleWindowTwo)
     {
         GUI.Window(1, windowRect, DoMyWindowTwo, "My Window 2");
         toggleWindow = false;
     }
 }

 void DoMyWindow(int windowID) 
 {
     float hSliderValue;
     float hSliderValue2=1.0f;
     float hSliderValue3=1.0f;
     float hSliderValue4=1.0f;
     float hSliderValue5=1.0f;
     float hSliderValue6=1.0f;
     float hSliderValue7=1.0f;
     float hSliderValue8=1.0f;
     float hSliderValue9=1.0f;
     float hSliderValue10=1.0f;
     float hSliderValue11=1.0f;

 
 

     //gets the shader
     


         // Animate the blend value

 hSliderValue =    cont.renderer.material.SetFloat( "_BaseColour", hSliderValue );
         
         if (GUI.Button(new Rect(350, 30, 100, 20), "Shader 1"))
     {
         print("Tweak Values");


     }

     hSliderValue = GUI.HorizontalSlider (new Rect (25, 40, 100, 30), hSliderValue, 0.0f, 1.0f);
     GUI.Label (new Rect (150, 35, 100, 30), "Base Colour");
     hSliderValue2 = GUI.HorizontalSlider (new Rect (25, 60, 100, 30), hSliderValue2, 0.0f, 1.0f);
     GUI.Label (new Rect (150, 55, 100, 30), "Subsurface");
     hSliderValue3 = GUI.HorizontalSlider (new Rect (25, 80, 100, 30), hSliderValue3, 0.0f, 1.0f);
     GUI.Label (new Rect (150, 75, 100, 30), "Metallic");
     hSliderValue4 = GUI.HorizontalSlider (new Rect (25, 100, 100, 30), hSliderValue4, 0.0f, 1.0f);
     GUI.Label (new Rect (150, 95, 100, 30), "Specular");
     hSliderValue5 = GUI.HorizontalSlider (new Rect (25, 120, 100, 30), hSliderValue5, 0.0f, 1.0f);
     GUI.Label (new Rect (150, 115, 100, 30), "Specular Tint");
     hSliderValue6 = GUI.HorizontalSlider (new Rect (25, 140, 100, 30), hSliderValue6, 0.0f, 1.0f);
     GUI.Label (new Rect (150, 135, 100, 30), "Roughness");
     hSliderValue7 = GUI.HorizontalSlider (new Rect (25, 160, 100, 30), hSliderValue7, 0.0f, 1.0f);
     GUI.Label (new Rect (150, 155, 100, 30), "Anisotropic");
     hSliderValue8 = GUI.HorizontalSlider (new Rect (25, 180, 100, 30), hSliderValue8, 0.0f, 1.0f);
     GUI.Label (new Rect (150, 175, 100, 30), "Sheen");
     hSliderValue9 = GUI.HorizontalSlider (new Rect (25, 200, 100, 30), hSliderValue9, 0.0f, 1.0f);
     GUI.Label (new Rect (150, 195, 100, 30), "Sheen Tint");
     hSliderValue10 = GUI.HorizontalSlider (new Rect (25, 220, 100, 30), hSliderValue10, 0.0f, 1.0f);
     GUI.Label (new Rect (150, 215, 100, 30), "Clearcoat");
     hSliderValue11 = GUI.HorizontalSlider (new Rect (25, 240, 100, 30), hSliderValue11, 0.0f, 1.0f);
     GUI.Label (new Rect (150, 235, 100, 30), "Clearcoat Gloss");



 }

 void DoMyWindowTwo(int windowID) 
 {
     if (GUI.Button(new Rect(10, 20, 100, 20), "Shader 2")){
         print("Tweak Values");
     }
 }

}

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
1

Answer by supernat · Mar 23, 2014 at 03:52 AM

See here for the GUI Slider (horizontal one anyway): http://docs.unity3d.com/Documentation/ScriptReference/GUI.HorizontalSlider.html

The value returned by the call (hSliderValue in the link above) can be stored in a variable. In the Update method, you can use this value to drive the shader (kind of like this):

 float value;
 float prevValue;
 
 public GameObject shadedObject;
 
 void  OnGUI() {
   value = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), value, 0.0F, 10.0F);
 }
 
 void Update() {
   if (prevValue != value) {
     prevValue = value;
     shadedObject.renderer.sharedMaterial.SetFloat("myShaderSpec", value);
   }
 }

Using sharedMaterial will modify every object in the scene that uses it. Use renderer.material if you just want to change a single object.

Unity Ref: http://docs.unity3d.com/Documentation/ScriptReference/Material.html

Comment
Add comment · Show 5 · 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 Charles_Gams · Mar 23, 2014 at 04:17 AM 0
Share

Oh okay sweet ill try this out .Does this principle still apply if i wanted to change the value eg//specular of a shader thats attached to another gameobject in the scene. Is the referencing any different

avatar image supernat · Mar 23, 2014 at 04:42 AM 0
Share

I guess I should have mentioned that. :) The variable in this script for shadedObject will be a reference to the object whose shader you wish to change. At design-time, you would drag the object onto this script's slot for shadedObject. If you use the shared$$anonymous$$aterial, all other objects in the scene will update. The key is that you can use any object you have access to, by referencing the renderer.material or renderer.shared$$anonymous$$aterial properties of that object. So no, its not. One other note, check out the reference online that I provided for $$anonymous$$aterial. There are some standard parameters, like material.color, that are just really shortcuts to calling GetInt, SetInt, GetFloat, SetFloat and having to know the exact name of the shader parameter (like specular). Also note there are special na$$anonymous$$g conventions, so you don't access the property using the exact name shown in the shader's inspector. If you have trouble, just post an update.

avatar image Charles_Gams · Mar 23, 2014 at 01:01 PM 0
Share

Hmm i still can't figure it out i'm going in regards to what i'm doing. I want to be able to use the GUIslider to control a value of i made in a shader which is exposed in the property eg// control _Specular with a GUI slider.

Like how you see in some demos of people who have GUI sliders that control the shader in the game rather than just outside. Ill update my question with the code

avatar image supernat · Mar 23, 2014 at 08:41 PM 0
Share

You're on the right path. What part isn't working? Can you post the top of your shader, specifically the Properties section? Are you using a standard shader? Is this for Stand Alone or mobile?

avatar image Charles_Gams · Mar 23, 2014 at 09:04 PM 0
Share

I'm managed to get it working :) what i did was make another C# class that handled getting the material , so basically making a material variable and then as you mentioned i used the set float function to get what i want and matched to the parameter i was controling so when i send the variable to the class that handles GUIs which i created (which is the posted code) the slider will move according to that float

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

21 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

Related Questions

Shader Slider 1 Answer

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

Add a slider to bumped specular shader for normal strength? 3 Answers

Unity 5 standard shader problems ? (Resolved) 1 Answer

Customize slider in Shader 3 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