fenomas / noa

Experimental voxel game engine.
MIT License
611 stars 87 forks source link

how do I make a tree Andy or EliteAsian123 #71

Closed levlups closed 5 years ago

levlups commented 5 years ago

EliteAsian nice code for hills

    for (var x1 = 0; x1 < data.shape[0]; x1++) {
                for (var y1 = 0; y1 < data.shape[1]; y1++) {
                    for (var z1 = 0; z1 < data.shape[2]; z1++) {
                        var voxelID = 0;
                        if (y + y1 < -3) voxelID = dirtID;
                            if (y + y1 === -3 && Math.random()*100<4) 

                                voxelID = flowerID; // added flowers 

                        var height = 2 * Math.sin((x + x1) / 10) + 3 * Math.cos((z + z1) / 20);

                        if (y + y1 < height) voxelID = grassID;
                        data.set(x1, y1, z1, voxelID);

                    }

                }
            }

As you see I added random number of flowers , ok I guessed I figured that out , but I tried to add a tree and then my mind when wathhhhhhh woodId + woodId + leafID , im lost can you guys give me a lead

EliteAsian123 commented 5 years ago

I know it's old but did you look at the code for the test-bed? There were trees in that example.

EliteAsian123 commented 5 years ago

Hello @levlups! I also wanted to mention I released a update for the plugin NoaTerrainGen which is for noa-plus-plugins. This update allows for pseudorandom terrain generation!

fenomas commented 5 years ago

Hi, so generating structures is a really hard problem and I don't know any simple answers (or, if you find any please let me know!). The complication is that worldgen needs to be completely deterministic - at least at chunk borders - because neighboring chunks need to match up at the edges even if they're generated in arbitrary orders.

The way I've been approaching this is to use a deterministic hash function (I use this one) to decide where to draw a tree, and I avoid putting trees at chunk borders to keep things simple. That is, I iterate over all the world X/Z coords in the chunk, avoiding the borders, and do something like:

// iterating over x/z world coords:
var h = hash(x, z)
if (h * 100 < 1) {
    var y = getTerrainHeight(x, z)
    drawTree(x, y, z)
}

As for actually drawing the data into the chunk, conceptually you need to take the tree's AABB and the chunk's AABB, and draw your tree data into the intersection of those AABBs. And the content needs to be deterministic, so one option is to dynamically generate data, but using hash functions instead of Math.random(). Or another option is to pre-build a single tree - e.g. by making an ndarray and generating voxel data in it - and then copy subsets of that data into each chunk's data wherever you need a tree.

Afraid it's pretty messy business, or at least it has been for me, but that's what I've been trying to do.