Lighting for entities

This commit is contained in:
2026-09-30 16:56:55 -03:00
parent 93382d3c97
commit cbc9edfd60
11 changed files with 163 additions and 18 deletions
+5 -3
View File
@@ -10,6 +10,7 @@ import type { SpriteRegion } from "$/common/constants.ts";
import { EverythingRegistry } from "$/common/everything_registry.ts";
import type { EntityBoneJson, EntityJson, EntityModelJson } from "$/common/mod_data.ts";
import { HURT_TINT, push_box, type Vec3 } from "./player_renderer.ts";
import { entity_tint } from "./entity_light.ts";
// models are in texture pixels, 16 to a block
const PIXEL = 1 / 16;
@@ -20,8 +21,10 @@ export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_
if (!json) return;
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
const region = get_sprite_region(json.texture);
const hurt = entity.hurt_time > 0 || entity.dead ? HURT_TINT : undefined;
const tint = entity_tint(entity, partial_tick, hurt, entity.data.on_fire === true);
if (json.model === undefined) {
render_sprite(entity, camera, atlas, region, partial_tick);
render_sprite(entity, camera, atlas, region, partial_tick, tint);
return;
}
const model = EverythingRegistry.get<EntityModelJson>("entity_models", json.model);
@@ -35,7 +38,6 @@ export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_
const { x, y, z } = entity.render_position(partial_tick);
const yaw_sin = Math.sin(entity.yaw);
const yaw_cos = Math.cos(entity.yaw);
const tint = entity.hurt_time > 0 || entity.dead ? HURT_TINT : undefined;
const bones = new Map<string, Matrix>();
for (const bone of model.bones) {
@@ -70,6 +72,7 @@ function render_sprite(
atlas: Texture,
region: SpriteRegion,
partial_tick: number,
[r, g, b]: Vec3,
) {
const { x, y, z } = entity.render_position(partial_tick);
const cy = y + entity.height / 2;
@@ -80,7 +83,6 @@ function render_sprite(
const [pitch_sin, pitch_cos] = [Math.sin(camera.pitch), Math.cos(camera.pitch)];
const right: Vec3 = [yaw_cos * half_width, 0, -yaw_sin * half_width];
const up: Vec3 = [yaw_sin * pitch_sin * half_height, pitch_cos * half_height, yaw_cos * pitch_sin * half_height];
const [r, g, b] = entity.hurt_time > 0 || entity.dead ? HURT_TINT : [1, 1, 1];
const u0 = region.x / atlas.width;
const v0 = region.y / atlas.height;