• 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 Qbz · Jun 22, 2017 at 11:00 AM · shader

Setting cbuffer floats in scripts

Hello. Trying to do a custom 2d lighting system. I'm mainly getting used to unity, i'm fine on the shader code. I'm having issues setting values in a const buffer via script. I have the const buffer...

     struct LightData
     {
         float foregroundFactor;
         float gameFactor;
         float backFactor;
         float minIntensity;
         float maxIntensity;
         float radius; 
         float2 pos;
         float angle;
         float2 direction;
     };
 
     static const int MAXLIGHTS = 64;
     CBUFFER_START(PerFrame);
     int layer; 
     LightData lights[MAXLIGHTS];
     int activeLights;
     CBUFFER_END

I am testing that i can set the cbuffer values with the following fragment shader code...

         if(layer > 2)
         {
           o.r = 1;
         }
 
         o.b = lights[0].foregroundFactor;
         o.g = 1;

And I am trying to set properties c# side with the following code...

       int id = Shader.PropertyToID("lights[0].foregroundFactor");
       GetComponent<SpriteRenderer>().material.SetFloat(id, 255.0f);
       GetComponent<SpriteRenderer>().material.SetInt("layer", 3);

The layer is acknowledged in the shader, but lights[0].foregroundFactor seems to always be 0. I'm only using id for lights[0].foregroundFactor b/c I wanted to confirm the id looked fine and wasn't some obvious error value, and the id is reasonable. In addition, hasproperty("lights[0].foregroundFactor") returns true.

What am I missing that is causing lights[0].foregroundFactor to not be updated? Another thing that might be relevant is I'm using custom vertex and fragment shaders, not unity's surface/lighting system.

EDIT: I moved it to Float array, and that works. So technically I could splat out the array of structs into many arrays of struct members but that seems bad. So I guess the question is how to set struct members in cbuffers correctly.

Comment
Add comment · Show 2
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 Qbz · Jun 18, 2017 at 01:12 AM 0
Share

Also, I know optimized out issues are a frequent cause of error, but I think they don't apply here b/c I'm using layer and im using lights[0].foregroundFactor, which is the first field in the first struct in the array, so the offset being used to set the val in the cbuffer should be unaffected by the rest of the lightdata struct being optimized out

avatar image eizenhorn · Jun 18, 2018 at 03:55 PM 0
Share

Hi! Some info about that? It's correct way to setup cbuffer data like as simple shader properties? I'm simply setup variables separately and they sended on GPU as one "batch" of data to my cbuffer?

0 Replies

· Add your reply
  • Sort: 

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

139 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

Related Questions

Sharp Cut-Out Height Fog? 0 Answers

Assets/Scripts/Transparent.cs(11,51): error CS1955: The member `UnityEngine.Material.shader' cannot be used as method or delegate 0 Answers

Performance of looping compute shader in Update() 2 Answers

[Android] Weird Standard Shader behaviour 0 Answers

How do I make part of an object visible? 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