three.js 给PLY增加根据Z值设置颜色值

		//    let          s = '../src/assets/ply/Result.ply'
		let s = plyUrl;  
		loader.load(s,
			function (geometry) {
				console.log('loader.load ');
				console.log(geometry);
				geometry.computeVertexNormals();

				const colorsArr = geometry.getAttribute('position').array.slice();
				//const colors = [];

				let colors = new Float32Array(colorsArr.length)
				console.log('colors', colorsArr)
				for (let i = 0; i < colorsArr.length; i++) {
					if ((i + 1) % 3 == 0)
					{
						colors[i-2] = Math.abs(colorsArr[i]) / 100;
						colors[i-1] = Math.abs(colorsArr[i]) / 400;
						colors[i-0] = Math.abs(colorsArr[i]) / 700;
						//colors.push(0.6, 0.5, Math.abs(colorsArr[i]) / colorsArr.length);
					}
						
				}

				console.log(colors)


				geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3, false));


				const material2 = new THREE.PointsMaterial({ size: 0.001, vertexColors: true, color: '#ffff00', });//THREE.VertexColors color: '#ffff00',
				//material2.sizeAttenuation = false; 		



				let mesh2 = new THREE.Points(geometry, material2);
				mesh2.position.x = 1;//0;
				mesh2.position.y = 2;//-1;
				mesh2.position.z = 3;//0;
				mesh2.scale.multiplyScalar(0.4);
				mesh2.castShadow = true;
				mesh2.receiveShadow = true;
				scene.add(mesh2);


				scene.background = new THREE.Color(0x52645b);
				console.log('loader.load OK');


				//}

				// 更新点云对象的顶点颜色属性
				// const attribute = new THREE.Uint8ClampedArray(colors).buffer;
				// geometry.setAttribute('color', new THREE.InterleavedBufferAttribute(attribute, 6, 39));


			},
			function (xhr) {
				//console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
			},
			function (err) {
				console.error(err);
			}
		);

效果如下图 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值