DrawUtil.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. import * as THREE from "../../../libs/three.js/build/three.module.js";
  2. import math from './math.js';
  3. import {Line2} from "../../../libs/three.js/lines/Line2.js";
  4. import {LineGeometry} from "../../../libs/three.js/lines/LineGeometry.js";
  5. import {LineMaterial} from "../../../libs/three.js/lines/LineMaterial.js";
  6. //画线等函数--by 许钟文
  7. import {Features} from "../../Features.js";
  8. import Common from './Common.js'
  9. var defaultColor = new THREE.Color(1,1,1);//config.applicationName == "zhiHouse" ? Colors.zhiBlue : Colors.lightGreen;
  10. function dealPosArr(points){//识别是否每个点都不一样,把连续点变为不连续的片段连接
  11. let add = (points)=>{
  12. let points2 = [] , len = points.length
  13. for(let i=0;i<len-1;i++){
  14. points2.push(points[i], points[i+1])
  15. }
  16. return points2
  17. }
  18. if(points[0] && points[0] instanceof Array){//多组,每组间连续,但组之间不连续
  19. let points2 = []
  20. points.forEach(ps=>points2.push(...add(ps)))
  21. return points2
  22. }else if(points.length > 2 && !points[2].equals(points[1])){
  23. return add(points)
  24. }else return points
  25. }
  26. let center = new THREE.Vector3
  27. function extractPos(posArr){//尽量让所有点都靠近原点
  28. //console.log('extractPos', posArr.map(e=>e.clone()))
  29. let bound = new THREE.Box3
  30. posArr.forEach(e=>bound.expandByPoint(e))
  31. bound.getCenter(center)
  32. posArr.forEach(e=>e.sub(center))
  33. //console.log('extractPos2 ', posArr.map(e=>e.clone()), center.clone())
  34. return center.clone()
  35. }
  36. var LineDraw = {
  37. createLine: function (posArr, o={}) {
  38. //多段普通线 (第二个点和第三个点之间是没有线段的, 所以不用在意线段顺序)
  39. var mat
  40. if(o.mat){
  41. mat = o.mat
  42. }else{
  43. let prop = Object.assign({
  44. lineWidth: o.lineWidth || 1,
  45. //windows无效。 似乎mac/ios上粗细有效 ?
  46. color: o.color || defaultColor
  47. },o)
  48. if(o.deshed ){
  49. prop.dashSize = o.dashSize || 0.1,
  50. prop.gapSize = o.gapSize || 0.1
  51. }
  52. mat = new THREE[o.deshed ? "LineDashedMaterial" : "LineBasicMaterial"](prop)
  53. }
  54. var line = new THREE.LineSegments(new THREE.BufferGeometry, mat);
  55. line.renderOrder = o.renderOrder || Potree.config.renderOrders.line
  56. this.moveLine(line, posArr)
  57. return line;
  58. },
  59. moveLine: function (line, posArr) {
  60. //if(posArr.length == 0)return
  61. if(!line.uncontinuous || posArr[0] && posArr[0] instanceof Array)posArr = dealPosArr(posArr)
  62. let position = []
  63. posArr.forEach(e=>position.push(e.x,e.y,e.z))
  64. if(line.avoidBigNumber){
  65. let moveVec = extractPos(posArr)
  66. line.position.copy(moveVec)//无视原本的position!
  67. }
  68. line.geometry.setAttribute('position', new THREE.Float32BufferAttribute(/* new Float32Array( */position/* ) */, 3));
  69. line.geometry.attributes.position.needsUpdate = true;
  70. line.geometry.computeBoundingSphere();
  71. if(line.material instanceof THREE.LineDashedMaterial){
  72. line.computeLineDistances()
  73. //line.geometry.attributes.lineDistance.needsUpdate = true;
  74. line.geometry.verticesNeedUpdate = true; //没用
  75. }
  76. }
  77. ,
  78. createFatLineMat : function(o){
  79. var supportExtDepth = !!Features.EXT_DEPTH.isSupported()
  80. let params = $.extend({}, {
  81. //默认
  82. lineWidth : 5,
  83. color:0xffffff,
  84. transparent : true, depthWrite:false, depthTest:false,
  85. dashSize : 0.1, gapSize:0.1,
  86. }, o, {
  87. //修正覆盖:
  88. dashed: o.dashWithDepth ? supportExtDepth && !!o.dashed : !!o.dashed ,
  89. dashWithDepth:!!o.dashWithDepth,//只在被遮住的部分显示虚线
  90. useDepth: !!o.useDepth,
  91. supportExtDepth,
  92. })
  93. var mat = new LineMaterial(params)
  94. //if(o.dashed)(mat.defines.USE_DASH = "")
  95. return mat;
  96. },
  97. /*
  98. 创建可以改变粗细的线。
  99. */
  100. createFatLine : function(posArr, o){
  101. var geometry = new LineGeometry();
  102. geometry.setColors( o.color || [1,1,1]);
  103. var matLine = o.mat || this.createFatLineMat(o);
  104. var line = new Line2( geometry, matLine );
  105. //line.computeLineDistances();
  106. line.uncontinuous = o.uncontinuous //线不连续,由线段组成
  107. line.scale.set( 1, 1, 1 );
  108. line.renderOrder = Potree.config.renderOrders.line;
  109. this.moveFatLine(line, posArr)
  110. return line;
  111. },
  112. moveFatLine: function(line, posArr ){
  113. posArr = Common.CloneObject(posArr)
  114. var geometry = line.geometry;
  115. var positions = [];
  116. if(!line.uncontinuous || posArr[0] && posArr[0] instanceof Array) posArr = dealPosArr(posArr)
  117. if(line.avoidBigNumber){
  118. let moveVec = extractPos(posArr)
  119. line.position.copy(moveVec)//无视原本的position!
  120. }
  121. posArr.forEach(e=>{positions.push(...e.toArray())})
  122. if(!geometry){
  123. geometry = line.geometry = new LineGeometry();
  124. }
  125. if(geometry.attributes.instanceEnd && geometry.attributes.instanceEnd.data.array.length != positions.length){//positions个数改变会有部分显示不出来,所以重建
  126. geometry.dispose();
  127. geometry = new LineGeometry();
  128. line.geometry = geometry
  129. }
  130. geometry.setPositions( positions )
  131. if(line.material.defines.USE_DASH != void 0){
  132. //line.geometry.verticesNeedUpdate = true; //没用
  133. line.geometry.computeBoundingSphere(); //for raycaster
  134. line.computeLineDistances();
  135. }
  136. },
  137. updateLine: function(line, posArr){
  138. if(line instanceof Line2){
  139. LineDraw.moveFatLine(line,posArr)
  140. }else{
  141. LineDraw.moveLine(line,posArr)
  142. }
  143. },
  144. /*
  145. 为line创建用于检测鼠标的透明mesh,实际是个1-2段圆台。
  146. 由于近大远小的原因,假设没有透视畸变、创建的是等粗的圆柱的话, 所看到的线上每个位置的粗细应该和距离成反比。所以将圆柱改为根据距离线性渐变其截面半径的圆台,在最近点(相机到线的垂足)最细。如果最近点在线段上,则分成两段圆台,否则一段。
  147. */
  148. createBoldLine:function(points, o){
  149. o = o || {}
  150. var cylinder = o && o.cylinder;
  151. var CD = points[1].clone().sub(points[0]);
  152. var rotate = function(){//根据端点旋转好模型
  153. cylinder.lastVector = CD;//记录本次的端点向量
  154. var AB = new THREE.Vector3(0,-1,0)
  155. var axisVec = AB.clone().cross(CD).normalize(); //得到垂直于它们的向量,也就是旋转轴
  156. var rotationAngle = AB.angleTo(CD);
  157. cylinder.quaternion.setFromAxisAngle( axisVec, rotationAngle )
  158. }
  159. if(o && o.type == "init"){
  160. cylinder = new THREE.Mesh()
  161. cylinder.material = o.mat
  162. if(CD.length() == 0)return cylinder;
  163. rotate()
  164. }
  165. if(CD.length() == 0)return cylinder;
  166. if(o.type != "update"){
  167. var CDcenter = points[0].clone().add(points[1]).multiplyScalar(.5);
  168. cylinder.position.copy(CDcenter);
  169. if(!cylinder.lastVector || o.type == "moveAndRotate")rotate()
  170. else if(cylinder.lastVector && CD.angleTo(cylinder.lastVector)>0) rotate()//线方向改了or线反向了 重新旋转一下模型
  171. if(config.isEdit && !objects.mainDesign.editing )return cylinder;//节省初始加载时间?
  172. }
  173. //为了保证线段任何地方的可检测点击范围看起来一样大,更新圆台的结构(但是在镜头边缘会比中心看起来大)
  174. var height = points[0].distanceTo(points[1]);
  175. var standPos = o && o.standPos || objects.player.position;
  176. var k = config.isMobile ? 20 : 40;
  177. var dis1 = points[0].distanceTo(standPos);
  178. var dis2 = points[1].distanceTo(standPos);
  179. var foot = math.getFootPoint(standPos, points[0], points[1]);//垂足
  180. if(o.constantBold || objects.player.mode != "panorama"){
  181. var width = 0.1//0.08;
  182. var pts = [new THREE.Vector2(width ,height/2),new THREE.Vector2(width ,-height/2)]
  183. }else if(foot.clone().sub(points[0]).dot( foot.clone().sub(points[1]) ) > 0){//foot不在线段上
  184. var pts = [new THREE.Vector2(dis1 / k,height/2),new THREE.Vector2(dis2 / k,-height/2)]
  185. }else{//在线段上的话,要在垂足这加一个节点,因它距离站位最近,而两端较远
  186. var dis3 = foot.distanceTo(standPos);
  187. var len = foot.distanceTo(points[0])
  188. var pts = [new THREE.Vector2(dis1 / k,height/2), new THREE.Vector2(dis3 / k,height/2-len), new THREE.Vector2(dis2 / k,-height/2)]
  189. }
  190. cylinder.geometry && cylinder.geometry.dispose();//若不删除会占用内存
  191. cylinder.geometry = new THREE.LatheBufferGeometry( pts, 4/* Math.min(dis1,dis2)<10?4:3 */ )
  192. cylinder.renderOrder = 2;
  193. return cylinder;
  194. },
  195. updateBoldLine:function(cylinder, points, type, standPos, constantBold){
  196. this.createBoldLine(points,{type:type, cylinder : cylinder, standPos:standPos, constantBold}) //type:move:平移 会改长短 , type:update根据距离和角度更新 不改长短
  197. },
  198. }
  199. var MeshDraw = {
  200. getShape: function(shapes, holes){
  201. //不一定闭合 暂时所有shapes共享holes。如果要单独的话, shapes改为[{shape:[],holes:[]},{}]的形式
  202. if(shapes[0] && !(shapes[0] instanceof Array) ){//仅是一个shape的点
  203. shapes = [shapes]
  204. }
  205. let holesArr = []
  206. if(holes){//挖空
  207. holes.forEach((points)=>{
  208. var holePath = new THREE.Path()
  209. holePath.moveTo( points[0].x, points[0].y )
  210. for(var i=1,len=points.length; i<len; i++){
  211. holePath.lineTo(points[i].x, points[i].y )
  212. }
  213. holesArr.push( holePath );
  214. })
  215. }
  216. let shapesArr = shapes.map(points=>{
  217. var shape = new THREE.Shape();
  218. shape.moveTo( points[0].x, points[0].y );
  219. for(var i=1,len=points.length; i<len; i++){
  220. shape.lineTo(points[i].x, points[i].y )
  221. }
  222. shape.holes.push(...holesArr)
  223. shape.dontClose = points.dontClose //add 有的shape不需要闭合
  224. return shape
  225. })
  226. return shapesArr
  227. },
  228. /* getShape:function(shapes, holes){
  229. var shape = new THREE.Shape();
  230. if(shapes[0] && !(shapes[0] instanceof Array) ){//仅是一个shape的点
  231. shapes = [shapes]
  232. }
  233. shapes.forEach((points)=>{
  234. shape.moveTo( points[0].x, points[0].y );
  235. for(var i=1,len=points.length; i<len; i++){
  236. shape.lineTo(points[i].x, points[i].y )
  237. }
  238. })
  239. //多个points的数组绘制在一个shape中,缺点是这些数组之间可能绘制出来会连在一起。
  240. if(holes){//挖空
  241. holes.forEach((points)=>{
  242. var holePath = new THREE.Path()
  243. holePath.moveTo( points[0].x, points[0].y )
  244. for(var i=1,len=points.length; i<len; i++){
  245. holePath.lineTo(points[i].x, points[i].y )
  246. }
  247. shape.holes.push( holePath );
  248. })
  249. }
  250. return shape
  251. }, */
  252. getShapeGeo: function(shapes, holes){//获取任意形状(多边形或弧形)的形状面 //quadraticCurveTo() 这是弧形的含函数
  253. var geometry = new THREE.ShapeBufferGeometry( this.getShape(shapes, holes) ); //ShapeGeometry
  254. /* var matrix = new THREE.Matrix4();//将竖直的面变为水平
  255. matrix.set(//z = y
  256. 1, 0, 0, 0,
  257. 0, 0, 0, 0,
  258. 0, 1, 0, 0,
  259. 0, 0, 0, 1
  260. )
  261. geometry.applyMatrix(matrix) */
  262. //geometry.computeVertexNormals();//对于光照需要的是点法线
  263. return geometry;
  264. },
  265. lessCurvePoints: function(points, oldCount, minRad=0.03, UtoTMapArr){//减少点数(拐弯的部分紧凑些,直线部分宽松些):
  266. let count = points.length
  267. let newUtoTMapArr = []
  268. let newPoints = []
  269. let pointIndexs = []
  270. let lastVec
  271. let startTime = performance.now()
  272. /* if(UtoTMapArr){
  273. for(let n=1;n<oldCount-1;n++){
  274. pointIndexs.push( UtoTMapArr.findIndex(e=>e>= n / (oldCount-1) ) )
  275. }
  276. } */
  277. //console.log('cost dur:', performance.now() - startTime )
  278. let nextUtoTIndex = 1
  279. for(let i=0;i<count;i++){
  280. let point = points[i];
  281. let last = points[i-1]
  282. let next = points[i+1]
  283. if(i == 0 || i == count-1 ) {
  284. newPoints.push(point) //直接加入
  285. UtoTMapArr && newUtoTMapArr.push(i == 0 ? 0 : 1)
  286. }else{
  287. let curVec = new THREE.Vector3().subVectors(next,point)
  288. if(!lastVec) lastVec = curVec
  289. if(i>1){// 和上一个加入点的vec之间的夹角如果过大就加入
  290. let reachNextUToT //找出新点中对应原先控制点的index,这些点必须加入拐点,否则会出现控制点偏移path(当它所在部分接近直线时)
  291. while(UtoTMapArr[i] > nextUtoTIndex / (oldCount-1)){//可能多个控制点对应一个点,当控制点很近时
  292. reachNextUToT = true
  293. nextUtoTIndex ++
  294. }
  295. if(/* pointIndexs.includes(i) || */ reachNextUToT || curVec.angleTo(lastVec) > minRad){//最小角度 (注意原始点不能太稀疏)
  296. newPoints.push(point)
  297. UtoTMapArr && newUtoTMapArr.push(UtoTMapArr[i])
  298. lastVec = curVec
  299. }
  300. }
  301. }
  302. }
  303. return {newUtoTMapArr, newPoints}
  304. },
  305. getExtrudeGeo: function(shapes, holes, options={openEnded:false, shapeDontClose:false}){//获得挤出棱柱,可以选择传递height,或者extrudePath
  306. var shape = this.getShape(shapes, holes) //points是横截面 [vector2,...]
  307. if(options.extrudePath ){// 路径 :[vector3,...]
  308. var length = options.extrudePath.reduce((total, currentValue, currentIndex, arr)=>{
  309. if(currentIndex == 0)return 0
  310. return total + currentValue.distanceTo(arr[currentIndex-1]);
  311. },0)
  312. //options.extrudePath = new THREE.CatmullRomCurve3(options.extrudePath)
  313. if(options.extrudePath.length == 2){
  314. options.tension = 0 ;//否则一端扭曲
  315. options.steps = 1
  316. }
  317. {//去掉重复的点
  318. let path = []
  319. const minDis = options.dontSmooth ? 0 : 0.2 //CatmullRomCurve3 经常扭曲,如果两个点靠得很近可能会扭曲,这里去除靠的太近的点。但去除后依旧会出现一定扭曲.
  320. options.extrudePath.forEach((p,i)=>{
  321. if(i==0 || i== options.extrudePath.length-1)return path.push(p) //首尾直接加入
  322. let last = path[path.length-1]//和上一个比
  323. let dis = last.distanceTo(p)
  324. if(dis <= minDis){
  325. console.log(`第${i}个点(${p.toArray()})因为和上一个数据(${last.toArray()})太接近(dis:${dis})所以删除`)
  326. }else if(i == options.extrudePath.length - 2){//因为最后一个必定加入,所以倒数第二个还也不能太靠近最后一个
  327. last = options.extrudePath[options.extrudePath.length-1] //和下一个(最后一个比)
  328. if(dis <= minDis){
  329. console.log(`第${i}个点(${p.toArray()})因为和下一个数据(${last.toArray()})太接近(dis:${dis})所以删除`)
  330. }else{
  331. path.push(p)
  332. }
  333. }else{
  334. path.push(p)
  335. }
  336. })
  337. options.extrudePath = path
  338. }
  339. if(!options.dontSmooth){
  340. //平滑连续的曲线(但经常会有扭曲的问题,tension:0能缓解, 另外shape和path都最好在原点附近,也就是点需减去bound.min )
  341. options.extrudePath = new THREE.CatmullRomCurve3(options.extrudePath, options.closed , 'catmullrom' /* 'centripetal' */ , options.tension)//tension:张力, 越大弯曲越大。 随着长度增长,该值需要减小,否则会扭曲
  342. if(options.lessPoints !== false ){//曲线但压缩直线部分点数量
  343. options.extrudePath.UtoTMapArr = [] //用于存储 getSpacedPoints得到的点对应points的百分比对应
  344. let count = Math.max(2, Math.round(length* (options.lessSpace || 200) )) //为了防止有大拐弯才设置这么高
  345. let points = options.extrudePath.getSpacedPoints(count-1) //拆分为更密集的点
  346. let result = this.lessCurvePoints(points, options.extrudePath.points.length, options.minRad, options.extrudePath.UtoTMapArr ) //传UtoTMapArr的话点太多了卡住了
  347. //options.extrudePath = points
  348. options.extrudePath = result.newPoints
  349. options.dontSmooth = true
  350. }
  351. }
  352. if(options.dontSmooth){
  353. let curvePath = new THREE.CurvePath()//通用的曲线路径对象,它可以包含直线段和曲线段。在这里只做折线
  354. curvePath.points = options.extrudePath//add
  355. for (let i = 0; i < options.extrudePath.length - 1; i++){
  356. let curve3 = new THREE.LineCurve3(options.extrudePath[i], options.extrudePath[i + 1]);//添加线段
  357. curvePath.add(curve3);
  358. }
  359. options.steps = options.extrudePath.length - 1
  360. options.extrudePath = curvePath
  361. options.tension = 0
  362. //已修改过three,原本会平分细分,现在dontSmooth时会直接按照控制点来分段
  363. }
  364. }
  365. var extrudeSettings = $.extend(options,{
  366. steps: options.steps != void 0 ? options.steps : ( options.extrudePath ? Math.round(length/(options.spaceDis || 0.2)) : 1), //分成几段 spaceDis每段长度
  367. bevelEnabled: false, //不加的话,height为0时会有圆弧高度
  368. //openEnded默认false
  369. })
  370. var geometry = new THREE.ExtrudeBufferGeometry( shape, extrudeSettings ); //修改了three.js文件, buildLidFaces处,创建顶底面加了选项,可以选择开口。
  371. return geometry;
  372. /* tension = 0:曲线会变成一条直线,没有弯曲。
  373. tension = 0.5:曲线会经过所有控制点,并保持自然的弯曲。
  374. tension > 0.5:曲线会更平滑,远离控制点之间的路径。
  375. tension < 0.5:曲线会更贴近控制点之间的路径,弯曲更明显。 */
  376. },
  377. getUnPosPlaneGeo : function(){//获取还没有赋值位置的plane geometry
  378. var e = new Uint16Array([0, 1, 2, 0, 2, 3])
  379. // , t = new Float32Array([-.5, -.5, 0, .5, -.5, 0, .5, .5, 0, -.5, .5, 0])
  380. , i = new Float32Array([0, 0, 1, 0, 1, 1, 0, 1])
  381. , g = new THREE.BufferGeometry;
  382. g.setIndex(new THREE.BufferAttribute(e, 1)),
  383. //g.addAttribute("position", new n.BufferAttribute(t, 3)),
  384. g.setAttribute("uv", new THREE.BufferAttribute(i, 2))
  385. return function(){
  386. return g
  387. }
  388. }(),
  389. getPlaneGeo : function(A,B,C,D){
  390. var geo = this.getUnPosPlaneGeo().clone();
  391. var pos = [
  392. A.x, A.y, A.z,
  393. B.x, B.y, B.z,
  394. C.x, C.y, C.z,
  395. D.x, D.y, D.z
  396. ]
  397. //geo.addAttribute("position", new THREE.BufferAttribute(pos, 3))
  398. geo.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3));
  399. geo.computeVertexNormals()
  400. geo.computeBoundingSphere() //for raycaster
  401. return geo;
  402. },
  403. drawPlane : function(A,B,C,D, material){
  404. var wall = new THREE.Mesh(this.getPlaneGeo(A,B,C,D), material);
  405. return wall;
  406. },
  407. movePlane: function(mesh, A,B,C,D){
  408. var pos = new Float32Array([
  409. A.x, A.y, A.z,
  410. B.x, B.y, B.z,
  411. C.x, C.y, C.z,
  412. D.x, D.y, D.z
  413. ])
  414. mesh.geometry.addAttribute("position", new THREE.BufferAttribute(pos, 3))
  415. mesh.geometry.computeBoundingSphere()//for checkIntersect
  416. }
  417. ,
  418. createGeometry:function(posArr, faceArr, uvArr, normalArr ){//创建复杂mesh. faceArr:[[0,1,2],[0,2,3]]
  419. let geo = new THREE.BufferGeometry;
  420. let positions = [];
  421. posArr.forEach(p=>positions.push(p.x,p.y,p.z));
  422. geo.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
  423. if(faceArr){
  424. let indice = []
  425. faceArr.forEach(f=>indice.push(...f));
  426. geo.setIndex(indice) // auto set Uint16BufferAttribute or Uint32BufferAttribute
  427. }
  428. if(uvArr){
  429. let uvs = []
  430. uvArr.forEach(uv=>uvs.push(uv.x,uv.y));
  431. geo.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2))
  432. }
  433. if(normalArr){
  434. let normals = []
  435. normalArr.forEach(n=>normals.push(n.x,n.y,n.z));
  436. geo.setAttribute("normal", new THREE.Float32BufferAttribute(normals, 3))
  437. }
  438. /*
  439. geo.computeVertexNormals()
  440. geo.computeBoundingSphere() //for raycaster
  441. */
  442. return geo
  443. },
  444. updateGeometry:function(geo, posArr, faceArr, uvArr, normalArr ){//创建复杂mesh. faceArr:[[0,1,2],[0,2,3]]
  445. let positions = [];
  446. posArr.forEach(p=>positions.push(p.x,p.y,p.z));
  447. geo.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
  448. geo.attributes.position.needsUpdate = true;
  449. if(faceArr){
  450. let indice = []
  451. faceArr.forEach(f=>indice.push(...f));
  452. geo.setIndex(indice) // auto set Uint16BufferAttribute or Uint32BufferAttribute
  453. }
  454. if(uvArr){
  455. let uvs = []
  456. uvArr.forEach(uv=>uvs.push(uv.x,uv.y));
  457. geo.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2))
  458. }
  459. if(normalArr){
  460. let normals = []
  461. normalArr.forEach(n=>normals.push(n.x,n.y,n.z));
  462. geo.setAttribute("normal", new THREE.Float32BufferAttribute(normals, 3))
  463. }
  464. /*
  465. geo.computeVertexNormals()
  466. */
  467. geo.computeBoundingSphere() //for raycaster and visi
  468. return geo
  469. }
  470. }
  471. export {LineDraw, MeshDraw} ;