fix: some small issues
Some checks failed
Deploy to GitHub Pages / build_site (push) Has been cancelled

This commit is contained in:
2024-05-06 01:38:59 +02:00
parent 10a12ad41c
commit 2351c65179
5 changed files with 69 additions and 57 deletions

View File

@ -1,14 +1,43 @@
<script lang="ts">
import { Canvas } from "@threlte/core";
import Scene from "./Scene.svelte";
import { Group, Vector3 } from "three";
import { Vector3 } from "three";
import { updateGeometries } from "./updateGeometries";
import { decodeFloat, splitNestedArray } from "@nodes/utils";
import type { PerformanceStore } from "@nodes/utils";
import { AppSettings } from "$lib/settings/app-settings";
import SmallPerformanceViewer from "$lib/performance/SmallPerformanceViewer.svelte";
import { MeshMatcapMaterial, TextureLoader, type Group } from "three";
import {
createGeometryPool,
createInstancedGeometryPool,
} from "./geometryPool";
const loader = new TextureLoader();
const matcap = loader.load("/matcap_green.jpg");
matcap.colorSpace = "srgb";
const material = new MeshMatcapMaterial({
color: 0xffffff,
matcap,
});
let geometryPool: ReturnType<typeof createGeometryPool>;
let instancePool: ReturnType<typeof createInstancedGeometryPool>;
export function updateGeometries(inputs: Int32Array[], group: Group) {
geometryPool = geometryPool || createGeometryPool(group, material);
instancePool = instancePool || createInstancedGeometryPool(group, material);
let meshes = geometryPool.update(inputs.filter((i) => i[0] === 1));
let faces = instancePool.update(inputs.filter((i) => i[0] === 2));
return {
totalFaces: meshes.totalFaces + faces.totalFaces,
totalVertices: meshes.totalVertices + faces.totalVertices,
};
}
export let centerCamera: boolean = true;
export let perf: PerformanceStore;
export let scene: Group;

View File

@ -24,6 +24,9 @@ export function createGeometryPool(parentScene: Group, material: Material) {
let meshes: Mesh[] = [];
let totalVertices = 0;
let totalFaces = 0;
function updateSingleGeometry(data: Int32Array, existingMesh: Mesh | null = null) {
let hash = fastArrayHash(data);
@ -34,7 +37,9 @@ export function createGeometryPool(parentScene: Group, material: Material) {
// const geometryType = encodedData[index++];
let index = 1;
const vertexCount = data[index++];
totalVertices += vertexCount;
const faceCount = data[index++];
totalFaces += faceCount;
if (geometry.userData?.hash === hash) {
return;
@ -111,6 +116,8 @@ export function createGeometryPool(parentScene: Group, material: Material) {
update(
newData: Int32Array[],
) {
totalVertices = 0;
totalFaces = 0;
for (let i = 0; i < Math.max(newData.length, meshes.length); i++) {
const existingMesh = meshes[i];
let input = newData[i];
@ -121,6 +128,7 @@ export function createGeometryPool(parentScene: Group, material: Material) {
scene.remove(existingMesh);
}
}
return { totalVertices, totalFaces };
}
}
}
@ -130,6 +138,8 @@ export function createInstancedGeometryPool(parentScene: Group, material: Materi
parentScene.add(scene);
const instances: InstancedMesh[] = [];
let totalVertices = 0;
let totalFaces = 0;
function updateSingleInstance(data: Int32Array, existingInstance: InstancedMesh | null = null) {
@ -144,6 +154,8 @@ export function createInstancedGeometryPool(parentScene: Group, material: Materi
const faceCount = data[index++];
const instanceCount = data[index++];
const stemDepth = data[index++];
totalVertices += vertexCount * instanceCount;
totalFaces += faceCount * instanceCount;
// Indices
const indicesEnd = index + faceCount * 3;
@ -237,6 +249,8 @@ export function createInstancedGeometryPool(parentScene: Group, material: Materi
update(
newData: Int32Array[],
) {
totalVertices = 0;
totalFaces = 0;
for (let i = 0; i < Math.max(newData.length, instances.length); i++) {
const existingMesh = instances[i];
let input = newData[i];
@ -247,6 +261,7 @@ export function createInstancedGeometryPool(parentScene: Group, material: Materi
scene.remove(existingMesh);
}
}
return { totalVertices, totalFaces };
}
}
}