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 rayben · Jun 16, 2015 at 09:53 PM · shadertexturematerialprocedural

How to avoid Texture.Apply()

Hello!

I'm trying to create a dynamic background by painting a texture, pixel by pixel, at runtime in the update function. If I understand it correctly I have to call apply() after having SetPixels32() for Unity to actually pass along the new texture data to the GPU. This, it seems, is a costly process.

I thought to change the texture that the shader uses directly by setting the pixels of the MainTexture of the Material instead, hoping that the shader would pick up the change without having to call Apply(). No luck. I think I've misunderstood how shaders, materials and textures work together. I've looked into shader tutorials but I still don't understand how I can pass along texture data directly to the GPU, even with custom shaders.

So for my question: is it possible to send texture data directly to the GPU using Unity? If so, how do I do that?

This is what I have:

 public class SkyCreator : MonoBehaviour {
 
     private Texture2D _texture;
     private MeshRenderer _renderer;
 
     private int _screenWidth;
     private int _screenHeight;
     private float _pixelsPerUnit;
     private Color32[] _pixels;
 
     private Vector3 _lastPos;
 
 
     // Use this for initialization
     void Start () {
         _screenWidth = Camera.main.pixelWidth;
         _screenHeight = Camera.main.pixelHeight;
         _pixels = new Color32[_screenWidth*_screenHeight];
 
         _renderer = GetComponent<MeshRenderer>();
         _texture = new Texture2D(_screenWidth,_screenHeight,TextureFormat.ARGB32, false);
         for(int y = 0; y < _screenHeight; y++){
     for(int x = 0; x < _screenWidth; x++){
             _pixels[y*_screenWidth + x] = new Color(Random.Range(0f,1f),Random.Range(0f,1f),Random.Range(0f,1f));
         }
     }
     _texture.SetPixels32(_pixels);
     _texture.Apply();
         _renderer.material.mainTexture = _texture;
 
 
         //Create the mesh
         var mesh = new Mesh();
         GetComponent<MeshFilter>().mesh = mesh;
         mesh.vertices = new Vector3[]{
             new Vector3(0,0,0),
             new Vector3(1,0,0),
             new Vector3(1,-1,0),
             new Vector3(0,-1,0)
 
         };
 
         mesh.triangles = new int[]{
             0,1,2,0,2,3
         };
 
         mesh.uv = new Vector2[]{
             new Vector2(0,0),
             new Vector2(1,0),
             new Vector2(1,1),
             new Vector2(0,1)
         };
 
         mesh.Optimize();
         mesh.RecalculateNormals();
     }

}

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

Answer by Dave-Carlile · Jun 16, 2015 at 09:54 PM

You pass (updated) texture data long to the GPU by calling Texture.Apply.

As the documentation states, and you discovered, it can be an expensive operation.

What are you modifying in the texture? If it's a dynamic sky texture your best bet is going to be a shader that does all of the changes on the GPU.

Comment
Add comment · Show 1 · 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 rayben · Jun 16, 2015 at 10:16 PM 0
Share

Oh, I see! So the actual "drawing logic" needs to go into the shader. And yes, it's a dynamic sky. I was actually hoping to be able to make something a bit more general so that I could use it for different things. But I think I understand now. Thanks.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

is there a way to make a gradient material, white to black, in Unity3D? 2 Answers

How to apply different textures to one model? 1 Answer

Assign Texture To Material Unity 4.6 1 Answer

Multiple materials on same object 0 Answers

Carpet in Shadergraph? 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