feature (plg_application_3d): revamp support for 3d files

This commit is contained in:
MickaelK 2025-04-23 00:36:38 +10:00
parent e7c8ee3bc9
commit 3b6253cfbd
5 changed files with 117 additions and 4 deletions

View file

@ -172,6 +172,8 @@
"sr2": "image/x-sony-sr2",
"srw": "image/x-samsung-srw",
"stl": "model/stl",
"step": "model/step",
"stp": "model/step",
"svg": "image/svg+xml",
"svgz": "image/svg+xml",
"swf": "application/x-shockwave-flash",

View file

@ -61,7 +61,8 @@ export default async function(render, { mime, acl$, getDownloadUrl = nop, getFil
)),
rxjs.catchError((err) => {
let _err = err;
if (err.response.status === 401) {
console.log("ERR", err);
if (err.response && err.response.status === 401) {
_err = new Error(err.message);
_err.status = err.response.status;
_err = new AjaxError(err.message, _err, "Not Authorised");
@ -82,7 +83,7 @@ function create3DScene({ mesh, $draw, $toolbar, $menubar, hasCube, is2D }) {
is2D,
});
withLight({ scene, box });
withLight({ scene, box, camera });
if (hasCube && !is2D()) withCube({ camera, renderer, refresh, controls });
ctrlToolbar(createRender($toolbar), {
mesh,

View file

@ -3,7 +3,7 @@ import * as THREE from "../../../../lib/vendor/three/three.module.js";
const LIGHT_COLOR = 0xf5f5f5;
export default function({ scene, box }) {
export default function({ scene, box, camera }) {
addLight(
scene,
new THREE.AmbientLight(LIGHT_COLOR),
@ -18,8 +18,10 @@ export default function({ scene, box }) {
l(0.35, [0, plus(box.max.y*4), 20]); // top
l(0.35, [0, minus(box.min.y*4), -20]); // bottom
l(0.5, [0, 0, plus(7*box.max.z)]); // front
l(0.2, [0, 0, plus(7*box.max.z)]); // front
l(0.2, [0, 0, minus(15*box.min.z)]); // back
l(0.4, [camera.position.x, camera.position.y, camera.position.z]); // camera
}
function addLight(scene, light, intensity, pos = []) {

View file

@ -0,0 +1,102 @@
import { loadJS } from "../../assets/helpers/loader.js";
await loadJS("https://cdn.jsdelivr.net", "/npm/occt-import-js@0.0.22/dist/occt-import-js.min.js");
async function LoadGeometry(url) {
const occt = await occtimportjs()
let fileUrl = "https://raw.githubusercontent.com/kovacsv/occt-import-js/main/test/testfiles/cax-if/as1_pe_203.stp"
// let response = await fetch(fileUrl)
let response = await fetch(url)
let buffer = await response.arrayBuffer()
let fileBuffer = new Uint8Array(buffer)
return occt.ReadStepFile(fileBuffer, null);
}
export default async function(I3D, { THREE }) {
return class Impl extends I3D {
constructor() {
super();
}
load(url, onLoad, onProgress, onError) {
LoadGeometry(url)
.then(({ success, ...obj }) => {
if (success === false) throw new Error("NOT SUPPORTED");
onLoad(obj);
})
.catch((err) => onError(err));
}
transform({ root, meshes }) {
const group = new THREE.Group();
const recurse = ({ meshes: m, children, name}) => {
if (m.length > 0) {
const obj = new THREE.Object3D();
obj.name = name;
for (let i=0; i<m.length; i++) {
const resultMesh = meshes[m[i]];
console.log(name, resultMesh);
let geometry = new THREE.BufferGeometry()
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(resultMesh.attributes.position.array, 3),
)
if (resultMesh.attributes.normal) geometry.setAttribute(
"normal",
new THREE.Float32BufferAttribute(resultMesh.attributes.normal.array, 3),
);
geometry.setIndex(new THREE.BufferAttribute(
Uint32Array.from(resultMesh.index.array),
1,
));
let material = null
if (resultMesh.color) material = new THREE.MeshPhongMaterial({ color: new THREE.Color(
resultMesh.color[0],
resultMesh.color[1],
resultMesh.color[2],
)});
else material = new THREE.MeshPhongMaterial({ color: 0xcccccc });
obj.add(new THREE.Mesh(geometry, material));
}
group.add(obj);
}
if (children.length > 0) children.forEach((obj) => recurse(obj));
}
root.children.forEach(recurse);
return group;
}
transformOld({ meshes, root }) {
console.log(meshes, root)
const targetObject = new THREE.Object3D();
for (let resultMesh of meshes) {
let geometry = new THREE.BufferGeometry()
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(resultMesh.attributes.position.array, 3),
)
if (resultMesh.attributes.normal) geometry.setAttribute(
"normal",
new THREE.Float32BufferAttribute(resultMesh.attributes.normal.array, 3),
);
geometry.setIndex(new THREE.BufferAttribute(
Uint32Array.from(resultMesh.index.array),
1,
));
let material = null
if (resultMesh.color) material = new THREE.MeshPhongMaterial({ color: new THREE.Color(
resultMesh.color[0],
resultMesh.color[1],
resultMesh.color[2],
)});
else material = new THREE.MeshPhongMaterial({ color: 0xcccccc });
targetObject.add(new THREE.Mesh(geometry, material));
}
return targetObject;
}
}
}

View file

@ -31,6 +31,12 @@
"mime": "image/svg+xml",
"entrypoint": "/index_svg.js",
"application": "3d"
},
{
"type": "xdg-open",
"mime": "model/step",
"entrypoint": "/index_step.js",
"application": "3d"
}
]
}