mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 16:54:04 +00:00
Draw trees in the studio's 3D world as whole blocks
Each column carried only its highest solid block, so a tree's leaves stood as a pillar down to the ground. The ground is now the terrain's top, and the solid blocks above it go to the studio as a list per column. The 3D world draws them as cubes and leaves out the faces that touch the ground or each other. The 2D map still colours and shades a column by its highest block.
This commit is contained in:
1 parent
55f6a06f9f
commit
4e46eb4a6a
17 files changed
+457
-57
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import type { LabelKind, MapLabel, MapTile } from './types'
|
||||
import type { LabelKind, MapLabel, MapTile, TileBlocks } from './types'
|
||||
|
||||
const ALIGNMENT_BYTES = 4
|
||||
|
||||
@@ -11,6 +11,8 @@ interface TileHeader {
|
||||
pickNoise: boolean
|
||||
/** The blocks the ground and cover grids index; absent or null on a tile without them. */
|
||||
blocks?: string[] | null
|
||||
/** How many blocks are raised over the ground of all cells together. */
|
||||
raised?: number
|
||||
}
|
||||
|
||||
function aligned(bytes: number): number {
|
||||
@@ -19,8 +21,8 @@ function aligned(bytes: number): number {
|
||||
|
||||
/**
|
||||
* Reads a tile as the bridge writes it (MapTileEncoding): a u32 header length, the JSON header, then the subject,
|
||||
* region, height, pick noise, ground and cover grids, each starting on a multiple of 4 bytes. The grids are views into the buffer,
|
||||
* not copies.
|
||||
* region, height, pick noise, ground, cover and raised block grids, each starting on a multiple of 4 bytes. The grids
|
||||
* are views into the buffer, not copies.
|
||||
*/
|
||||
export function decodeTile(buffer: ArrayBuffer): MapTile {
|
||||
const view = new DataView(buffer)
|
||||
@@ -33,7 +35,6 @@ export function decodeTile(buffer: ArrayBuffer): MapTile {
|
||||
const heightsAt = aligned(regionsAt + cells * Int16Array.BYTES_PER_ELEMENT)
|
||||
const pickNoiseAt = heightsAt + (header.heights ? cells * Float32Array.BYTES_PER_ELEMENT : 0)
|
||||
const groundAt = pickNoiseAt + (header.pickNoise ? cells * Float32Array.BYTES_PER_ELEMENT : 0)
|
||||
const coverAt = aligned(groundAt + cells * Int16Array.BYTES_PER_ELEMENT)
|
||||
return {
|
||||
size: header.size,
|
||||
blocksPerPixel: header.blocksPerPixel,
|
||||
@@ -43,13 +44,37 @@ export function decodeTile(buffer: ArrayBuffer): MapTile {
|
||||
regions: new Int16Array(buffer, regionsAt, cells),
|
||||
heights: header.heights ? new Float32Array(buffer, heightsAt, cells) : null,
|
||||
pickNoise: header.pickNoise ? new Float32Array(buffer, pickNoiseAt, cells) : null,
|
||||
blocks:
|
||||
header.blocks == null
|
||||
? null
|
||||
: { names: header.blocks, ground: new Int16Array(buffer, groundAt, cells), cover: new Int16Array(buffer, coverAt, cells), colors: [], looks: [], covers: [] }
|
||||
blocks: header.blocks == null ? null : decodeBlocks(buffer, groundAt, cells, header.blocks, header.raised ?? 0)
|
||||
}
|
||||
}
|
||||
|
||||
function decodeBlocks(buffer: ArrayBuffer, groundAt: number, cells: number, names: string[], raisedCount: number): TileBlocks {
|
||||
const gridBytes = aligned(cells * Int16Array.BYTES_PER_ELEMENT)
|
||||
const coverAt = groundAt + gridBytes
|
||||
const raisedCountsAt = coverAt + gridBytes
|
||||
const raisedYAt = raisedCountsAt + gridBytes
|
||||
const raisedAt = aligned(raisedYAt + raisedCount * Int16Array.BYTES_PER_ELEMENT)
|
||||
return {
|
||||
names,
|
||||
ground: new Int16Array(buffer, groundAt, cells),
|
||||
cover: new Int16Array(buffer, coverAt, cells),
|
||||
raisedStart: startsOf(new Int16Array(buffer, raisedCountsAt, cells)),
|
||||
raisedY: new Int16Array(buffer, raisedYAt, raisedCount),
|
||||
raised: new Int16Array(buffer, raisedAt, raisedCount),
|
||||
colors: [],
|
||||
looks: [],
|
||||
covers: []
|
||||
}
|
||||
}
|
||||
|
||||
function startsOf(counts: Int16Array): Int32Array {
|
||||
const starts = new Int32Array(counts.length + 1)
|
||||
for (let cell = 0; cell < counts.length; cell++) {
|
||||
starts[cell + 1] = starts[cell] + counts[cell]
|
||||
}
|
||||
return starts
|
||||
}
|
||||
|
||||
/** The grid index of a pixel; pixel coordinates run from -1 to size, the ring being the neighbours'. */
|
||||
export function gridIndex(tile: MapTile, pixelX: number, pixelZ: number): number {
|
||||
return (pixelZ + 1) * (tile.size + 2) + pixelX + 1
|
||||
|
||||
@@ -344,7 +344,10 @@ export interface MapTile {
|
||||
/** Where a tile's blocks come from: the chunks the server generates in memory. */
|
||||
export type BlockSource = 'generated'
|
||||
|
||||
/** The top of each cell's column: its ground block, and the block standing on it, such as grass. */
|
||||
/**
|
||||
* The top of each cell's column: its ground block, the terrain's top; the block standing on it, such as grass; and the
|
||||
* solid blocks raised above it, such as a tree's wood and leaves.
|
||||
*/
|
||||
export interface TileBlocks {
|
||||
/** The block states the grids index, as BlockState keys. */
|
||||
names: string[]
|
||||
@@ -352,6 +355,12 @@ export interface TileBlocks {
|
||||
ground: Int16Array
|
||||
/** {@link NO_BLOCK} where nothing stands on the ground. */
|
||||
cover: Int16Array
|
||||
/** Where each cell's raised blocks start in {@link raisedY} and {@link raised}, with one more entry for where the last cell's end. */
|
||||
raisedStart: Int32Array
|
||||
/** The Y of each raised block, cell after cell and each cell's highest first. */
|
||||
raisedY: Int16Array
|
||||
/** Each raised block, in the order of {@link raisedY}. */
|
||||
raised: Int16Array
|
||||
/** The colour of each named block from above, as 0xRRGGBB; the tile source fills it in. */
|
||||
colors: number[]
|
||||
/** What each named block looks like on a column, where the tile is fine enough to show textures; else empty. */
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { decodeTile, gridIndex } from '../bridge/tileDecoding'
|
||||
import type { MapLabel, MapTile } from '../bridge/types'
|
||||
import type { MapLabel, MapTile, TileBlocks } from '../bridge/types'
|
||||
import { countShares, heightBandShares } from './census'
|
||||
import { bandIndexOf, HEIGHT_BANDS, heightColor } from './heightColors'
|
||||
import { arrowCorners } from './playerArrow'
|
||||
@@ -57,7 +57,42 @@ function encode(size: number, withHeights: boolean, withPickNoise = false): Arra
|
||||
return buffer
|
||||
}
|
||||
|
||||
function nothingRaised(cells: number): Pick<TileBlocks, 'raisedStart' | 'raisedY' | 'raised'> {
|
||||
return { raisedStart: new Int32Array(cells + 1), raisedY: new Int16Array(0), raised: new Int16Array(0) }
|
||||
}
|
||||
|
||||
/** A tile of one pixel with its blocks as the bridge sends them: ground, cover and the raised blocks' counts, heights and blocks. */
|
||||
function encodeBlocks(raisedCounts: number[], raisedY: number[], raised: number[]): ArrayBuffer {
|
||||
const size = 1
|
||||
const cells = (size + 2) ** 2
|
||||
const header = new TextEncoder().encode(
|
||||
JSON.stringify({ size, blocksPerPixel: 1, subjectKind: 'BIOME', labels: [PLAINS], heights: false, pickNoise: false, blocks: ['stone', 'leaves', 'log'], raised: raised.length })
|
||||
)
|
||||
const align = (bytes: number): number => Math.ceil(bytes / 4) * 4
|
||||
const grid = align(cells * 2)
|
||||
const groundAt = align(4 + header.length) + grid * 2
|
||||
const raisedYAt = groundAt + grid * 3
|
||||
const raisedAt = align(raisedYAt + raised.length * 2)
|
||||
const buffer = new ArrayBuffer(raisedAt + align(raised.length * 2))
|
||||
new DataView(buffer).setUint32(0, header.length, true)
|
||||
new Uint8Array(buffer, 4).set(header)
|
||||
new Int16Array(buffer, groundAt + grid, cells).fill(-1)
|
||||
new Int16Array(buffer, groundAt + grid * 2, cells).set(raisedCounts)
|
||||
new Int16Array(buffer, raisedYAt, raised.length).set(raisedY)
|
||||
new Int16Array(buffer, raisedAt, raised.length).set(raised)
|
||||
return buffer
|
||||
}
|
||||
|
||||
describe('tile decoding', () => {
|
||||
it('reads the raised blocks of each cell after the cover grid', () => {
|
||||
const blocks = decodeTile(encodeBlocks([0, 0, 0, 0, 2, 0, 0, 1, 0], [70, 69, 66], [1, 2, 1])).blocks!
|
||||
|
||||
expect([...blocks.raisedStart]).toEqual([0, 0, 0, 0, 0, 2, 2, 2, 3, 3])
|
||||
expect([...blocks.raisedY]).toEqual([70, 69, 66])
|
||||
expect([...blocks.raised]).toEqual([1, 2, 1])
|
||||
expect(blocks.cover[4]).toBe(-1)
|
||||
})
|
||||
|
||||
it('reads the header and views each grid where the bridge put it', () => {
|
||||
const tile = decodeTile(encode(3, true))
|
||||
|
||||
@@ -138,13 +173,35 @@ describe('tile style', () => {
|
||||
const cells = tile.subjects.length
|
||||
const cover = new Int16Array(cells).fill(-1)
|
||||
cover[gridIndex(tile, 3, 3)] = 1
|
||||
const blocks = { names: ['stone', 'poppy'], ground: new Int16Array(cells).fill(0), cover, colors: [0x808080, 0xff0000], looks: [], covers: [] }
|
||||
const blocks = { ...nothingRaised(cells), names: ['stone', 'poppy'], ground: new Int16Array(cells).fill(0), cover, colors: [0x808080, 0xff0000], looks: [], covers: [] }
|
||||
const pixels = stylePixels({ ...tile, blocks }, { ...FLAT, byBlock: true })
|
||||
|
||||
expect(pixel(pixels, 4, 0, 0)).toBe(0x808080)
|
||||
expect(pixel(pixels, 4, 3, 3)).toBe(0xff0000)
|
||||
})
|
||||
|
||||
it('paints a column with blocks raised over its ground in the colour of the highest', () => {
|
||||
const tile = splitTile()
|
||||
const cells = tile.subjects.length
|
||||
const raisedStart = new Int32Array(cells + 1)
|
||||
raisedStart.fill(2, gridIndex(tile, 3, 3) + 1)
|
||||
const blocks = {
|
||||
names: ['stone', 'leaves', 'log'],
|
||||
ground: new Int16Array(cells).fill(0),
|
||||
cover: new Int16Array(cells).fill(-1),
|
||||
raisedStart,
|
||||
raisedY: Int16Array.of(70, 69),
|
||||
raised: Int16Array.of(1, 2),
|
||||
colors: [0x808080, 0x00ff00, 0x654321],
|
||||
looks: [],
|
||||
covers: []
|
||||
}
|
||||
const pixels = stylePixels({ ...tile, blocks }, { ...FLAT, byBlock: true })
|
||||
|
||||
expect(pixel(pixels, 4, 0, 0)).toBe(0x808080)
|
||||
expect(pixel(pixels, 4, 3, 3)).toBe(0x00ff00)
|
||||
})
|
||||
|
||||
it('keeps the label colours of a tile without blocks', () => {
|
||||
expect(pixel(stylePixels(splitTile(), { ...FLAT, byBlock: true }), 4, 0, 0)).toBe(PLAINS.color)
|
||||
})
|
||||
|
||||
@@ -39,7 +39,7 @@ export function mapTileSource(client: BridgeClient, tileSize: number, art: Block
|
||||
return tile
|
||||
},
|
||||
bytesOf: (tile) =>
|
||||
tile.subjects.byteLength * 2 + (tile.heights?.byteLength ?? 0) + (tile.pickNoise?.byteLength ?? 0) + (tile.blocks?.ground.byteLength ?? 0) * 2 + tile.size * tile.size * 4,
|
||||
tile.subjects.byteLength * 2 + (tile.heights?.byteLength ?? 0) + (tile.pickNoise?.byteLength ?? 0) + (tile.blocks?.ground.byteLength ?? 0) * 2 + (tile.blocks?.raisedStart.byteLength ?? 0) + (tile.blocks?.raised.byteLength ?? 0) * 2 + tile.size * tile.size * 4,
|
||||
styleKey,
|
||||
pixels: (tile, style) => new ImageData(stylePixels(tile, style), tile.size, tile.size)
|
||||
}
|
||||
|
||||
@@ -55,10 +55,11 @@ const DARKEST_PICK_SHADE = 0.3
|
||||
|
||||
/** The tile's pixels as RGBA, as {@code style} draws them (v3e MapTile.styledPixels). */
|
||||
export function stylePixels(tile: MapTile, style: TileStyle): Uint8ClampedArray<ArrayBuffer> {
|
||||
const { size, heights } = tile
|
||||
const { size } = tile
|
||||
const pixels = new Uint8ClampedArray(size * size * 4)
|
||||
const colors = tile.labels.map((label) => label.color)
|
||||
const blocks = style.byBlock ? tile.blocks : null
|
||||
const heights = blocks === null || tile.heights === null ? tile.heights : heightsSeenFromAbove(tile.heights, blocks)
|
||||
const pickNoise = style.pickDisplay === 'picks' || blocks !== null ? null : tile.pickNoise
|
||||
// The bare noise is read by its brightness, which relief shading would mix up.
|
||||
const shading = style.shading && heights !== null && !(pickNoise !== null && style.pickDisplay === 'noise')
|
||||
@@ -93,13 +94,34 @@ function labelColor(colors: number[], index: number): number {
|
||||
return index === NO_LABEL ? UNKNOWN_SUBJECT : colors[index]
|
||||
}
|
||||
|
||||
// What stands on the ground is what is seen from above; ground under the sea shows through its water.
|
||||
// Seen from above, the highest block raised over the ground hides it, and what stands on the ground hides the ground;
|
||||
// what lies under the sea shows through its water.
|
||||
function blockColor(blocks: TileBlocks, cell: number, heights: Float32Array | null, seaLevel: number): number {
|
||||
const top = blocks.cover[cell] === NO_BLOCK ? blocks.ground[cell] : blocks.cover[cell]
|
||||
const top = blockSeenFromAbove(blocks, cell)
|
||||
const color = top === NO_BLOCK ? UNKNOWN_SUBJECT : blocks.colors[top]
|
||||
return heights !== null && heights[cell] < seaLevel ? blend(color, WATER_SURFACE_COLOR, WATER_OVER_GROUND) : color
|
||||
}
|
||||
|
||||
function blockSeenFromAbove(blocks: TileBlocks, cell: number): number {
|
||||
const highestRaised = blocks.raisedStart[cell]
|
||||
if (highestRaised < blocks.raisedStart[cell + 1]) {
|
||||
return blocks.raised[highestRaised]
|
||||
}
|
||||
return blocks.cover[cell] === NO_BLOCK ? blocks.ground[cell] : blocks.cover[cell]
|
||||
}
|
||||
|
||||
// The ground's heights with each column's highest raised block in place of its ground, so trees stand out in relief.
|
||||
function heightsSeenFromAbove(groundHeights: Float32Array, blocks: TileBlocks): Float32Array {
|
||||
const heights = Float32Array.from(groundHeights)
|
||||
for (let cell = 0; cell < heights.length; cell++) {
|
||||
const highestRaised = blocks.raisedStart[cell]
|
||||
if (highestRaised < blocks.raisedStart[cell + 1]) {
|
||||
heights[cell] = blocks.raisedY[highestRaised]
|
||||
}
|
||||
}
|
||||
return heights
|
||||
}
|
||||
|
||||
// Where nothing picked the spot, as on an image map, it keeps its colour.
|
||||
function byNoise(color: number, noise: number, display: PickDisplay): number {
|
||||
if (Number.isNaN(noise)) {
|
||||
|
||||
@@ -208,6 +208,7 @@ function sameSpan(span: Span, other: Span | null): boolean {
|
||||
return span.look === null && other !== null && other.look === null && other.top === span.top && other.bottom === span.bottom && other.color === span.color && other.water === span.water
|
||||
}
|
||||
|
||||
function shaded(color: number, light: number): Vec3 {
|
||||
/** A 0xRRGGBB colour in a light, as red, green and blue from 0 to 1. */
|
||||
export function shaded(color: number, light: number): Vec3 {
|
||||
return [(((color >> 16) & 0xff) / 255) * light, (((color >> 8) & 0xff) / 255) * light, ((color & 0xff) / 255) * light]
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import type { BlockLook } from './blockLooks'
|
||||
import type { CoverLook } from './coverLooks'
|
||||
import type { TerrainPictures } from './terrainAtlas'
|
||||
import type { TerrainSurface } from './terrainMesh'
|
||||
import type { RaisedBlock, TerrainSurface } from './terrainMesh'
|
||||
import type { ColumnArea } from './terrainTiles'
|
||||
|
||||
const NO_VALUE = Number.NaN
|
||||
const NOTHING_RAISED: readonly RaisedBlock[] = []
|
||||
|
||||
/**
|
||||
* The surface read once into columns: where each column's ground and water faces stand, and its colour. A top face
|
||||
@@ -24,6 +25,8 @@ export class ColumnField {
|
||||
readonly looks: (BlockLook | null)[]
|
||||
/** What stands on each column's ground, or null for nothing that is drawn. */
|
||||
readonly covers: (CoverLook | null)[]
|
||||
/** The solid blocks above each column's ground, highest first, each without a look where the field has no atlas. */
|
||||
readonly raised: (readonly RaisedBlock[])[]
|
||||
/** The atlas of the looks, or null where no column has one. */
|
||||
readonly pictures: TerrainPictures | null
|
||||
/** How low and high the top faces stand, ground and water alike; null where no column has ground. */
|
||||
@@ -41,6 +44,7 @@ export class ColumnField {
|
||||
this.color = new Int32Array(count)
|
||||
this.looks = new Array<BlockLook | null>(count).fill(null)
|
||||
this.covers = new Array<CoverLook | null>(count).fill(null)
|
||||
this.raised = new Array<readonly RaisedBlock[]>(count).fill(NOTHING_RAISED)
|
||||
const hasPictures = surface.pictures !== undefined
|
||||
let anyLook = false
|
||||
let [lowestTop, highestTop] = [Infinity, -Infinity]
|
||||
@@ -59,7 +63,9 @@ export class ColumnField {
|
||||
const look = hasPictures ? (surface.lookAt?.(column, row) ?? null) : null
|
||||
this.looks[index] = look
|
||||
this.covers[index] = hasPictures ? (surface.coverAt?.(column, row) ?? null) : null
|
||||
anyLook ||= look !== null
|
||||
const raised = surface.raisedAt?.(column, row) ?? NOTHING_RAISED
|
||||
this.raised[index] = hasPictures ? raised : raised.map((block) => ({ ...block, look: null }))
|
||||
anyLook ||= look !== null || this.raised[index].some((block) => block.look !== null)
|
||||
lowestTop = Math.min(lowestTop, ground + 1)
|
||||
highestTop = Math.max(highestTop, ground + 1, water === null ? ground : water + 1)
|
||||
}
|
||||
@@ -80,6 +86,15 @@ export class ColumnField {
|
||||
return this.contains(column, row) && !Number.isNaN(this.groundTop[this.indexOf(column, row)])
|
||||
}
|
||||
|
||||
/** Whether the block at {@code y} in the column is the column's ground or a block raised over it; false outside the field. */
|
||||
isSolidAt(column: number, row: number, y: number): boolean {
|
||||
if (!this.hasGround(column, row)) {
|
||||
return false
|
||||
}
|
||||
const index = this.indexOf(column, row)
|
||||
return y < this.groundTop[index] || this.raised[index].some((block) => block.y === y)
|
||||
}
|
||||
|
||||
/** Whether any column has ground. */
|
||||
hasAnyGround(): boolean {
|
||||
return this.groundTop.some((top) => !Number.isNaN(top))
|
||||
|
||||
@@ -3,7 +3,7 @@ import { heightColor } from '../map/heightColors'
|
||||
import type { BlockLook } from './blockLooks'
|
||||
import type { CoverLook } from './coverLooks'
|
||||
import type { TerrainPictures } from './terrainAtlas'
|
||||
import type { TerrainSurface } from './terrainMesh'
|
||||
import type { RaisedBlock, TerrainSurface } from './terrainMesh'
|
||||
import { blocksPerColumn, originXOf, originZOf, type TerrainTile } from './terrainTiles'
|
||||
|
||||
export type MapTerrainColoring = 'biomes' | 'regions' | 'height' | 'blocks'
|
||||
@@ -57,6 +57,20 @@ export class MapTerrain implements TerrainSurface {
|
||||
return cover === NO_BLOCK ? null : (this.tile.blocks!.covers[cover] ?? null)
|
||||
}
|
||||
|
||||
raisedAt(column: number, row: number): readonly RaisedBlock[] {
|
||||
const blocks = this.coloring === 'blocks' ? this.tile.blocks : null
|
||||
if (blocks === null) {
|
||||
return []
|
||||
}
|
||||
const cell = this.cellAt(column, row)
|
||||
const raised: RaisedBlock[] = []
|
||||
for (let at = blocks.raisedStart[cell]; at < blocks.raisedStart[cell + 1]; at++) {
|
||||
const block = blocks.raised[at]
|
||||
raised.push({ y: blocks.raisedY[at], color: blocks.colors[block], look: blocks.looks[block] ?? null })
|
||||
}
|
||||
return raised
|
||||
}
|
||||
|
||||
colorAt(column: number, row: number): number {
|
||||
if (this.coloring === 'height') {
|
||||
return heightColor(this.heightAt(column, row), this.fluidHeight)
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import type { FaceLook } from './blockLooks'
|
||||
import type { ColumnField } from './columnField'
|
||||
import { shaded } from './columnFaces'
|
||||
import { FACE_LIGHT } from './coverLooks'
|
||||
import type { Picture, QuadBatch, Vec3 } from './quadBatch'
|
||||
import type { RaisedBlock } from './terrainMesh'
|
||||
import { sunk, tintAt } from './waterTint'
|
||||
|
||||
/** One face of a whole block: the step to the block it touches, and its quad on a block of {@code step} wide. */
|
||||
interface BlockFace {
|
||||
columnStep: number
|
||||
yStep: number
|
||||
rowStep: number
|
||||
light: number
|
||||
/** Which of the block's pictures the face shows, and whether that picture stands upright. */
|
||||
picture: (look: NonNullable<RaisedBlock['look']>) => FaceLook
|
||||
standing: boolean
|
||||
/** The quad's corner, along and up from the block's lowest north west corner; it faces the way up crosses along. */
|
||||
quad: (step: number) => [Vec3, Vec3, Vec3]
|
||||
}
|
||||
|
||||
// A block's bottom shows the picture of its top, as the end of a log does.
|
||||
const BLOCK_FACES: readonly BlockFace[] = [
|
||||
{ columnStep: 0, yStep: 1, rowStep: 0, light: FACE_LIGHT.up, picture: (look) => look.top, standing: false, quad: (step) => [[0, 1, 0], [step, 0, 0], [0, 0, step]] },
|
||||
{ columnStep: 0, yStep: -1, rowStep: 0, light: FACE_LIGHT.down, picture: (look) => look.top, standing: false, quad: (step) => [[0, 0, 0], [0, 0, step], [step, 0, 0]] },
|
||||
{ columnStep: 0, yStep: 0, rowStep: -1, light: FACE_LIGHT.north, picture: (look) => look.side, standing: true, quad: (step) => [[0, 0, 0], [step, 0, 0], [0, 1, 0]] },
|
||||
{ columnStep: 0, yStep: 0, rowStep: 1, light: FACE_LIGHT.south, picture: (look) => look.side, standing: true, quad: (step) => [[step, 0, step], [-step, 0, 0], [0, 1, 0]] },
|
||||
{ columnStep: -1, yStep: 0, rowStep: 0, light: FACE_LIGHT.west, picture: (look) => look.side, standing: true, quad: (step) => [[0, 0, step], [0, 0, -step], [0, 1, 0]] },
|
||||
{ columnStep: 1, yStep: 0, rowStep: 0, light: FACE_LIGHT.east, picture: (look) => look.side, standing: true, quad: (step) => [[step, 0, 0], [0, 0, step], [0, 1, 0]] }
|
||||
]
|
||||
|
||||
/**
|
||||
* The blocks raised above the ground, such as trees, as whole blocks: each face that touches neither the ground nor
|
||||
* another raised block. A face at the field's edge is drawn, as the field next to it is not known here. Blocks under
|
||||
* water sink toward its deep colour as the ground does.
|
||||
*/
|
||||
export function addRaisedFaces(field: ColumnField, batch: QuadBatch): void {
|
||||
const step = field.blocksPerColumn
|
||||
for (let row = 0; row < field.rows; row++) {
|
||||
for (let column = 0; column < field.columns; column++) {
|
||||
const index = field.indexOf(column, row)
|
||||
const water = field.waterTopAt(column, row)
|
||||
for (const block of field.raised[index]) {
|
||||
const corner: Vec3 = [field.originX + column * step, block.y, field.originZ + row * step]
|
||||
for (const face of BLOCK_FACES) {
|
||||
if (field.isSolidAt(column + face.columnStep, row + face.rowStep, block.y + face.yStep)) {
|
||||
continue
|
||||
}
|
||||
addFace(batch, face, corner, step, block, water)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function addFace(batch: QuadBatch, face: BlockFace, corner: Vec3, step: number, block: RaisedBlock, water: number | null): void {
|
||||
const [start, along, up] = face.quad(step)
|
||||
const origin: Vec3 = [corner[0] + start[0], corner[1] + start[1], corner[2] + start[2]]
|
||||
const look = block.look === null ? null : face.picture(block.look)
|
||||
const light = shaded(look === null ? block.color : look.tint, face.light)
|
||||
const color = water === null ? light : sunk(light, tintAt(water - (block.y + start[1])))
|
||||
const picture: Picture | null = look === null ? null : { cell: look.cell, standing: face.standing }
|
||||
batch.add(origin, along, up, color, color, picture)
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Vector3, type BufferGeometry, type Mesh } from 'three'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { buildTerrainMesh, type TerrainSurface } from './terrainMesh'
|
||||
import { buildTerrainMesh, type RaisedBlock, type TerrainSurface } from './terrainMesh'
|
||||
import { submergedTint, tintAt } from './waterTint'
|
||||
|
||||
const GROUND = 10
|
||||
@@ -184,3 +184,33 @@ describe('terrain mesh', () => {
|
||||
expect(nothing.object.children).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('raised blocks', () => {
|
||||
const raisedAt = (y: number): RaisedBlock => ({ y, color: RED, look: null })
|
||||
// The faces drawn for the raised blocks, which come after the ground's.
|
||||
const raisedFaces = (raised: TerrainSurface['raisedAt']): ReturnType<typeof faces> =>
|
||||
faces(meshes({ ...dry, raisedAt: raised }).ground.geometry).slice(quads(meshes(dry).ground))
|
||||
|
||||
it('stands a raised block as a whole block, each face looking away from it', () => {
|
||||
const leaf = raisedFaces((column, row) => (column === 2 && row === 2 ? [raisedAt(GROUND + 4)] : []))
|
||||
const middle = new Vector3(102.5, GROUND + 4.5, 202.5)
|
||||
|
||||
expect(leaf).toHaveLength(6)
|
||||
for (const face of leaf) {
|
||||
expect(face.middle.clone().sub(middle).normalize().dot(face.normal)).toBeCloseTo(1)
|
||||
}
|
||||
})
|
||||
|
||||
it('leaves out the faces that touch the ground or another raised block', () => {
|
||||
const tree = raisedFaces((column, row) => {
|
||||
if (column === 2 && row === 1) {
|
||||
return [raisedAt(GROUND + 2), raisedAt(GROUND + 1)]
|
||||
}
|
||||
return column === 2 && row === 2 ? [raisedAt(GROUND + 2)] : []
|
||||
})
|
||||
|
||||
// The trunk's lower block shows four sides, its upper one three and its top, the leaf all but the side on the trunk.
|
||||
expect(tree).toHaveLength(13)
|
||||
expect(tree.some((face) => face.normal.y === -1 && face.middle.y === GROUND + 1)).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -4,6 +4,7 @@ import { groundFaces, waterFaces } from './columnFaces'
|
||||
import { ColumnField } from './columnField'
|
||||
import { coverGeometry } from './coverFaces'
|
||||
import type { CoverLook } from './coverLooks'
|
||||
import { addRaisedFaces } from './raisedFaces'
|
||||
import type { TerrainPictures } from './terrainAtlas'
|
||||
import type { ColumnArea } from './terrainTiles'
|
||||
import { WATER_SURFACE_COLOR, WATER_SURFACE_OPACITY } from './waterTint'
|
||||
@@ -31,6 +32,16 @@ export interface TerrainSurface {
|
||||
lookAt?(column: number, row: number): BlockLook | null
|
||||
/** What stands on the column's ground, such as grass or a flower, or null for nothing that is drawn. */
|
||||
coverAt?(column: number, row: number): CoverLook | null
|
||||
/** The solid blocks above the column's ground, such as a tree's wood and leaves, highest first. */
|
||||
raisedAt?(column: number, row: number): readonly RaisedBlock[]
|
||||
}
|
||||
|
||||
/** A solid block above a column's ground, drawn as a whole block. */
|
||||
export interface RaisedBlock {
|
||||
y: number
|
||||
color: number
|
||||
/** What the block looks like, or null to show its colour. */
|
||||
look: BlockLook | null
|
||||
}
|
||||
|
||||
const CUTOUT_ALPHA = 0.5
|
||||
@@ -45,7 +56,8 @@ export interface TerrainMesh {
|
||||
* The surface as columns with their faces merged where neighbours show the same one, flat-shaded as Minecraft shades
|
||||
* blocks, with the ground under water sunk toward a deep colour and a light sheet of water over it. Sides on the edge reach down to {@code base}, so tiles of ground next to
|
||||
* each other leave no cracks. The columns in {@code holes} are left out, for finer ground to fill. Columns with a look
|
||||
* show their block's textures, one picture to a block face.
|
||||
* show their block's textures, one picture to a block face. Blocks raised above the ground, such as trees, stand as
|
||||
* whole blocks.
|
||||
*/
|
||||
export function buildTerrainMesh(surface: TerrainSurface, base: number, holes: readonly ColumnArea[] = []): TerrainMesh {
|
||||
const field = new ColumnField(surface, holes)
|
||||
@@ -53,7 +65,9 @@ export function buildTerrainMesh(surface: TerrainSurface, base: number, holes: r
|
||||
if (!field.hasAnyGround()) {
|
||||
return { object: group, field, dispose: () => {} }
|
||||
}
|
||||
const ground = groundFaces(field, base).geometry()
|
||||
const groundBatch = groundFaces(field, base)
|
||||
addRaisedFaces(field, groundBatch)
|
||||
const ground = groundBatch.geometry()
|
||||
const water = waterFaces(field).geometry()
|
||||
const groundMaterial = new MeshBasicMaterial({ vertexColors: true, side: FrontSide, map: field.pictures?.texture ?? null })
|
||||
const waterMaterial = new MeshBasicMaterial({ color: WATER_SURFACE_COLOR, transparent: true, opacity: WATER_SURFACE_OPACITY, depthWrite: false, side: DoubleSide })
|
||||
|
||||
Reference in new issue
Block a user