【发布时间】:2021-08-07 11:44:43
【问题描述】:
我目前正在使用threejs 库来创建 3d 对象,但不幸的是我无法将对象放入画布中。如果对象有点长,它总是溢出画布之外。请在JSFiddle 中查看我的代码。
脚本
import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r127/build/three.module.js'
function generate3DBox(selector, angle, boxDepth, boxWidth, boxHeight) {
let allowedAngles = [
'standard-0',
'standard-90',
'turn-up-0',
'turn-up-90',
'turn-side-0',
'turn-side-90'
]
if(allowedAngles.indexOf(angle) < 0) {
console.log("Angle is incorrect")
return false
}
const canvas = document.querySelector(selector)
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true })
const canvasWidth = canvas.getBoundingClientRect().width
const canvasHeight = canvas.getBoundingClientRect().height
const minSize = Math.min(...[boxDepth, boxWidth, boxHeight])
const maxSize = Math.max(...[boxDepth, boxWidth, boxHeight])
const aspect = canvasWidth / canvasHeight
const fov = 75
const near = 0.1
const far = 1000
let cameraZoom = 1
let cameraPosition = {
left: canvasWidth / -2,
right: canvasWidth / 2,
top: canvasHeight / 2,
bottom: canvasHeight / -2
}
const camera = new THREE.OrthographicCamera(
cameraPosition.left,
cameraPosition.right,
cameraPosition.top,
cameraPosition.bottom,
near,
far
)
camera.position.z = maxSize + minSize
camera.zoom = cameraZoom
camera.updateProjectionMatrix()
const scene = new THREE.Scene()
scene.background = new THREE.Color(0xe0e0e0)
const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth)
const material = new THREE.MeshBasicMaterial({ color: 0xff4500 }) // greenish blue
const cube = new THREE.Mesh(geometry, material)
var edge = new THREE.EdgesGeometry(cube.geometry)
var edgeMaterial = new THREE.LineBasicMaterial({ color: 0xffffff, linewidth: 1 })
var wireframe = new THREE.LineSegments(edge, edgeMaterial)
scene.add(cube, wireframe)
function animate() {
requestAnimationFrame(animate)
let rotationX = 0
let rotationY = 0
let rotationZ = 0
if(angle == 'standard-0') {
rotationX = 0.60
rotationY = -0.80
rotationZ = 0
}
if(angle == 'standard-90') {
rotationX = 0.60
rotationY = 0.80
rotationZ = 0
}
if(angle == 'turn-up-0') {
rotationX = -1.20
rotationY = 0
rotationZ = 0.80
}
if(angle == 'turn-up-90') {
rotationX = -1.20
rotationY = 0
rotationZ = -0.80
}
if(angle == 'turn-side-0') {
rotationX = 0.60
rotationY = -0.60
rotationZ = -1.60
}
if(angle == 'turn-side-90') {
rotationX = 0.60
rotationY = 0.60
rotationZ = -1.60
}
cube.rotation.x = rotationX
cube.rotation.y = rotationY
cube.rotation.z = rotationZ
wireframe.rotation.x = rotationX
wireframe.rotation.y = rotationY
wireframe.rotation.z = rotationZ
renderer.render(scene, camera)
}
animate()
}
generate3DBox('.standard-0', 'standard-0', 50, 45, 65)
generate3DBox('.standard-90', 'standard-90', 50, 45, 65)
generate3DBox('.turn-up-0', 'turn-up-0', 50, 45, 65)
generate3DBox('.turn-up-90', 'turn-up-90', 50, 45, 65)
generate3DBox('.turn-side-0', 'turn-side-0', 50, 45, 65)
generate3DBox('.turn-side-90', 'turn-side-90', 50, 45, 65)
需要输出
【问题讨论】:
-
@WestLangley 你怎么得到
dist那里? -
@WestLangley 我已经更新了小提琴代码,但结果仍然相同。请查看jsfiddle.net/jonjieviduya/ehz62psd/5
-
由于您的几何体很长并且正在旋转,因此请考虑计算网格几何体的边界球并参见stackoverflow.com/questions/22500214/…。如果您不理解这些概念,可以通过discourse.threejs.org 获得帮助。其他人必须帮助您调试代码。
-
想要的效果是什么?让相机自行调整以始终将物体放在视野中?这不是将对象放入画布内,而是将对象放入相机视图中。我建议在这里阅读一下三个 js 的工作原理threejs.org/docs/index.html#manual/en/introduction/…
标签: javascript three.js