• 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 JasonBricco · May 14, 2014 at 10:57 PM · c#lightingvoxel

Voxel Engine Lighting

Hey guys,

I've been working on a voxel engine for some time now. And I have all kinds of functionality in it - terrain generation, all kinds of block types, block functionality, and a bunch of unique things.

But one thing I absolutely can't seem to get my mind wrapped around is lighting. And by that I mean I can't even figure it out at the most basic level.

I think I'm stuck with the wrong perspective on this. I don't see how I'd even begin going about lighting. I hear about using vertices, shaders, etc. But how do you actually use vertices and shaders to make a block light up, so that you can begin a flood-fill type system (with the byte representing light, checking neighbors, etc.)?

So basically, I'm not sure where you even start. I'm pretty new with this kind of thing.

I've done a lot of research about this, but I can't get seem to find anything that helps me get going in the right direction on this. I think people assume that people making these engines know a lot about shaders, procedural mesh building, etc. And maybe that's true... but I sure don't! I'm trying to learn about all of that through making this game.

Thanks in advance if you can point me in the right direction.

PS: I'm not looking for detailed code giving an implementation, exactly. I'm just looking for some information on what I can do. I mean, am I suppose to use vertices to do this, and if so, how?

Am I supposed to use a shader? Say I have a shader, how do I apply it to blocks in a voxel engine in a way to make this work?

Am I supposed to have materials? Then I can use shaders. But how do I use materials? Do I apply one to each face of the cube? And still, how would lighting be done with this system?

As you can see, I'm rather confused.

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

Answer by MakeCodeNow · May 15, 2014 at 01:16 AM

If you don't care about shadows, you can use the regular built in Unity lights, either vertex or pixel based. If you do care about shadows, then you either need to do raycasting or a lot of shader fanciness. Raycasting is almost certainly easier. The basic idea is when a light changes (is placed, moves, etc) then cast a ray to each voxel and see if it hits anything. If it doesn't then compute the brightness based on distance attenuation from the light. If it hits something, then it's in shadow. Keep track of all voxels affected by a light so that you can undo the light effect when it moves or is deleted. This approach works for the global sunlight, too, just cast from the voxel center in the direction of the sun for a certain "long enough" distance. If it hits anything, it's shadowed.

Comment
Add comment · Show 5 · 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 JasonBricco · May 15, 2014 at 01:24 AM 0
Share

Thanks. But what I still don't understand is how to make the lighting happen to begin with. That is, assu$$anonymous$$g I have already figured out all the blocks and the light value for those lights, how do I make them light up? How do I make the brightness happen? I have literally done no work with things like this before.

I'm guessing this is some really simple thing. But for some reason, I can't seem to get my $$anonymous$$d wrapped around it.

avatar image Kiloblargh · May 15, 2014 at 03:41 AM 1
Share

You just need to find a shader that uses vertex colors, and any voxel-based lighting you're doing in addition to the built-in directional lights will need to work by setting the mesh vertex colors.

avatar image JasonBricco · May 15, 2014 at 03:52 AM 0
Share

Okay, that's helpful. One thing still confusing me is: right now, the whole chunk of 16x16x16 blocks is a single mesh. You go through making each cube face with vertices, triangles, UVs and then that makes the whole mesh.

So there's a way to apply these vertex colors to each cube face that makes up the larger mesh?

As I said, I'm very new to this area of program$$anonymous$$g.

avatar image MakeCodeNow · May 15, 2014 at 04:51 AM 1
Share

If there are multiple blocks in a single mesh, then vertex colors are the way to go. You can set vertex colors just like positions, normals, etc. and you can read them similarly, too. There are plenty of examples of shaders that read vertex colors and combine them with a base texture.

avatar image Kiloblargh · May 15, 2014 at 05:02 AM 0
Share

When you create the mesh, you can also create a List.< int > [,,] that stores a list of the indices in the merged mesh.vertices of all the verts beloning each [x,y,z] voxel.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a light turn on and off with the same button 1 Answer

No shadows on directional and keep on getting this error 1 Answer

Image Effect lighting 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