当前位置:网站首页>threejs 动态调整相机位置,使相机正好能看到对象
threejs 动态调整相机位置,使相机正好能看到对象
2022-08-02 22:01:00 【K歌、之王】

核心代码
obj为目标对象\ controls 为OrbitControls
let box3 = new THREE.Box3()
box3.expandByObject(obj) // 计算模型包围盒
let size = new THREE.Vector3()
box3.getSize(size) // 计算包围盒尺寸
let center = new THREE.Vector3()
box3.getCenter(center) // 计算一个层级模型对应包围盒的几何体中心坐标
function maxSize(vec3) {
let max
if (vec3.x > vec3.y) {
max = vec3.x
} else {
max = vec3.y
}
if (max > vec3.z) {
} else {
max = vec3.z
}
return max
}
let max = maxSize(size) //包围盒长宽高中最大的一个值,用来表征模型的尺寸
// 1.控制渲染范围,但是不要忘记相机位于模型包围盒之外
this.camera.position.copy(center.clone().addScalar(max))
// 2. 居中渲染:设置相机目标观察点,指向包围盒几何中心
this.camera.lookAt(center)
// 3.注意near和far尺寸控制
this.camera.near = max * 0.1//最好和相机位置或者说包围盒关联,别设置0.1 1之类看似小的值
this.camera.far = max * 300//根据相机位置和包围大小设置,把包围盒包含进去即可,宁可把偏大,不可偏小
this.camera.updateProjectionMatrix()//渲染范围改变,注意更新投影矩阵
this.controls.target.copy(center)
this.controls.update()
边栏推荐
- The only way to go from a monthly salary of 10k to 30k: automated testing
- 总数据量超万亿行,玉溪卷烟厂通过正确选择时序数据库轻松应对
- ZCMU--5230: 排练方阵(C语言)
- today‘s task
- 在迁移测试中,源表、中间表、目标表的迁移规则
- What is the core business model of the "advertising e-commerce" that has recently become popular in the circle of friends, and is the advertising revenue really reliable?
- Summary of @Transactional transaction invocation and effective scenarios
- go context 包
- [TypeScript] Deep Learning of TypeScript Classes (Part 1)
- PHP实现登录失败三次需要输入验证码需求
猜你喜欢

Add and delete all these years, finally planted in MySQL architecture design!

centos7安装mysql5.7

牛客每日刷题之链表

如何通过 IDEA 数据库管理工具连接 TDengine?

【STM32学习2】存储器相关概念与操作
![[TypeScript] Deep Learning of TypeScript Classes (Part 1)](/img/47/34954f1e01b844816d74f3ac556f9b.png)
[TypeScript] Deep Learning of TypeScript Classes (Part 1)

Task 4 Machine Learning Library Scikit-learn

GameStop NFT 市场分析

H.265视频流媒体播放器EasyPlayer.js集成时出现“SourceBuffer ”报错,该如何解决?

若依集成minio实现分布式文件存储
随机推荐
[TypeScript] Deep Learning of TypeScript Classes (Part 1)
TDengine 在中天钢铁 GPS、 AIS 调度中的落地
Task 4 Machine Learning Library Scikit-learn
Flink优化及相关
饥荒联机版Mod开发——制作简单的物品(三)
【TypeScript】深入学习TypeScript模块化
命令行启动常见问题及解决方案
用于中文文本分类的中文停用词
RuoYi-App启动教程
不堪哥哥殴打谩骂,妹妹申请人身安全保护令获支持
软件测试笔试题1(附答案)
mysql查询表中重复记录
学习基因富集工具DAVID(3)
Finally understand: With threads, why do we need coroutines?
你离「TDengine 开发者大会」只差一条 SQL 语句!
Do you understand the factory pattern?
【DEBUG】ImportError: Unable to import required dependencies: numpy: DLL load failed: 找不到指定的模块。
You and I will meet the needs of: how to export the data in a MySQL simple ~!Practical!
YAML文件格式
FastCorrect:语音识别快速纠错模型丨RTC Dev Meetup