Handle external textures.

This commit is contained in:
kovacsv 2021-08-08 13:50:37 +02:00
parent d244cd458a
commit 9432b5b9de

View File

@ -76,8 +76,9 @@ OV.ThreeImporter = class extends OV.ImporterBase
LoadModel (fileContent, onFinish) LoadModel (fileContent, onFinish)
{ {
let loadedScene = null; let loadedScene = null;
let blobToBuffer = {};
let loadingManager = new THREE.LoadingManager (() => { let loadingManager = new THREE.LoadingManager (() => {
this.OnThreeObjectsLoaded (loadedScene, onFinish); this.OnThreeObjectsLoaded (loadedScene, blobToBuffer, onFinish);
}); });
const mainFileUrl = OV.CreateObjectUrl (fileContent); const mainFileUrl = OV.CreateObjectUrl (fileContent);
@ -90,6 +91,10 @@ OV.ThreeImporter = class extends OV.ImporterBase
} }
const fileBuffer = this.callbacks.getFileBuffer (url); const fileBuffer = this.callbacks.getFileBuffer (url);
const fileUrl = OV.CreateObjectUrl (fileBuffer); const fileUrl = OV.CreateObjectUrl (fileBuffer);
blobToBuffer[fileUrl] = {
name : OV.GetFileName (url),
buffer : fileBuffer
};
return fileUrl; return fileUrl;
}); });
@ -114,9 +119,9 @@ OV.ThreeImporter = class extends OV.ImporterBase
); );
} }
OnThreeObjectsLoaded (scene, onFinish) OnThreeObjectsLoaded (scene, blobToBuffer, onFinish)
{ {
function ConvertThreeMaterialToMaterial (threeMaterial) function ConvertThreeMaterialToMaterial (threeMaterial, blobToBuffer)
{ {
function SetColor (color, threeColor) function SetColor (color, threeColor)
{ {
@ -127,19 +132,34 @@ OV.ThreeImporter = class extends OV.ImporterBase
); );
} }
function CreateTexture (threeMap) function CreateTexture (threeMap, blobToBuffer)
{ {
if (threeMap.image === undefined || threeMap.image === null) { if (threeMap.image === undefined || threeMap.image === null) {
return null; return null;
} }
let base64Buffer = OV.Base64DataURIToArrayBuffer (threeMap.image.currentSrc); let imageSrc = threeMap.image.currentSrc;
let texture = new OV.TextureMap (); if (imageSrc.startsWith ('data:')) {
texture.name = 'Embedded.' + OV.GetFileExtensionFromMimeType (base64Buffer.mimeType); let base64Buffer = OV.Base64DataURIToArrayBuffer (threeMap.image.currentSrc);
texture.url = OV.CreateObjectUrlWithMimeType (base64Buffer.buffer, base64Buffer.mimeType); let texture = new OV.TextureMap ();
texture.buffer = base64Buffer.buffer; texture.name = 'Embedded.' + OV.GetFileExtensionFromMimeType (base64Buffer.mimeType);
return texture; texture.url = OV.CreateObjectUrlWithMimeType (base64Buffer.buffer, base64Buffer.mimeType);
texture.buffer = base64Buffer.buffer;
return texture;
} else if (imageSrc.startsWith ('blob:')) {
let buffer = blobToBuffer[imageSrc];
if (buffer === undefined) {
return null;
}
let texture = new OV.TextureMap ();
texture.name = buffer.name;
texture.url = imageSrc;
texture.buffer = buffer.buffer;
return texture;
}
return null;
} }
// TODO: PBR materials
let material = new OV.Material (OV.MaterialType.Phong); let material = new OV.Material (OV.MaterialType.Phong);
material.name = threeMaterial.name; material.name = threeMaterial.name;
SetColor (material.color, threeMaterial.color); SetColor (material.color, threeMaterial.color);
@ -148,19 +168,28 @@ OV.ThreeImporter = class extends OV.ImporterBase
material.shininess = threeMaterial.shininess / 100.0; material.shininess = threeMaterial.shininess / 100.0;
} }
if (threeMaterial.map !== undefined && threeMaterial.map !== null) { if (threeMaterial.map !== undefined && threeMaterial.map !== null) {
material.diffuseMap = CreateTexture (threeMaterial.map); material.diffuseMap = CreateTexture (threeMaterial.map, blobToBuffer);
} }
return material; return material;
} }
function FindMatchingMaterialIndex (model, material)
{
for (let i = 0; i < model.MaterialCount (); i++) {
let modelMaterial = model.GetMaterial (i);
if (OV.MaterialIsEqual (modelMaterial, material)) {
return i;
}
}
return model.AddMaterial (material);
}
this.loadedScene = scene; this.loadedScene = scene;
scene.traverse ((child) => { scene.traverse ((child) => {
if (child.isMesh) { if (child.isMesh) {
// TODO: merge same materials // TODO: array of materials
// TODO: PBR materials let material = ConvertThreeMaterialToMaterial (child.material, blobToBuffer);
console.log (child); const materialIndex = FindMatchingMaterialIndex (this.model, material);
let material = ConvertThreeMaterialToMaterial (child.material);
const materialIndex = this.model.AddMaterial (material);
let mesh = OV.ConvertThreeGeometryToMesh (child.geometry, materialIndex); let mesh = OV.ConvertThreeGeometryToMesh (child.geometry, materialIndex);
if (child.matrixWorld !== undefined && child.matrixWorld !== null) { if (child.matrixWorld !== undefined && child.matrixWorld !== null) {
const matrix = new OV.Matrix (child.matrixWorld.elements); const matrix = new OV.Matrix (child.matrixWorld.elements);