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 /
avatar image
1
Question by Ates Akaydin · May 13, 2010 at 01:23 PM · shadercgparametersshader-replacementrenderwithshader

I cannot pass parameters to my custom shaders... help please...

Hello There,

I have been trying to pass parameters from unity to my custom depth/normal rendering shader. The code is as follows:

Shader "MIHAS/SAR" { Properties { _NormalContribution( "Normal Contribution", Float ) = 0.999 _DepthContribution( "Depth Contribution", Float ) = 0.001 _NormalPower( "Normal Contribution", Float ) = 8 _DepthPower( "Depth Contribution", Float ) = 2 _BaseColor("Base Color", Color) = (1,1,1,0) } SubShader { Tags { "RenderType"="Opaque" } Pass { Fog { Mode Off }

CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc"

struct v2f { float4 pos : POSITION; float4 color: COLOR; };

uniform float _NormalContribution; uniform float _DepthContribution; uniform float _NormalPower; uniform float _DepthPower; uniform float4 _BaseColor;

v2f vert( appdata_base v ) {

 v2f o;

 o.pos = mul(glstate.matrix.mvp, v.vertex);

 //float3 eNormal = mul((float3x3)glstate.matrix.mvp, v.normal );    

 //float depth;
 //TRANSFER_EYEDEPTH(depth);
 //depth = pow(1.0 - depth,_DepthPower);

 //float dotP = pow(abs(dot( (float3)normalize(o.pos), eNormal )), _NormalPower);

 float4 baseColor = { 0.0, 1.0, 0.0, 0.0 };

 //Calculate vertex color
 //o.color = ( (_NormalContribution * dotP)+(_DepthContribution * depth ) ) * _BaseColor;
 o.color = _BaseColor;

 return o;

}

float4 frag(v2f i) : COLOR {

 return i.color;

}

ENDCG

 }

}

Fallback Off

}

However the values of all of my custom variables(like _BaseColor) are read as zero on the shader side. Removing "uniform" specifier does not change anything. And if I initialize these values to local variables or if I provide a constant instead of them at necessary positions it works without a problem.

Although not necessary I have even written the below script to force parameter setting(in case it is initialized wrong) but this doesnt make a difference either:

using UnityEngine; using System.Collections;

 public class SarShader : MonoBehaviour {

     public float normalContribution = 0.999f;
     public float depthContribution = 0.001f;
     public float normalPower = 8f;
     public float depthPower = 2f;
     public Color baseColor = new Color(1.0f,1.0f,1.0f,0.0f);

     public Material sarMaterial;

     // Use this for initialization
     void Start () {

         if( sarMaterial != null ){

             setParameters();

             Shader sarShader = sarMaterial.shader;
             //!SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Depth)
             if( sarShader != null && sarShader.isSupported && camera != null ){
                     camera.SetReplacementShader( sarShader, "" );
             }       
         }

     }

     void setParameters(){

         sarMaterial.SetColor("_BaseColor", baseColor );
         sarMaterial.SetFloat("_NormalContribution", normalContribution );
         sarMaterial.SetFloat("_DepthContribution", depthContribution );
         sarMaterial.SetFloat("_NormalPower", normalPower );
         sarMaterial.SetFloat("_DepthPower", depthPower );       
     }

}

Do anyone have any idea on what might be the problem??? All of these 5 parameters are always read as zero... Without removing these if I only copy constant in their place in the shader code it works perfectly without a problem.

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 Ates Akaydin · May 13, 2010 at 01:26 PM 0
Share

Well I have accidentally linked the commented code... But it doesnt matter if you remove those comments. It still can't read any values... I have added the script to a game object with camera attached by the way...

avatar image sean · May 14, 2010 at 09:21 PM 0
Share

have you tried using the plain-text names of your params in the .set* functions (e.g. .SetColor("Base Color",basecolor))? I'd imagine those names in the shader get used for something, and a compiler pattern-matching script strings to shader handles would be unusual if not per se unprecedented. Also, are there any separators (",", ";", etc.) needed between the Properties in the shader?

avatar image Ates Akaydin · May 20, 2010 at 02:48 PM 0
Share

Yes I have tried using the plane text names in the set functions. In fact I do not think that those set functions are actually necessary. I have written them as it didnt work without them. But it does not work with them either. Are seperators necessary for shader properties? I havent seen any examples of them anywhere. I think it would be inappropriate syntax for shader definition. Thanks for help Sean. I really appreciate it.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Edy · Jan 30, 2011 at 08:35 PM

Use the function Shader.SetGlobalFloat in the SetParameters funcion, then you can discard all the code related to the material.

Also, include the SetReplacementShader call at the end of the SetParameters function. Call SetParameters once after each render, for instance at the PreCull function.

Comment
Add comment · 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
0

Answer by schetty · Apr 11, 2014 at 12:21 PM

it works for me.

I did the same thing what you have explained here.

Actually i changed the value of my shader slide value using my GUI.HorizontalSlider it working fine. The thing is i call every thing in Update function.

Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

Displaying a different shader ONLY where a spotlight hits an object. 1 Answer

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

Can I read `CGPROGRAM` `#define` constants in `.cs` at runtime? 0 Answers

How to use array in cg shader 1 Answer

Problem with custom shader (ShaderLab + Cg) 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