Painted Valley
README.md
paintedValley/village — the village, the trees and the tarn
Port of Painted Valley (blendi/painted-valley on Patch, code MIT): src/valley/village.js, src/valley/trees.js,
src/valley/water.js. Entry module: require("paintedValley.village.village") (village.module).
Files
| file | what |
|---|---|
| village.module | the part: build / update / setHour / destroy; exposes trees, BUSH, cast(sunDir), tagEntities, buildingMaterial, buildingList, drawShells, meshFrom |
| buildings.module | village.js: shell / walls / gable / hip / cap / chimney, three's CylinderGeometry and SphereGeometry, the eight SHAPES, layVillage, layFarm, FARMS, and drawing them |
| building.shader | village.js's building material (vertex + fragment), self-shading, returns alpha 0.4 |
| trees.module | trees.js: mulberry, three's CatmullRomCurve3 (centripetal, arc lengths, Frenet frames), IcosahedronGeometry(1, 1), the leaf-spray picture, growTree, plant, createTrees, the shade cast (tShade) |
| leaf.shader, leafCore.shader, wood.shader | leafVertex + leafFragment, leafVertex + coreFragment, woodFragment |
| tarn.module, tarn.shader | water.js: createTarn and its fragment shader |
| test/villageTest.scene | the porter's test scene (empty; the part is built into it from test scripts) |
How it is checked against the original
Reference numbers were taken by running the original's own modules in node (three 0.186.1, the version its lockfile pins, integrity checked):
- buildings: all 53 (village + 6 farmsteads) match the original's x, z, turn, scale, shape, wall and roof exactly (max difference 0). The eight shapes' positions, normals, aPart, aUp, aFace match to float32 rounding (< 1e-6).
- trees: all 1063 entries of the
treeslist match ([x, z, kind, scale, turn], max difference 1e-13, every kind the same); every kind's wood, core and leaf geometry has the original's vertex and index counts and position sums. - shade:
castat 15.5 h gives the original's byte-for-byte sum (5309286) and texel count (24213).
Decisions and differences from the original
- Where the village's random stream starts. The original lays the village and farmsteads in farwoods.js, with
farwoods'
mulberry(1967)after the far woods, farmland, poplars and the village's ring of trees have drawn 960455 numbers (counted by running the original). mulberry's state is a counter, soB.mulberry(1967, 960455)starts where the original's does (state 3837831202, the same as far'sVILLAGE_RANDOM_STATE). The mill is the far part's (farMill.shader). - Attributes in a Zero mesh. The engine's packed vertex format has positions, a unit normal (8-bit), uv (16 bits
over the mesh's range) and colour (8-bit). The original's extra attributes ride in them:
- buildings: uv = (aUp, aFace.x); colour = (aPart / 2, aFace.y in 16 bits over 0..16 m in g and b, a flag in a for the round tower's 600 and the "not known" 1e3). aRoof, aWall and what the shader needs to find vOwn (the point in the building's own metres) are per-building lanes: lane 0 = roof rgb + scale, lane 1 = wall rgb, lane 2 = origin, lane 3 = rotation quaternion. vOwn is the world position turned back by the building's rotation and scale.
- leaves and cores: normal = aBall, colour = aRand (tone, phase, open, height / 32). aRand is quantised to 8 bits: each clump's sway phase can differ from the original's by up to 1/255 of its range (no visible change).
- wood: uv = aBark.
- The village's "already painted" tag. On this deferred path a fragment's alpha does not reach the frame, so the
buildings are 53 local, temporary entities (a Model wearing its kind's mesh, the building material as a session
material, the lanes set with renderer.instanceData) under one root, and
tagEntitieslists each with tag 0.4. Drawn with renderer.mesh.drawInstanced (one draw a kind, as the original) the frame's tag pass did not reach them: a deterministic A/B with the tags on and off gave identical pixels for the instanced draws and a change for entities. The shader still returns 0.4. - Alpha-to-coverage. The leaf sprays are a cutout kept where the sharpened coverage is over a half (the original uses alpha-to-coverage on 4x MSAA); TAA smooths the edge where MSAA did. The core's own discard is the original's.
- The leaf's sway moves the clip position in vertex_clip, in world space as the original; the fragment's world position is the unmoved one (a few centimetres; it feeds the distance cut, the light through the leaves and the fog). vBall is the engine's world normal (the instance's normal matrix) where the original uses mat3(m) * aBall: the same direction but for the 0.94..1.06 height scale.
- Wood winding. The original's tube triangles face inwards and are drawn FrontSide, so it shows each tube's far inner wall, lit by the outward normals. Here the triples are turned to agree with the normals and wood.material culls front faces: the same faces are drawn (and the engine does not warn).
- The leaf-spray picture is drawn as the original's canvas draws it (the same random leaves, the same quadratic outlines, filled over black, red = lightness, green = coverage, Y flipped as a CanvasTexture is), rasterised with 4x4 samples a pixel for the canvas's anti-aliasing; mipmaps by the engine's encoder.
- tShade is a 1024 x 1024 RGBA8 texture (the shade in r, linear, clamped) over the field's extent, made by the original's cast exactly; re-cast on setHour (on the next update, once the sky has moved the sun) and when the hour drifts by more than 0.04 h. A cast takes ~20 ms at 15.5 h and ~270 ms at sunset, yielding to the frame as it goes.
- tNoise is no longer a shared slot: building, leafCore and wood declare it and bind
core.textures.tNoise. - The tarn mirrors the sky through the sky part's cloud atlas (paintedValley.sky.skyCommon skyAt, kept up to date
by
sky.bindTo); the original reads a cube map with mipmaps, the atlas is read at level 0. - Shadows: none of these draws cast or receive the engine's shadows; they light themselves as the original does.