# InstancedModel组件

使用InstancedModel组件可以创建多个重复的模型,由 @tx3d/extension 包提供。


import { InstancedModel } from '@tx3d/extension';

// 构建参数
let params = {
    modelAsset: "./assets/models/树木/Tree-B/Tree-B.gltf",
    instancedInfos: [
        {
            position:[14,3,1],
            rotation:[0,Math.PI/2,0],
            scale:[2,2,2]
        },
        {
            position:[0,5,10],
            rotation:[0,0,Math.PI/2],
            scale:[3,3,3]
        },
        {
            position:[5,8,7],
        }
    ],
    maxCount: 100,
    castShadow: true,
    receiveShadow: false
};

// 创建实体
const entity = engine.createEntity({ transform: { position: [0, 0, 0] } });

// 添加水块渲染组件
const instancedModel = entity.addComponent(InstancedModel, params);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

提示

InstancedModel组件初始化参数,详见InstancedModelParameters

# 属性

🌏 instancedInfos 重新设置实例化的位置缩放旋转信息

import { Vector3 } from 'three';

// 摆放两排树 大小不一样 旋转随机
let geoSize = new Vector3(4.3400046825408936, 5.199821839109063, 4.61873197555542);
let info = [];
for (let i = 0; i < 2; i++) {

    for (let j = 0; j < 20; j++) {

        let params = {};

        let rotationY = Math.random() * 360;
        params.rotation = [0, rotationY, 0];

        let x = j * (geoSize.x + 4);
        let z = i * (geoSize.z + 4);
        params.position = [x, 0, z];

        let scale = Math.random() * 1 + 0.5;
        params.scale = [scale, Math.random() * 1 + 0.5, scale];

        info.push(params);
    }
}
// 重新设置位置信息
instancedModel.instancedInfos = info;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Last Updated: 5/15/2024, 5:14:45 PM