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

Feat: Ability to view resulting model layer by layer #103

Closed
mduft opened this issue Jan 25, 2023 · 13 comments
Closed

Feat: Ability to view resulting model layer by layer #103

mduft opened this issue Jan 25, 2023 · 13 comments
Labels
feature New feature or request

Comments

@mduft
Copy link

mduft commented Jan 25, 2023

I'm building all of my models in survival, most of the time bottom up. Its super helpful in some more complex models to be able to view them layer by layer, much like mineprints.net for example supports.

@mduft
Copy link
Author

mduft commented Jan 25, 2023

My naive take would be to limit the resulting mesh to a certain height, settable through a slider. If you could give me one or two hints on where to start looking, I would try to do this :)

@LucasDower LucasDower added the feature New feature or request label Jan 25, 2023
@LucasDower
Copy link
Owner

Firstly you'll need to pass a new uniform variable to the voxel mesh shader called something like u_sliceHeight, this would be in Renderer._drawBlockMesh. Then you'll need to modify the vertex shader file block_vertex.vert to check if the vertex position height is above u_sliceHeight and if so discard the vertex (or set it's position to (0, 0, 0)).

That leaves actually creating a UI element for a slider that sets the u_sliceHeight, there's already a SliderUIElement that could be tweaked and placed on the viewpoint toolbar somewhere.

Hope this helps.

@mduft
Copy link
Author

mduft commented Jan 25, 2023

Thanks :) Will check and go on an adventure :D

@mduft
Copy link
Author

mduft commented Jan 25, 2023

OK. Making some progress, but I need a little input if possible please :)

I am at a point where the model is cut off X blocks above the grid (the last blocks are cut in half and miss the closing surface, but I hope that changes when my question is answered :D). However my model does not start at y=0. How would I be able to find the lowest block's starting Y coordinate? I need that offset so I can add the number of blocks I want to show in height.

I added this to the shader:

if(u_sliceHeight > 0.0 && (position.y + u_gridOffset.y) > u_sliceHeight)
    {
        v_sliced = 1.0;
    } else {
        v_sliced = 0.0;
    }

where u_sliceHeight would be a value above 0.0 in amount of blocks I want to show in height. the fragment shader discards if v_sliced > 0.5. Setting the position only to 0,0,0 will result in ugly rendering artifacts.

@mduft
Copy link
Author

mduft commented Jan 25, 2023

That results currently in something like this (for sliceHeight=3):

image
image

The +/- buttons I added are currently simply incrementing, decrementing sliceHeight on the shader. That would need to change obviously :D

@LucasDower
Copy link
Owner

How would I be able to find the lowest block's starting Y coordinate?

The block mesh is to the renderer in chunks in Renderer.useBlockMeshChunk, it's passed a RenderNextBlockMeshChunkParams.Output parameter which contains a dimensions member variable which is the dimensions of the whole mesh. You can then cache this value somewhere and use it where you need to. An important thing to note is that if dimensions.y is 35, for example, it means the block mesh is 36 blocks tall.

Hope this helps.

@LucasDower
Copy link
Owner

As for the other problem what you can do is subtract half of the normal from the vertex to get the actual block position instead of the vertex position.

The problem is that if a block is at (0.0, 30.0, 0.0), the vertices are (-0.5, 29.5, -0.5), (0.5, 29.5, -0.5), (0.5, 29.5, 0.5), (-0.5, 29.5, 0.5), (-0.5, 30.5, -0.5), (0.5, 30.5, -0.5), (0.5, 30.5, 0.5), (-0.5, 30.5, 0.5). (That's before they're multiplied by u_voxelSize). So if your u_sliceHeight is 30, then the bottom vertices will be drawn but the top vertices will be discarded. When you subtract half of each vertex's normal from its position the vertices become (0.0, 30.0, 0.0), (0.0, 30.0, 0.0), (0.0, 30.0, 0.0), (0.0, 30.0, 0.0), (0.0, 30.0, 0.0), (0.0, 30.0, 0.0). Which should then allow you to treat all vertices of the same block in the same manner.

Hope this helps.

@LucasDower
Copy link
Owner

There will most definitely be floating-point precision issues, however, so you'll need to incorperate some nearly equals when checking the height of the normal-corrected vertex as.

@mduft
Copy link
Author

mduft commented Jan 26, 2023

My initial take on this is at mduft@b5328d0 - however I'm unable to cope with the half-block-rendering issue xD I'm completely lost on where to correct the vertex positions, and how to deal with that in the shader. Maybe what I have so far is of use to someone...

@LucasDower
Copy link
Owner

No problem, I can take a look and fix up the rest and get it merged in sometime soon! Thanks for getting this in a good position 😁

@mduft
Copy link
Author

mduft commented Jan 26, 2023

Heyaaa, that sounds incredible :D It will be easier than ever to build things in minecraft :D Thanks a ton for all the advice (even though that resulted in less lines of code than advice written by you xD), and for having a look at it. Feel free to pick the commit and/or tell me if I should open a PR with the unfinished changes.

@LucasDower
Copy link
Owner

Opened PR #104, adding to new /0.8-slice-viewer branch.

@LucasDower
Copy link
Owner

Closed with 901e83e.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants