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 bgulanowski · Nov 26, 2018 at 05:41 PM · materialtexture2d

Configure standard shader/material with Texture2Darray: how?

Is there a way to use a Texture2DArray with a material using the Standard Shader? If so, how?

I create a new Texture2DArray and then copy sub-textures from a standard Texture2D, using Graphics.CopyTexture. That seems to work.

But when I try to assign the new texture2darray to an existing material, I get the error " Error assigning texarray texture to 2D texture property '_MainTex': Dimensions must match UnityEngine.Material:set_mainTexture(Texture)".

In the code below, "original", "source", and "dimensions" are set in the Unity Editor.

I suppose I will have no choice but to use a custom shader. I found instructions on how to modify the standard shader to accept an index value and use a texture array, so I'll try that. Was just wondering if it's absolutely necessary.

     void Start() {
 
         copy = new Material(original);
 
         if (copy != null) {
             copy.mainTexture = CreateTextureArray();
             GetComponent<MeshRenderer>().material = copy;
         }
     }
 
     Texture2DArray CreateTextureArray() {
 
         int w = source.width / dimensions.x;
         int h = source.height / dimensions.y;
 
         Texture2DArray ta = new Texture2DArray(w, h, dimensions.x * dimensions.y, source.format, true);
         ta.filterMode = FilterMode.Point;
         ta.wrapMode = TextureWrapMode.Repeat;
 
         for (int i = 0; i < dimensions.y; i++) {
             for (int j = 0; j < dimensions.x; j++) {
                 int index = i * dimensions.x + j;
                 int x = j * w;
                 int y = i * h;
                 Graphics.CopyTexture(source, 0, 0, x, y, w, h, ta, x, 0, 0, 0);
             }
         }
 
         return ta;
     }
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
3
Best Answer

Answer by bgulanowski · Nov 26, 2018 at 08:35 PM

Short answer is "It cannot be done."

The Standard Shader expects 2-component (Vector2) UVs. A Texture2DArray requires 3-componnent (Vector3) UVs.

This article details how to modify copies of standard shader files and make them work with Texture2DArray:

https://medium.com/@calebfaith/how-to-use-texture-arrays-in-unity-a830ae04c98b

The conversion is conceptually simple: find wherever the shader accesses the main texture using 2d coordinates, and replace with a 3d coordinate access. Then generate 3d UVs on your models. This probably requires procedurally generated content, but then, that's generally the situation when this requirement comes up.

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

113 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

Related Questions

!texture.texture error 0 Answers

Mesh Render Texture Blurry 1 Answer

assign texture to asset material via script 1 Answer

Get Texture 2D after all shader passes are applied 0 Answers

is there a way to invers multiply a black/white texture 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