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

How to define new objects in NOA engine. #194

Open
officialblueify opened this issue Jun 16, 2023 · 6 comments
Open

How to define new objects in NOA engine. #194

officialblueify opened this issue Jun 16, 2023 · 6 comments

Comments

@officialblueify
Copy link

officialblueify commented Jun 16, 2023

I have this, https://mineblox3.blueify.repl.co, and I'm using the stress test generation but I'd like to stop the water from going under the dirt, and I want to prevent trees from spawning in the water. How could I go about this?

// worldgen
var decideVoxel = (x, y, z, ht, clo, chi, pillar) => {
    if (y < ht) {
        if (y < -1.8) return water
        if (y < 1.2) return grass
        return (ht - y < 1) ? grass : grass
    }
    if (y < ht + pillar) return 
    return 0
}
noa.world.on('worldDataNeeded', (requestID, data, cx, cy, cz) => {
    // fast cases when very high/low
    if (cy > 40 || cy < -20) {
        var monoID = (cy > 0) ? 0 : water
        return noa.world.setChunkData(requestID, data, null, monoID)
    }

    for (var i = 0; i < data.shape[0]; i++) {
        var x = cx + i
        for (var k = 0; k < data.shape[2]; k++) {
            var z = cz + k
            var ht = Math.sqrt(x * x + z * z) / 100
            var a = noise(x, 150)
            var b = noise(z + 50, 140)
            var c = noise(x - z - 50, 120)
            ht += 2 * a + b + c
            var pillar = (Math.random() < 0.002) ? 5 : 0
            var clo = 39 + 2 * (b - c)
            var chi = 35 + 2 * (a - b)
            for (var j = 0; j < data.shape[1]; j++) {
                var y = cy + j
                var id = decideVoxel(x, y, z, ht, clo, chi, pillar)
                data.set(i, j, k, id)
            }
        }
    }
    // tell noa the chunk's terrain data is now set
    noa.world.setChunkData(requestID, data)
})
@fenomas
Copy link
Owner

fenomas commented Jun 17, 2023

Hi, so this voxel engine doesn't impose any particular rules on what voxels can go where - so the task of making "plausible" voxel worlds, like where trees don't grow out of water, is entirely up to the game logic above the engine.

One common approach is to pick a y value for the world's water level, then have a general height-map function that defines a value for terrain height everywhere in the world, and then assign land/water/tree blocks depending on whether each voxel is above or below the terrain or water heights. That's what the stress-test code above is doing, so for example if you wanted to not have "pillars" spawn on top of water, you'd want to skip that bit (or set the pillar height to 0) wherever the terrain height ht is below the water line (which here is 0).

But it's a big topic and there are lots of possible ways to go - you might like to check out r/procedural/ or similar communities for ideas.

@officialblueify
Copy link
Author

Thanks for that. How would I go about making trees?

@fenomas
Copy link
Owner

fenomas commented Jul 12, 2023

Making large structures is a bit thorny, because if a tree is at the edge of a chunk then parts of it will need to be generated at different times. In my own content I went through several iterations. One relatively easy approach is to just only generate trees on the "internal" parts of a chunk, not at the edges. If you don't have very many trees this winds up not being that noticeable.

But the more robust thing to do is something like:

  1. have one function that, given a world (x,z) coordinate, returns the terrain height at that position
  2. have a second function that, given a world (x,z) coord, returns a list of all tree positions nearby.
  3. have a third function which, given a world data chunk and a tree position, draws that tree into the chunk wherever they overlap. E.g. if the tree is slightly outside of the chunk then this function wouldn't set any voxels for the trunk, but it might set voxels for leaves.

Then at runtime, for each chunk you'd get a list of nearby trees (2), and for each tree you draw it (3) into the current chunk, relative to the terrain height (1) at the tree center (which is where the trunk starts). But note that all of the logic involved needs to be deterministic - if tree locations are random then you'll have half-trees at chunk borders.

Or at least, that's what I wound up doing. There may be better ways!

@officialblueify
Copy link
Author

Ok. I'm a beginner trying to make sense of this lol.

@officialblueify
Copy link
Author

My main problem is actually adding leaves on the pillars

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

No branches or pull requests

3 participants
@fenomas @officialblueify and others