Skip to content
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

Use buildShape in objToMesh #658

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
80 changes: 54 additions & 26 deletions packages/base/src/3dview/mainview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { TransformControls } from 'three/examples/jsm/controls/TransformControls
import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter.js';
import { STLLoader } from 'three/examples/jsm/loaders/STLLoader';
import { ViewHelper } from 'three/examples/jsm/helpers/ViewHelper';
import { IParsedShape } from '@jupytercad/schema';

import { FloatingAnnotation } from '../annotation';
import { getCSSVariableColor, throttle } from '../tools';
Expand Down Expand Up @@ -996,38 +997,65 @@ export class MainView extends React.Component<IProps, IStates> {
postResult: IPostResult
): Promise<void> {
const { binary, format, value } = postResult;
let obj: THREE.BufferGeometry | undefined = undefined;
if (format === 'STL') {
let buff: string | ArrayBuffer;
if (binary) {
const str = `data:application/octet-stream;base64,${value}`;
const b = await fetch(str);
buff = await b.arrayBuffer();
} else {
buff = value;
}
const loader = new STLLoader();
obj = loader.parse(buff);
if (format !== 'STL') {
return;
}
let buff: string | ArrayBuffer;
if (binary) {
const str = `data:application/octet-stream;base64,${value}`;
const b = await fetch(str);
buff = await b.arrayBuffer();
} else {
buff = value;
}
const loader = new STLLoader();
const geometry = loader.parse(buff);

if (!obj) {
if (!geometry) {
return;
}

const material = new THREE.MeshPhongMaterial({
color: DEFAULT_MESH_COLOR,
wireframe: this.state.wireframe
const parsedShape: IParsedShape = {
jcObject: { name: 'example', visible: true },
faceList: [],
edgeList: []
};

const obj = this._model.sharedModel.getObjectByName(name);
const objColor = obj?.parameters?.Color;
const isWireframe = this.state.wireframe;

const output = buildShape({
objName: name,
data: parsedShape,
clippingPlanes: this._clippingPlanes,
isSolid: true,
isWireframe,
objColor
});
const mesh = new THREE.Mesh(obj, material);

const lineGeo = new THREE.WireframeGeometry(mesh.geometry);
const mat = new THREE.LineBasicMaterial({ color: 'black' });
const wireframe = new THREE.LineSegments(lineGeo, mat);
mesh.add(wireframe);
mesh.name = name;
if (this._meshGroup) {
this._meshGroup.add(mesh);
this._boundingGroup?.expandByObject(mesh);

if (output) {
const { meshGroup, mainMesh, edgesMeshes } = output;

if (meshGroup.userData.jcObject.visible) {
this._boundingGroup?.expandByObject(meshGroup);
}

if (mainMesh.material?.color) {
const originalMeshColor = new THREE.Color(
objColor || DEFAULT_MESH_COLOR
);
mainMesh.material.color = originalMeshColor;
mainMesh.userData.originalColor = originalMeshColor.clone();
}

edgesMeshes.forEach(edgeMesh => {
this._edgeMaterials.push(edgeMesh.material);
const edgeColor = new THREE.Color(objColor || DEFAULT_EDGE_COLOR);
edgeMesh.material.color = edgeColor;
});
Comment on lines +1067 to +1079
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems weird to me we have to do this?

Shouldn't this be done in buildShape?


this._meshGroup?.add(meshGroup);
}
this._updateRefLength(true);
}
Expand Down
Loading