threejs 添加几何模型

添加cube,若不添加纹理则将{map:texture }改为{color:0xffff00}

function addTexCube(mesh){
  let texture = new THREE.TextureLoader().load( mesh.url );
  let geo=new THREE.CubeGeometry(mesh.data.width,mesh.data.height, mesh.data.long);
  let mat=new THREE.MeshLambertMaterial({map:texture });
  let cube= new THREE.Mesh(geo,mat);
  cube.position.x=mesh.position.x;
  cube.position.y=mesh.position.y;
  cube.position.z=mesh.position.z;
  cube.name=mesh.name;
  scene.add(cube);
}

在这里插入图片描述

添加圆柱

/**
 * @description:  添加圆柱
 * @param {*} mesh={name:name,data:{topR:5,bottomR:10,height:10},position:{x:0,y:0,z:0},color:0xffff00}
 * @return {*}
 */
function addCylinder(mesh){
  //顶圆半径、底圆半径、圆柱体高度 圆滑度;
  let geo=new THREE.CylinderGeometry(mesh.data.topR, mesh.data.bottomR ,mesh.data.height ,30 ,30);
  let mat=new THREE.MeshLambertMaterial({color: mesh.color});
  let cylinder=new THREE.Mesh(geo,mat);
  cylinder.position.x=mesh.position.x;
  cylinder.position.y=mesh.position.y;
  cylinder.position.z=mesh.position.z;
  cylinder.name=mesh.name;
  scene.add(cylinder);
}

添加球体

function addTexSphere(mesh){
  let texture = new THREE.TextureLoader().load( mesh.url );
  let geo=new THREE.SphereGeometry(mesh.data.R ,30 ,30);
  let mat=new THREE.MeshLambertMaterial({map:texture});
  let cylinder=new THREE.Mesh(geo,mat);
  cylinder.position.x=mesh.position.x;
  cylinder.position.y=mesh.position.y;
  cylinder.position.z=mesh.position.z;
  cylinder.name=mesh.name;
  scene.add(cylinder);
}


根据点绘制几何,以立方体为例,三角形逆时针绘制

function addElement(){
  let geometry = new THREE.BufferGeometry();
  let vertices= new Float32Array([
    //前面 012 023
    10, 10, 10,
    -10, 10, 10,
    -10, -10, 10,
    10, 10, 10,
    -10, -10, 10,
    10, -10, 10,
    //右面 034 045
    10, 10, 10,
    10, -10, 10,
    10, -10, -10,
    10, 10, 10,
    10, -10, -10,
    10, 10, -10,
    //左面 167 172
    -10, 10, 10,
    -10, 10, -10,
    -10, -10, -10,
    -10, 10, 10,
    -10, -10, -10,
    -10, -10, 10,
    //后面 654 647
    -10, 10, -10,
    10, 10, -10,
    10, -10, -10,
    -10, 10, -10,
    10, -10, -10,
    -10, -10, -10,
    //顶面 561 510
    10, 10, -10,
    -10, 10, -10,
    -10, 10, 10,
    10, 10, -10,
    -10, 10, 10,
    10, 10, 10,
    //底面 327 374
    10, -10, 10,
    -10, -10, 10,
    -10, -10, -10,
    10, -10, 10,
    -10, -10, -10,
    10, -10, -10
  ])
 
  
  // 创建属性缓冲区对象
  let attribue = new THREE.BufferAttribute(vertices, 3); //3个为一组
  // 设置几何体attributes属性的位置position属性
  geometry.attributes.position = attribue;
  //计算法向量
  let normalArr=[];
  for(let i=0;i<vertices.length;i+=3){
    let normal=new THREE.Vector3(vertices[i],vertices[i+1],vertices[i+2]).normalize();
    //console.log(normal)
    normalArr.push(normal.x);
    normalArr.push(normal.y);
    normalArr.push(normal.z);
  }
   //设置法向量
   var normals = new Float32Array(normalArr)
   // 设置几何体attributes属性的位置normal属性
   geometry.attributes.normal = new THREE.BufferAttribute(normals, 3); //3个为一组,表示一个顶点的法向量数据
   
  
  // 材质
  let material = new THREE.MeshLambertMaterial({
    color: 0xffff00, 
    side: THREE.DoubleSide 
  }); 
  let mesh = new THREE.Mesh(geometry, material); 
  scene.add(mesh);
  //console.log('结束',new Date().getMilliseconds())
}

可用于不规则几何体绘制,效果如图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值