【发布时间】:2023-03-24 13:45:01
【问题描述】:
我对此进行了搜索,并在stackoverflow上找到了几个示例,但答案并没有解决我的问题。
我尝试过的:
首先,我创建了用于组的几何存储桶和一个数组来存储我的材质。
var totalGeom = new THREE.Geometry();
var materials = [];
然后我使用 for 循环遍历我的数据 (strData) 并调用 addMapMarker3d。
for(var i=0; i<strData.length;i++){
addMapMarker3d([strData[i].lat,strData[i].lon], strData[i].time, measureColors[i]);
}
addMapMarker3d函数如下:
addMapMarker3d = function(latLng, height, color){
var max = 600;
var dist = 25;
var opacity = (height/max);
var geometry = new THREE.BoxGeometry( Math.floor(dist), height, Math.floor(dist));
//create the material and add it to the materials array
var material = new THREE.MeshBasicMaterial({ transparent:true, color: Number(color), opacity:opacity});
this.materials.push(material);
//create a mesh from the geometry and material.
var cube = new THREE.Mesh( geometry, material);
//leaf is a simple lat/lng to pixel position converter
var actualMarkerPos = leaf.getPoint(latLng);
//apply the position on a 5000x5000 cartesian plane
var extent = 5000;
cube.position.setZ((actualMarkerPos[1] * extent) - extent/2);
cube.position.setX(-((actualMarkerPos[0] * extent) - extent/2));
cube.position.setY(height/2);
//update the matrix and add the cube to the totalGeom bucket
cube.updateMatrix();
totalGeom.merge( cube.geometry, cube.matrix);
}
在 for 循环运行并创建所有立方体之后:
var mats = new THREE.MeshFaceMaterial(materials)
var total = new THREE.Mesh(totalGeom, mats);
world.scene.add(total);
问题
虽然几何合并功能,并且我的视口以大大提高的 FPS 运行,但所有立方体都具有完全相同的颜色和不透明度。看来合并使用的是我提供的 10k 的单一材料。 有什么方法可以确保几何图形使用数组中提供的材料?我做错了什么吗?
如果我在 addMapMarker3d 中尝试这个:
totalGeom.merge( cube.geometry, cube.matrix, materials.length-1);
我得到“Uncaught TypeError: Cannot read property 'transparent' of undefined”并且没有渲染,我不明白,因为通过示例,每个几何图形都应该索引到材料数组中的材料。
three.js r.70
【问题讨论】:
标签: three.js