GeometryCompressionUtils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. import {
  2. BufferAttribute,
  3. Matrix3,
  4. Matrix4,
  5. Vector3
  6. } from 'three';
  7. /** @module GeometryCompressionUtils */
  8. // Octahedron and Quantization encodings based on work by: https://github.com/tsherif/mesh-quantization-example
  9. /**
  10. * Compressed the given geometry's `normal` attribute by the selected encode method.
  11. *
  12. * @param {BufferGeometry} geometry - The geometry whose normals should be compressed.
  13. * @param {('DEFAULT'|'OCT1Byte'|'OCT2Byte'|'ANGLES')} encodeMethod - The compression method.
  14. */
  15. function compressNormals( geometry, encodeMethod ) {
  16. const normal = geometry.attributes.normal;
  17. if ( ! normal ) {
  18. console.error( 'THREE.GeometryCompressionUtils.compressNormals(): Geometry must contain normal attribute.' );
  19. }
  20. if ( normal.isPacked ) return;
  21. if ( normal.itemSize != 3 ) {
  22. console.error( 'THREE.GeometryCompressionUtils.compressNormals(): normal.itemSize is not 3, which cannot be encoded.' );
  23. }
  24. const array = normal.array;
  25. const count = normal.count;
  26. let result;
  27. if ( encodeMethod == 'DEFAULT' ) {
  28. // TODO: Add 1 byte to the result, making the encoded length to be 4 bytes.
  29. result = new Uint8Array( count * 3 );
  30. for ( let idx = 0; idx < array.length; idx += 3 ) {
  31. const encoded = defaultEncode( array[ idx ], array[ idx + 1 ], array[ idx + 2 ], 1 );
  32. result[ idx + 0 ] = encoded[ 0 ];
  33. result[ idx + 1 ] = encoded[ 1 ];
  34. result[ idx + 2 ] = encoded[ 2 ];
  35. }
  36. geometry.setAttribute( 'normal', new BufferAttribute( result, 3, true ) );
  37. geometry.attributes.normal.bytes = result.length * 1;
  38. } else if ( encodeMethod == 'OCT1Byte' ) {
  39. // It is not recommended to use 1-byte octahedron normals encoding unless you want to extremely reduce the memory usage
  40. // As it makes vertex data not aligned to a 4 byte boundary which may harm some WebGL implementations and sometimes the normal distortion is visible
  41. // Please refer to @zeux 's comments in https://github.com/mrdoob/three.js/pull/18208
  42. result = new Int8Array( count * 2 );
  43. for ( let idx = 0; idx < array.length; idx += 3 ) {
  44. const encoded = octEncodeBest( array[ idx ], array[ idx + 1 ], array[ idx + 2 ], 1 );
  45. result[ idx / 3 * 2 + 0 ] = encoded[ 0 ];
  46. result[ idx / 3 * 2 + 1 ] = encoded[ 1 ];
  47. }
  48. geometry.setAttribute( 'normal', new BufferAttribute( result, 2, true ) );
  49. geometry.attributes.normal.bytes = result.length * 1;
  50. } else if ( encodeMethod == 'OCT2Byte' ) {
  51. result = new Int16Array( count * 2 );
  52. for ( let idx = 0; idx < array.length; idx += 3 ) {
  53. const encoded = octEncodeBest( array[ idx ], array[ idx + 1 ], array[ idx + 2 ], 2 );
  54. result[ idx / 3 * 2 + 0 ] = encoded[ 0 ];
  55. result[ idx / 3 * 2 + 1 ] = encoded[ 1 ];
  56. }
  57. geometry.setAttribute( 'normal', new BufferAttribute( result, 2, true ) );
  58. geometry.attributes.normal.bytes = result.length * 2;
  59. } else if ( encodeMethod == 'ANGLES' ) {
  60. result = new Uint16Array( count * 2 );
  61. for ( let idx = 0; idx < array.length; idx += 3 ) {
  62. const encoded = anglesEncode( array[ idx ], array[ idx + 1 ], array[ idx + 2 ] );
  63. result[ idx / 3 * 2 + 0 ] = encoded[ 0 ];
  64. result[ idx / 3 * 2 + 1 ] = encoded[ 1 ];
  65. }
  66. geometry.setAttribute( 'normal', new BufferAttribute( result, 2, true ) );
  67. geometry.attributes.normal.bytes = result.length * 2;
  68. } else {
  69. console.error( 'Unrecognized encoding method, should be `DEFAULT` or `ANGLES` or `OCT`. ' );
  70. }
  71. geometry.attributes.normal.needsUpdate = true;
  72. geometry.attributes.normal.isPacked = true;
  73. geometry.attributes.normal.packingMethod = encodeMethod;
  74. }
  75. /**
  76. * Compressed the given geometry's `position` attribute.
  77. *
  78. * @param {BufferGeometry} geometry - The geometry whose position values should be compressed.
  79. */
  80. function compressPositions( geometry ) {
  81. const position = geometry.attributes.position;
  82. if ( ! position ) {
  83. console.error( 'THREE.GeometryCompressionUtils.compressPositions(): Geometry must contain position attribute.' );
  84. }
  85. if ( position.isPacked ) return;
  86. if ( position.itemSize != 3 ) {
  87. console.error( 'THREE.GeometryCompressionUtils.compressPositions(): position.itemSize is not 3, which cannot be packed.' );
  88. }
  89. const array = position.array;
  90. const encodingBytes = 2;
  91. const result = quantizedEncode( array, encodingBytes );
  92. const quantized = result.quantized;
  93. // IMPORTANT: calculate original geometry bounding info first, before updating packed positions
  94. if ( geometry.boundingBox == null ) geometry.computeBoundingBox();
  95. if ( geometry.boundingSphere == null ) geometry.computeBoundingSphere();
  96. geometry.setAttribute( 'position', new BufferAttribute( quantized, 3 ) );
  97. geometry.attributes.position.isPacked = true;
  98. geometry.attributes.position.needsUpdate = true;
  99. geometry.attributes.position.bytes = quantized.length * encodingBytes;
  100. }
  101. /**
  102. * Compressed the given geometry's `uv` attribute.
  103. *
  104. * @param {BufferGeometry} geometry - The geometry whose texture coordinates should be compressed.
  105. */
  106. function compressUvs( geometry ) {
  107. const uvs = geometry.attributes.uv;
  108. if ( ! uvs ) {
  109. console.error( 'THREE.GeometryCompressionUtils.compressUvs(): Geometry must contain uv attribute.' );
  110. }
  111. if ( uvs.isPacked ) return;
  112. const range = { min: Infinity, max: - Infinity };
  113. const array = uvs.array;
  114. for ( let i = 0; i < array.length; i ++ ) {
  115. range.min = Math.min( range.min, array[ i ] );
  116. range.max = Math.max( range.max, array[ i ] );
  117. }
  118. let result;
  119. if ( range.min >= - 1.0 && range.max <= 1.0 ) {
  120. // use default encoding method
  121. result = new Uint16Array( array.length );
  122. for ( let i = 0; i < array.length; i += 2 ) {
  123. const encoded = defaultEncode( array[ i ], array[ i + 1 ], 0, 2 );
  124. result[ i ] = encoded[ 0 ];
  125. result[ i + 1 ] = encoded[ 1 ];
  126. }
  127. geometry.setAttribute( 'uv', new BufferAttribute( result, 2, true ) );
  128. geometry.attributes.uv.isPacked = true;
  129. geometry.attributes.uv.needsUpdate = true;
  130. geometry.attributes.uv.bytes = result.length * 2;
  131. } else {
  132. // use quantized encoding method
  133. result = quantizedEncodeUV( array, 2 );
  134. geometry.setAttribute( 'uv', new BufferAttribute( result.quantized, 2 ) );
  135. geometry.attributes.uv.isPacked = true;
  136. geometry.attributes.uv.needsUpdate = true;
  137. geometry.attributes.uv.bytes = result.quantized.length * 2;
  138. }
  139. }
  140. // Encoding functions
  141. function defaultEncode( x, y, z, bytes ) {
  142. if ( bytes == 1 ) {
  143. const tmpx = Math.round( ( x + 1 ) * 0.5 * 255 );
  144. const tmpy = Math.round( ( y + 1 ) * 0.5 * 255 );
  145. const tmpz = Math.round( ( z + 1 ) * 0.5 * 255 );
  146. return new Uint8Array( [ tmpx, tmpy, tmpz ] );
  147. } else if ( bytes == 2 ) {
  148. const tmpx = Math.round( ( x + 1 ) * 0.5 * 65535 );
  149. const tmpy = Math.round( ( y + 1 ) * 0.5 * 65535 );
  150. const tmpz = Math.round( ( z + 1 ) * 0.5 * 65535 );
  151. return new Uint16Array( [ tmpx, tmpy, tmpz ] );
  152. } else {
  153. console.error( 'number of bytes must be 1 or 2' );
  154. }
  155. }
  156. // for `Angles` encoding
  157. function anglesEncode( x, y, z ) {
  158. const normal0 = parseInt( 0.5 * ( 1.0 + Math.atan2( y, x ) / Math.PI ) * 65535 );
  159. const normal1 = parseInt( 0.5 * ( 1.0 + z ) * 65535 );
  160. return new Uint16Array( [ normal0, normal1 ] );
  161. }
  162. // for `Octahedron` encoding
  163. function octEncodeBest( x, y, z, bytes ) {
  164. let oct, dec, best, currentCos, bestCos;
  165. // Test various combinations of ceil and floor
  166. // to minimize rounding errors
  167. best = oct = octEncodeVec3( x, y, z, 'floor', 'floor' );
  168. dec = octDecodeVec2( oct );
  169. bestCos = dot( x, y, z, dec );
  170. oct = octEncodeVec3( x, y, z, 'ceil', 'floor' );
  171. dec = octDecodeVec2( oct );
  172. currentCos = dot( x, y, z, dec );
  173. if ( currentCos > bestCos ) {
  174. best = oct;
  175. bestCos = currentCos;
  176. }
  177. oct = octEncodeVec3( x, y, z, 'floor', 'ceil' );
  178. dec = octDecodeVec2( oct );
  179. currentCos = dot( x, y, z, dec );
  180. if ( currentCos > bestCos ) {
  181. best = oct;
  182. bestCos = currentCos;
  183. }
  184. oct = octEncodeVec3( x, y, z, 'ceil', 'ceil' );
  185. dec = octDecodeVec2( oct );
  186. currentCos = dot( x, y, z, dec );
  187. if ( currentCos > bestCos ) {
  188. best = oct;
  189. }
  190. return best;
  191. function octEncodeVec3( x0, y0, z0, xfunc, yfunc ) {
  192. let x = x0 / ( Math.abs( x0 ) + Math.abs( y0 ) + Math.abs( z0 ) );
  193. let y = y0 / ( Math.abs( x0 ) + Math.abs( y0 ) + Math.abs( z0 ) );
  194. if ( z < 0 ) {
  195. const tempx = ( 1 - Math.abs( y ) ) * ( x >= 0 ? 1 : - 1 );
  196. const tempy = ( 1 - Math.abs( x ) ) * ( y >= 0 ? 1 : - 1 );
  197. x = tempx;
  198. y = tempy;
  199. let diff = 1 - Math.abs( x ) - Math.abs( y );
  200. if ( diff > 0 ) {
  201. diff += 0.001;
  202. x += x > 0 ? diff / 2 : - diff / 2;
  203. y += y > 0 ? diff / 2 : - diff / 2;
  204. }
  205. }
  206. if ( bytes == 1 ) {
  207. return new Int8Array( [
  208. Math[ xfunc ]( x * 127.5 + ( x < 0 ? 1 : 0 ) ),
  209. Math[ yfunc ]( y * 127.5 + ( y < 0 ? 1 : 0 ) )
  210. ] );
  211. }
  212. if ( bytes == 2 ) {
  213. return new Int16Array( [
  214. Math[ xfunc ]( x * 32767.5 + ( x < 0 ? 1 : 0 ) ),
  215. Math[ yfunc ]( y * 32767.5 + ( y < 0 ? 1 : 0 ) )
  216. ] );
  217. }
  218. }
  219. function octDecodeVec2( oct ) {
  220. let x = oct[ 0 ];
  221. let y = oct[ 1 ];
  222. if ( bytes == 1 ) {
  223. x /= x < 0 ? 127 : 128;
  224. y /= y < 0 ? 127 : 128;
  225. } else if ( bytes == 2 ) {
  226. x /= x < 0 ? 32767 : 32768;
  227. y /= y < 0 ? 32767 : 32768;
  228. }
  229. const z = 1 - Math.abs( x ) - Math.abs( y );
  230. if ( z < 0 ) {
  231. const tmpx = x;
  232. x = ( 1 - Math.abs( y ) ) * ( x >= 0 ? 1 : - 1 );
  233. y = ( 1 - Math.abs( tmpx ) ) * ( y >= 0 ? 1 : - 1 );
  234. }
  235. const length = Math.sqrt( x * x + y * y + z * z );
  236. return [
  237. x / length,
  238. y / length,
  239. z / length
  240. ];
  241. }
  242. function dot( x, y, z, vec3 ) {
  243. return x * vec3[ 0 ] + y * vec3[ 1 ] + z * vec3[ 2 ];
  244. }
  245. }
  246. function quantizedEncode( array, bytes ) {
  247. let quantized, segments;
  248. if ( bytes == 1 ) {
  249. quantized = new Uint8Array( array.length );
  250. segments = 255;
  251. } else if ( bytes == 2 ) {
  252. quantized = new Uint16Array( array.length );
  253. segments = 65535;
  254. } else {
  255. console.error( 'number of bytes error! ' );
  256. }
  257. const decodeMat = new Matrix4();
  258. const min = new Float32Array( 3 );
  259. const max = new Float32Array( 3 );
  260. min[ 0 ] = min[ 1 ] = min[ 2 ] = Number.MAX_VALUE;
  261. max[ 0 ] = max[ 1 ] = max[ 2 ] = - Number.MAX_VALUE;
  262. for ( let i = 0; i < array.length; i += 3 ) {
  263. min[ 0 ] = Math.min( min[ 0 ], array[ i + 0 ] );
  264. min[ 1 ] = Math.min( min[ 1 ], array[ i + 1 ] );
  265. min[ 2 ] = Math.min( min[ 2 ], array[ i + 2 ] );
  266. max[ 0 ] = Math.max( max[ 0 ], array[ i + 0 ] );
  267. max[ 1 ] = Math.max( max[ 1 ], array[ i + 1 ] );
  268. max[ 2 ] = Math.max( max[ 2 ], array[ i + 2 ] );
  269. }
  270. decodeMat.scale( new Vector3(
  271. ( max[ 0 ] - min[ 0 ] ) / segments,
  272. ( max[ 1 ] - min[ 1 ] ) / segments,
  273. ( max[ 2 ] - min[ 2 ] ) / segments
  274. ) );
  275. decodeMat.elements[ 12 ] = min[ 0 ];
  276. decodeMat.elements[ 13 ] = min[ 1 ];
  277. decodeMat.elements[ 14 ] = min[ 2 ];
  278. decodeMat.transpose();
  279. const multiplier = new Float32Array( [
  280. max[ 0 ] !== min[ 0 ] ? segments / ( max[ 0 ] - min[ 0 ] ) : 0,
  281. max[ 1 ] !== min[ 1 ] ? segments / ( max[ 1 ] - min[ 1 ] ) : 0,
  282. max[ 2 ] !== min[ 2 ] ? segments / ( max[ 2 ] - min[ 2 ] ) : 0
  283. ] );
  284. for ( let i = 0; i < array.length; i += 3 ) {
  285. quantized[ i + 0 ] = Math.floor( ( array[ i + 0 ] - min[ 0 ] ) * multiplier[ 0 ] );
  286. quantized[ i + 1 ] = Math.floor( ( array[ i + 1 ] - min[ 1 ] ) * multiplier[ 1 ] );
  287. quantized[ i + 2 ] = Math.floor( ( array[ i + 2 ] - min[ 2 ] ) * multiplier[ 2 ] );
  288. }
  289. return {
  290. quantized: quantized,
  291. decodeMat: decodeMat
  292. };
  293. }
  294. function quantizedEncodeUV( array, bytes ) {
  295. let quantized, segments;
  296. if ( bytes == 1 ) {
  297. quantized = new Uint8Array( array.length );
  298. segments = 255;
  299. } else if ( bytes == 2 ) {
  300. quantized = new Uint16Array( array.length );
  301. segments = 65535;
  302. } else {
  303. console.error( 'number of bytes error! ' );
  304. }
  305. const decodeMat = new Matrix3();
  306. const min = new Float32Array( 2 );
  307. const max = new Float32Array( 2 );
  308. min[ 0 ] = min[ 1 ] = Number.MAX_VALUE;
  309. max[ 0 ] = max[ 1 ] = - Number.MAX_VALUE;
  310. for ( let i = 0; i < array.length; i += 2 ) {
  311. min[ 0 ] = Math.min( min[ 0 ], array[ i + 0 ] );
  312. min[ 1 ] = Math.min( min[ 1 ], array[ i + 1 ] );
  313. max[ 0 ] = Math.max( max[ 0 ], array[ i + 0 ] );
  314. max[ 1 ] = Math.max( max[ 1 ], array[ i + 1 ] );
  315. }
  316. decodeMat.scale(
  317. ( max[ 0 ] - min[ 0 ] ) / segments,
  318. ( max[ 1 ] - min[ 1 ] ) / segments
  319. );
  320. decodeMat.elements[ 6 ] = min[ 0 ];
  321. decodeMat.elements[ 7 ] = min[ 1 ];
  322. decodeMat.transpose();
  323. const multiplier = new Float32Array( [
  324. max[ 0 ] !== min[ 0 ] ? segments / ( max[ 0 ] - min[ 0 ] ) : 0,
  325. max[ 1 ] !== min[ 1 ] ? segments / ( max[ 1 ] - min[ 1 ] ) : 0
  326. ] );
  327. for ( let i = 0; i < array.length; i += 2 ) {
  328. quantized[ i + 0 ] = Math.floor( ( array[ i + 0 ] - min[ 0 ] ) * multiplier[ 0 ] );
  329. quantized[ i + 1 ] = Math.floor( ( array[ i + 1 ] - min[ 1 ] ) * multiplier[ 1 ] );
  330. }
  331. return {
  332. quantized: quantized,
  333. decodeMat: decodeMat
  334. };
  335. }
  336. export {
  337. compressNormals,
  338. compressPositions,
  339. compressUvs,
  340. };
粤ICP备19079148号