HollowBond/Public/ecma/Geometries/Game/ElevatorGeometryGame.ecma.js

122 lines
5.0 KiB
JavaScript

"use strict";
const ElevatorGeometryGame = (function(){
const ElevatorGeometryGame = function(hollow_bond){
const cache = {};
const append_box = (buffers, min_x, max_x, min_y, max_y, min_z, max_z) => {
const { positions, normals, uvs, indices } = buffers;
let offset = positions.length / 3;
const add_quad = (p1, p2, p3, p4, n) => {
positions.push(...p1, ...p2, ...p3, ...p4);
normals.push(...n, ...n, ...n, ...n);
uvs.push(0, 0, 1, 0, 1, 1, 0, 1);
indices.push(offset, offset + 2, offset + 1, offset, offset + 3, offset + 2);
offset += 4;
};
add_quad([min_x, max_y, max_z], [max_x, max_y, max_z], [max_x, max_y, min_z], [min_x, max_y, min_z], [0, 1, 0]);
add_quad([min_x, min_y, min_z], [max_x, min_y, min_z], [max_x, min_y, max_z], [min_x, min_y, max_z], [0, -1, 0]);
add_quad([max_x, min_y, min_z], [min_x, min_y, min_z], [min_x, max_y, min_z], [max_x, max_y, min_z], [0, 0, -1]);
add_quad([min_x, min_y, max_z], [max_x, min_y, max_z], [max_x, max_y, max_z], [min_x, max_y, max_z], [0, 0, 1]);
add_quad([min_x, min_y, min_z], [min_x, min_y, max_z], [min_x, max_y, max_z], [min_x, max_y, min_z], [-1, 0, 0]);
add_quad([max_x, min_y, max_z], [max_x, min_y, min_z], [max_x, max_y, min_z], [max_x, max_y, max_z], [1, 0, 0]);
};
const create_buffer_geometry = buffers => {
const geo = new THREE.BufferGeometry();
geo.setAttribute("position", new THREE.Float32BufferAttribute(buffers.positions, 3));
geo.setAttribute("normal", new THREE.Float32BufferAttribute(buffers.normals, 3));
geo.setAttribute("uv", new THREE.Float32BufferAttribute(buffers.uvs, 2));
geo.setIndex(buffers.indices);
return geo;
};
this.get_cabin_geometry = model => {
const key = `cabin_${model.width}_${model.height}`;
if(cache[key]) return cache[key];
const buffers = { positions: [], normals: [], uvs: [], indices: [] };
const w = model.width || hollow_bond.elevator_size || 2.0;
const h = model.height || hollow_bond.room_height || 3.0;
const d = model.depth || hollow_bond.elevator_size || 2.0;
const hw = w * 0.5;
const hd = d * 0.5;
const th = 0.08;
const door_w = 1.1;
const door_h = 2.15;
const h_door_w = door_w * 0.5;
// 1. Suelo y Techo
append_box(buffers, -hw, hw, 0, th, -hd, hd);
append_box(buffers, -hw, hw, h - th, h, -hd, hd);
// 2. Pared trasera (-Z)
append_box(buffers, -hw, hw, th, h - th, -hd, -hd + th);
// 3. Paredes laterales (-X y +X)
append_box(buffers, -hw, -hw + th, th, h - th, -hd, hd);
append_box(buffers, hw - th, hw, th, h - th, -hd, hd);
// 4. Pared frontal con vano para la puerta (+Z)
append_box(buffers, -hw, -h_door_w, th, h - th, hd - th, hd);
append_box(buffers, h_door_w, hw, th, h - th, hd - th, hd);
append_box(buffers, -h_door_w, h_door_w, door_h, h - th, hd - th, hd);
// // 5. Botonera exterior e interior
// append_box(buffers, h_door_w + 0.06, h_door_w + 0.18, 1.1, 1.35, hd - 0.01, hd + 0.03); // Pulsador fuera
// append_box(buffers, hw - th - 0.02, hw - th, 1.1, 1.45, 0.1, 0.35); // Panel de botones interior
// 5. Botonera interior de la cabina (conservamos solo el panel de dentro)
append_box(buffers, hw - th - 0.02, hw - th, 1.1, 1.45, 0.1, 0.35);
const geo = create_buffer_geometry(buffers);
cache[key] = geo;
return geo;
};
this.get_door_leaf_geometry = () => {
const key = `door_leaf`;
if(cache[key]) return cache[key];
const buffers = { positions: [], normals: [], uvs: [], indices: [] };
const leaf_w = 0.58;
const leaf_h = 2.14;
const leaf_th = 0.03;
append_box(buffers, -leaf_w * 0.5, leaf_w * 0.5, 0.02, leaf_h, -leaf_th * 0.5, leaf_th * 0.5);
const geo = create_buffer_geometry(buffers);
cache[key] = geo;
return geo;
};
this.get_light_geometry = model => {
const key = `light_panel`;
if(cache[key]) return cache[key];
const buffers = { positions: [], normals: [], uvs: [], indices: [] };
const h = model.height || hollow_bond.room_height || 3.0;
append_box(buffers, -0.4, 0.4, h - 0.1, h - 0.08, -0.4, 0.4);
const geo = create_buffer_geometry(buffers);
cache[key] = geo;
return geo;
};
this.reset = () => {
for(const k in cache){
cache[k].dispose();
delete cache[k];
}
};
};
return ElevatorGeometryGame;
})();