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 RobertIvaldi · Dec 27, 2013 at 11:04 PM · texturemeshcolorvoxeltriangles

Render voxels with RGB data passed to them

I followed along with a guide on creating a nice voxel terrain generator, but it's flexibility is very slim. I want to be able to pass RGB color data so when the face is rendered, it has a solid color texture. The original code would have required me to have a single texture sheet with all possible textures I wanted; I don't see how this would work with every possible RGB color.

This function draws vertices for a single face (of 6), then calls Cube method:

 void CubeBottom (int x, int y, int z) {
     newVertices.Add(new Vector3 (x,  y-1,  z ));
     newVertices.Add(new Vector3 (x + 1, y-1,  z ));
     newVertices.Add(new Vector3 (x + 1, y-1,  z + 1));
     newVertices.Add(new Vector3 (x,  y-1,  z + 1));
     Cube(x, y, z);
 }


Then the Cube method, where the old texture code was:

 void Cube (int x,int y,int z) {
     newTriangles.Add(faceCount * 4  ); //1
     newTriangles.Add(faceCount * 4 + 1 ); //2
     newTriangles.Add(faceCount * 4 + 2 ); //3
     newTriangles.Add(faceCount * 4  ); //1
     newTriangles.Add(faceCount * 4 + 2 ); //3
     newTriangles.Add(faceCount * 4 + 3 ); //4

     //Vector2 texturePos = new Vector2(0,0); //Old code, switch to RGB

     //newUV.Add(new Vector2 (tUnit * texturePos.x + tUnit, tUnit * texturePos.y));
     //newUV.Add(new Vector2 (tUnit * texturePos.x + tUnit, tUnit * texturePos.y + tUnit));
     //newUV.Add(new Vector2 (tUnit * texturePos.x, tUnit * texturePos.y + tUnit));
     //newUV.Add(new Vector2 (tUnit * texturePos.x, tUnit * texturePos.y));
     
     faceCount++;
 }

At the moment it only generates 1 cube at x,y,z = 0,0,0 which of course is magenta and texture-less.

How and where would I implement the ability to pass RGB data and apply it to each cube/voxel? This script is kept in an empty gameObject with a Mesh Renderer, Mesh Filter, and Mesh Collider.

More simply, how would I draw a [red] cube with Color (255,0,0) purely from code? I could work from that.

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

Answer by Statement · Dec 27, 2013 at 11:09 PM

You need to set mesh.colors.

If you design your code to have a SetColor(1f, 0f, 0f); then you could just store that color as a member variable. Whenever your builder add vertices (given they are NOT shared among triangles) just add an equal amount of colors to a Color list with the most recently set Color via SetColor. When you finally construct the mesh, just populate mesh.colors just as you populate mesh.vertices and mesh.uv.

You will also require a shader that uses the vertex color for it to have any effect.

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 RobertIvaldi · Dec 28, 2013 at 12:33 AM 0
Share

I went into your answer not knowing a thing about how shaders work. I grabbed a VertexShader that took pos and color; then simply added a color whenever a vertex was created. At the end I populated the list and to my amazement it all worked out! Thank you so much! $$anonymous$$y only issue is that I oversaw that, since this is vertex lit, it will not cast shadows. Is there a way to add shadow functionality to a vertex shader..? (I am not very knowledgeable about custom shaders)

avatar image Statement · Dec 28, 2013 at 03:54 AM 0
Share

A simple way is to write (or modify) a surface shader. If you don't know how to write shaders and if you have no desire to learn it, check the asset store for visual shader tools where you can just drag things together to compose shaders, kismet style (I guess - haven't used it but seem popular). If you want to learn shaders, I guess apart from learning CG shading language you should also study ShaderLab which is Unitys wrapper into shaders. You can also google for existing shaders - a shader that adds color to a textured, lit, shadowed, shadowcasting cube is very simple to make and I would be truly amazed if there isn't one out there. Check the Unify commmunity wiki for stuff, too.

http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaders.html

http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html

http://wiki.unity3d.com/index.php/Shaders

http://en.wikibooks.org/wiki/Cg_Program$$anonymous$$g/Unity

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

19 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

Related Questions

Change Color of model from Down to up! 1 Answer

Colour cubes after CombineMesh() 0 Answers

How to make colored voxels?!? 1 Answer

can anybody help with script that takes webcam texture pixel information to deform mesh vertices? 0 Answers

Save a procedurally generated mesh and load it after? 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