diff --git a/src/core/data_formats/parsing/ninja/index.ts b/src/core/data_formats/parsing/ninja/index.ts index 696a8f64..420eb798 100644 --- a/src/core/data_formats/parsing/ninja/index.ts +++ b/src/core/data_formats/parsing/ninja/index.ts @@ -23,15 +23,12 @@ export class NjObject { private readonly _children: NjObject[]; readonly evaluation_flags: NjEvaluationFlags; - readonly model: M | undefined; + readonly model?: M; readonly position: Vec3; readonly rotation: Vec3; // Euler angles in radians. readonly scale: Vec3; readonly children: readonly NjObject[]; - private readonly bone_cache = new Map | null>(); - private _bone_count = -1; - constructor( evaluation_flags: NjEvaluationFlags, model: M | undefined, @@ -50,33 +47,23 @@ export class NjObject { } bone_count(): number { - if (this._bone_count === -1) { - const id_ref: [number] = [0]; - this.get_bone_internal(this, Number.MAX_SAFE_INTEGER, id_ref); - this._bone_count = id_ref[0]; - } - - return this._bone_count; + const id_ref: [number] = [0]; + this.get_bone_internal(this, Number.MAX_SAFE_INTEGER, id_ref); + return id_ref[0]; } get_bone(bone_id: number): NjObject | undefined { - let bone = this.bone_cache.get(bone_id); - - // Strict === check because null means there's no bone with this id. - if (bone === undefined) { - bone = this.get_bone_internal(this, bone_id, [0]); - this.bone_cache.set(bone_id, bone || null); - } - - return bone || undefined; + return this.get_bone_internal(this, bone_id, [0]); } add_child(child: NjObject): void { - this._bone_count = -1; - this.bone_cache.clear(); this._children.push(child); } + clear_children(): void { + this._children.splice(0); + } + private get_bone_internal( object: NjObject, bone_id: number, @@ -84,7 +71,6 @@ export class NjObject { ): NjObject | undefined { if (!object.evaluation_flags.skip) { const id = id_ref[0]++; - this.bone_cache.set(id, object); if (id === bone_id) { return object; diff --git a/src/core/rendering/Camera.ts b/src/core/rendering/Camera.ts index f82b332a..d1fe51fd 100644 --- a/src/core/rendering/Camera.ts +++ b/src/core/rendering/Camera.ts @@ -25,8 +25,8 @@ export class Camera { return 2 * Math.atan(Math.tan(0.5 * this.fov) / this._zoom); } - readonly view_mat4 = Mat4.identity(); - readonly projection_mat4 = Mat4.identity(); + readonly view_matrix = Mat4.identity(); + readonly projection_matrix = Mat4.identity(); constructor( private viewport_width: number, @@ -49,7 +49,7 @@ export class Camera { const f = 100; // prettier-ignore - this.projection_mat4.set_all( + this.projection_matrix.set_all( 2/w, 0, 0, 0, 0, 2/h, 0, 0, 0, 0, 2/(n-f), 0, @@ -69,7 +69,7 @@ export class Camera { const w /* width */ = 2 * aspect * t; // prettier-ignore - this.projection_mat4.set_all( + this.projection_matrix.set_all( 2*n / w, 0, 0, 0, 0, 2*n / h, 0, 0, 0, 0, (n+f) / (n-f), 2*n*f / (n-f), @@ -141,11 +141,11 @@ export class Camera { } private update_matrix(): void { - this.view_mat4.data[12] = -this.position.x; - this.view_mat4.data[13] = -this.position.y; - this.view_mat4.data[14] = -this.position.z; - this.view_mat4.data[0] = this._zoom; - this.view_mat4.data[5] = this._zoom; - this.view_mat4.data[10] = this._zoom; + this.view_matrix.data[12] = -this.position.x; + this.view_matrix.data[13] = -this.position.y; + this.view_matrix.data[14] = -this.position.z; + this.view_matrix.data[0] = this._zoom; + this.view_matrix.data[5] = this._zoom; + this.view_matrix.data[10] = this._zoom; } } diff --git a/src/core/rendering/ShaderProgram.ts b/src/core/rendering/ShaderProgram.ts index a374f1b3..9d571143 100644 --- a/src/core/rendering/ShaderProgram.ts +++ b/src/core/rendering/ShaderProgram.ts @@ -5,7 +5,7 @@ export class ShaderProgram { private readonly gl: WebGL2RenderingContext; private readonly program: WebGLProgram; private readonly mat_projection_loc: WebGLUniformLocation; - private readonly mat_camera_loc: WebGLUniformLocation; + private readonly mat_model_view_loc: WebGLUniformLocation; private readonly mat_normal_loc: WebGLUniformLocation | null; private readonly tex_sampler_loc: WebGLUniformLocation | null; @@ -36,7 +36,7 @@ export class ShaderProgram { } this.mat_projection_loc = this.get_required_uniform_location(program, "mat_projection"); - this.mat_camera_loc = this.get_required_uniform_location(program, "mat_camera"); + this.mat_model_view_loc = this.get_required_uniform_location(program, "mat_model_view"); this.mat_normal_loc = gl.getUniformLocation(program, "mat_normal"); this.tex_sampler_loc = gl.getUniformLocation(program, "tex_sampler"); @@ -57,8 +57,8 @@ export class ShaderProgram { this.gl.uniformMatrix4fv(this.mat_projection_loc, false, matrix.data); } - set_mat_camera_uniform(matrix: Mat4): void { - this.gl.uniformMatrix4fv(this.mat_camera_loc, false, matrix.data); + set_mat_model_view_uniform(matrix: Mat4): void { + this.gl.uniformMatrix4fv(this.mat_model_view_loc, false, matrix.data); } set_mat_normal_uniform(matrix: Mat3): void { diff --git a/src/core/rendering/conversion/MeshBuilder.ts b/src/core/rendering/conversion/MeshBuilder.ts deleted file mode 100644 index 6306dad7..00000000 --- a/src/core/rendering/conversion/MeshBuilder.ts +++ /dev/null @@ -1,186 +0,0 @@ -import { - Bone, - BufferGeometry, - Float32BufferAttribute, - Uint16BufferAttribute, - Vector3, -} from "three"; -import { map_get_or_put } from "../../util"; - -export type BuilderData = { - readonly created_by_geometry_builder: boolean; - readonly materials: BuilderMaterial[]; - readonly bones: Bone[]; -}; - -export type BuilderVec2 = { - readonly x: number; - readonly y: number; -}; - -export type BuilderVec3 = { - readonly x: number; - readonly y: number; - readonly z: number; -}; - -export type BuilderMaterial = { - readonly texture_id?: number; - readonly alpha: boolean; - readonly additive_blending: boolean; -}; - -/** - * Maps various material properties to material IDs. - */ -export class MaterialMap { - private readonly materials: BuilderMaterial[] = [{ alpha: false, additive_blending: false }]; - private readonly map = new Map(); - - /** - * Returns an index to an existing material if one exists for the given arguments. Otherwise - * adds a new material and returns its index. - */ - add_material( - texture_id?: number, - alpha: boolean = false, - additive_blending: boolean = false, - ): number { - if (texture_id == undefined) { - return 0; - } else { - const key = (texture_id << 2) | (alpha ? 0b10 : 0) | (additive_blending ? 1 : 0); - return map_get_or_put(this.map, key, () => { - this.materials.push({ texture_id, alpha, additive_blending }); - return this.materials.length - 1; - }); - } - } - - get_materials(): BuilderMaterial[] { - return this.materials; - } -} - -type VertexGroup = { - offset: number; - size: number; - material_index: number; -}; - -export class GeometryBuilder { - private readonly positions: number[] = []; - private readonly normals: number[] = []; - private readonly uvs: number[] = []; - private readonly indices: number[] = []; - private readonly bones: Bone[] = []; - private readonly bone_indices: number[] = []; - private readonly bone_weights: number[] = []; - private readonly groups: VertexGroup[] = []; - /** - * Will contain all material indices used in {@link this.groups} and -1 for the dummy material. - */ - private readonly material_map = new MaterialMap(); - - get vertex_count(): number { - return this.positions.length / 3; - } - - get index_count(): number { - return this.indices.length; - } - - get_position(index: number): Vector3 { - return new Vector3( - this.positions[3 * index], - this.positions[3 * index + 1], - this.positions[3 * index + 2], - ); - } - - get_normal(index: number): Vector3 { - return new Vector3( - this.normals[3 * index], - this.normals[3 * index + 1], - this.normals[3 * index + 2], - ); - } - - add_vertex(position: BuilderVec3, normal: BuilderVec3, uv: BuilderVec2): void { - this.positions.push(position.x, position.y, position.z); - this.normals.push(normal.x, normal.y, normal.z); - this.uvs.push(uv.x, uv.y); - } - - add_index(index: number): void { - this.indices.push(index); - } - - add_bone(bone: Bone): void { - this.bones.push(bone); - } - - add_bone_weight(index: number, weight: number): void { - this.bone_indices.push(index); - this.bone_weights.push(weight); - } - - add_group( - offset: number, - size: number, - texture_id?: number, - alpha: boolean = false, - additive_blending: boolean = false, - ): void { - const last_group = this.groups[this.groups.length - 1]; - const material_index = this.material_map.add_material(texture_id, alpha, additive_blending); - - if (last_group && last_group.material_index === material_index) { - last_group.size += size; - } else { - this.groups.push({ - offset, - size, - material_index, - }); - } - } - - build(): BufferGeometry { - const geom = new BufferGeometry(); - - geom.setAttribute("position", new Float32BufferAttribute(this.positions, 3)); - geom.setAttribute("normal", new Float32BufferAttribute(this.normals, 3)); - geom.setAttribute("uv", new Float32BufferAttribute(this.uvs, 2)); - - geom.setIndex(new Uint16BufferAttribute(this.indices, 1)); - - let bones: Bone[]; - - if (this.bone_indices.length && this.bones.length) { - geom.setAttribute("skinIndex", new Uint16BufferAttribute(this.bone_indices, 4)); - geom.setAttribute("skinWeight", new Float32BufferAttribute(this.bone_weights, 4)); - bones = this.bones; - } else { - bones = []; - } - - for (const group of this.groups) { - geom.addGroup(group.offset, group.size, group.material_index); - } - - // noinspection UnnecessaryLocalVariableJS - const data: BuilderData = { - created_by_geometry_builder: true, - materials: this.material_map.get_materials(), - bones, - }; - - geom.userData = data; - - geom.computeBoundingSphere(); - geom.computeBoundingBox(); - - return geom; - } -} diff --git a/src/core/rendering/conversion/ninja_geometry.ts b/src/core/rendering/conversion/ninja_geometry.ts index e816dd8a..453c076f 100644 --- a/src/core/rendering/conversion/ninja_geometry.ts +++ b/src/core/rendering/conversion/ninja_geometry.ts @@ -3,10 +3,16 @@ import { NjcmModel } from "../../data_formats/parsing/ninja/njcm"; import { XjModel } from "../../data_formats/parsing/ninja/xj"; import { vec3_to_math } from "./index"; import { Mesh } from "../Mesh"; -import { SceneNode } from "../Scene"; import { VertexFormat } from "../VertexFormat"; import { EulerOrder, Quat } from "../../math/quaternions"; -import { Mat4, Vec2, Vec3 } from "../../math/linear_algebra"; +import { + mat3_vec3_multiply, + Mat4, + mat4_product, + mat4_vec3_multiply, + Vec2, + Vec3, +} from "../../math/linear_algebra"; const DEFAULT_NORMAL = new Vec3(0, 1, 0); const DEFAULT_UV = new Vec2(0, 0); @@ -14,8 +20,8 @@ const NO_TRANSLATION = new Vec3(0, 0, 0); const NO_ROTATION = new Quat(1, 0, 0, 0); const NO_SCALE = new Vec3(1, 1, 1); -export function ninja_object_to_node(object: NjObject): SceneNode { - return new NodeCreator().to_node(object); +export function ninja_object_to_mesh(object: NjObject): Mesh { + return new MeshCreator().to_mesh(object); } type Vertex = { @@ -48,10 +54,16 @@ class VerticesHolder { } } -class NodeCreator { +class MeshCreator { private readonly vertices = new VerticesHolder(); + private readonly builder = Mesh.builder(VertexFormat.PosNorm); - to_node(object: NjObject): SceneNode { + to_mesh(object: NjObject): Mesh { + this.object_to_mesh(object, Mat4.identity()); + return this.builder.build(); + } + + private object_to_mesh(object: NjObject, parent_matrix: Mat4): void { const { no_translate, no_rotate, @@ -63,48 +75,54 @@ class NodeCreator { } = object.evaluation_flags; const { position, rotation, scale } = object; - const matrix = Mat4.compose( - no_translate ? NO_TRANSLATION : vec3_to_math(position), - no_rotate - ? NO_ROTATION - : Quat.euler_angles( - rotation.x, - rotation.y, - rotation.z, - zxy_rotation_order ? EulerOrder.ZXY : EulerOrder.ZYX, - ), - no_scale ? NO_SCALE : vec3_to_math(scale), + const matrix = mat4_product( + parent_matrix, + Mat4.compose( + no_translate ? NO_TRANSLATION : vec3_to_math(position), + no_rotate + ? NO_ROTATION + : Quat.euler_angles( + rotation.x, + rotation.y, + rotation.z, + zxy_rotation_order ? EulerOrder.ZXY : EulerOrder.ZYX, + ), + no_scale ? NO_SCALE : vec3_to_math(scale), + ), ); - let mesh: Mesh | undefined; - if (object.model && !hidden) { - mesh = this.model_to_mesh(object.model); + this.model_to_mesh(object.model, matrix); } - const node = new SceneNode(mesh, matrix); - if (!break_child_trace) { for (const child of object.children) { - node.add_child(this.to_node(child)); + this.object_to_mesh(child, matrix); } } - - return node; } - private model_to_mesh(model: NjModel): Mesh { + private model_to_mesh(model: NjModel, matrix: Mat4): void { if (is_njcm_model(model)) { - return this.njcm_model_to_mesh(model); + this.njcm_model_to_mesh(model, matrix); } else { - return this.xj_model_to_mesh(model); + this.xj_model_to_mesh(model, matrix); } } - private njcm_model_to_mesh(model: NjcmModel): Mesh { + private njcm_model_to_mesh(model: NjcmModel, matrix: Mat4): void { + const normal_matrix = matrix.normal_mat3(); + const new_vertices = model.vertices.map(vertex => { const position = vec3_to_math(vertex.position); - const normal = vertex.normal ? vec3_to_math(vertex.normal) : DEFAULT_NORMAL; + mat4_vec3_multiply(matrix, position); + + let normal: Vec3 | undefined = undefined; + + if (vertex.normal) { + normal = vec3_to_math(vertex.normal); + mat3_vec3_multiply(normal_matrix, normal); + } return { position, @@ -117,8 +135,6 @@ class NodeCreator { this.vertices.put(new_vertices); - const builder = Mesh.builder(VertexFormat.PosNorm); - for (const mesh of model.meshes) { for (let i = 0; i < mesh.vertices.length; ++i) { const mesh_vertex = mesh.vertices[i]; @@ -126,50 +142,55 @@ class NodeCreator { if (vertices.length) { const vertex = vertices[0]; - const normal = vertex.normal ?? mesh_vertex.normal ?? DEFAULT_NORMAL; - const index = builder.vertex_count; + const normal = + vertex.normal ?? + (mesh_vertex.normal ? vec3_to_math(mesh_vertex.normal) : DEFAULT_NORMAL); + const index = this.builder.vertex_count; - builder.vertex(vertex.position, vec3_to_math(normal)); + this.builder.vertex(vertex.position, normal); if (index >= 2) { if (i % 2 === (mesh.clockwise_winding ? 1 : 0)) { - builder.triangle(index - 2, index - 1, index); + this.builder.triangle(index - 2, index - 1, index); } else { - builder.triangle(index - 2, index, index - 1); + this.builder.triangle(index - 2, index, index - 1); } } } } } - - return builder.build(); } - private xj_model_to_mesh(model: XjModel): Mesh { - const builder = Mesh.builder(VertexFormat.PosNorm); + private xj_model_to_mesh(model: XjModel, matrix: Mat4): void { + const index_offset = this.builder.vertex_count; + const normal_matrix = matrix.normal_mat3(); for (const { position, normal } of model.vertices) { - builder.vertex(vec3_to_math(position), normal ? vec3_to_math(normal) : DEFAULT_NORMAL); + const p = vec3_to_math(position); + mat4_vec3_multiply(matrix, p); + + const n = normal ? vec3_to_math(normal) : new Vec3(0, 1, 0); + mat3_vec3_multiply(normal_matrix, n); + + this.builder.vertex(p, n); } for (const mesh of model.meshes) { let clockwise = false; for (let j = 2; j < mesh.indices.length; ++j) { - const a = mesh.indices[j - 2]; - const b = mesh.indices[j - 1]; - const c = mesh.indices[j]; + const a = index_offset + mesh.indices[j - 2]; + const b = index_offset + mesh.indices[j - 1]; + const c = index_offset + mesh.indices[j]; if (clockwise) { - builder.triangle(b, a, c); + this.builder.triangle(b, a, c); } else { - builder.triangle(a, b, c); + this.builder.triangle(a, b, c); } clockwise = !clockwise; } } - - return builder.build(); } } diff --git a/src/core/rendering/conversion/ninja_three_geometry.ts b/src/core/rendering/conversion/ninja_three_geometry.ts index 44132b1a..38e217f8 100644 --- a/src/core/rendering/conversion/ninja_three_geometry.ts +++ b/src/core/rendering/conversion/ninja_three_geometry.ts @@ -169,7 +169,7 @@ class GeometryCreator { if (vertices.length) { const vertex = vertices[0]; - const normal = vertex.normal || mesh_vertex.normal || DEFAULT_NORMAL; + const normal = vertex.normal ?? mesh_vertex.normal ?? DEFAULT_NORMAL; const index = this.builder.vertex_count; this.builder.add_vertex( diff --git a/src/core/rendering/webgl/WebglRenderer.ts b/src/core/rendering/webgl/WebglRenderer.ts index 5b2bc6c0..684a0cbb 100644 --- a/src/core/rendering/webgl/WebglRenderer.ts +++ b/src/core/rendering/webgl/WebglRenderer.ts @@ -1,4 +1,4 @@ -import { mat4_product } from "../../math/linear_algebra"; +import { Mat4, mat4_product } from "../../math/linear_algebra"; import { ShaderProgram } from "../ShaderProgram"; import pos_norm_vert_shader_source from "./pos_norm.vert"; import pos_norm_frag_shader_source from "./pos_norm.frag"; @@ -7,6 +7,8 @@ import pos_tex_frag_shader_source from "./pos_tex.frag"; import { GfxRenderer } from "../GfxRenderer"; import { WebglGfx, WebglMesh } from "./WebglGfx"; import { Projection } from "../Camera"; +import { VertexFormat } from "../VertexFormat"; +import { SceneNode } from "../Scene"; export class WebglRenderer extends GfxRenderer { private readonly gl: WebGL2RenderingContext; @@ -27,13 +29,20 @@ export class WebglRenderer extends GfxRenderer { this.gfx = new WebglGfx(gl); gl.enable(gl.DEPTH_TEST); - gl.enable(gl.CULL_FACE); + // gl.enable(gl.CULL_FACE); gl.clearColor(0.1, 0.1, 0.1, 1); - this.shader_programs = [ - new ShaderProgram(gl, pos_norm_vert_shader_source, pos_norm_frag_shader_source), - new ShaderProgram(gl, pos_tex_vert_shader_source, pos_tex_frag_shader_source), - ]; + this.shader_programs = []; + this.shader_programs[VertexFormat.PosNorm] = new ShaderProgram( + gl, + pos_norm_vert_shader_source, + pos_norm_frag_shader_source, + ); + this.shader_programs[VertexFormat.PosTex] = new ShaderProgram( + gl, + pos_tex_vert_shader_source, + pos_tex_frag_shader_source, + ); this.set_size(800, 600); } @@ -59,34 +68,69 @@ export class WebglRenderer extends GfxRenderer { gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); - this.scene.traverse((node, parent_mat) => { - const mat = mat4_product(parent_mat, node.transform); + this.render_node(this.scene.root_node, this.camera.view_matrix); - if (node.mesh) { - const program = this.shader_programs[node.mesh.format]; - program.bind(); + // this.scene.traverse((node, parent_mat) => { + // const mat = mat4_product(parent_mat, node.transform); + // + // if (node.mesh) { + // const program = this.shader_programs[node.mesh.format]; + // program.bind(); + // + // program.set_mat_projection_uniform(this.camera.projection_matrix); + // program.set_mat_model_view_uniform(mat); + // program.set_mat_normal_uniform(mat.normal_mat3()); + // + // if (node.mesh.texture?.gfx_texture) { + // gl.activeTexture(gl.TEXTURE0); + // gl.bindTexture(gl.TEXTURE_2D, node.mesh.texture.gfx_texture as WebGLTexture); + // program.set_texture_uniform(gl.TEXTURE0); + // } + // + // const gfx_mesh = node.mesh.gfx_mesh as WebglMesh; + // gl.bindVertexArray(gfx_mesh.vao); + // gl.drawElements(gl.TRIANGLES, node.mesh.index_count, gl.UNSIGNED_SHORT, 0); + // gl.bindVertexArray(null); + // + // gl.bindTexture(gl.TEXTURE_2D, null); + // + // program.unbind(); + // } + // + // return mat; + // }, this.camera.view_matrix); + } - program.set_mat_projection_uniform(this.camera.projection_mat4); - program.set_mat_camera_uniform(mat); - program.set_mat_normal_uniform(mat.normal_mat3()); + private render_node(node: SceneNode, parent_mat: Mat4): void { + const gl = this.gl; + const mat = mat4_product(parent_mat, node.transform); - if (node.mesh.texture?.gfx_texture) { - gl.activeTexture(gl.TEXTURE0); - gl.bindTexture(gl.TEXTURE_2D, node.mesh.texture.gfx_texture as WebGLTexture); - program.set_texture_uniform(gl.TEXTURE0); - } + if (node.mesh) { + const program = this.shader_programs[node.mesh.format]; + program.bind(); - const gfx_mesh = node.mesh.gfx_mesh as WebglMesh; - gl.bindVertexArray(gfx_mesh.vao); - gl.drawElements(gl.TRIANGLES, node.mesh.index_count, gl.UNSIGNED_SHORT, 0); - gl.bindVertexArray(null); + program.set_mat_projection_uniform(this.camera.projection_matrix); + program.set_mat_model_view_uniform(mat); + program.set_mat_normal_uniform(mat.normal_mat3()); - gl.bindTexture(gl.TEXTURE_2D, null); - - program.unbind(); + if (node.mesh.texture?.gfx_texture) { + gl.activeTexture(gl.TEXTURE0); + gl.bindTexture(gl.TEXTURE_2D, node.mesh.texture.gfx_texture as WebGLTexture); + program.set_texture_uniform(gl.TEXTURE0); } - return mat; - }, this.camera.view_mat4); + const gfx_mesh = node.mesh.gfx_mesh as WebglMesh; + gl.bindVertexArray(gfx_mesh.vao); + gl.drawElements(gl.TRIANGLES, node.mesh.index_count, gl.UNSIGNED_SHORT, 0); + gl.bindVertexArray(null); + + gl.bindTexture(gl.TEXTURE_2D, null); + + program.unbind(); + } + + for (const child of node.children) { + this.render_node(child, mat); + } } } diff --git a/src/core/rendering/webgl/pos_norm.vert b/src/core/rendering/webgl/pos_norm.vert index 7bcd3e03..12ae3eb4 100644 --- a/src/core/rendering/webgl/pos_norm.vert +++ b/src/core/rendering/webgl/pos_norm.vert @@ -3,7 +3,7 @@ precision mediump float; uniform mat4 mat_projection; -uniform mat4 mat_camera; +uniform mat4 mat_model_view; uniform mat3 mat_normal; in vec4 pos; @@ -12,6 +12,6 @@ in vec3 normal; out vec3 frag_normal; void main() { - gl_Position = mat_projection * mat_camera * pos; + gl_Position = mat_projection * mat_model_view * pos; frag_normal = normalize(mat_normal * normal); } diff --git a/src/core/rendering/webgl/pos_tex.vert b/src/core/rendering/webgl/pos_tex.vert index a7c55ab1..b563b1b3 100644 --- a/src/core/rendering/webgl/pos_tex.vert +++ b/src/core/rendering/webgl/pos_tex.vert @@ -3,7 +3,7 @@ precision mediump float; uniform mat4 mat_projection; -uniform mat4 mat_camera; +uniform mat4 mat_model_view; in vec4 pos; in vec2 tex; @@ -11,6 +11,6 @@ in vec2 tex; out vec2 f_tex; void main() { - gl_Position = mat_projection * mat_camera * pos; + gl_Position = mat_projection * mat_model_view * pos; f_tex = tex; } diff --git a/src/core/rendering/webgpu/WebgpuRenderer.ts b/src/core/rendering/webgpu/WebgpuRenderer.ts index becc1b79..09e40935 100644 --- a/src/core/rendering/webgpu/WebgpuRenderer.ts +++ b/src/core/rendering/webgpu/WebgpuRenderer.ts @@ -176,7 +176,7 @@ export class WebgpuRenderer extends GfxRenderer { pass_encoder.setPipeline(pipeline); - const camera_project_mat = mat4_product(this.camera.projection_mat4, this.camera.view_mat4); + const camera_project_mat = mat4_product(this.camera.projection_matrix, this.camera.view_matrix); this.scene.traverse((node, parent_mat) => { const mat = mat4_product(parent_mat, node.transform); diff --git a/src/viewer/rendering/ModelGfxRenderer.ts b/src/viewer/rendering/ModelGfxRenderer.ts index ff06e28e..9c482d28 100644 --- a/src/viewer/rendering/ModelGfxRenderer.ts +++ b/src/viewer/rendering/ModelGfxRenderer.ts @@ -2,7 +2,9 @@ import { ModelStore } from "../stores/ModelStore"; import { Disposer } from "../../core/observable/Disposer"; import { Renderer } from "../../core/rendering/Renderer"; import { GfxRenderer } from "../../core/rendering/GfxRenderer"; -import { ninja_object_to_node } from "../../core/rendering/conversion/ninja_geometry"; +import { ninja_object_to_mesh } from "../../core/rendering/conversion/ninja_geometry"; +import { SceneNode } from "../../core/rendering/Scene"; +import { Mat4 } from "../../core/math/linear_algebra"; export class ModelGfxRenderer implements Renderer { private readonly disposer = new Disposer(); @@ -73,7 +75,7 @@ export class ModelGfxRenderer implements Renderer { if (nj_object) { // Convert textures and geometry. - const node = ninja_object_to_node(nj_object); + const node = new SceneNode(ninja_object_to_mesh(nj_object), Mat4.identity()); this.renderer.scene.root_node.add_child(node); this.renderer.scene.traverse(node => { diff --git a/src/viewer/stores/ModelStore.ts b/src/viewer/stores/ModelStore.ts index c6d1f546..1be8be10 100644 --- a/src/viewer/stores/ModelStore.ts +++ b/src/viewer/stores/ModelStore.ts @@ -246,6 +246,24 @@ export class ModelStore extends Store { body, ); + // nj_object.children[0].children[0].children[0].clear_children(); + // nj_object.children[0].children[0].children[1].clear_children(); + // nj_object.children[0].children[0].children[2].clear_children(); + // nj_object.children[0].children[0].children[3].clear_children(); + // nj_object.children[0].children[0].children[4].clear_children(); + // nj_object.children[0].children[0].children[5].children[0].children[0].clear_children(); + // nj_object.children[0].children[0].children[5].children[0].children[1].clear_children(); + // nj_object.children[0].children[0].children[5].children[0].children[2].clear_children(); + // nj_object.children[0].children[0].children[5].children[0].children[3].children[0].clear_children(); + // const model = nj_object.children[0].children[0].children[5].children[0].children[3] + // .children[0].model!; + // model.meshes.splice(0, 1); + // model.meshes.splice(1); + // console.log( + // nj_object.children[0].children[0].children[5].children[0].children[3].children[0] + // .model, + // ); + this._current_nj_object.val = nj_object; } catch (e) { logger.error(`Couldn't load model for ${character_class.name}.`);