feat: some stuff

This commit is contained in:
max_richter 2024-04-17 16:39:56 +02:00
parent 7579c6c00b
commit 1da13523ea
9 changed files with 99 additions and 36 deletions

7
Cargo.lock generated
View File

@ -85,6 +85,12 @@ dependencies = [
"rand",
]
[[package]]
name = "glam"
version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e05e7e6723e3455f4818c7b26e855439f7546cf617ef669d1adedb8669e5cb9"
[[package]]
name = "itoa"
version = "1.0.11"
@ -364,6 +370,7 @@ version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"gl_matrix",
"glam",
"serde",
"serde_json",
"wasm-bindgen",

View File

@ -34,7 +34,7 @@
<T.PointsMaterial size={0.25} />
</T.Points>
<T.Mesh geometry={geo}>
<T.MeshBasicMaterial color="red" />
<T.MeshStandardMaterial color="hotpink" />
</T.Mesh>
{:else}
<T.Mesh>

View File

@ -1,12 +1,7 @@
<script lang="ts">
import { Canvas } from "@threlte/core";
import Scene from "./Scene.svelte";
import {
BufferAttribute,
BufferGeometry,
Float32BufferAttribute,
} from "three";
import { decodeFloat } from "$lib/helpers/encode";
import { BufferGeometry, Float32BufferAttribute } from "three";
export let result: Int32Array;
@ -24,15 +19,9 @@
const faceCount = encodedData[index++];
// Indices
const indices = encodedData.subarray(index, index + faceCount * 3);
index = index + faceCount * 3;
const normals = new Float32Array(
encodedData.buffer,
index * 4,
faceCount * 3,
);
index = index + faceCount * 3;
const indicesEnd = index + faceCount * 3;
const indices = encodedData.subarray(index, indicesEnd);
index = indicesEnd;
// Vertices
const vertices = new Float32Array(
@ -40,12 +29,21 @@
index * 4,
vertexCount * 3,
);
index = index + vertexCount * 3;
const normals = new Float32Array(
encodedData.buffer,
index * 4,
vertexCount * 3,
);
index = index + vertexCount * 3;
// Add data to geometry
geometry.setIndex([...indices]);
geometry.setAttribute("position", new Float32BufferAttribute(vertices, 3));
// geometry.setAttribute("normal", new Float32BufferAttribute(normals, 3));
geometry.computeVertexNormals();
geometry.setAttribute("normal", new Float32BufferAttribute(normals, 3));
// geometry.computeVertexNormals();
//geometry.computeVertexNormals();
return geometry;
}

View File

@ -1,5 +1,8 @@
use crate::geometry::calculate_normals::calculate_normals;
use macros::include_definition_file;
use utils::{decode_float, encode_float, evaluate_args, get_args, set_panic_hook, wrap_arg};
use utils::{
decode_float, encode_float, evaluate_args, geometry, get_args, set_panic_hook, wrap_arg,
};
use wasm_bindgen::prelude::*;
use web_sys::console;
@ -24,7 +27,7 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
// [[1,3, x, y, z, x, y,z,x,y,z]];
wrap_arg(&[
let mut cube_geometry = [
1, // 1: geometry
8, // 8 vertices
@ -52,20 +55,6 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
7, 6, 4,
4, 6, 5,
// this is the normal for every single face 1065353216 == 1.0f encoded is i32
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
// Bottom plate
p, n, n,
p, n, p,
@ -78,6 +67,21 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
p, p, p,
n, p, p,
])
// this is the normal for every single vert 1065353216 == 1.0f encoded is i32
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
];
calculate_normals(&mut cube_geometry);
wrap_arg(&cube_geometry)
}

View File

@ -12,3 +12,4 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
console_error_panic_hook = { version = "0.1.7", optional = true }
gl_matrix = "0.0.2"
glam = "0.27.0"

View File

@ -0,0 +1,50 @@
use crate::{decode_float, encode_float};
use glam::Vec3;
pub fn calculate_normals(geometry: &mut [i32]) {
let vertex_count = geometry[1] as usize;
let face_count = geometry[2] as usize;
let index_start = 3;
let indices_end = index_start + face_count * 3;
let positions_start = indices_end;
let positions_end = positions_start + vertex_count * 3;
let normals_start = positions_end;
let mut last_normal = Vec3::ZERO;
for i in 0..face_count {
let base_idx1 = geometry[index_start + i * 3] as usize * 3;
let base_idx2 = geometry[index_start + i * 3 + 1] as usize * 3;
let base_idx3 = geometry[index_start + i * 3 + 2] as usize * 3;
let v1 = Vec3::new(
decode_float(geometry[positions_start + base_idx1]),
decode_float(geometry[positions_start + base_idx1 + 1]),
decode_float(geometry[positions_start + base_idx1 + 2]),
);
let v2 = Vec3::new(
decode_float(geometry[positions_start + base_idx2]) - v1.x,
decode_float(geometry[positions_start + base_idx2 + 1]) - v1.y,
decode_float(geometry[positions_start + base_idx2 + 2]) - v1.z,
);
let v3 = Vec3::new(
decode_float(geometry[positions_start + base_idx3]) - v1.x,
decode_float(geometry[positions_start + base_idx3 + 1]) - v1.y,
decode_float(geometry[positions_start + base_idx3 + 2]) - v1.z,
);
let mut normal = v2.cross(v3).normalize();
if normal.length_squared() == 0.0 {
normal = last_normal;
} else {
last_normal = normal;
}
for j in 0..3 {
let idx = geometry[index_start + i * 3 + j] as usize * 3;
geometry[normals_start + idx] = encode_float(normal.x);
geometry[normals_start + idx + 1] = encode_float(normal.y);
geometry[normals_start + idx + 2] = encode_float(normal.z);
}
}
}

View File

@ -1,2 +1 @@
pub fn extrude_path(path: &[i32]) {}

View File

@ -0,0 +1,3 @@
pub mod calculate_normals;
pub mod extrude_path;

View File

@ -5,6 +5,7 @@ mod tree;
pub use encoding::*;
pub use helpers::*;
pub use tree::*;
pub mod geometry;
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the