Babylon Viewer isn’t displaying my STL model correctly #180087
Unanswered
manm44484-tech asked this question in General
Replies: 0 comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Select Topic Area
Question
Body
“I’m using Babylon Viewer to display a 3D model on my website, but no matter how I adjust the settings, the model always appears in X-Ray mode. Here’s my viewer.jsx code—does anyone know why this happens?
import { useEffect, useRef } from 'react';
import * as BABYLON from '@babylonjs/core';
export default function BabylonViewer({
stlFile,
color = '#FF0000',
scale = 1,
width = 400,
height = 400,
autoRotate = false
}) {
const canvasRef = useRef(null);
const engineRef = useRef(null);
const sceneRef = useRef(null);
useEffect(() => {
if (!canvasRef.current || !stlFile) return;
}, [stlFile, color, scale, width, height, autoRotate]);
// 加載 STL 文件
const loadSTLMesh = (buffer, scene, color, scale, autoRotate) => {
try {
const arrayBuffer = buffer;
const dataView = new DataView(arrayBuffer);
};
// 解析 Binary STL
const parseBinarySTL = (dataView) => {
const numTriangles = dataView.getUint32(80, true);
const vertices = [];
const indices = [];
const normals = [];
};
// 解析 ASCII STL
const parseASCIISTL = (buffer) => {
const text = new TextDecoder().decode(buffer);
const lines = text.split('\n');
const vertices = [];
const indices = [];
const normals = [];
};
return (
<div
style={{
width:
${width}px,height:
${height}px,position: 'relative',
overflow: 'hidden', // ✅ 修正:防止溢出
borderRadius: '8px',
}}
>
<canvas
ref={canvasRef}
style={{
width: '100%',
height: '100%',
display: 'block', // ✅ 修正:移除底部空白
touchAction: 'none',
}}
/>
);
}
Beta Was this translation helpful? Give feedback.
All reactions