修正三维视图导轨长度

This commit is contained in:
zhangshun
2026-06-16 11:03:03 +08:00
parent 58d4c6660e
commit c9e76d0850

View File

@@ -716,9 +716,10 @@
</main>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.5.1/dist/echarts.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.160.1/build/three.min.js"></script>
<script src="./wasm/smart_math.js"></script>
<script>
<script type="module">
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.184.0/build/three.module.js";
const validSample = {
L_AB: 0.5,
L_BS: 2.0,
@@ -860,10 +861,6 @@
// 初始化 Three.js 场景,负责更直观地展示接口返回的机构姿态。
function ensureThreeView() {
if (!window.THREE) {
throw new Error("Three.js 加载失败,请检查网络或 CDN 可用性");
}
if (threeView) return threeView;
const scene = new THREE.Scene();
@@ -960,6 +957,20 @@
view.camera.lookAt(span * 0.35, 0, input.S_OFS * 0.5);
}
// 依据机构参数固定导轨显示范围,避免播放时导轨随滑块位置伸缩。
function getFixedRailRange(input) {
const crank = Math.max(input.L_AB, 0);
const rod = Math.max(input.L_BS, 0);
const yDeltaMax = Math.abs(input.S_OFS) + crank;
const reachableXOffset = Math.sqrt(Math.max(0, rod ** 2 - yDeltaMax ** 2));
const minX = -crank - 0.35;
const maxX = crank + reachableXOffset + 0.7;
return {
minX: Math.min(minX, maxX - 0.8),
maxX: Math.max(maxX, minX + 0.8),
};
}
// 调整 Three.js 画布尺寸。
function resizeThreeView() {
if (!threeView) return;
@@ -981,14 +992,14 @@
const a = points.A || points.pointA;
const b = points.B || points.pointB;
const s = points.S || points.pointS;
const railHalfLength = Math.max(input.L_AB + input.L_BS + Math.abs(input.S_OFS), Math.abs(s.x) + 0.8, 1);
const railRange = getFixedRailRange(input);
placeBar(view.objects.crank, a, b);
placeBar(view.objects.rod, b, s);
placeBar(
view.objects.rail,
{ x: -railHalfLength * 0.25, y: input.S_OFS },
{ x: railHalfLength, y: input.S_OFS },
{ x: railRange.minX, y: input.S_OFS },
{ x: railRange.maxX, y: input.S_OFS },
);
view.objects.pointA.position.copy(toThreePoint(a));