Remove url from texture, since it's not a core data, and needed only for three.js conversion.

This commit is contained in:
kovacsv 2022-04-27 18:19:55 +02:00
parent fd6922617f
commit afe6cce6da
14 changed files with 50 additions and 134 deletions

View File

@ -1,5 +1,4 @@
import { RunTaskAsync } from '../core/taskrunner.js';
import { CreateObjectUrl, RevokeObjectUrl } from '../io/bufferutils.js';
import { LoadExternalLibrary } from '../io/externallibs.js';
import { FileSource, GetFileName } from '../io/fileutils.js';
import { Color } from '../model/color.js';
@ -61,7 +60,6 @@ export class ImporterFileAccessor
{
this.getBufferCallback = getBufferCallback;
this.fileBuffers = new Map ();
this.textureBuffers = new Map ();
}
GetFileBuffer (filePath)
@ -74,24 +72,6 @@ export class ImporterFileAccessor
this.fileBuffers.set (fileName, buffer);
return buffer;
}
GetTextureBuffer (filePath)
{
let fileName = GetFileName (filePath);
if (this.textureBuffers.has (fileName)) {
return this.textureBuffers.get (fileName);
}
let buffer = null;
let textureBuffer = this.getBufferCallback (fileName);
if (textureBuffer !== null) {
buffer = {
url : CreateObjectUrl (textureBuffer),
buffer : textureBuffer
};
}
this.textureBuffers.set (fileName, buffer);
return buffer;
}
}
export class Importer
@ -210,7 +190,6 @@ export class Importer
return;
}
this.RevokeModelUrls ();
this.model = null;
this.usedFiles = [];
this.missingFiles = [];
@ -237,9 +216,6 @@ export class Importer
getFileBuffer : (filePath) => {
return fileAccessor.GetFileBuffer (filePath);
},
getTextureBuffer : (filePath) => {
return fileAccessor.GetTextureBuffer (filePath);
},
onSuccess : () => {
this.model = importer.GetModel ();
let result = new ImportResult ();
@ -332,19 +308,4 @@ export class Importer
}
return importableFiles;
}
RevokeModelUrls ()
{
if (this.model === null) {
return;
}
for (let i = 0; i < this.model.MaterialCount (); i++) {
let material = this.model.GetMaterial (i);
material.EnumerateTextureMaps ((texture) => {
if (texture.url !== null) {
RevokeObjectUrl (texture.url);
}
});
}
}
}

View File

@ -290,12 +290,9 @@ export class Importer3dm extends ImporterBase
if (rhinoTexture) {
let texture = new TextureMap ();
let textureName = GetFileName (rhinoTexture.fileName);
let textureBuffer = callbacks.getTextureBuffer (textureName);
let textureBuffer = callbacks.getFileBuffer (textureName);
texture.name = textureName;
if (textureBuffer !== null) {
texture.url = textureBuffer.url;
texture.buffer = textureBuffer.buffer;
}
texture.buffer = textureBuffer;
material.diffuseMap = texture;
}

View File

@ -224,12 +224,9 @@ export class Importer3ds extends ImporterBase
this.ReadChunks (reader, endByte, (chunkId, chunkLength) => {
if (chunkId === CHUNK3DS.MAT_TEXMAP_NAME) {
let textureName = this.ReadName (reader);
let textureBuffer = this.callbacks.getTextureBuffer (textureName);
let textureBuffer = this.callbacks.getFileBuffer (textureName);
texture.name = textureName;
if (textureBuffer !== null) {
texture.url = textureBuffer.url;
texture.buffer = textureBuffer.buffer;
}
texture.buffer = textureBuffer;
} else if (chunkId === CHUNK3DS.MAT_TEXMAP_UOFFSET) {
texture.offset.x = reader.ReadFloat32 ();
} else if (chunkId === CHUNK3DS.MAT_TEXMAP_VOFFSET) {

View File

@ -6,7 +6,7 @@ import { Matrix } from '../geometry/matrix.js';
import { ArrayToQuaternion } from '../geometry/quaternion.js';
import { Transformation } from '../geometry/transformation.js';
import { BinaryReader } from '../io/binaryreader.js';
import { ArrayBufferToUtf8String, Base64DataURIToArrayBuffer, CreateObjectUrlWithMimeType, GetFileExtensionFromMimeType } from '../io/bufferutils.js';
import { ArrayBufferToUtf8String, Base64DataURIToArrayBuffer, GetFileExtensionFromMimeType } from '../io/bufferutils.js';
import { LoadExternalLibrary } from '../io/externallibs.js';
import { Color, ColorComponentFromFloat, ColorFromFloatComponents, LinearToSRGB } from '../model/color.js';
import { PhongMaterial, PhysicalMaterial, TextureMap } from '../model/material.js';
@ -751,7 +751,7 @@ export class ImporterGltf extends ImporterBase
} else {
textureParams = {
name : null,
url : null,
mimeType : null,
buffer : null
};
let textureIndexString = gltfImageIndex.toString ();
@ -759,15 +759,12 @@ export class ImporterGltf extends ImporterBase
let base64Buffer = Base64DataURIToArrayBuffer (gltfImage.uri);
if (base64Buffer !== null) {
textureParams.name = 'Embedded_' + textureIndexString + '.' + GetFileExtensionFromMimeType (base64Buffer.mimeType);
textureParams.url = CreateObjectUrlWithMimeType (base64Buffer.buffer, base64Buffer.mimeType);
textureParams.mimeType = base64Buffer.mimeType;
textureParams.buffer = base64Buffer.buffer;
} else {
let textureBuffer = this.callbacks.getTextureBuffer (gltfImage.uri);
let textureBuffer = this.callbacks.getFileBuffer (gltfImage.uri);
textureParams.name = gltfImage.uri;
if (textureBuffer !== null) {
textureParams.url = textureBuffer.url;
textureParams.buffer = textureBuffer.buffer;
}
textureParams.buffer = textureBuffer;
}
} else if (gltfImage.bufferView !== undefined) {
let bufferView = gltf.bufferViews[gltfImage.bufferView];
@ -775,7 +772,7 @@ export class ImporterGltf extends ImporterBase
if (reader !== null) {
let buffer = reader.ReadArrayBuffer (bufferView.byteLength);
textureParams.name = 'Binary_' + textureIndexString + '.' + GetFileExtensionFromMimeType (gltfImage.mimeType);
textureParams.url = CreateObjectUrlWithMimeType (buffer, gltfImage.mimeType);
textureParams.mimeType = gltfImage.mimeType;
textureParams.buffer = buffer;
}
}
@ -783,7 +780,7 @@ export class ImporterGltf extends ImporterBase
}
texture.name = textureParams.name;
texture.url = textureParams.url;
texture.mimeType = textureParams.mimeType;
texture.buffer = textureParams.buffer;
this.gltfExtensions.ProcessTexture (gltfTextureRef, texture);

View File

@ -230,12 +230,9 @@ export class ImporterObj extends ImporterBase
{
let texture = new TextureMap ();
let textureName = NameFromLine (line, keyword.length, '#');
let textureBuffer = callbacks.getTextureBuffer (textureName);
let textureBuffer = callbacks.getFileBuffer (textureName);
texture.name = textureName;
if (textureBuffer !== null) {
texture.url = textureBuffer.url;
texture.buffer = textureBuffer.buffer;
}
texture.buffer = textureBuffer;
return texture;
}

View File

@ -259,7 +259,7 @@ export class ImporterThreeBase extends ImporterBase
textureName = 'Embedded_' + threeMap.id.toString () + '.' + GetFileExtensionFromMimeType (base64Buffer.mimeType);
}
texture.name = textureName;
texture.url = dataUrl;
texture.mimeType = base64Buffer.mimeType;
texture.buffer = base64Buffer.buffer;
texture.rotation = threeMap.rotation;
texture.offset.x = threeMap.offset.x;

View File

@ -7,7 +7,7 @@ export class TextureMap
constructor ()
{
this.name = null;
this.url = null;
this.mimeType = null;
this.buffer = null;
this.offset = new Coord2D (0.0, 0.0);
this.scale = new Coord2D (1.0, 1.0);
@ -16,7 +16,7 @@ export class TextureMap
IsValid ()
{
return this.name !== null && this.url !== null && this.buffer !== null;
return this.name !== null && this.buffer !== null;
}
HasTransformation ()
@ -38,7 +38,7 @@ export class TextureMap
if (this.name !== rhs.name) {
return false;
}
if (this.url !== rhs.url) {
if (this.mimeType !== rhs.mimeType) {
return false;
}
if (!CoordIsEqual2D (this.offset, rhs.offset)) {
@ -158,22 +158,6 @@ export class FaceMaterial extends MaterialBase
}
return true;
}
EnumerateTextureMaps (enumerator)
{
if (this.diffuseMap !== null) {
enumerator (this.diffuseMap);
}
if (this.bumpMap !== null) {
enumerator (this.bumpMap);
}
if (this.normalMap !== null) {
enumerator (this.normalMap);
}
if (this.emissiveMap !== null) {
enumerator (this.emissiveMap);
}
}
}
export class PhongMaterial extends FaceMaterial
@ -207,14 +191,6 @@ export class PhongMaterial extends FaceMaterial
}
return true;
}
EnumerateTextureMaps (enumerator)
{
super.EnumerateTextureMaps (enumerator);
if (this.specularMap !== null) {
enumerator (this.specularMap);
}
}
}
export class PhysicalMaterial extends FaceMaterial
@ -244,14 +220,6 @@ export class PhysicalMaterial extends FaceMaterial
}
return true;
}
EnumerateTextureMaps (enumerator)
{
super.EnumerateTextureMaps (enumerator);
if (this.metalnessMap !== null) {
enumerator (this.metalnessMap);
}
}
}
export function TextureIsEqual (a, b)
@ -259,7 +227,7 @@ export function TextureIsEqual (a, b)
if (a.name !== b.name) {
return false;
}
if (a.url !== b.url) {
if (a.mimeType !== b.mimeType) {
return false;
}
if (!CoordIsEqual2D (a.offset, b.offset)) {

View File

@ -1,5 +1,6 @@
import { RunTasksBatch } from '../core/taskrunner.js';
import { IsEqual } from '../geometry/geometry.js';
import { CreateObjectUrl, CreateObjectUrlWithMimeType } from '../main.js';
import { MaterialType } from '../model/material.js';
import { MeshInstanceId } from '../model/meshinstance.js';
import { GetMeshType, MeshType } from '../model/meshutils.js';
@ -18,6 +19,7 @@ export class ModelToThreeConversionOutput
constructor ()
{
this.defaultMaterial = null;
this.objectUrls = [];
}
}
@ -106,14 +108,21 @@ export function ConvertModelToThreeObject (model, params, output, callbacks)
threeTexture.repeat.y = texture.scale.y;
}
function LoadTexture (stateHandler, threeMaterial, texture, onTextureLoaded)
function LoadTexture (stateHandler, threeMaterial, texture, output, onTextureLoaded)
{
if (texture === null || !texture.IsValid ()) {
return;
}
let loader = new THREE.TextureLoader ();
stateHandler.OnTextureNeeded ();
loader.load (texture.url,
let textureObjectUrl = null;
if (texture.mimeType !== null) {
textureObjectUrl = CreateObjectUrlWithMimeType (texture.buffer, texture.mimeType);
} else {
textureObjectUrl = CreateObjectUrl (texture.buffer);
}
output.objectUrls.push (textureObjectUrl);
loader.load (textureObjectUrl,
(threeTexture) => {
SetTextureParameters (texture, threeTexture);
threeMaterial.needsUpdate = true;
@ -156,7 +165,7 @@ export function ConvertModelToThreeObject (model, params, output, callbacks)
}
threeMaterial.specular = specularColor;
threeMaterial.shininess = material.shininess * 100.0;
LoadTexture (stateHandler, threeMaterial, material.specularMap, (threeTexture) => {
LoadTexture (stateHandler, threeMaterial, material.specularMap, output, (threeTexture) => {
threeMaterial.specularMap = threeTexture;
});
}
@ -165,7 +174,7 @@ export function ConvertModelToThreeObject (model, params, output, callbacks)
if (material.type === MaterialType.Physical) {
threeMaterial.metalness = material.metalness;
threeMaterial.roughness = material.roughness;
LoadTexture (stateHandler, threeMaterial, material.metalnessMap, (threeTexture) => {
LoadTexture (stateHandler, threeMaterial, material.metalnessMap, output, (threeTexture) => {
threeMaterial.metalness = 1.0;
threeMaterial.roughness = 1.0;
threeMaterial.metalnessMap = threeTexture;
@ -177,19 +186,19 @@ export function ConvertModelToThreeObject (model, params, output, callbacks)
let emissiveColor = ConvertColorToThreeColor (material.emissive);
threeMaterial.emissive = emissiveColor;
LoadTexture (stateHandler, threeMaterial, material.diffuseMap, (threeTexture) => {
LoadTexture (stateHandler, threeMaterial, material.diffuseMap, output, (threeTexture) => {
if (!material.multiplyDiffuseMap) {
threeMaterial.color.setRGB (1.0, 1.0, 1.0);
}
threeMaterial.map = threeTexture;
});
LoadTexture (stateHandler, threeMaterial, material.bumpMap, (threeTexture) => {
LoadTexture (stateHandler, threeMaterial, material.bumpMap, output, (threeTexture) => {
threeMaterial.bumpMap = threeTexture;
});
LoadTexture (stateHandler, threeMaterial, material.normalMap, (threeTexture) => {
LoadTexture (stateHandler, threeMaterial, material.normalMap, output, (threeTexture) => {
threeMaterial.normalMap = threeTexture;
});
LoadTexture (stateHandler, threeMaterial, material.emissiveMap, (threeTexture) => {
LoadTexture (stateHandler, threeMaterial, material.emissiveMap, output, (threeTexture) => {
threeMaterial.emissiveMap = threeTexture;
});

View File

@ -1,5 +1,6 @@
import { Direction } from '../geometry/geometry.js';
import { Importer } from '../import/importer.js';
import { RevokeObjectUrl } from '../main.js';
import { ConvertModelToThreeObject, ModelToThreeConversionOutput, ModelToThreeConversionParams } from './threeconverter.js';
import { ConvertColorToThreeColor, HasHighpDriverIssue } from './threeutils.js';
@ -10,6 +11,7 @@ export class ThreeModelLoader
this.importer = new Importer ();
this.inProgress = false;
this.defaultMaterial = null;
this.objectUrls = null;
this.hasHighpDriverIssue = HasHighpDriverIssue ();
}
@ -26,6 +28,7 @@ export class ThreeModelLoader
this.inProgress = true;
callbacks.onLoadStart ();
this.RevokeObjectUrls ();
this.importer.ImportFiles (files, fileSource, settings, {
onFilesLoaded : () => {
callbacks.onImportStart ();
@ -48,6 +51,7 @@ export class ThreeModelLoader
},
onModelLoaded : (threeObject) => {
this.defaultMaterial = output.defaultMaterial;
this.objectUrls = output.objectUrls;
if (importResult.upVector === Direction.X) {
let rotation = new THREE.Quaternion ().setFromAxisAngle (new THREE.Vector3 (0.0, 0.0, 1.0), Math.PI / 2.0);
threeObject.quaternion.multiply (rotation);
@ -83,4 +87,15 @@ export class ThreeModelLoader
this.defaultMaterial.color = ConvertColorToThreeColor (defaultColor);
}
}
RevokeObjectUrls ()
{
if (this.objectUrls === null) {
return;
}
for (let objectUrl of this.objectUrls) {
RevokeObjectUrl (objectUrl);
}
this.objectUrls = null;
}
}

View File

@ -15,15 +15,12 @@ function CreateTestModel ()
material1.specular = new OV.Color (51, 51, 51);
material1.diffuseMap = new OV.TextureMap ();
material1.diffuseMap.name = 'textures/texture1.png';
material1.diffuseMap.url = 'texture1_url';
material1.diffuseMap.buffer = new ArrayBuffer (1);
material1.specularMap = new OV.TextureMap ();
material1.specularMap.name = 'textures/texture2.png';
material1.specularMap.url = 'texture2_url';
material1.specularMap.buffer = new ArrayBuffer (2);
material1.bumpMap = new OV.TextureMap ();
material1.bumpMap.name = 'textures/texture3.png';
material1.bumpMap.url = 'texture3_url';
material1.bumpMap.buffer = new ArrayBuffer (3);
model.AddMaterial (material1);
@ -352,9 +349,6 @@ describe ('Exporter', function () {
}
return null;
},
getTextureBuffer (filePath) {
return null;
},
onSuccess () {
let importedModel = importer.GetModel ();
assert.ok (OV.CheckModel (importedModel));
@ -387,9 +381,6 @@ describe ('Exporter', function () {
getFileBuffer (filePath) {
return null;
},
getTextureBuffer (filePath) {
return null;
},
onSuccess () {
let importedModel = importer.GetModel ();
assert.ok (OV.CheckModel (importedModel));

View File

@ -10,7 +10,6 @@ function CreateTestModel ()
{
let texture = new OV.TextureMap ();
texture.name = name;
texture.url = url;
texture.buffer = new ArrayBuffer (1);
return texture;
}

View File

@ -438,7 +438,6 @@ describe ('Obj Importer', function () {
it ('two_materials_same_texture.obj', function (done) {
ImportObjFile ('two_materials_same_texture.obj', function (model) {
assert.strictEqual (model.GetMaterial (0).diffuseMap.url, model.GetMaterial (1).diffuseMap.url);
assert.strictEqual (model.GetMaterial (0).diffuseMap.buffer.byteLength, model.GetMaterial (1).diffuseMap.buffer.byteLength);
assert.ok (OV.CheckModel (model));
assert.deepStrictEqual (ModelToObject (model), {

View File

@ -12,17 +12,6 @@ export default function SetGlobals ()
};
var objectUrlCounter = 0;
global.URL = {
createObjectURL : function () {
objectUrlCounter += 1;
return 'ObjectUrl:' + objectUrlCounter.toString ();
},
revokeObjectURL : function () {
}
};
global.FileObject = function (folderName, fileName, fileContent)
{
this.name = path.join (folderName, fileName);

View File

@ -17,9 +17,6 @@ export function ImportFile (importer, folder, fileName, onReady)
getFileBuffer : function (filePath) {
return fileAccessor.GetFileBuffer (filePath);
},
getTextureBuffer : function (filePath) {
return fileAccessor.GetTextureBuffer (filePath);
},
onSuccess : function () {
let model = importer.GetModel ();
onReady (model);