Fluid containers
This commit is contained in:
@@ -3,11 +3,11 @@
|
||||
|
||||
import { mat4, vec3 } from "gl-matrix";
|
||||
import type { SpriteRegion } from "$/common/constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { BlockRegistry, EverythingRegistry, type ItemLayer, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import type { ItemEntity } from "$/client/entity/item_entity.ts";
|
||||
import { type ItemData, ItemStack } from "$/common/inventory.ts";
|
||||
import { contained_fluid, type ItemData, ItemStack } from "$/common/inventory.ts";
|
||||
import { light_color, push_vertex, Texture } from "$/client/renderer/mod.ts";
|
||||
import {
|
||||
bake_model,
|
||||
@@ -23,12 +23,14 @@ import {
|
||||
type ModelDisplay,
|
||||
type ModelJson,
|
||||
} from "$/common/block_models.ts";
|
||||
import { item_model } from "./item_model.ts";
|
||||
import { item_model, type ItemQuad } from "./item_model.ts";
|
||||
import { get_state_value, item_texture } from "$/common/utils.ts";
|
||||
import { ID_MASK, TEXTURE_SIZE } from "$/common/constants.ts";
|
||||
import type { ClientLevel } from "$/client/level/client_level.ts";
|
||||
import { entity_tint } from "./entity_light.ts";
|
||||
import type { Vec3 } from "./player_renderer.ts";
|
||||
import { fluid_rules } from "$/client/fluid_rules.ts";
|
||||
import { color_rgb, tint_without_biome, type TintColor } from "$/common/worldgen/biome_colors.ts";
|
||||
|
||||
// how big items on the ground are drawn, minecraft's ground transform
|
||||
const BLOCK_SCALE = 0.25;
|
||||
@@ -50,6 +52,9 @@ const CORNER_UVS = [[0, 1], [1, 1], [1, 0], [0, 0]] as const;
|
||||
// where the extra copies of a bigger stack sit, as fractions of the model's size
|
||||
const COPY_OFFSETS = [[0, 0, 0], [0.35, 0.2, -0.25], [-0.3, 0.4, 0.2], [0.15, 0.6, 0.35], [-0.2, 0.8, -0.3]];
|
||||
|
||||
// how far each of a flat item's layers sits out from the one under it, so it's drawn over it
|
||||
const LAYER_GAP = 0.001;
|
||||
|
||||
// minecraft draws more copies of the model the bigger the stack is
|
||||
function copies(amount: number) {
|
||||
if (amount > 48) return 5;
|
||||
@@ -320,19 +325,54 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
|
||||
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);
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
// a flat item's sprite made 3d (see item_model.ts), its front and back gap further out than the item's
|
||||
function push_sprite(
|
||||
atlas: Texture,
|
||||
texture_id: string,
|
||||
quads: ItemQuad[],
|
||||
[r, g, b]: Vec3,
|
||||
gap: number,
|
||||
transform: ItemTransform,
|
||||
) {
|
||||
const region = get_sprite_region(texture_id);
|
||||
const u = (pixels: number) => (region.x + pixels) / atlas.width;
|
||||
const v = (pixels: number) => (region.y + pixels) / atlas.height;
|
||||
for (const quad of item_model(texture_id)) {
|
||||
for (const quad of quads) {
|
||||
for (const i of [0, 1, 2, 0, 2, 3]) {
|
||||
const [px, py, pz] = transform(...quad.corners[i]);
|
||||
const [cx, cy, cz] = quad.corners[i];
|
||||
const [px, py, pz] = transform(cx, cy, cz + Math.sign(cz) * gap);
|
||||
const [pu, pv] = quad.uvs[i];
|
||||
push_vertex(px, py, pz, u(pu), v(pv), quad.shade * tr, quad.shade * tg, quad.shade * tb, 1);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the layers drawn over a flat item's texture (see ItemLayer), each in its tint's color, without the ones whose tint
|
||||
// has no color for this stack
|
||||
export function item_layers(item: ItemStack): { texture: string; color: Vec3 }[] {
|
||||
const layers = EverythingRegistry.get<ItemRegistry>("items", item.type_id)?.layers ?? [];
|
||||
return layers.flatMap(({ texture, tint }) => {
|
||||
const color = layer_color(item, tint);
|
||||
return color ? [{ texture, color }] : [];
|
||||
});
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
// where a model's uv in pixels (0-16) is in the atlas. the atlas's border around each sprite (see ATLAS_BORDER) keeps
|
||||
// lookups that round past its edge on its own pixels
|
||||
export function sprite_u(atlas: Texture, region: SpriteRegion, pixels: number) {
|
||||
|
||||
@@ -15,7 +15,15 @@ import {
|
||||
ITEM_FACE_SHADE,
|
||||
item_tint,
|
||||
} from "$/common/block_models.ts";
|
||||
import { display_matrix, item_block, item_model_of, type ItemModel, sprite_u, sprite_v } from "./item_renderer.ts";
|
||||
import {
|
||||
display_matrix,
|
||||
item_block,
|
||||
item_layers,
|
||||
item_model_of,
|
||||
type ItemModel,
|
||||
sprite_u,
|
||||
sprite_v,
|
||||
} from "./item_renderer.ts";
|
||||
import {
|
||||
draw_rect,
|
||||
draw_text,
|
||||
@@ -77,7 +85,8 @@ export function draw_nine_slice(
|
||||
}
|
||||
}
|
||||
|
||||
export function draw_item(item: ItemStack, x: number, y: number) {
|
||||
// show_count is for drawing an item where its count means nothing, like one shown on a screen
|
||||
export function draw_item(item: ItemStack, x: number, y: number, show_count = item.max_amount !== 1) {
|
||||
const item_info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
|
||||
|
||||
if (!item_info) {
|
||||
@@ -95,7 +104,7 @@ export function draw_item(item: ItemStack, x: number, y: number) {
|
||||
draw_item_item(item, item_info, x, y);
|
||||
}
|
||||
|
||||
if (item.max_amount !== 1) {
|
||||
if (show_count) {
|
||||
draw_text(
|
||||
String(item.amount),
|
||||
x + SLOT_SIZE + 2 - 11 * 2,
|
||||
@@ -128,19 +137,27 @@ export function draw_item(item: ItemStack, x: number, y: number) {
|
||||
|
||||
// ey its not a bad name
|
||||
function draw_item_item(item: ItemStack, item_info: ItemRegistry, x: number, y: number) {
|
||||
const texture_id = item_texture(item, item_info);
|
||||
const sprite_region = get_sprite_region(texture_id);
|
||||
draw_texture_region(
|
||||
AssetManager.instance.get("bworld:textures"),
|
||||
sprite_region.x,
|
||||
sprite_region.y,
|
||||
sprite_region.width,
|
||||
sprite_region.height,
|
||||
x + 3,
|
||||
y + 3,
|
||||
SLOT_SIZE - 6,
|
||||
SLOT_SIZE - 6,
|
||||
);
|
||||
const draw = (texture_id: string, [r, g, b] = [1, 1, 1]) => {
|
||||
const sprite_region = get_sprite_region(texture_id);
|
||||
draw_texture_region(
|
||||
AssetManager.instance.get("bworld:textures"),
|
||||
sprite_region.x,
|
||||
sprite_region.y,
|
||||
sprite_region.width,
|
||||
sprite_region.height,
|
||||
x + 3,
|
||||
y + 3,
|
||||
SLOT_SIZE - 6,
|
||||
SLOT_SIZE - 6,
|
||||
false,
|
||||
false,
|
||||
r,
|
||||
g,
|
||||
b,
|
||||
);
|
||||
};
|
||||
draw(item_texture(item, item_info));
|
||||
for (const layer of item_layers(item)) draw(layer.texture, layer.color);
|
||||
}
|
||||
|
||||
// from a model's points to a slot, as screen x and y and depth toward the viewer. with a gui transform, the transform
|
||||
|
||||
Reference in New Issue
Block a user