Lighting for entities
This commit is contained in:
@@ -4,12 +4,14 @@ 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 { ItemStack } from "$/common/inventory.ts";
|
||||
import { push_vertex, Texture } from "$/client/renderer/mod.ts";
|
||||
import { light_color, push_vertex, Texture } from "$/client/renderer/mod.ts";
|
||||
import { block_item_texture, block_variant, cube_face_texture, cube_face_tint } from "$/common/block_models.ts";
|
||||
import { item_model } 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";
|
||||
|
||||
// how big items on the ground are drawn, minecraft's ground transform
|
||||
const BLOCK_SCALE = 0.25;
|
||||
@@ -65,6 +67,7 @@ export function render_item_entity(entity: ItemEntity, partial_tick: number) {
|
||||
const cos = Math.cos(angle);
|
||||
|
||||
const scale = item_block(entity.item) ? BLOCK_SCALE : ITEM_SCALE;
|
||||
const tint = entity_tint(entity, partial_tick);
|
||||
for (let i = 0; i < copies(entity.item.amount); i++) {
|
||||
const [ox, oy, oz] = COPY_OFFSETS[i];
|
||||
const bx = x + ox * scale;
|
||||
@@ -74,7 +77,7 @@ export function render_item_entity(entity: ItemEntity, partial_tick: number) {
|
||||
bx + (lx * cos - lz * sin) * scale,
|
||||
by + ly * scale,
|
||||
bz + (lx * sin + lz * cos) * scale,
|
||||
]);
|
||||
], tint);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +92,11 @@ export function render_block_items(level: ClientLevel) {
|
||||
const value = level.get_block_value(x, y, z);
|
||||
const block = blocks[value & ID_MASK];
|
||||
if (!block?.item_display) continue;
|
||||
push_shown_item(atlas, block, value, item, x, y, z);
|
||||
// the block's own light, or the one above for blocks light doesn't get into, since the items sit on top
|
||||
const here = level.get_light(x, y, z);
|
||||
const above = level.get_light(x, y + 1, z);
|
||||
const tint = light_color((Math.max(here >> 4, above >> 4) << 4) | Math.max(here & 15, above & 15));
|
||||
push_shown_item(atlas, block, value, item, x, y, z, tint);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,6 +109,7 @@ function push_shown_item(
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
tint: Vec3,
|
||||
) {
|
||||
const states: Record<string, number> = {};
|
||||
for (const state of block.states ?? []) states[state.name] = get_state_value(value, block, state.name)!;
|
||||
@@ -133,7 +141,7 @@ function push_shown_item(
|
||||
cx + (rx * lx - bx * lz) * scale,
|
||||
bottom + ly * scale,
|
||||
cz + (rz * lx - bz * lz) * scale,
|
||||
]);
|
||||
], tint);
|
||||
} else {
|
||||
// lying face up, its top away from whoever it faces so they read it the right way round
|
||||
const half = scale / TEXTURE_SIZE / 2;
|
||||
@@ -141,7 +149,7 @@ function push_shown_item(
|
||||
cx + (rx * lx + bx * (ly - 0.5)) * scale,
|
||||
bottom + half + lz * scale,
|
||||
cz + (rz * lx + bz * (ly - 0.5)) * scale,
|
||||
]);
|
||||
], tint);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -166,15 +174,16 @@ export function item_block(item: ItemStack): BlockRegistry | undefined {
|
||||
return block && block_item_texture(block) === undefined ? block : undefined;
|
||||
}
|
||||
|
||||
// an item's model: a cube for blocks, its sprite made 3d for everything else (see item_model.ts)
|
||||
export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransform) {
|
||||
// an item's model: a cube for 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]) {
|
||||
const block = item_block(item);
|
||||
if (block) {
|
||||
for (const face of CUBE_FACES) {
|
||||
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, face.shade * g, face.shade * b]);
|
||||
push_textured_quad(atlas, region, corners, [face.shade * r * tr, face.shade * g * tg, face.shade * b * tb]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -187,7 +196,7 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
|
||||
for (const i of [0, 1, 2, 0, 2, 3]) {
|
||||
const [px, py, pz] = transform(...quad.corners[i]);
|
||||
const [pu, pv] = quad.uvs[i];
|
||||
push_vertex(px, py, pz, u(pu), v(pv), quad.shade, quad.shade, quad.shade, 1);
|
||||
push_vertex(px, py, pz, u(pu), v(pv), quad.shade * tr, quad.shade * tg, quad.shade * tb, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user