Oaks, cherry blossoms and willows that sway in the wind.

3D & HD-2D

3D Foliage: Grass, Flowers, Ferns, Reeds, Bushes

reactor_3d_foliage.js · 1,161 lines · 34 public functions

Oaks, cherry blossoms and willows that sway in the wind.

What This Module Does

An extension of Reactor3D (reactor_3d.js), loaded after the world builder: in the game from scriptUrls, in the editor from Reactor3D.EXTENSIONS, and in Node by the core's own tail.

Classes & Namespaces

Design Notes from the Source Code

An extension of Reactor3D (reactor_3d.js), loaded after the world builder: in the game from scriptUrls, in the editor from Reactor3D.EXTENSIONS, and in Node by the core's own tail.

A map's foliage lives in its sidecar as foliage:

{ "wind": { "dir": 30, "strength": 0.6, "gust": 1 }, "layers": [ { "kind": "grass", "density": "0x812 4x6 7x3 0x...", "height": 0.8, "colour": "#4f7d34", "tip": "#b4c45e", "vary": 0.25, "seed": 1 } ], "bushes": [ { "x": 41.5, "y": 30.2, "size": 2.2, "colour": "#3d6a2a", "seed": 7 } ] }

A layer is one kind painted over the map: density is a level 0-9 per cell, row by row over the whole map, written as runs ("0x812" is 812 cells of none, "7" one cell at level 7). Blades are not stored: they are grown from the density by a seeded scatter, on the ground the map has where it is drawn, so the file stays small and the grass follows the terrain. Grass does not grow where a piece or a rock stands, under water (reeds stand in shallow water), or on a slope the room draws as cliff.

Drawing: one instanced mesh per layer per chunk of the map; an instance is a clump (three grass blades, a flower, a fern's fronds, a stand of reeds) built in the vertex shader from a few numbers, so a field is a few megabytes of attributes and no per-frame CPU work. Wind is a rolling gust field over the map in the same shader; characters walking through bend it. Density thins with distance by rank: each chunk's clumps are sorted by a random rank and drawn up to the share the distance keeps, the last of them shrinking away, so nothing pops. Bushes are seeded clusters of leaf cards round a woody core, merged per chunk, swaying gently, casting shadows; a big one blocks its cells.

View reactor_3d_foliage.js on GitHub · Hide internal (_) functions