Skip to content

Configuration for external libs / worker source #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ build/*
plugins/*
node_modules
__pycache__
.idea
123 changes: 123 additions & 0 deletions libs/loaders/draco_decoder_nodejs.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions libs/loaders/rhino3dm.min.js

Large diffs are not rendered by default.

Binary file added libs/loaders/rhino3dm.wasm
Binary file not shown.
66,263 changes: 66,263 additions & 0 deletions libs/loaders/web-ifc-api-browser.js

Large diffs are not rendered by default.

375 changes: 375 additions & 0 deletions libs/loaders/web-ifc.license.md

Large diffs are not rendered by default.

Binary file added libs/loaders/web-ifc.wasm
Binary file not shown.
504 changes: 504 additions & 0 deletions libs/occt/license.occt-import-js.txt

Large diffs are not rendered by default.

502 changes: 502 additions & 0 deletions libs/occt/license.occt.txt

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions libs/occt/occt-import-js-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

importScripts ('occt-import-js.js');

onmessage = async function (ev)
{
let modulOverrides = {
locateFile: function (path) {
return path;
}
};
let occt = await occtimportjs (modulOverrides);
let result = occt.ReadFile (ev.data.format, ev.data.buffer, ev.data.params);
postMessage (result);
};
19 changes: 19 additions & 0 deletions libs/occt/occt-import-js.js

Large diffs are not rendered by default.

Binary file added libs/occt/occt-import-js.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "online-3d-viewer",
"version": "0.15.0",
"version": "0.16.0",
"description": "Online 3D Viewer",
"license": "MIT",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion source/engine/import/importerifc.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class ImporterIfc extends ImporterBase
ImportContent (fileContent, onFinish)
{
if (this.ifc === null) {
LoadExternalLibrary ('webifc').then (() => {
LoadExternalLibrary ('web-ifc-api').then (() => {
this.ifc = new WebIFC.IfcAPI ();
this.ifc.Init ().then (() => {
this.ImportIfcContent (fileContent);
Expand Down
53 changes: 47 additions & 6 deletions source/engine/import/importerutils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IsLower } from '../geometry/geometry.js';
import { PhongMaterial } from '../model/material.js';
import { RGBColor, IntegerToHexString } from '../model/color.js';
import { LoadExternalLibraryFromUrl } from '../io/externallibs.js';
import {GetExternalLibPath, LoadExternalLibraryFromLibs, LoadExternalLibraryFromUrl} from '../io/externallibs.js';

export function NameFromLine (line, startIndex, commentChar)
{
Expand Down Expand Up @@ -102,11 +102,27 @@ export class ColorToMaterialConverter
}
}

let occtWorkerUrl = null;

let shouldLoadExternalLibsFromCdn = false;

/**
* Sets the location of the external libraries used by the engine for rhino3dm & draco. This is the content of the libs
* folder. Can be used to rely on jsdelivr to deliver theses libs to the client.
* @param {boolean} newValue.
*/
export function SetShouldLoadExternalLibsFromCdn (newValue) {
shouldLoadExternalLibsFromCdn = newValue;
}

let occtWorkerUrl = null;
export function CreateOcctWorker (worker)
{
return new Promise ((resolve, reject) => {
if(!shouldLoadExternalLibsFromCdn && occtWorkerUrl === null) {
resolve (new Worker (GetExternalLibPath('occt/occt-import-js-worker.js')));
return;
}

if (occtWorkerUrl !== null) {
resolve (new Worker (occtWorkerUrl));
return;
Expand Down Expand Up @@ -134,12 +150,37 @@ export function CreateOcctWorker (worker)
export function LoadExternalLibrary (libraryName)
{
if (libraryName === 'rhino3dm') {
return LoadExternalLibraryFromUrl ('https://cdn.jsdelivr.net/npm/rhino3dm@8.4.0/rhino3dm.min.js');
} else if (libraryName === 'webifc') {
return LoadExternalLibraryFromUrl ('https://cdn.jsdelivr.net/npm/web-ifc@0.0.55/web-ifc-api-iife.js');
return LoadRhino3dm ();
} else if (libraryName === 'draco3d') {
return LoadDraco ();
} else if (libraryName === 'web-ifc-api') {
return LoadIfcApi ();
}
else {
return null;
}
}

function LoadRhino3dm () {
if (shouldLoadExternalLibsFromCdn) {
return LoadExternalLibraryFromUrl ('https://cdn.jsdelivr.net/npm/rhino3dm@8.4.0/rhino3dm.min.js');
} else {
return LoadExternalLibraryFromLibs ('loaders/rhino3dm.min.js');
}
}

function LoadDraco () {
if (shouldLoadExternalLibsFromCdn) {
return LoadExternalLibraryFromUrl ('https://cdn.jsdelivr.net/npm/draco3d@1.5.7/draco_decoder_nodejs.min.js');
} else {
return null;
return LoadExternalLibraryFromLibs ('loaders/draco_decoder_nodejs.min.js');
}
}

function LoadIfcApi () {
if(shouldLoadExternalLibsFromCdn) {
return LoadExternalLibraryFromUrl('https://cdn.jsdelivr.net/npm/web-ifc@0.0.55/web-ifc-api-iife.js');
} else {
return LoadExternalLibraryFromLibs ('loaders/web-ifc-api-browser.js');
}
}
54 changes: 53 additions & 1 deletion source/engine/io/externallibs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@
let externalLibLocation = null;
let loadedExternalLibs = new Set ();
let loadedExternalLibUrls = new Set ();

/**
* Sets the location of the external libraries used by the engine. This is the content of the libs
* folder in the package. The location must be relative to the main file.
* @param {string} newExternalLibLocation Relative path to the libs folder.
*/
export function SetExternalLibLocation (newExternalLibLocation)
{
externalLibLocation = newExternalLibLocation;
}

export function GetExternalLibPath (libName)
{
if (externalLibLocation === null) {
return null;
}
return externalLibLocation + '/' + libName;
}

export function LoadExternalLibraryFromLibs (libName)
{
return new Promise ((resolve, reject) => {
if (externalLibLocation === null) {
reject ();
return;
}

if (loadedExternalLibs.has (libName)) {
resolve ();
return;
}

let scriptElement = document.createElement ('script');
scriptElement.type = 'text/javascript';
scriptElement.src = GetExternalLibPath (libName);
scriptElement.onload = () => {
loadedExternalLibs.add (libName);
resolve ();
};
scriptElement.onerror = () => {
reject ();
};
document.head.appendChild (scriptElement);
});
}

export function LoadExternalLibraryFromUrl (libraryUrl)
{
return new Promise ((resolve, reject) => {
if (externalLibLocation === null) {
reject ();
return;
}

if (loadedExternalLibUrls.has (libraryUrl)) {
resolve ();
return;
Expand All @@ -20,4 +72,4 @@ export function LoadExternalLibraryFromUrl (libraryUrl)
};
document.head.appendChild (scriptElement);
});
}
}
8 changes: 6 additions & 2 deletions source/engine/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ import { ImporterPly } from './import/importerply.js';
import { ImporterStl } from './import/importerstl.js';
import { ImporterThreeSvg } from './import/importersvg.js';
import { ImporterThreeBase, ImporterThreeFbx, ImporterThreeDae, ImporterThreeWrl, ImporterThree3mf, ImporterThreeAmf } from './import/importerthree.js';
import { ColorToMaterialConverter, NameFromLine, ParametersFromLine, ReadLines, IsPowerOfTwo, NextPowerOfTwo, UpdateMaterialTransparency, CreateOcctWorker, LoadExternalLibrary } from './import/importerutils.js';
import { ColorToMaterialConverter, NameFromLine, ParametersFromLine, ReadLines, IsPowerOfTwo, NextPowerOfTwo, UpdateMaterialTransparency, SetShouldLoadExternalLibsFromCdn, CreateOcctWorker, LoadExternalLibrary } from './import/importerutils.js';
import { BinaryReader } from './io/binaryreader.js';
import { BinaryWriter } from './io/binarywriter.js';
import { ArrayBufferToUtf8String, ArrayBufferToAsciiString, AsciiStringToArrayBuffer, Utf8StringToArrayBuffer, Base64DataURIToArrayBuffer, GetFileExtensionFromMimeType, CreateObjectUrl, CreateObjectUrlWithMimeType, RevokeObjectUrl } from './io/bufferutils.js';
import { LoadExternalLibraryFromUrl } from './io/externallibs.js';
import { SetExternalLibLocation, GetExternalLibPath, LoadExternalLibraryFromLibs, LoadExternalLibraryFromUrl } from './io/externallibs.js';
import { GetFileName, GetFileExtension, RequestUrl, ReadFile, TransformFileHostUrls, IsUrl, FileSource, FileFormat } from './io/fileutils.js';
import { TextWriter } from './io/textwriter.js';
import { RGBColor, RGBAColor, ColorComponentFromFloat, ColorComponentToFloat, RGBColorFromFloatComponents, SRGBToLinear, LinearToSRGB, IntegerToHexString, RGBColorToHexString, RGBAColorToHexString, HexStringToRGBColor, HexStringToRGBAColor, ArrayToRGBColor, RGBColorIsEqual } from './model/color.js';
Expand Down Expand Up @@ -193,6 +193,7 @@ export {
IsPowerOfTwo,
NextPowerOfTwo,
UpdateMaterialTransparency,
SetShouldLoadExternalLibsFromCdn,
CreateOcctWorker,
LoadExternalLibrary,
BinaryReader,
Expand All @@ -206,6 +207,9 @@ export {
CreateObjectUrl,
CreateObjectUrlWithMimeType,
RevokeObjectUrl,
SetExternalLibLocation,
GetExternalLibPath,
LoadExternalLibraryFromLibs,
LoadExternalLibraryFromUrl,
GetFileName,
GetFileExtension,
Expand Down
11 changes: 9 additions & 2 deletions source/website/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SetExternalLibLocation } from '../engine/io/externallibs.js';
import { Loc } from '../engine/core/localization.js';
import { AddDiv, AddDomElement } from '../engine/viewer/domutils.js';
import { Embed } from './embed.js';
Expand All @@ -6,6 +7,7 @@ import { SetEventHandler, HandleEvent } from './eventhandler.js';
import { PluginType, RegisterPlugin } from './pluginregistry.js';
import { ButtonDialog, ProgressDialog } from './dialog.js';
import { ShowMessageDialog } from './dialogs.js';
import { SetShouldLoadExternalLibsFromCdn } from '../engine/import/importerutils.js';

import * as Engine from '../engine/main.js';
export { Engine };
Expand Down Expand Up @@ -45,7 +47,7 @@ export function RegisterToolbarPlugin (plugin)
RegisterPlugin (PluginType.Toolbar, plugin);
}

export function StartWebsite ()
export function StartWebsite (externalLibLocation)
{
window.addEventListener ('load', () => {
if (window.self !== window.top) {
Expand All @@ -57,6 +59,9 @@ export function StartWebsite ()
return;
}

SetExternalLibLocation (externalLibLocation);
SetShouldLoadExternalLibsFromCdn (true);

document.getElementById ('intro_dragdrop_text').innerHTML = Loc ('Drag and drop 3D models here.');
document.getElementById ('intro_formats_title').innerHTML = Loc ('Check an example file:');

Expand All @@ -81,8 +86,10 @@ export function StartWebsite ()
});
}

export function StartEmbed ()
export function StartEmbed (externalLibLocation)
{
SetExternalLibLocation (externalLibLocation);
SetShouldLoadExternalLibsFromCdn (true);
window.addEventListener ('load', () => {
let embed = new Embed ({
viewerDiv : document.getElementById ('embed_viewer'),
Expand Down
2 changes: 1 addition & 1 deletion website/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<!-- website analytics end -->

<script type="text/javascript">
OV.StartWebsite ();
OV.StartWebsite ('../libs');
</script>

</head>
Expand Down