当前位置:网站首页>three物体围绕一周呈球形排列
three物体围绕一周呈球形排列
2022-08-05 09:54:00 【Jedi Hongbin】
圆形


思路: 根据指定半径的圆形 和需要物体存在的角度 计算点在圆上的坐标
/** * 根据一个模型生成一个围绕一圈的组合 */
export const modelSurround = (model: Object3D, radius: number, count: number) => {
const group = new THREE.Group();
const onceAngle = (Math.PI * 2) / 360; //一度
const spaceAngle = (360 / count) * onceAngle; //两个物体中间的夹角度数
for (let i = 1; i <= count; i++) {
const item = model.clone();
const x = Math.sin(spaceAngle * i) * radius;
const y = Math.cos(spaceAngle * i) * radius;
item.position.set(x, y, 0);
group.add(item);
}
return group;
};
调用
const model = new THREE.Mesh(
new THREE.BoxGeometry(3, 3, 3),
new THREE.MeshPhongMaterial({
color: 0xa88afa,
flatShading: true,
side: THREE.DoubleSide,
})
);
const roundModel = modelSurround(model, 10, 10);
scene.add(roundModel);
球形

/** * 根据一个模型生成一个围绕一圈的组合 */
export const modelSurround = (model: Object3D, radius: number, count: number, column: number = 1) => {
const group = new THREE.Group();
const onceAngle = (Math.PI * 2) / 360; //一度
const spaceAngle = (360 / count) * onceAngle; //两个物体中间的夹角度数
for (let l = 0; l < column; l++) {
const columnGroup = new THREE.Group();
for (let i = 0; i < count; i++) {
const item = model.clone();
const x = Math.sin(spaceAngle * i) * radius;
const y = Math.cos(spaceAngle * i) * radius;
item.position.set(x, y, 0);
columnGroup.add(item);
}
columnGroup.rotation.y = (180 / column) * onceAngle * l;
group.add(columnGroup);
}
return group;
};
调用
const model = new THREE.Mesh(
new THREE.BoxGeometry(3, 3, 3),
new THREE.MeshPhongMaterial({
color: 0xa88afa,
flatShading: true,
side: THREE.DoubleSide,
})
);
const roundModel = modelSurround(model, 10, 20, 8);
roundModel.position.y = 70;
scene.add(roundModel);
指定弧度 生成半圆


/** * 根据一个模型生成一个围绕一圈的组合 */
export const modelSurround = ({
model,
radius,
count,
radian = 360,
column = 1,
}: {
/** * 模型 */
model: Object3D;
/** * 半径 */
radius: number;
/** * 重复数量 */
count: number;
/** * 排列弧度默认360度(一周) * @default 360 */
radian?: number;
/** * 绕y轴旋转几列360列即为球形 默认1列 * @default 1 */
column?: number;
}) => {
const group = new THREE.Group();
const onceAngle = (Math.PI * 2) / 360; //一度
const spaceAngle = (radian / count) * onceAngle; //两个物体中间的夹角度数
for (let l = 0; l < column; l++) {
const columnGroup = new THREE.Group();
for (let i = 0; i < count; i++) {
const item = model.clone();
const x = Math.sin(spaceAngle * i) * radius;
const y = Math.cos(spaceAngle * i) * radius;
item.position.set(x, y, 0);
columnGroup.add(item);
}
columnGroup.rotation.y = (180 / column) * onceAngle * l;
group.add(columnGroup);
}
return group;
};
调用
const model = new THREE.Mesh(
new THREE.BoxGeometry(3, 3, 3),
new THREE.MeshPhongMaterial({
color: 0xa88afa,
flatShading: true,
side: THREE.DoubleSide,
})
);
const roundModel = modelSurround({
model, radius: 10, count: 10, radian: 90, column: 1 });
roundModel.position.y = 70;
scene.add(roundModel);
加入 多个模型随机选择排列

/** * 根据一个模型生成一个围绕一圈的组合 */
export const modelSurround = ({
model,
radius,
count,
radian = 360,
column = 1,
syncRotation = false,
}: {
/** * 模型 或者一个返回模型的函数 */
model: Object3D | (() => Object3D);
/** * 半径 */
radius: number;
/** * 重复数量 */
count: number;
/** * 排列弧度默认360度(一周) * @default 360 */
radian?: number;
/** * 绕y轴旋转几列360列即为球形 默认1列 * @default 1 */
column?: number;
/** * 旋转角度同步旋转 * @default false */
syncRotation?: boolean;
}) => {
const group = new THREE.Group();
const onceAngle = (Math.PI * 2) / 360; //一度
const spaceAngle = (radian / count) * onceAngle; //两个物体中间的夹角度数
for (let l = 0; l < column; l++) {
const columnGroup = new THREE.Group();
for (let i = 0; i < count; i++) {
const item = typeof model === "function" ? model().clone() : model.clone();
const x = Math.sin(spaceAngle * i) * radius;
const y = Math.cos(spaceAngle * i) * radius;
item.position.set(x, y, 0);
columnGroup.add(item);
syncRotation && (item.rotation.z = spaceAngle * -i);
}
columnGroup.rotation.y = (360 / column) * onceAngle * l;
group.add(columnGroup);
}
return group;
};
调用 传入一个随机生成的模型
/** * 获得三块砖的模型 */
const one = gltf.scene.getObjectByName("1");
const two = gltf.scene.getObjectByName("2");
const three = gltf.scene.getObjectByName("3");
if (!one || !two || !three) return;
const provider = [one, two, three];
const {
length: count } = provider;
const space = getSize(one).z + 1;
const blocksGroup = modelSurround({
model: () => {
const row = new Group();
for (let i = 0; i < count; i++) {
//随机获取得一个模型
const model = provider[Math.floor(Math.random() * count)].clone();
row.add(model);
//这样 0,1,2 是 0 3,4,5 是 1 有序排列保持坐标一致
const column = Math.floor(i / count);
const x = column * space;
const y = 0;
const z = (i % count) * space;
model.position.set(x, y, z);
}
return row;
},
count: 30,
radius: 460 * 4,
radian: 6,
syncRotation: true,
column: 4,
});
边栏推荐
- hcip BGP enhancement experiment
- 首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析
- CCVR eases heterogeneous federated learning based on classifier calibration
- Dry goods!Generative Model Evaluation and Diagnosis
- 韦东山 数码相框 项目学习(六)tslib的移植
- QSS 选择器
- js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
- 无题二
- Neuron Newsletter 2022-07|新增非 A11 驱动、即将支持 OPC DA
- PAT Class B-B1019 Digital Black Hole (20)
猜你喜欢

There is only one switch, how to realize the nqa of master-slave automatic switching

Pycharm 常用外部工具

哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?

Keil升级到AC6后,到底有哪些变化?

皕杰报表的下拉框联动

微服务 技术栈

欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)

The technological achievements of Shanghai Konan were selected into the "2021 Shanghai Network Security Industry Innovation Research Achievement Catalog" by the Municipal Commission of Economy and Inf

js 图形操作一(兼容pc、移动端实现 draggable属性 拖放效果)

Microservice Technology Stack
随机推荐
无题二
Analysis and practice of antjian webshell dynamic encrypted connection
电竞、便捷、高效、安全,盘点OriginOS功能的关键词
2022-08-01 回顾基础二叉树以及操作
PAT Level B - B1021 Single Digit Statistics (15)
开源一夏|OpenHarmony如何查询设备类型(eTS)
HStreamDB Newsletter 2022-07|分区模型优化、数据集成框架进一步完善
数据中台建设(十):数据安全管理
MySQL advanced (twenty-seven) database index principle
Wei Dongshan Digital Photo Frame Project Learning (6) Transplantation of tslib
Development common manual link sharing
Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need
hcip BGP 增强实验
仿SBUS与串口数据固定转换
MySQL使用聚合函数可以不搭配GROUP BY分组吗?
无题一
C语言的高级用法
Why do I recommend using smart async?
无题四
高质量 DeFi 应用构建指南,助力开发者玩转 DeFi Summer