GeometryCompressionUtils.js 13 KB

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