// @ts-ignore import {convert} from './objparser'; import {initBuffers, deleteBuffers} from './buffers'; /** * Fetch an obj file * @param {string} url the url to fetch the object from * @return {string} the raw data of the obj file */ export async function fetchObj(url: string) { const response = await fetch(url); const data = await response.text(); return data; } /** * Pushes a new obj file to the gl buffer * @param {any} gl the WebGL context * @param {string} data the obj file to push * @param {any} buffers the buffers to be updated * @param {any} params the params to be updated * @param {boolean} firstCall is it first object updated ? * @return {Array} the updated buffers and params */ export function updateObj( gl: any, data: string, buffers: any, params: any, firstCall: boolean = false) { const [ positions, normals, uvs, indices, ] = convert(data); params.length = indices.length; let x = 0; let y = 0; let z = 0; let maxx = positions[0]; let maxy = positions[1]; let maxz = positions[2]; let minx = positions[0]; let miny = positions[1]; let minz = positions[2]; for (let i = 0; i < positions.length; i++) { if (i % 3 == 0) { if (positions[i] > maxx) { maxx = positions[i]; } else if (positions[i] < minx) { minx = positions[i]; } x += positions[i]; } else if (i % 3 == 1) { if (positions[i] > maxy) { maxy = positions[i]; } else if (positions[i] < miny) { miny = positions[i]; } y += positions[i]; } else { if (positions[i] > maxz) { maxz = positions[i]; } else if (positions[i] < minz) { minz = positions[i]; } z += positions[i]; } } params.range = Math.max(maxx - minx, maxy - miny, maxz - minz); params.avg.x = x / (positions.length / 3); params.avg.y = y / (positions.length / 3); params.avg.z = z / (positions.length / 3); if (!firstCall) { deleteBuffers(gl, buffers); } buffers = initBuffers(gl, positions, indices, normals, uvs); return [buffers, params]; }