- Home /
Using Shaders in Voxel Engines (With Procedural Meshes)
Hi all,
I've been making my own unique twist on a voxel game now. And one thing that's confusing me is how I would go about using shaders.
The thing is, I have little experience with shaders. And I'm going to need to use shaders for all kinds of things (I assume lighting being one of them).
I use Shader Forge to make shaders. And I know in Unity you make a material and assign a shader to the material. And that's about my knowledge right there.
Let's say I'm not using Unity's material system, and I'm generating meshes procedurally as is done in a voxel engine (where I define vertices, triangles, UVs). Now how on Earth would I go about using shaders on the blocks?
I read somewhere that it relates to UV mapping, or something of the sort. But I'm honestly clueless. How would I use UV mapping to work with a shader?
So what I'm asking for here is for you to give me a direction to go in. Even if you don't give a detailed explanation, perhaps you could link me to something that will help me learn about this?
Thanks in advance, it's much appreciated.
-Jason
PS: I have searched a lot for this answer, and I can't seem to find much that answers the actual question I have. But if you know of an answer I missed, do let me know.
Are you using the mesh class to make your blocks? If so, you should have the mesh renderer on your object, therefore, you can edit the mesh renderer's material's shader.
Example:
//Then equals your shader.
renderer.material.shader
I didn't know that, actually. However, the Game Object represents a chunk of many blocks. I need a shader applied to a specific block in the chunk, not to the whole chunk. So how could I go about doing that?
Answer by Cherno · Apr 30, 2014 at 05:28 AM
There isn't anything to it, as long as you have a meshrenderer you can use any materials you want, no matter if the mesh is pre-fabricated or procedurally generated at runtime.
After generating your vertices, triangles, and uvs, you just pass the materials to the renderer:
renderer.material = material;//if using a single mesh with one material
renderer.materials = materialArray;//if using submeshes
If you want to use multiple materials, you need to have at least as many materials as there are submeshes. I suggest creating only as many submeshes as you need different materials.
check this thread, especially the linked page and following:
Doesn't 'renderer' specify the overall mesh of the Game Object (which would be the entire chunk of blocks, not a specific block)? I'm not entirely sure how I would get the shader to be used for a specific block.
The thread Cherno provided has a huge wealth of information regarding voxel games in Unity.
I assume you have a cubic voxel mesh, and that block refers to a discrete world unit. And if you're trying to assign a material per block, DON'T. That is massively ineffficent.
'$$anonymous$$inecraft' style graphics on a voxel mesh, i.e. one face of one cube is one texture, is achieved with uv offsets of a lookup texture in one material which is applied to the entire mesh / mesh chunk.
I'm not entirely sure how I would get the shader to be used for a specific block.
You will have to store the texture information youself, for example the texture could derive directly from the fill data of the cubic space... Or you create a custom class that holds information about each cube face...
I have a system currently that builds meshes face by face and I store information about textures in classes, so I can figure out which texture to put on the face. I get that.
What I don't get is how I use shaders. I haven't done anything with shaders yet. I don't even get how you do vertex lighting, though I may be able to figure that out... like I said, probably stuck in the wrong perspective.
I looked at the $$anonymous$$inecraft topic on the forums, and I mostly only saw people telling the amazing features they were able to add, and not that much about how to actually do things.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143912im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to get a plane to blend in with intersections,How to get blend a edge of a plane to wall 0 Answers
How to make a voxel terrain generate all around the start point 1 Answer
How to set GameObject inactive without messing up GPU instancing? 0 Answers