Skip to content

Commit

Permalink
support loading .ply
Browse files Browse the repository at this point in the history
  • Loading branch information
PZerua committed Oct 18, 2024
1 parent 1981eb8 commit ad2eeaa
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
52 changes: 38 additions & 14 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
},

Expand All @@ -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;
}
});

Expand Down Expand Up @@ -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 } );
Expand All @@ -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() );
Expand Down Expand Up @@ -209,15 +215,15 @@ window.App = {
this._loadEnvironment( file.name, data );
},

loadGltf( file, data ) {
loadLocation( loader, file, data ) {

if( !data )
{
// file is the path URL
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 );
} );
Expand All @@ -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 ) {
Expand All @@ -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 );

Expand All @@ -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 );
Expand Down
12 changes: 12 additions & 0 deletions src/engine/sample_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ std::vector<std::string> 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<Node*> entities;
parse_scene(filename.c_str(), entities);

main_scene->add_nodes(entities);

cameras.clear();
}

void SampleEngine::toggle_rotation()
{
rotate_scene = !rotate_scene;
Expand Down
1 change: 1 addition & 0 deletions src/engine/sample_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class SampleEngine : public Engine {
// Methods to use in web demonstrator
void set_skybox_texture(const std::string& filename);
std::vector<std::string> 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);
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ad2eeaa

Please sign in to comment.