Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by romT · May 11, 2017 at 08:44 AM · scripting problemshadervariableaccess

How to access to a variable in shader from a script ?

Hi, I have a variable to control a value of a something in my shader. I need to control and change that value from a script.

The variable is "fovSize". Bellow my shader :

 Pass
             {
                 CGPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
 
                 #include "UnityCG.cginc"
 
                 static const float fovSize = 0.1; 

And

 fragmentOutput frag(v2f i)
                 {
                     fragmentOutput o;
                     o.zvalue = 0.0;
                     o.color = (1, 1, 1, 1);
 
                     if ((i.posInObjectCoords.y < -fovSize || i.posInObjectCoords.y > fovSize)   ||
                         (i.posInObjectCoords.x < -fovSize || i.posInObjectCoords.x > fovSize))
                     {
                        o.zvalue = 1.0;
                     }
                     return o;
                 }

But it looks like I can't use that kind of code in my script because its not in the Properties :

 rend.material.SetFloat("_FovSize", fovSize);

I tried to declare in the Properties : _FovSize("fovSize", Float) = 1
And to use : if (i.posInObjectCoords.y < -_FovSize)
But doesn't work.

Any idea ?

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
Best Answer

Answer by MaT227 · May 11, 2017 at 08:55 AM

If you want to control a shader value from a script you don't need to declare it in the properties. Just declare it like this.

 // Shader
 uniform float _myValue;

In your script use Shader.SetGlobalFloat or Material.SetFloat depending on you use case.

 // Script
 public float myValue = 1.0f;
 Shader.SetGlobalFloat("_myValue", myValue);

I would also suggest you to take a look at those chapters Accessing shader properties in Cg/HLSL and MaterialPropertyBlock.

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 romT · May 11, 2017 at 09:11 AM 0
Share

Thank you,

Why don't you declare it like this :

 _FovSize("fovSize", Float) = 1.0

What is the difference ? And why do you use uniform ?

avatar image MaT227 romT · May 11, 2017 at 09:19 AM 0
Share

The declaration you showed is used to expose the properties in the inspector.

 // Visible in the material inspector
  _FovSize("fovSize", Float) = 1.0
 float _FovSize;
 // Not visible in the material inspector
 float _FovSize;

the uniform attribute is used when you want to control the value from the application or if you prefer from "outside" the shader.

avatar image romT MaT227 · May 11, 2017 at 09:46 AM 0
Share

Ok, So first I tried to do every thing in the shader but I have two new errors :
Shader error in 'FadeBlack': Parse error: syntax error, unexpected TO$$anonymous$$_FLOAT, expecting '(' at line 6
Shader warning in 'FadeBlack': Shader is not supported on this GPU (none of subshaders/fallbacks are suitable)

I declare _fovSize in the Properties, define it in the Pass and affect a value in fragmentOutput to use it.

I post my complete shader bellow :

 Shader "Custom/FadeBlack"
 {        Properties 
         {
             uniform float _fovSize;
         }
         
         SubShader
         {
             Tags{ "Queue" = "Geometry+5" "IgnoreProjector" = "True" "RenderType" = "Opaque" }
 
             ZTest Always
             ZWrite On
             Color$$anonymous$$ask 0
 
             Pass
             {
                 CGPROGRA$$anonymous$$
                 #pragma vertex vert
                 #pragma fragment frag
 
                 #include "UnityCG.cginc"
 
                 float _fovSize;
 
                 struct appdata
                 {
                     float4 vertex : POSITION;
                 };
 
                 struct v2f
                 {
                     float4 vertex : SV_POSITION;
                     float4 posInObjectCoords : TEXCOORD0;
                 };
     
                 v2f vert(appdata v)
                 {
                     v2f o;
                     o.vertex = UnityObjectToClipPos(v.vertex);
                     o.posInObjectCoords = v.vertex;
                     return o;
                 }
 
                 struct fragmentOutput 
                 {
                     float zvalue : SV_Depth;
                     fixed4 color : SV_Target;
                 };
 
                 fragmentOutput frag(v2f i)
                 {
                     fragmentOutput o;
                     o.zvalue = 0.0;
                     o.color = (1, 1, 1, 1);
                     _fovSize = 0.1;
 
                     if ((i.posInObjectCoords.y <(-_fovSize) || i.posInObjectCoords.y >(_fovSize))   ||
                         (i.posInObjectCoords.x <(-_fovSize) || i.posInObjectCoords.x >(_fovSize)))
                     {
                        o.zvalue = 1.0;
                     }
                     return o;
                 }
                 ENDCG
             }
         }
 }

I'm really novice with shader and i read pages you have suggested.

Show more comments

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

164 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 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 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 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 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 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 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 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

How to fix these errors when launching a built game 0 Answers

Mirror reflection not working as planned 0 Answers

Change values in ALL objects possessing the script 1 Answer

how to Add a Certain Amount into a Variable From another Script Unity 2 Answers

Problem with the Inspector and Script 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