From ad2eeaa0fb4e647d388d3834b74d6bae95d42aed Mon Sep 17 00:00:00 2001 From: PZerua Date: Fri, 18 Oct 2024 18:42:16 +0200 Subject: [PATCH] support loading .ply --- js/script.js | 52 ++++++++++++++++++++++++++---------- libraries/wgpuEngine | 2 +- src/engine/sample_engine.cpp | 12 +++++++++ src/engine/sample_engine.h | 1 + src/main.cpp | 2 ++ 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/js/script.js b/js/script.js index 2a14b1a..7c2c67e 100644 --- a/js/script.js +++ b/js/script.js @@ -11,11 +11,11 @@ function _processVector( vector ) window.App = { - dragSupportedExtensions: [ 'hdr', 'hdre', 'glb' ], + dragSupportedExtensions: [ 'hdr', 'hdre', 'glb', 'ply' ], init() { - this.cameraTypes = [ "Flyover", "Orbit" ]; + this.cameraTypes = [ "Orbit", "Flyover" ]; this.cameraNames = [ ]; this.cameraSpeed = 0.75; @@ -31,7 +31,12 @@ window.App = { if( this.location ) { this.toggleModal( true ); - this.loadGltf( this.location ); + + const ext = LX.getExtension( this.location ); + switch( ext ) { + case 'glb': this.loadLocation.call( this, this._loadGltf, this.location ); break; + case 'ply': this.loadLocation.call( this, this._loadPly, this.location ); break; + } } }, @@ -54,7 +59,8 @@ window.App = { switch( ext ) { case 'hdr': case 'hdre': this.loadEnvironment( file ); break; - case 'glb': this.loadGltf( file ); break; + case 'glb': this.loadLocation( this._loadGltf, file ); break; + case 'ply': this.loadLocation( this._loadPly, file ); break; } }); @@ -90,7 +96,7 @@ window.App = { { p.branch( "Digital Location", { closed: true } ); p.addText( "Name", "", null, { signal: "@location_name", disabled: true } ); - p.addFile( "Load", (data, file) => this.loadGltf( file, data ), { type: 'buffer', local: false, onBeforeRead: onBeforeRead } ); + // p.addFile( "Load", (data, file) => this.loadGltf( file, data ), { type: 'buffer', local: false, onBeforeRead: onBeforeRead } ); p.addCheckbox( "Rotate", false, () => window.engineInstance.toggleSceneRotation() ); p.branch( "Environment", { closed: true } ); @@ -104,7 +110,7 @@ window.App = { p.addTitle( "Camera" ); } - p.addDropdown( "Type", this.cameraTypes, "Flyover", (value) => this.setCameraType( value ) ); + p.addDropdown( "Type", this.cameraTypes, "Orbit", (value) => this.setCameraType( value ) ); p.addNumber( "Speed", this.cameraSpeed, (value) => this.setCameraSpeed( value ), { min: 0.01, max: 8.0, step: 0.1 } ); p.addList( "Look at", this.cameraNames, "", (value) => this.lookAtCameraIndexFromName( value ) ); p.addButton( null, "Reset", () => this.resetCamera() ); @@ -209,7 +215,7 @@ window.App = { this._loadEnvironment( file.name, data ); }, - loadGltf( file, data ) { + loadLocation( loader, file, data ) { if( !data ) { @@ -217,7 +223,7 @@ window.App = { if( file.constructor == String ) { const path = file; - LX.requestBinary( path, ( data ) => this._loadGltf( path, data ), ( e ) => { + LX.requestBinary( path, ( data ) => loader.call(this, path, data ), ( e ) => { LX.popup( e.constructor === String ? e : `[${ path }] can't be loaded.`, "Request Blocked", { size: ["400px", "auto"], timeout: 10000 } ); this.toggleModal( false ); } ); @@ -226,11 +232,11 @@ window.App = { const reader = new FileReader(); reader.readAsArrayBuffer( file ); - reader.onload = e => this._loadGltf( file.name, e.target.result ); + reader.onload = e => loader.call(this, file.name, e.target.result ); return; } - this._loadGltf( file.name ?? file, data ); + loader.call(this, file.name ?? file, data ); }, _loadEnvironment( name, buffer ) { @@ -250,17 +256,15 @@ window.App = { LX.emit( '@environment_name', name.replace( /.hdre|.hdr/, "" ) ); }, - async _loadGltf( name, buffer ) { + _loadGltf( name, buffer ) { name = name.substring( name.lastIndexOf( '/' ) + 1 ); console.log( "Loading glb", [ name, buffer ] ); - console.log(name, buffer); - this._fileStore( name, buffer ); - var cameraNamesVector = await window.engineInstance.loadGLB( name ); + var cameraNamesVector = window.engineInstance.loadGLB( name ); this.toggleModal( false ); @@ -280,6 +284,26 @@ window.App = { } }, + _loadPly( name, buffer ) { + + name = name.substring( name.lastIndexOf( '/' ) + 1 ); + + console.log( "Loading ply", [ name, buffer ] ); + + this._fileStore( name, buffer ); + + window.engineInstance.loadPly( name ); + + this.cameraNames = [] + this.panel.get( "Look at" ).updateValues( this.cameraNames ); + + this.toggleModal( false ); + + // Update UI + + LX.emit( '@location_name', name.replace( '.ply', '' ) ); + }, + _fileStore( filename, buffer ) { let data = new Uint8Array( buffer ); diff --git a/libraries/wgpuEngine b/libraries/wgpuEngine index f80b889..53892c9 160000 --- a/libraries/wgpuEngine +++ b/libraries/wgpuEngine @@ -1 +1 @@ -Subproject commit f80b8896df47545bcd3533758cf5033b9c6f0331 +Subproject commit 53892c90169335b4789f76a87337cf6f63f1b8bf diff --git a/src/engine/sample_engine.cpp b/src/engine/sample_engine.cpp index 9ad4bee..f22b834 100644 --- a/src/engine/sample_engine.cpp +++ b/src/engine/sample_engine.cpp @@ -181,6 +181,18 @@ std::vector SampleEngine::load_glb(const std::string& filename) return get_cameras_names(); } +void SampleEngine::load_ply(const std::string& filename) +{ + main_scene->delete_all(); + + std::vector entities; + parse_scene(filename.c_str(), entities); + + main_scene->add_nodes(entities); + + cameras.clear(); +} + void SampleEngine::toggle_rotation() { rotate_scene = !rotate_scene; diff --git a/src/engine/sample_engine.h b/src/engine/sample_engine.h index 2cca787..4986713 100644 --- a/src/engine/sample_engine.h +++ b/src/engine/sample_engine.h @@ -41,6 +41,7 @@ class SampleEngine : public Engine { // Methods to use in web demonstrator void set_skybox_texture(const std::string& filename); std::vector load_glb(const std::string& filename); + void load_ply(const std::string& filename); void toggle_rotation(); void set_camera_type(int camera_type); void set_camera_lookat_index(int index); diff --git a/src/main.cpp b/src/main.cpp index 3e6acea..a3e570c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,7 @@ EMSCRIPTEN_BINDINGS(_Class_) { .constructor<>() .function("setEnvironment", &SampleEngine::set_skybox_texture) .function("loadGLB", &SampleEngine::load_glb) + .function("loadPly", &SampleEngine::load_ply) .function("setCameraType", &SampleEngine::set_camera_type) .class_function("getInstance", &SampleEngine::get_sample_instance, emscripten::return_value_policy::reference()) .function("setCameraLookAtIndex", &SampleEngine::set_camera_lookat_index) @@ -49,6 +50,7 @@ int main() configuration.window_width = 1600; configuration.window_height = 900; configuration.camera_type = CAMERA_ORBIT; + configuration.msaa_count = 4; if (engine->initialize(renderer, configuration)) { return 1;