import * as structs from '../const/structs' ; import { TRIANGLE_FAN , TRIANGLE_STRIP } from '../const/constants' ; /**
 * Returns type of a series of connected triangles
 */ export const getTriangleSeriesType = ( trianglesSeriesHead ) => trianglesSeriesHead < 0 ? TRIANGLE_FAN : TRIANGLE_STRIP ; param trianglesBuffer return export const countVertices = ( trianglesBuffer ) : number => { let vertCount = 0 ; let p = 0 ; while ( trianglesBuffer [ p ] ) { const verticesNum = Math . abs ( trianglesBuffer [ p ] ) ; p += verticesNum * 4 + 1 ; vertCount += ( verticesNum - 3 ) * 3 + 3 ; } return vertCount ; } ; param trianglesBuffer param verticesBuffer param texture returns todo export const readFacesData = ( trianglesBuffer , verticesBuffer , texture ) => { const vertNumber = countVertices ( trianglesBuffer ) ; const verticesData = [ ] ; let trisPos = 0 ; while ( trianglesBuffer [ trisPos ] ) { const trianglesType = trianglesBuffer [ trisPos ] < 0 ? TRIANGLE_FAN : TRIANGLE_STRIP ; let startVert = null ; const trianglesNum = Math . abs ( trianglesBuffer [ trisPos ] ) ; trisPos ++ ; for ( let j = 0 ; j < trianglesNum ; j ++ , trisPos += 4 ) { const vertIndex = trianglesBuffer [ trisPos ] ; const vert = trianglesBuffer [ trisPos ] * 3 ; const vertexData = [ verticesBuffer [ vert + 0 ] , verticesBuffer [ vert + 1 ] , verticesBuffer [ vert + 2 ] , trianglesBuffer [ trisPos + 2 ] / texture . width , 1 - trianglesBuffer [ trisPos + 3 ] / texture . height , vertIndex ] ; if ( trianglesType === TRIANGLE_STRIP ) { if ( j > 2 ) { if ( j % 2 === 0 ) { verticesData . push ( verticesData [ verticesData . length - 3 ] , verticesData [ verticesData . length - 1 ] ) ; } else { verticesData . push ( verticesData [ verticesData . length - 1 ] , verticesData [ verticesData . length - 2 ] ) ; } } } if ( trianglesType === TRIANGLE_FAN ) { startVert = startVert || vertexData ; if ( j > 2 ) { verticesData . push ( startVert , verticesData [ verticesData . length - 1 ] ) ; } } verticesData . push ( vertexData ) ; } } const vertices = new Float32Array ( vertNumber * 3 ) ; const uv = new Float32Array ( vertNumber * 2 ) ; const indices = new Int16Array ( vertNumber ) ; for ( let i = 0 ; i < vertNumber ; i ++ ) { vertices [ i * 3 + 0 ] = verticesData [ i ] [ 0 ] ; vertices [ i * 3 + 1 ] = verticesData [ i ] [ 1 ] ; vertices [ i * 3 + 2 ] = verticesData [ i ] [ 2 ] ; uv [ i * 2 + 0 ] = verticesData [ i ] [ 3 ] ; uv [ i * 2 + 1 ] = verticesData [ i ] [ 4 ] ; indices [ i ] = verticesData [ i ] [ 5 ] ; } return { vertices , uv , indices } ; } ;