Jump to content
Josh

Voxel Code

Recommended Posts

I've decided not to continue my work on voxel global illumination. I was pretty far along but other better techniques are now available (raytracing).

This library can be used to turn mesh data into voxels.

voxelizer.zip

I was using a geometry shader to visualize the data:

voxel.geom

Here is my code for using it:

	std::vector<float> Mesh::BuildVoxels(const Vec3& scale, const float voxelsize, AABB& bounds)
	{
		std::vector<float> cloud;
		
		vx_mesh_t mesh;
		mesh.nvertices = CountVertices();

		if (mesh.nvertices == 0) return cloud;

		std::vector<float> positions;
		positions.resize(CountVertices() * 3);
		//std::vector<float> normals;
		//normals.resize(CountVertices() * 3);
		Vec3 pos;

		for (int v = 0; v < CountVertices(); ++v)
		{
			pos = GetVertexPosition(v) * scale + voxelsize * 0.5f;// add this to align voxels to grid
			positions[v * 3 + 0] = pos[0];
			positions[v * 3 + 1] = pos[1];
			positions[v * 3 + 2] = pos[2];
			//pos = GetVertexNormal(v);
			//normals[v * 3 + 0] = pos[0];
			//normals[v * 3 + 1] = pos[1];
			//normals[v * 3 + 2] = pos[2];
		}

		mesh.vertices = (vx_vertex*)&positions[0];
		std::vector<unsigned int> indices;
		
		//for (int t = 0; t < CountTriangles(); ++t)
		//{
		//	int a = GetTriangleVertex(t, 0);
		//	int b = GetTriangleVertex(t, 1);
		//	int c = GetTriangleVertex(t, 2);
		//	if (bounds.IntersectsPoint(GetVertexPosition(a)) or bounds.IntersectsPoint(GetVertexPosition(b)) or bounds.IntersectsPoint(GetVertexPosition(c)))
		//	{
		//		indices.push_back(a);
		//		indices.push_back(b);
		//		indices.push_back(c);
		//	}
		//}

		indices.reserve(CountIndices());

		//indices.resize(CountIndices());
		for (int t = 0; t < CountTriangles(); ++t)
		{
			int a = GetTriangleVertex(t, 0);
			int b = GetTriangleVertex(t, 1);
			int c = GetTriangleVertex(t, 2);
			Vec3 v0 = GetVertexPosition(a);
			Vec3 v1 = GetVertexPosition(b);
			Vec3 v2 = GetVertexPosition(c);
			AABB aabb = AABB(v0, v0);
			aabb.min = min(aabb.min, v1);
			aabb.max = max(aabb.max, v1);
			aabb.min = min(aabb.min, v2);
			aabb.max = max(aabb.max, v2);
			aabb.Update();
			//if (aabb.IntersectsAABB(bounds))
			//{
				indices.push_back(a);
				indices.push_back(b);
				indices.push_back(c);
			//}
			//indices[v] = GetIndiceVertex(v);
		}

		mesh.nindices = indices.size();
		mesh.indices = &indices[0];
		//mesh.normals = (vx_vec3_t*)&normals[0];
		//mesh.nnormals = CountVertices();
		//mesh.normalindices = mesh.indices;
		mesh.normalindices = nullptr;
		mesh.colors = nullptr;
		
		auto pointcloud = vx_voxelize_pc(&mesh, voxelsize, voxelsize, voxelsize, voxelsize * 0.01f);
		cloud.resize(pointcloud->nvertices*3);
		for (int v = 0; v < pointcloud->nvertices; ++v)
		{
			cloud.at(v * 3 + 0) = pointcloud->vertices[v].x - voxelsize * 0.5f;
			cloud.at(v * 3 + 1) = pointcloud->vertices[v].y - voxelsize * 0.5f;
			cloud.at(v * 3 + 2) = pointcloud->vertices[v].z - voxelsize * 0.5f;
		}
		vx_point_cloud_free(pointcloud);
		
		return cloud;
	}

 

  • Like 2

Share this post


Link to post

was looking interesting this, be careful of ray tracing as it take a lot gpu power and even thought the new top end card are starting to support think it be another few gen before it main stream  

Share this post


Link to post
13 hours ago, carlb said:

was looking interesting this, be careful of ray tracing as it take a lot gpu power and even thought the new top end card are starting to support think it be another few gen before it main stream  

We will see. One thing that is pushing me towards raytracing is my experience with Vulkan has shown me that branded technology always attracts more interest. Everything else asise, a technology with a logo someone else spent a lot of money to promote will attract more interest than something I invent myself, like our vegetation system, which I feel like is totally revolutionary but not properly appreciated.

In fact, our whole marketing message is going to switch from "I figured out a super efficient way to make graphics faster" to "I am using Vulkan to make graphics fast and I guess other devs are too stupid to use it right lol" even though Vulkan was a small part compared to what I implemented first in OpenGL. It's just more digestable if what I am telling people conforms to their previous expectations. I realized this when I went to GDC and when I talked about performance the first response was a big grin and the word "Vulkan?"

 

  • Like 2

Share this post


Link to post
8 hours ago, Josh said:

vegetation system, which I feel like is totally revolutionary but not properly appreciated

The problem might be that the benefits are not seen but the limitations are.  Not just between two possible version of Leadwerks but between Leadwerks and other engines.  The natural question that comes up is "other engines let me put trees wherever I want, why doesn't Leadwerks?"

Also, at the risk of further derailing this topic, why can't we place a tree wherever we want?  If I set the density to 1 and make a forest as thick as possible, I'm then able to remove trees around any single tree, which makes it seem like you could represent individual trees this way.  Right now, if I click the mouse once with this density, I'll usually get at least two or three trees in one place.  With a higher number density, I may get only one but likely not where I clicked.  Just curious.

trees.thumb.jpg.e667b5efc4d2e99e848b32b8ad7816a7.jpg

 

Share this post


Link to post

Trees are not stored in memory at all. They are totally dynamic, so you can have millions, and they are extremely fast.

Share this post


Link to post

If there was a big marketing campaign for the same exact thing everyone would be singing the praises of Nvidia TreeTek(R)

Share this post


Link to post
28 minutes ago, Josh said:

Trees are not stored in memory at all. They are totally dynamic, so you can have millions, and they are extremely fast.

Again, with a density of 1 and a brush size of 1 you can almost put a single tree exactly where you want.  Imagine if you could have an even smaller density and a smaller brush (to avoid placing multiple trees at once).  It doesn't have to be pixel perfect but it seems like it could be close enough.

Share this post


Link to post
4 minutes ago, gamecreator said:

Again, with a density of 1 and a brush size of 1 you can almost put a single tree exactly where you want.  Imagine if you could have an even smaller density and a smaller brush (to avoid placing multiple trees at once).  It doesn't have to be pixel perfect but it seems like it could be close enough.

The way the routine works is it distributes a grid of instances across the scene with a random offset. There is no "exact" placement of one instance because there is no position stored, just a grid with a sort of random offset within the space the density value allows.

When you click with the vegetation tool, you aren't saying "place one instance here" you are saying "this is an area in which trees are allowed to grow".

I got the idea when I walked by an orchard and saw the trees spaced exactly in rows, and then noticed that in nature the same thing happens, although not as precisely, for the same reason...to maximize the sunlight and nutrients each tree gets.

  • Like 1

Share this post


Link to post

Yeah, the vegetation system is actually amazing. Even comparing to the "other engine". The most popular one. Yours is so much better.

 

Sad to see voxel lighting go. I had really high hopes for this. I guess raytracing will be super expensive and nobody is going to use it.

Share this post


Link to post
8 minutes ago, Genebris said:

Yeah, the vegetation system is actually amazing. Even comparing to the "other engine". The most popular one. Yours is so much better.

 

Sad to see voxel lighting go. I had really high hopes for this. I guess raytracing will be super expensive and nobody is going to use it.

Well, let's see how Quake 2 looks when Nvidia releases the raytraced version in a few days. The stuff I am seeing seems like it gives much better results than voxel approaches. Voxels are pretty good for blurry GI but the reflections raytracing provides are so much better. If I feel enough faith that raytracing is actually going to catch on and work well then it is probably better to work on that.

Voxels have limitations. They don't really work with animated models. You have to figure out a really hacky way to account for the texture color at each voxel. They aren't high-resolution enough for good reflections. And they can only extend in front of the camera for a limited distance, so vast landscapes don't get any enhancement. And there is a delay while the voxel data is updated, so you have GI that sort of fades smoothly as the scene changes.

So there's this approach that will still take a lot of work and will probably never produce results people are totally happy with, and then there is another approach that produces perfect results, has a nice marketing campaign already in place, and requires Vulkan, so now we are part of a much smaller group of companies that can even access it. And it meshes well with some of the CAD / aerospace stuff we do as well.

Based on this there's enough there for me to want to experiment with the technology and see if it is appropriate. That's as much as I know right now.

Share this post


Link to post
27 minutes ago, Josh said:

When you click with the vegetation tool, you aren't saying "place one instance here" you are saying "this is an area in which trees are allowed to grow".

I understand that it's not an exact science but the lower the editor density value, the higher the chance that there will be a tree there, no?  At some point it's close enough to 100% that it doesn't matter.

Share this post


Link to post
3 minutes ago, gamecreator said:

I understand that it's not an exact science but the lower the editor density value, the higher the chance that there will be a tree there, no?  At some point it's close enough to 100% that it doesn't matter.

Yeah, but then trees would be growing inside each other if you used a bigger brush. The resolution of the mask texture would also be way too high, because it uses one pixel per grid space (defined by the density size). It's probably best to just place a regular model in this situation.

In the future it could be a good idea to have a point-and-click-to-create-a-model-here mode for when you want to do stuff like this. But that is using the regular entity system.

  • Like 1

Share this post


Link to post
27 minutes ago, Josh said:

In the future it could be a good idea to have a point-and-click-to-create-a-model-here mode for when you want to do stuff like this. But that is using the regular entity system.

This would be nice.

  • Like 1

Share this post


Link to post

The only limitation I found with the vegetation tool is when using shaped entities with collision and then it is almost impossible to let fall a rain with collision detection (for the splash effect ^^) because FPS are going enorm down.

Whereas using shaped models without the vegetation tool + rain was relativ ok

And what is a forest without rain 

 

Share this post


Link to post
On ‎6‎/‎4‎/‎2019 at 7:33 PM, gamecreator said:

This would be nice.

Question: What is the difference between this and the existing drag-and-drop method?

Share this post


Link to post
5 hours ago, Josh said:

Question: What is the difference between this and the existing drag-and-drop method?

It's faster, especially if you have to put a lot of individual trees/rocks/crates whatever down.  😀  Not having to keep going out of scene and into the right panel and back again when you just want to work with the same "brush" is valuable.  You can just focus on your scene.

The tricky thing here might be that you'll want these individual models to act like vegetation too, in that they have a billboard and they fade in/out.  It will look bad if some trees fade in while others pop in around them.

But it would be nice if this option was available to all models too.  Maybe both in the vegetation tool section and the model section you can hold down a button (Alt?) and left-click to instantly place a single model.  Or use the middle mouse button.

Share this post


Link to post

Okay, that's kind of what I thought but I wanted to get your perspective.

Share this post


Link to post

As a related example, this is what it looks like right now when I want to place a player start point pivot.  How much simpler would this be if you can just place a pivot on the ground like you do with vegetation?

 

Share this post


Link to post

I have zero doubt that in a few years we will all be using real-time raytracing all the time, and it will be completely taken for granted. All the weird hacks programmers use for indirect lighting will go in the trash heap of history. Just like all the weird shadow hacks that came before shadow mapping replaced everything.

I don't think Quake 2 is a very good showcase of this technology, but there are some moments in this video you see things that you've never seen in any game.

 

Share this post


Link to post

The resolution issues of shadowmaps goes away. Check out the dark area under the stairs. This is great.

20190608144550_1.thumb.jpg.848e907a803a1ca6825a6e835a363c47.jpg

 

 

Share this post


Link to post

be a while before cards are cheap to run that at the speed you need at the moment you talking of over 1 thousand pound for the rtx cards

when they down to 300 pounds then it ready for games to have this feature but again this is nvidia only not AMD or intel.

 

Share this post


Link to post

@Josh - Will the ray tracing implementation at least be vendor agnostic? I do not have an Nvidia card and I am more than likely to buy an AMD Card that supports ray tracing. :)

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...