Fluid and item visuals
This commit is contained in:
@@ -8,6 +8,7 @@ import { AssetManager } from "$/client/assets.ts";
|
||||
import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import type { ItemEntity } from "$/client/entity/item_entity.ts";
|
||||
import { contained_fluid, type ItemData, ItemStack } from "$/common/inventory.ts";
|
||||
import type { FluidVariant } from "$/common/mod_api/shared.ts";
|
||||
import { light_color, push_vertex, Texture } from "$/client/renderer/mod.ts";
|
||||
import {
|
||||
bake_model,
|
||||
@@ -76,6 +77,18 @@ const SHOWN_COPY_SPREAD = 0.075;
|
||||
// a point of an item's model to where it's drawn. models are 1 across, from y 0 to 1, centered on x and z
|
||||
export type ItemTransform = (x: number, y: number, z: number) => [number, number, number];
|
||||
|
||||
// where push_item's vertices go: the batch, or anything else that takes them the same way, like a visual's model
|
||||
export type VertexSink = typeof push_vertex;
|
||||
|
||||
// an item the size it's drawn on the ground, with its middle at the middle of the block. for a visual's item, which
|
||||
// is placed from there, like create's items in a basin
|
||||
export function ground_transform(item: ItemStack): ItemTransform {
|
||||
const ground = item_model_of(item)?.display.ground;
|
||||
if (ground) return display_transform(mat4.fromTranslation(mat4.create(), [0.5, 0.5, 0.5]), ground);
|
||||
const scale = drawn_as_model(item) ? BLOCK_SCALE : ITEM_SCALE;
|
||||
return (x, y, z) => [0.5 + x * scale, 0.5 + (y - 0.5) * scale, 0.5 + z * scale];
|
||||
}
|
||||
|
||||
// the atlas has to be the current texture
|
||||
export function render_item_entity(entity: ItemEntity, partial_tick: number) {
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
@@ -288,8 +301,15 @@ export function display_transform(base: mat4, display: DisplayTransform): ItemTr
|
||||
}
|
||||
|
||||
// an item's model: its own model or its block's item_model, a cube for other blocks, its sprite made 3d for everything
|
||||
// else (see item_model.ts). tint multiplies its colors, like the light it's in
|
||||
export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransform, [tr, tg, tb]: Vec3 = [1, 1, 1]) {
|
||||
// else (see item_model.ts). tint multiplies its colors, like the light it's in. two triangles a quad, its corners 0 1 2
|
||||
// and 0 2 3
|
||||
export function push_item(
|
||||
atlas: Texture,
|
||||
item: ItemStack,
|
||||
transform: ItemTransform,
|
||||
[tr, tg, tb]: Vec3 = [1, 1, 1],
|
||||
vertex: VertexSink = push_vertex,
|
||||
) {
|
||||
const model = item_model_of(item);
|
||||
if (model) {
|
||||
for (const quad of model.quads) {
|
||||
@@ -299,7 +319,7 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
|
||||
for (const i of [0, 1, 2, 0, 2, 3]) {
|
||||
const p = quad.positions;
|
||||
const [px, py, pz] = transform(p[i * 3] - 0.5, p[i * 3 + 1], p[i * 3 + 2] - 0.5);
|
||||
push_vertex(
|
||||
vertex(
|
||||
px,
|
||||
py,
|
||||
pz,
|
||||
@@ -320,17 +340,18 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
|
||||
const region = get_sprite_region(cube_face_texture(block, face.texture));
|
||||
const corners = face.corners.map(([cx, cy, cz]) => transform(cx - 0.5, cy, cz - 0.5));
|
||||
const [r, g, b] = cube_face_tint(block, face.texture);
|
||||
push_textured_quad(atlas, region, corners, [face.shade * r * tr, face.shade * g * tg, face.shade * b * tb]);
|
||||
const color = [face.shade * r * tr, face.shade * g * tg, face.shade * b * tb];
|
||||
push_textured_quad(atlas, region, corners, color, vertex);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// its sprite, then its layers over it: only their fronts and backs, the first two quads, each a little further out
|
||||
const texture_id = item_texture(item);
|
||||
push_sprite(atlas, texture_id, item_model(texture_id), [tr, tg, tb], 0, transform);
|
||||
push_sprite(atlas, texture_id, item_model(texture_id), [tr, tg, tb], 0, transform, vertex);
|
||||
item_layers(item).forEach(({ texture, color: [r, g, b] }, i) => {
|
||||
const color: Vec3 = [r * tr, g * tg, b * tb];
|
||||
push_sprite(atlas, texture, item_model(texture).slice(0, 2), color, (i + 1) * LAYER_GAP, transform);
|
||||
push_sprite(atlas, texture, item_model(texture).slice(0, 2), color, (i + 1) * LAYER_GAP, transform, vertex);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -342,6 +363,7 @@ function push_sprite(
|
||||
[r, g, b]: Vec3,
|
||||
gap: number,
|
||||
transform: ItemTransform,
|
||||
vertex: VertexSink,
|
||||
) {
|
||||
const region = get_sprite_region(texture_id);
|
||||
for (const quad of quads) {
|
||||
@@ -351,7 +373,7 @@ function push_sprite(
|
||||
const [pu, pv] = quad.uvs[i];
|
||||
const u = (region.x + pu) / atlas.width;
|
||||
const v = (region.y + pv) / atlas.height;
|
||||
push_vertex(px, py, pz, u, v, quad.shade * r, quad.shade * g, quad.shade * b, 1);
|
||||
vertex(px, py, pz, u, v, quad.shade * r, quad.shade * g, quad.shade * b, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -370,7 +392,12 @@ function layer_color(item: ItemStack, tint: ItemLayer["tint"]): Vec3 | undefined
|
||||
if (tint === undefined) return [1, 1, 1];
|
||||
if (tint !== "fluid") return color_rgb(tint_without_biome(tint));
|
||||
const fluid = contained_fluid(item.type_id, item.data);
|
||||
return fluid && color_rgb(tint_without_biome(fluid_rules.color(fluid) as TintColor));
|
||||
return fluid && fluid_rgb(fluid);
|
||||
}
|
||||
|
||||
// a fluid's color by its rules, as r g b from 0 to 1
|
||||
export function fluid_rgb(fluid: FluidVariant): Vec3 {
|
||||
return color_rgb(tint_without_biome(fluid_rules.color(fluid) as TintColor));
|
||||
}
|
||||
|
||||
// where a model's uv in pixels (0-16) is in the atlas. the atlas's border around each sprite (see ATLAS_BORDER) keeps
|
||||
@@ -383,7 +410,13 @@ export function sprite_v(atlas: Texture, region: SpriteRegion, pixels: number) {
|
||||
return (region.y + pixels / TEXTURE_SIZE * region.height) / atlas.height;
|
||||
}
|
||||
|
||||
function push_textured_quad(atlas: Texture, region: SpriteRegion, corners: number[][], color: number[]) {
|
||||
function push_textured_quad(
|
||||
atlas: Texture,
|
||||
region: SpriteRegion,
|
||||
corners: number[][],
|
||||
color: number[],
|
||||
vertex: VertexSink,
|
||||
) {
|
||||
const u0 = region.x / atlas.width;
|
||||
const v0 = region.y / atlas.height;
|
||||
const u1 = (region.x + region.width) / atlas.width;
|
||||
@@ -392,6 +425,6 @@ function push_textured_quad(atlas: Texture, region: SpriteRegion, corners: numbe
|
||||
for (const i of [0, 1, 2, 0, 2, 3]) {
|
||||
const [px, py, pz] = corners[i];
|
||||
const [cu, cv] = CORNER_UVS[i];
|
||||
push_vertex(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, color[0], color[1], color[2], 1);
|
||||
vertex(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, color[0], color[1], color[2], 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user