Rhino import experiment.

This commit is contained in:
Viktor Kovacs 2021-04-15 20:23:08 +02:00
parent 79207aa413
commit a2aaedbbae
8 changed files with 238 additions and 1 deletions

View File

@ -21,6 +21,7 @@
"globals" : {
"OV" : true,
"THREE" : false,
"rhino3dm" : false,
"TextEncoder" : false,
"TextDecoder" : false,
"XMLHttpRequest" : false,

21
libs/rhino3dm.license.txt Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Robert McNeel & Associates
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

8
libs/rhino3dm.min.js vendored Normal file

File diff suppressed because one or more lines are too long

BIN
libs/rhino3dm.wasm Normal file

Binary file not shown.

199
source/external/rhino.importer.js vendored Normal file
View File

@ -0,0 +1,199 @@
OV.Importer3dm = class extends OV.ImporterBase
{
constructor ()
{
super ();
this.rhino = null;
this.colorToMaterial = null;
}
ResetState ()
{
this.colorToMaterial = {};
}
CanImportExtension (extension)
{
return extension === '3dm';
}
GetKnownFileFormats ()
{
return {
'3dm' : OV.FileFormat.Binary
};
}
GetUpDirection ()
{
return OV.Direction.X;
}
ImportContent (fileContent, onFinish)
{
if (this.rhino === null) {
let obj = this;
rhino3dm ().then (function (rhino) {
obj.rhino = rhino;
obj.ImportRhinoContent (fileContent);
onFinish ();
});
} else {
this.ImportRhinoContent (fileContent);
onFinish ();
}
}
ImportRhinoContent (fileContent)
{
let rhinoDoc = this.rhino.File3dm.fromByteArray (fileContent);
this.ImportRhinoDocument (rhinoDoc);
}
ImportRhinoDocument (rhinoDoc)
{
let rhinoObjects = rhinoDoc.objects ();
for (let i = 0; i < rhinoObjects.count; i++) {
let rhinoObject = rhinoObjects.get (i);
this.ImportRhinoObject (rhinoDoc, rhinoObject);
}
}
ImportRhinoObject (rhinoDoc, rhinoObject)
{
let rhinoMesh = null;
let deleteMesh = false;
let rhinoGeometry = rhinoObject.geometry ();
let rhinoAttributes = rhinoObject.attributes ();
if (rhinoAttributes.isInstanceDefinitionObject) {
// TODO: handle instances
return;
}
let objectType = rhinoGeometry.objectType;
if (objectType === this.rhino.ObjectType.Mesh) {
rhinoMesh = rhinoGeometry;
deleteMesh = false;
} else if (objectType === this.rhino.ObjectType.Extrusion) {
rhinoMesh = rhinoGeometry.getMesh (this.rhino.MeshType.Any);
deleteMesh = true;
} else if (objectType === this.rhino.ObjectType.Brep) {
rhinoMesh = new this.rhino.Mesh ();
let faces = rhinoGeometry.faces ();
for (let i = 0; i < faces.count; i++) {
let face = faces.get (i);
let mesh = face.getMesh (this.rhino.MeshType.Any);
if (mesh) {
rhinoMesh.append (mesh);
mesh.delete ();
}
face.delete ();
}
faces.delete ();
rhinoMesh.compact ();
deleteMesh = true;
}
if (rhinoMesh !== null) {
this.ImportRhinoMesh (rhinoDoc, rhinoMesh, rhinoAttributes);
if (deleteMesh) {
rhinoMesh.delete ();
}
}
}
ImportRhinoMesh (rhinoDoc, rhinoMesh, rhinoAttributes)
{
let mesh = new OV.Mesh ();
mesh.SetName (rhinoAttributes.name);
let materialIndex = this.GetMaterialIndex (rhinoDoc, rhinoAttributes);
let threeJson = rhinoMesh.toThreejsJSON ();
let vertices = threeJson.data.attributes.position.array;
for (let i = 0; i < vertices.length; i += 3) {
let x = vertices[i];
let y = vertices[i + 1];
let z = vertices[i + 2];
mesh.AddVertex (new OV.Coord3D (x, y, z));
}
let hasNormals = (threeJson.data.attributes.normal !== undefined);
if (hasNormals) {
let normals = threeJson.data.attributes.normal.array;
for (let i = 0; i < normals.length; i += 3) {
let x = normals[i];
let y = normals[i + 1];
let z = normals[i + 2];
mesh.AddNormal (new OV.Coord3D (x, y, z));
}
}
let hasUVs = (threeJson.data.attributes.uv !== undefined);
if (hasUVs) {
let uvs = threeJson.data.attributes.uv.array;
for (let i = 0; i < uvs.length; i += 2) {
let x = uvs[i];
let y = uvs[i + 1];
mesh.AddTextureUV (new OV.Coord2D (x, y));
}
}
let indices = threeJson.data.index.array;
for (let i = 0; i < indices.length; i += 3) {
let v0 = indices[i];
let v1 = indices[i + 1];
let v2 = indices[i + 2];
let triangle = new OV.Triangle (v0, v1, v2);
if (hasNormals) {
triangle.SetNormals (v0, v1, v2);
}
if (hasUVs) {
triangle.SetTextureUVs (v0, v1, v2);
}
if (materialIndex !== null) {
triangle.SetMaterial (materialIndex);
}
mesh.AddTriangle (triangle);
}
this.model.AddMesh (mesh);
}
GetMaterialIndex (rhinoDoc, rhinoAttributes)
{
function IntegerToHex (intVal)
{
let result = parseInt (intVal, 10).toString (16);
while (result.length < 2) {
result = '0' + result;
}
return result;
}
let rhinoColor = null;
if (rhinoAttributes.colorSource === this.rhino.ObjectColorSource.ColorFromObject) {
rhinoColor = rhinoAttributes.objectColor;
} else if (rhinoAttributes.colorSource === this.rhino.ObjectColorSource.ColorFromLayer) {
let layerIndex = rhinoAttributes.layerIndex;
if (layerIndex > 0) {
let layer = rhinoDoc.layers ().get (layerIndex);
rhinoColor = layer.color;
}
} else if (rhinoAttributes.colorSource === this.rhino.ObjectColorSource.ColorFromParent) {
// TODO: handle instances
}
let color = new OV.Color (250, 250, 250);
if (rhinoColor !== null) {
color = new OV.Color (rhinoColor.r, rhinoColor.g, rhinoColor.b);
}
// TODO: transparency?
let materialName = '#' + IntegerToHex (color.r) + IntegerToHex (color.g) + IntegerToHex (color.b);
let materialIndex = this.colorToMaterial[materialName];
if (materialIndex === undefined) {
let material = new OV.Material ();
material.diffuse = color;
materialIndex = this.model.AddMaterial (material);
this.colorToMaterial[materialName] = materialIndex;
}
return materialIndex;
}
};

View File

@ -3,6 +3,7 @@ OV.ThreeModelLoader = class
constructor ()
{
this.importer = new OV.Importer ();
this.importer.AddImporter (new OV.Importer3dm ());
this.callbacks = null;
this.inProgress = false;
}

View File

@ -273,6 +273,11 @@ OV.Importer = class
this.missingFiles = [];
}
AddImporter (importer)
{
this.importers.push (importer);
}
LoadFilesFromUrls (fileList, onReady)
{
this.LoadFiles (fileList, OV.FileSource.Url, onReady);

View File

@ -1,7 +1,8 @@
{
"lib_files" : [
"libs/jquery-3.5.1.min.js",
"libs/three.min-126.js"
"libs/three.min-126.js",
"libs/rhino3dm.min.js"
],
"importer_files" : [
"source/core/core.js",
@ -40,6 +41,7 @@
"source/export/exporteroff.js",
"source/export/exportergltf.js",
"source/export/exporter.js",
"source/external/rhino.importer.js",
"source/external/three.converter.js",
"source/external/three.model.loader.js",
"source/parameters/parameterlist.js",