Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VoxelGeneratorScript does not show / draw anything #626

Open
Anyeos opened this issue Apr 16, 2024 · 2 comments
Open

VoxelGeneratorScript does not show / draw anything #626

Anyeos opened this issue Apr 16, 2024 · 2 comments
Labels

Comments

@Anyeos
Copy link

Anyeos commented Apr 16, 2024

Describe the bug
VoxelGeneratorScript does not work at all. I tried a lot of things, from using a noise, to only calling set_voxel to calling set_voxel_f and so. There are no way to make it work. It does not take in account at all the data. I can set 1, 0, 100, 10000, 10.0, etc but it does not do anything.

To Reproduce
Use VoxelGeneratorScript.

Example:

func _generate_block(out_buffer, origin_in_voxels, lod):
	out_buffer.fill_f(10.0, VoxelBuffer.CHANNEL_SDF)

func _get_used_channels_mask() -> int:
	return 1 << VoxelBuffer.CHANNEL_SDF

Expected behavior
Show the voxels.

Environment

  • OS: Ubuntu 20.04
  • Vulkan API 1.3.242 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce RTX 3070 Ti
  • Godot 4.2 stable
  • Module version an old one and the last from github (commit 19faf4b)
  • Forward+
@Zylann
Copy link
Owner

Zylann commented Apr 16, 2024

out_buffer.fill_f(10.0, VoxelBuffer.CHANNEL_SDF)

Assuming you are using the Transvoxel mesher, this line fills all space with signed distance 10, which is outside matter, so you will see nothing. In fact, if you set the same value everywhere, you will rarely ever see anything, no matter the value or mesher. With smooth voxels, (Transvoxel) it either means all space is air when > 0, or all space is matter when < 0, so no surface will ever show up. For surfaces to show up, voxels have to vary across 0.

As shown in https://voxel-tools.readthedocs.io/en/latest/smooth_terrain/#signed-distance-fields, to write a smooth voxels generator, you need an understanding of signed distance fields.

For example, a generator producing a sphere of radius 10 centered at the origin, would look like this (untested, just wrote on the fly to illustrate my post):

func _generate_block(out_buffer, origin_in_voxels, lod):
	for rz in out_buffer.get_size().z:
		for rx in out_buffer.get_size().x:
			for ry in out_buffer.get_size().y:
				var pos_world := Vector3(origin_in_voxels) + Vector3(rx, ry, rz)
				var sd := pos_world.distance_to(Vector3(0,0,0)) - 10.0
				out_buffer.set_voxel_f(sd, rx, ry, rz, VoxelBuffer.CHANNEL_SDF)

Or a plane at altitude 5:

func _generate_block(out_buffer, origin_in_voxels, lod):
	for rz in out_buffer.get_size().z:
		for rx in out_buffer.get_size().x:
			for ry in out_buffer.get_size().y:
				var pos_world := Vector3(origin_in_voxels) + Vector3(rx, ry, rz)
				var sd := pos_world.y - 5.0
				out_buffer.set_voxel_f(sd, rx, ry, rz, VoxelBuffer.CHANNEL_SDF)

However if you intented to use blocky voxels, your code would look a bit different.

If things still don't work, there are a number of possible reasons:

  • If you are expecting to see something in the editor, you need your script to have @tool at the top. You also need to enable the property run_stream_in_editor, which is automatically turned off by default because that code will run in multiple threads, and if you modify your script while it still runs, Godot might crash
  • If you are expecting to see something in game, make sure your camera actually look at where stuff is supposed to generate. Sometimes people move things around and the camera ends up looking away.
  • Make sure you have a light and an environment in your scene otherwise the game will look completely grey, which is hard to distinguish anything.
  • Make sure you generate voxels in the right channel according to the mesher you use. If you use blocky but generate SDF, you should populate the TYPE channel with integer values instead.
  • Make sure you have a camera and a VoxelViewer, usually as child of the camera, otherwise the terrain will not know where to load chunks and how far.

@Anyeos
Copy link
Author

Anyeos commented Apr 16, 2024

Yes, sorry, I noted it recently. I was following the C code under the other generators and I missunderstood this. But anyway maybe there can be some notation or doc that explains how is it. Thank you for your explanation that makes me understand it better.

@Anyeos Anyeos changed the title VoxelGeneratorScript does not work VoxelGeneratorScript does not show / draw anything Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants