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
1
Question by warrenwhipple · Feb 08, 2015 at 02:23 AM · 2dshadermeshgridpixel

How to efficiently render a 2D grid?

I'm rendering the output of a 2D cellular simulation as solid colored squares. Cell colors change every frame. Vertex positions are static. My first attempt uses a Mesh and a flat color vertex shader. It works. But I'm new to Unity and graphics coding, so... Are there obvious optimizations I'm missing?

Can/should I use a ParticleSystem? A stretched 2DTexture redered with SetPixels()? Something else?

This is for Android/iOS, so newer GPU features may not be available.

Also, the simulation relies on cell update order, so (I think) not parallelizable enough for a clever simulate-on-the-shader solution as suggested here. http://answers.unity3d.com/questions/16870/how-can-i-draw-an-array-of-data-640x480-very-fast-.html


my naive mesh handler

 public class PixelGrid : MonoBehaviour {
     Mesh mesh;
 
     // called once for setup
     public void setGridSpace (int columnCount, int rowCount) {
         Vector3[] verticies = new Vector3[4 * columnCount * rowCount];
         int[] triangles = new int[6 * columnCount * rowCount];
         for (int j=0; j<rowCount; j++) {
             for (int i=0; i<columnCount; i++) {
                 int n = i + j * columnCount;
                 verticies [4 * n] = new Vector3 (i, j);
                 verticies [4 * n + 1] = new Vector3 (i, j + 1);
                 verticies [4 * n + 2] = new Vector3 (i + 1, j);
                 verticies [4 * n + 3] = new Vector3 (i + 1, j + 1);
                 triangles [6 * n] = 4 * n;
                 triangles [6 * n + 1] = 4 * n + 1;
                 triangles [6 * n + 2] = 4 * n + 2;
                 triangles [6 * n + 3] = 4 * n + 2;
                 triangles [6 * n + 4] = 4 * n + 1;
                 triangles [6 * n + 5] = 4 * n + 3;
             }
         }
         mesh = gameObject.GetComponent<MeshFilter> ().mesh;
         mesh.Clear ();
         mesh.vertices = verticies;
         mesh.triangles = triangles;
         mesh.Optimize ();
     }
 
     // called every frame
     public void setCellColors32 (Color32[] cellColors32) {
         Color32[] vertexColors32 = new Color32[4 * cellColors32.Length];
         for (int i = 0; i < cellColors32.Length; i++) {
             vertexColors32 [4 * i] = cellColors32 [i];
             vertexColors32 [4 * i + 1] = cellColors32 [i];
             vertexColors32 [4 * i + 2] = cellColors32 [i];
             vertexColors32 [4 * i + 3] = cellColors32 [i];
         }
         mesh.colors32 = vertexColors32;
     }
 }


my naive flat color vertex shader

 Shader "Flat Vertex Color" {
     Subshader {
         Cull Off
         ZWrite Off
         Fog { Mode Off }
         AlphaTest Off
         Blend Off
         Pass {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             struct vertexInput {
                 float4 vertex : POSITION;
                 float4 color : COLOR;
             };
             struct vertexOutput {
                 float4 pos : SV_POSITION;
                 float4 color: COLOR;
             };
             vertexOutput vert(vertexInput v) {
                 vertexOutput o;
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.color = v.color;
                 return o;
             }
             fixed4 frag(vertexOutput i) : COLOR {
                 return i.color;
             }
             ENDCG
         }
     }
 }
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

Answer by VesuvianPrime · Feb 08, 2015 at 02:24 AM

I'd try using GL to draw coloured quads.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Shader vs Sprites - Sorting layer (ZWrite Off not working) 1 Answer

How to apply a gradient to a 2D mesh? 0 Answers

Any way to access SpriteRenderer mesh UV2? 0 Answers

Problem with coloring mesh with shaders properly 1 Answer

Edge outline & 2D shadow Shader for planar mesh 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