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 valyard · Jun 02, 2015 at 03:25 PM · shaderarray

How to pass an array to a shader?

Is there a way to pass an array of floats to a shader?

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 VelocityStrike · May 28, 2017 at 07:47 PM 0
Share

wanna co op to make a game?

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Aras · Jun 07, 2016 at 03:14 PM

Starting with Unity 5.4, there is native "arrays in shaders" support. Material.SetFloatArray, Shader.SetGlobalFloatArray, MaterialPropertyBlock.SetFloatArray

Comment
Add comment · Show 2 · 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 tanoshimi · Jun 07, 2016 at 04:48 PM 0
Share

Woot! Thanks @aras :)

avatar image Ben-BearFish · Jan 03, 2017 at 10:15 PM 0
Share

@Aras Is there a way to achieve this with Unity version prior to 5.4? Like 5.3?

avatar image
2

Answer by valyard · Jun 02, 2015 at 03:27 PM

Currently that is possible, but cumbersome. E.g. in the shader, you can do float myarray[100], and then that becomes individual float material properties (myarray0, myarray1, .., myarray99) that can be set from scripting. So cumbersome + somewhat high overhead of applying them one by one internally.

For example, take this shader code

 Shader "Custom/ArrayTest" {
     Properties {
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
         
         CGPROGRAM
         #pragma surface surf Standard fullforwardshadows
         #pragma target 3.0
 
         float myarray[3];
 
         struct Input {
             float2 uv_MainTex;
         };
 
         void surf (Input IN, inout SurfaceOutputStandard o) {
             o.Albedo = float3(myarray[0], myarray[1], myarray[2]);
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }
 

And a script to test it. Must be placed on the same object.

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
 
     void Awake() {
         var material = GetComponent<Renderer>().sharedMaterial;
         material.SetFloat("myarray0", 1);
         material.SetFloat("myarray1", 1);
         material.SetFloat("myarray2", 0);
     }
 }
 

Hope in the future Unity will add proper arrays support.

Comment
Add comment · Show 2 · 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 RYL123 · May 17, 2017 at 12:51 AM 0
Share

Hi, Why after I SetFloat separately , like material.SetFloat("myarray0", 1);

the value in the shader still zero? However, GetFloat("myarray0") = 1.

avatar image VelocityStrike · May 28, 2017 at 07:47 PM 0
Share

wanna co op to make a game?

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

24 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

Related Questions

GLSL shader - array of uniform const 0 Answers

Setting and storing vertex locations in a shader 0 Answers

Texture2D to Texture3D 2 Answers

I have read up on having arrays in shaders but I just can't get it to work, the shader code is here: 0 Answers

Can I get the a list of the properties in a shader at runtime? 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