GeometryCompressionUtils.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /**
  2. * Octahedron and Quantization encodings based on work by:
  3. *
  4. * @link https://github.com/tsherif/mesh-quantization-example
  5. *
  6. */
  7. import {
  8. BufferAttribute,
  9. Matrix3,
  10. Matrix4,
  11. MeshPhongMaterial,
  12. ShaderChunk,
  13. ShaderLib,
  14. UniformsUtils,
  15. Vector3
  16. } from "../../../build/three.module.js";
  17. var GeometryCompressionUtils = {
  18. /**
  19. * Make the input mesh.geometry's normal attribute encoded and compressed by 3 different methods.
  20. * Also will change the mesh.material to `PackedPhongMaterial` which let the vertex shader program decode the normal data.
  21. *
  22. * @param {THREE.Mesh} mesh
  23. * @param {String} encodeMethod "DEFAULT" || "OCT1Byte" || "OCT2Byte" || "ANGLES"
  24. *
  25. */
  26. compressNormals: function ( mesh, encodeMethod ) {
  27. if ( ! mesh.geometry ) {
  28. console.error( "Mesh must contain geometry. " );
  29. }
  30. let normal = mesh.geometry.attributes.normal;
  31. if ( ! normal ) {
  32. console.error( "Geometry must contain normal attribute. " );
  33. }
  34. if ( normal.isPacked ) return;
  35. if ( normal.itemSize != 3 ) {
  36. console.error( "normal.itemSize is not 3, which cannot be encoded. " );
  37. }
  38. let array = normal.array;
  39. let count = normal.count;
  40. let result;
  41. if ( encodeMethod == "DEFAULT" ) {
  42. // TODO: Add 1 byte to the result, making the encoded length to be 4 bytes.
  43. result = new Uint8Array( count * 3 );
  44. for ( let idx = 0; idx < array.length; idx += 3 ) {
  45. let encoded;
  46. encoded = this.EncodingFuncs.defaultEncode( array[ idx ], array[ idx + 1 ], array[ idx + 2 ], 1 );
  47. result[ idx + 0 ] = encoded[ 0 ];
  48. result[ idx + 1 ] = encoded[ 1 ];
  49. result[ idx + 2 ] = encoded[ 2 ];
  50. }
  51. mesh.geometry.setAttribute( 'normal', new BufferAttribute( result, 3, true ) );
  52. mesh.geometry.attributes.normal.bytes = result.length * 1;
  53. } else if ( encodeMethod == "OCT1Byte" ) {
  54. /**
  55. * It is not recommended to use 1-byte octahedron normals encoding unless you want to extremely reduce the memory usage
  56. * 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
  57. * Please refer to @zeux 's comments in https://github.com/mrdoob/three.js/pull/18208
  58. */
  59. result = new Int8Array( count * 2 );
  60. for ( let idx = 0; idx < array.length; idx += 3 ) {
  61. let encoded;
  62. encoded = this.EncodingFuncs.octEncodeBest( array[ idx ], array[ idx + 1 ], array[ idx + 2 ], 1 );
  63. result[ idx / 3 * 2 + 0 ] = encoded[ 0 ];
  64. result[ idx / 3 * 2 + 1 ] = encoded[ 1 ];
  65. }
  66. mesh.geometry.setAttribute( 'normal', new BufferAttribute( result, 2, true ) );
  67. mesh.geometry.attributes.normal.bytes = result.length * 1;
  68. } else if ( encodeMethod == "OCT2Byte" ) {
  69. result = new Int16Array( count * 2 );
  70. for ( let idx = 0; idx < array.length; idx += 3 ) {
  71. let encoded;
  72. encoded = this.EncodingFuncs.octEncodeBest( array[ idx ], array[ idx + 1 ], array[ idx + 2 ], 2 );
  73. result[ idx / 3 * 2 + 0 ] = encoded[ 0 ];
  74. result[ idx / 3 * 2 + 1 ] = encoded[ 1 ];
  75. }
  76. mesh.geometry.setAttribute( 'normal', new BufferAttribute( result, 2, true ) );
  77. mesh.geometry.attributes.normal.bytes = result.length * 2;
  78. } else if ( encodeMethod == "ANGLES" ) {
  79. result = new Uint16Array( count * 2 );
  80. for ( let idx = 0; idx < array.length; idx += 3 ) {
  81. let encoded;
  82. encoded = this.EncodingFuncs.anglesEncode( array[ idx ], array[ idx + 1 ], array[ idx + 2 ] );
  83. result[ idx / 3 * 2 + 0 ] = encoded[ 0 ];
  84. result[ idx / 3 * 2 + 1 ] = encoded[ 1 ];
  85. }
  86. mesh.geometry.setAttribute( 'normal', new BufferAttribute( result, 2, true ) );
  87. mesh.geometry.attributes.normal.bytes = result.length * 2;
  88. } else {
  89. console.error( "Unrecognized encoding method, should be `DEFAULT` or `ANGLES` or `OCT`. " );
  90. }
  91. mesh.geometry.attributes.normal.needsUpdate = true;
  92. mesh.geometry.attributes.normal.isPacked = true;
  93. mesh.geometry.attributes.normal.packingMethod = encodeMethod;
  94. // modify material
  95. if ( ! ( mesh.material instanceof PackedPhongMaterial ) ) {
  96. mesh.material = new PackedPhongMaterial().copy( mesh.material );
  97. }
  98. if ( encodeMethod == "ANGLES" ) {
  99. mesh.material.defines.USE_PACKED_NORMAL = 0;
  100. }
  101. if ( encodeMethod == "OCT1Byte" ) {
  102. mesh.material.defines.USE_PACKED_NORMAL = 1;
  103. }
  104. if ( encodeMethod == "OCT2Byte" ) {
  105. mesh.material.defines.USE_PACKED_NORMAL = 1;
  106. }
  107. if ( encodeMethod == "DEFAULT" ) {
  108. mesh.material.defines.USE_PACKED_NORMAL = 2;
  109. }
  110. },
  111. /**
  112. * Make the input mesh.geometry's position attribute encoded and compressed.
  113. * Also will change the mesh.material to `PackedPhongMaterial` which let the vertex shader program decode the position data.
  114. *
  115. * @param {THREE.Mesh} mesh
  116. *
  117. */
  118. compressPositions: function ( mesh ) {
  119. if ( ! mesh.geometry ) {
  120. console.error( "Mesh must contain geometry. " );
  121. }
  122. let position = mesh.geometry.attributes.position;
  123. if ( ! position ) {
  124. console.error( "Geometry must contain position attribute. " );
  125. }
  126. if ( position.isPacked ) return;
  127. if ( position.itemSize != 3 ) {
  128. console.error( "position.itemSize is not 3, which cannot be packed. " );
  129. }
  130. let array = position.array;
  131. let encodingBytes = 2;
  132. let result = this.EncodingFuncs.quantizedEncode( array, encodingBytes );
  133. let quantized = result.quantized;
  134. let decodeMat = result.decodeMat;
  135. // IMPORTANT: calculate original geometry bounding info first, before updating packed positions
  136. if ( mesh.geometry.boundingBox == null ) mesh.geometry.computeBoundingBox();
  137. if ( mesh.geometry.boundingSphere == null ) mesh.geometry.computeBoundingSphere();
  138. mesh.geometry.setAttribute( 'position', new BufferAttribute( quantized, 3 ) );
  139. mesh.geometry.attributes.position.isPacked = true;
  140. mesh.geometry.attributes.position.needsUpdate = true;
  141. mesh.geometry.attributes.position.bytes = quantized.length * encodingBytes;
  142. // modify material
  143. if ( ! ( mesh.material instanceof PackedPhongMaterial ) ) {
  144. mesh.material = new PackedPhongMaterial().copy( mesh.material );
  145. }
  146. mesh.material.defines.USE_PACKED_POSITION = 0;
  147. mesh.material.uniforms.quantizeMatPos.value = decodeMat;
  148. mesh.material.uniforms.quantizeMatPos.needsUpdate = true;
  149. },
  150. /**
  151. * Make the input mesh.geometry's uv attribute encoded and compressed.
  152. * Also will change the mesh.material to `PackedPhongMaterial` which let the vertex shader program decode the uv data.
  153. *
  154. * @param {THREE.Mesh} mesh
  155. *
  156. */
  157. compressUvs: function ( mesh ) {
  158. if ( ! mesh.geometry ) {
  159. console.error( "Mesh must contain geometry property. " );
  160. }
  161. let uvs = mesh.geometry.attributes.uv;
  162. if ( ! uvs ) {
  163. console.error( "Geometry must contain uv attribute. " );
  164. }
  165. if ( uvs.isPacked ) return;
  166. let range = { min: Infinity, max: - Infinity };
  167. let array = uvs.array;
  168. for ( let i = 0; i < array.length; i ++ ) {
  169. range.min = Math.min( range.min, array[ i ] );
  170. range.max = Math.max( range.max, array[ i ] );
  171. }
  172. let result;
  173. if ( range.min >= - 1.0 && range.max <= 1.0 ) {
  174. // use default encoding method
  175. result = new Uint16Array( array.length );
  176. for ( let i = 0; i < array.length; i += 2 ) {
  177. let encoded = this.EncodingFuncs.defaultEncode( array[ i ], array[ i + 1 ], 0, 2 );
  178. result[ i ] = encoded[ 0 ];
  179. result[ i + 1 ] = encoded[ 1 ];
  180. }
  181. mesh.geometry.setAttribute( 'uv', new BufferAttribute( result, 2, true ) );
  182. mesh.geometry.attributes.uv.isPacked = true;
  183. mesh.geometry.attributes.uv.needsUpdate = true;
  184. mesh.geometry.attributes.uv.bytes = result.length * 2;
  185. if ( ! ( mesh.material instanceof PackedPhongMaterial ) ) {
  186. mesh.material = new PackedPhongMaterial().copy( mesh.material );
  187. }
  188. mesh.material.defines.USE_PACKED_UV = 0;
  189. } else {
  190. // use quantized encoding method
  191. result = this.EncodingFuncs.quantizedEncodeUV( array, 2 );
  192. mesh.geometry.setAttribute( 'uv', new BufferAttribute( result.quantized, 2 ) );
  193. mesh.geometry.attributes.uv.isPacked = true;
  194. mesh.geometry.attributes.uv.needsUpdate = true;
  195. mesh.geometry.attributes.uv.bytes = result.quantized.length * 2;
  196. if ( ! ( mesh.material instanceof PackedPhongMaterial ) ) {
  197. mesh.material = new PackedPhongMaterial().copy( mesh.material );
  198. }
  199. mesh.material.defines.USE_PACKED_UV = 1;
  200. mesh.material.uniforms.quantizeMatUV.value = result.decodeMat;
  201. mesh.material.uniforms.quantizeMatUV.needsUpdate = true;
  202. }
  203. },
  204. EncodingFuncs: {
  205. defaultEncode: function ( x, y, z, bytes ) {
  206. if ( bytes == 1 ) {
  207. let tmpx = Math.round( ( x + 1 ) * 0.5 * 255 );
  208. let tmpy = Math.round( ( y + 1 ) * 0.5 * 255 );
  209. let tmpz = Math.round( ( z + 1 ) * 0.5 * 255 );
  210. return new Uint8Array( [ tmpx, tmpy, tmpz ] );
  211. } else if ( bytes == 2 ) {
  212. let tmpx = Math.round( ( x + 1 ) * 0.5 * 65535 );
  213. let tmpy = Math.round( ( y + 1 ) * 0.5 * 65535 );
  214. let tmpz = Math.round( ( z + 1 ) * 0.5 * 65535 );
  215. return new Uint16Array( [ tmpx, tmpy, tmpz ] );
  216. } else {
  217. console.error( "number of bytes must be 1 or 2" );
  218. }
  219. },
  220. defaultDecode: function ( array, bytes ) {
  221. if ( bytes == 1 ) {
  222. return [
  223. ( ( array[ 0 ] / 255 ) * 2.0 ) - 1.0,
  224. ( ( array[ 1 ] / 255 ) * 2.0 ) - 1.0,
  225. ( ( array[ 2 ] / 255 ) * 2.0 ) - 1.0,
  226. ];
  227. } else if ( bytes == 2 ) {
  228. return [
  229. ( ( array[ 0 ] / 65535 ) * 2.0 ) - 1.0,
  230. ( ( array[ 1 ] / 65535 ) * 2.0 ) - 1.0,
  231. ( ( array[ 2 ] / 65535 ) * 2.0 ) - 1.0,
  232. ];
  233. } else {
  234. console.error( "number of bytes must be 1 or 2" );
  235. }
  236. },
  237. // for `Angles` encoding
  238. anglesEncode: function ( x, y, z ) {
  239. let normal0 = parseInt( 0.5 * ( 1.0 + Math.atan2( y, x ) / Math.PI ) * 65535 );
  240. let normal1 = parseInt( 0.5 * ( 1.0 + z ) * 65535 );
  241. return new Uint16Array( [ normal0, normal1 ] );
  242. },
  243. // for `Octahedron` encoding
  244. octEncodeBest: function ( x, y, z, bytes ) {
  245. var oct, dec, best, currentCos, bestCos;
  246. // Test various combinations of ceil and floor
  247. // to minimize rounding errors
  248. best = oct = octEncodeVec3( x, y, z, "floor", "floor" );
  249. dec = octDecodeVec2( oct );
  250. bestCos = dot( x, y, z, dec );
  251. oct = octEncodeVec3( x, y, z, "ceil", "floor" );
  252. dec = octDecodeVec2( oct );
  253. currentCos = dot( x, y, z, dec );
  254. if ( currentCos > bestCos ) {
  255. best = oct;
  256. bestCos = currentCos;
  257. }
  258. oct = octEncodeVec3( x, y, z, "floor", "ceil" );
  259. dec = octDecodeVec2( oct );
  260. currentCos = dot( x, y, z, dec );
  261. if ( currentCos > bestCos ) {
  262. best = oct;
  263. bestCos = currentCos;
  264. }
  265. oct = octEncodeVec3( x, y, z, "ceil", "ceil" );
  266. dec = octDecodeVec2( oct );
  267. currentCos = dot( x, y, z, dec );
  268. if ( currentCos > bestCos ) {
  269. best = oct;
  270. }
  271. return best;
  272. function octEncodeVec3( x0, y0, z0, xfunc, yfunc ) {
  273. var x = x0 / ( Math.abs( x0 ) + Math.abs( y0 ) + Math.abs( z0 ) );
  274. var y = y0 / ( Math.abs( x0 ) + Math.abs( y0 ) + Math.abs( z0 ) );
  275. if ( z < 0 ) {
  276. var tempx = ( 1 - Math.abs( y ) ) * ( x >= 0 ? 1 : - 1 );
  277. var tempy = ( 1 - Math.abs( x ) ) * ( y >= 0 ? 1 : - 1 );
  278. x = tempx;
  279. y = tempy;
  280. var diff = 1 - Math.abs( x ) - Math.abs( y );
  281. if ( diff > 0 ) {
  282. diff += 0.001;
  283. x += x > 0 ? diff / 2 : - diff / 2;
  284. y += y > 0 ? diff / 2 : - diff / 2;
  285. }
  286. }
  287. if ( bytes == 1 ) {
  288. return new Int8Array( [
  289. Math[ xfunc ]( x * 127.5 + ( x < 0 ? 1 : 0 ) ),
  290. Math[ yfunc ]( y * 127.5 + ( y < 0 ? 1 : 0 ) )
  291. ] );
  292. }
  293. if ( bytes == 2 ) {
  294. return new Int16Array( [
  295. Math[ xfunc ]( x * 32767.5 + ( x < 0 ? 1 : 0 ) ),
  296. Math[ yfunc ]( y * 32767.5 + ( y < 0 ? 1 : 0 ) )
  297. ] );
  298. }
  299. }
  300. function octDecodeVec2( oct ) {
  301. var x = oct[ 0 ];
  302. var y = oct[ 1 ];
  303. if ( bytes == 1 ) {
  304. x /= x < 0 ? 127 : 128;
  305. y /= y < 0 ? 127 : 128;
  306. } else if ( bytes == 2 ) {
  307. x /= x < 0 ? 32767 : 32768;
  308. y /= y < 0 ? 32767 : 32768;
  309. }
  310. var z = 1 - Math.abs( x ) - Math.abs( y );
  311. if ( z < 0 ) {
  312. var tmpx = x;
  313. x = ( 1 - Math.abs( y ) ) * ( x >= 0 ? 1 : - 1 );
  314. y = ( 1 - Math.abs( tmpx ) ) * ( y >= 0 ? 1 : - 1 );
  315. }
  316. var length = Math.sqrt( x * x + y * y + z * z );
  317. return [
  318. x / length,
  319. y / length,
  320. z / length
  321. ];
  322. }
  323. function dot( x, y, z, vec3 ) {
  324. return x * vec3[ 0 ] + y * vec3[ 1 ] + z * vec3[ 2 ];
  325. }
  326. },
  327. quantizedEncode: function ( array, bytes ) {
  328. let quantized, segments;
  329. if ( bytes == 1 ) {
  330. quantized = new Uint8Array( array.length );
  331. segments = 255;
  332. } else if ( bytes == 2 ) {
  333. quantized = new Uint16Array( array.length );
  334. segments = 65535;
  335. } else {
  336. console.error( "number of bytes error! " );
  337. }
  338. let decodeMat = new Matrix4();
  339. let min = new Float32Array( 3 );
  340. let max = new Float32Array( 3 );
  341. min[ 0 ] = min[ 1 ] = min[ 2 ] = Number.MAX_VALUE;
  342. max[ 0 ] = max[ 1 ] = max[ 2 ] = - Number.MAX_VALUE;
  343. for ( let i = 0; i < array.length; i += 3 ) {
  344. min[ 0 ] = Math.min( min[ 0 ], array[ i + 0 ] );
  345. min[ 1 ] = Math.min( min[ 1 ], array[ i + 1 ] );
  346. min[ 2 ] = Math.min( min[ 2 ], array[ i + 2 ] );
  347. max[ 0 ] = Math.max( max[ 0 ], array[ i + 0 ] );
  348. max[ 1 ] = Math.max( max[ 1 ], array[ i + 1 ] );
  349. max[ 2 ] = Math.max( max[ 2 ], array[ i + 2 ] );
  350. }
  351. decodeMat.scale( new Vector3(
  352. ( max[ 0 ] - min[ 0 ] ) / segments,
  353. ( max[ 1 ] - min[ 1 ] ) / segments,
  354. ( max[ 2 ] - min[ 2 ] ) / segments
  355. ) );
  356. decodeMat.elements[ 12 ] = min[ 0 ];
  357. decodeMat.elements[ 13 ] = min[ 1 ];
  358. decodeMat.elements[ 14 ] = min[ 2 ];
  359. decodeMat.transpose();
  360. let multiplier = new Float32Array( [
  361. max[ 0 ] !== min[ 0 ] ? segments / ( max[ 0 ] - min[ 0 ] ) : 0,
  362. max[ 1 ] !== min[ 1 ] ? segments / ( max[ 1 ] - min[ 1 ] ) : 0,
  363. max[ 2 ] !== min[ 2 ] ? segments / ( max[ 2 ] - min[ 2 ] ) : 0
  364. ] );
  365. for ( let i = 0; i < array.length; i += 3 ) {
  366. quantized[ i + 0 ] = Math.floor( ( array[ i + 0 ] - min[ 0 ] ) * multiplier[ 0 ] );
  367. quantized[ i + 1 ] = Math.floor( ( array[ i + 1 ] - min[ 1 ] ) * multiplier[ 1 ] );
  368. quantized[ i + 2 ] = Math.floor( ( array[ i + 2 ] - min[ 2 ] ) * multiplier[ 2 ] );
  369. }
  370. return {
  371. quantized: quantized,
  372. decodeMat: decodeMat
  373. };
  374. },
  375. quantizedEncodeUV: function ( array, bytes ) {
  376. let quantized, segments;
  377. if ( bytes == 1 ) {
  378. quantized = new Uint8Array( array.length );
  379. segments = 255;
  380. } else if ( bytes == 2 ) {
  381. quantized = new Uint16Array( array.length );
  382. segments = 65535;
  383. } else {
  384. console.error( "number of bytes error! " );
  385. }
  386. let decodeMat = new Matrix3();
  387. let min = new Float32Array( 2 );
  388. let max = new Float32Array( 2 );
  389. min[ 0 ] = min[ 1 ] = Number.MAX_VALUE;
  390. max[ 0 ] = max[ 1 ] = - Number.MAX_VALUE;
  391. for ( let i = 0; i < array.length; i += 2 ) {
  392. min[ 0 ] = Math.min( min[ 0 ], array[ i + 0 ] );
  393. min[ 1 ] = Math.min( min[ 1 ], array[ i + 1 ] );
  394. max[ 0 ] = Math.max( max[ 0 ], array[ i + 0 ] );
  395. max[ 1 ] = Math.max( max[ 1 ], array[ i + 1 ] );
  396. }
  397. decodeMat.scale(
  398. ( max[ 0 ] - min[ 0 ] ) / segments,
  399. ( max[ 1 ] - min[ 1 ] ) / segments
  400. );
  401. decodeMat.elements[ 6 ] = min[ 0 ];
  402. decodeMat.elements[ 7 ] = min[ 1 ];
  403. decodeMat.transpose();
  404. let multiplier = new Float32Array( [
  405. max[ 0 ] !== min[ 0 ] ? segments / ( max[ 0 ] - min[ 0 ] ) : 0,
  406. max[ 1 ] !== min[ 1 ] ? segments / ( max[ 1 ] - min[ 1 ] ) : 0
  407. ] );
  408. for ( let i = 0; i < array.length; i += 2 ) {
  409. quantized[ i + 0 ] = Math.floor( ( array[ i + 0 ] - min[ 0 ] ) * multiplier[ 0 ] );
  410. quantized[ i + 1 ] = Math.floor( ( array[ i + 1 ] - min[ 1 ] ) * multiplier[ 1 ] );
  411. }
  412. return {
  413. quantized: quantized,
  414. decodeMat: decodeMat
  415. };
  416. }
  417. }
  418. };
  419. /**
  420. * `PackedPhongMaterial` inherited from THREE.MeshPhongMaterial
  421. *
  422. * @param {Object} parameters
  423. */
  424. function PackedPhongMaterial( parameters ) {
  425. MeshPhongMaterial.call( this );
  426. this.defines = {};
  427. this.type = 'PackedPhongMaterial';
  428. this.uniforms = UniformsUtils.merge( [
  429. ShaderLib.phong.uniforms,
  430. {
  431. quantizeMatPos: { value: null },
  432. quantizeMatUV: { value: null }
  433. }
  434. ] );
  435. this.vertexShader = [
  436. "#define PHONG",
  437. "varying vec3 vViewPosition;",
  438. "#ifndef FLAT_SHADED",
  439. "varying vec3 vNormal;",
  440. "#endif",
  441. ShaderChunk.common,
  442. ShaderChunk.uv_pars_vertex,
  443. ShaderChunk.uv2_pars_vertex,
  444. ShaderChunk.displacementmap_pars_vertex,
  445. ShaderChunk.envmap_pars_vertex,
  446. ShaderChunk.color_pars_vertex,
  447. ShaderChunk.fog_pars_vertex,
  448. ShaderChunk.morphtarget_pars_vertex,
  449. ShaderChunk.skinning_pars_vertex,
  450. ShaderChunk.shadowmap_pars_vertex,
  451. ShaderChunk.logdepthbuf_pars_vertex,
  452. ShaderChunk.clipping_planes_pars_vertex,
  453. `#ifdef USE_PACKED_NORMAL
  454. #if USE_PACKED_NORMAL == 0
  455. vec3 decodeNormal(vec3 packedNormal)
  456. {
  457. float x = packedNormal.x * 2.0 - 1.0;
  458. float y = packedNormal.y * 2.0 - 1.0;
  459. vec2 scth = vec2(sin(x * PI), cos(x * PI));
  460. vec2 scphi = vec2(sqrt(1.0 - y * y), y);
  461. return normalize( vec3(scth.y * scphi.x, scth.x * scphi.x, scphi.y) );
  462. }
  463. #endif
  464. #if USE_PACKED_NORMAL == 1
  465. vec3 decodeNormal(vec3 packedNormal)
  466. {
  467. vec3 v = vec3(packedNormal.xy, 1.0 - abs(packedNormal.x) - abs(packedNormal.y));
  468. if (v.z < 0.0)
  469. {
  470. v.xy = (1.0 - abs(v.yx)) * vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0);
  471. }
  472. return normalize(v);
  473. }
  474. #endif
  475. #if USE_PACKED_NORMAL == 2
  476. vec3 decodeNormal(vec3 packedNormal)
  477. {
  478. vec3 v = (packedNormal * 2.0) - 1.0;
  479. return normalize(v);
  480. }
  481. #endif
  482. #endif`,
  483. `#ifdef USE_PACKED_POSITION
  484. #if USE_PACKED_POSITION == 0
  485. uniform mat4 quantizeMatPos;
  486. #endif
  487. #endif`,
  488. `#ifdef USE_PACKED_UV
  489. #if USE_PACKED_UV == 1
  490. uniform mat3 quantizeMatUV;
  491. #endif
  492. #endif`,
  493. `#ifdef USE_PACKED_UV
  494. #if USE_PACKED_UV == 0
  495. vec2 decodeUV(vec2 packedUV)
  496. {
  497. vec2 uv = (packedUV * 2.0) - 1.0;
  498. return uv;
  499. }
  500. #endif
  501. #if USE_PACKED_UV == 1
  502. vec2 decodeUV(vec2 packedUV)
  503. {
  504. vec2 uv = ( vec3(packedUV, 1.0) * quantizeMatUV ).xy;
  505. return uv;
  506. }
  507. #endif
  508. #endif`,
  509. "void main() {",
  510. ShaderChunk.uv_vertex,
  511. `#ifdef USE_UV
  512. #ifdef USE_PACKED_UV
  513. vUv = decodeUV(vUv);
  514. #endif
  515. #endif`,
  516. ShaderChunk.uv2_vertex,
  517. ShaderChunk.color_vertex,
  518. ShaderChunk.beginnormal_vertex,
  519. `#ifdef USE_PACKED_NORMAL
  520. objectNormal = decodeNormal(objectNormal);
  521. #endif
  522. #ifdef USE_TANGENT
  523. vec3 objectTangent = vec3( tangent.xyz );
  524. #endif
  525. `,
  526. ShaderChunk.morphnormal_vertex,
  527. ShaderChunk.skinbase_vertex,
  528. ShaderChunk.skinnormal_vertex,
  529. ShaderChunk.defaultnormal_vertex,
  530. "#ifndef FLAT_SHADED",
  531. " vNormal = normalize( transformedNormal );",
  532. "#endif",
  533. ShaderChunk.begin_vertex,
  534. `#ifdef USE_PACKED_POSITION
  535. #if USE_PACKED_POSITION == 0
  536. transformed = ( vec4(transformed, 1.0) * quantizeMatPos ).xyz;
  537. #endif
  538. #endif`,
  539. ShaderChunk.morphtarget_vertex,
  540. ShaderChunk.skinning_vertex,
  541. ShaderChunk.displacementmap_vertex,
  542. ShaderChunk.project_vertex,
  543. ShaderChunk.logdepthbuf_vertex,
  544. ShaderChunk.clipping_planes_vertex,
  545. "vViewPosition = - mvPosition.xyz;",
  546. ShaderChunk.worldpos_vertex,
  547. ShaderChunk.envmap_vertex,
  548. ShaderChunk.shadowmap_vertex,
  549. ShaderChunk.fog_vertex,
  550. "}",
  551. ].join( "\n" );
  552. // Use the original MeshPhongMaterial's fragmentShader.
  553. this.fragmentShader = [
  554. "#define PHONG",
  555. "uniform vec3 diffuse;",
  556. "uniform vec3 emissive;",
  557. "uniform vec3 specular;",
  558. "uniform float shininess;",
  559. "uniform float opacity;",
  560. ShaderChunk.common,
  561. ShaderChunk.packing,
  562. ShaderChunk.dithering_pars_fragment,
  563. ShaderChunk.color_pars_fragment,
  564. ShaderChunk.uv_pars_fragment,
  565. ShaderChunk.uv2_pars_fragment,
  566. ShaderChunk.map_pars_fragment,
  567. ShaderChunk.alphamap_pars_fragment,
  568. ShaderChunk.aomap_pars_fragment,
  569. ShaderChunk.lightmap_pars_fragment,
  570. ShaderChunk.emissivemap_pars_fragment,
  571. ShaderChunk.envmap_common_pars_fragment,
  572. ShaderChunk.envmap_pars_fragment,
  573. ShaderChunk.cube_uv_reflection_fragment,
  574. ShaderChunk.fog_pars_fragment,
  575. ShaderChunk.bsdfs,
  576. ShaderChunk.lights_pars_begin,
  577. ShaderChunk.lights_phong_pars_fragment,
  578. ShaderChunk.shadowmap_pars_fragment,
  579. ShaderChunk.bumpmap_pars_fragment,
  580. ShaderChunk.normalmap_pars_fragment,
  581. ShaderChunk.specularmap_pars_fragment,
  582. ShaderChunk.logdepthbuf_pars_fragment,
  583. ShaderChunk.clipping_planes_pars_fragment,
  584. "void main() {",
  585. ShaderChunk.clipping_planes_fragment,
  586. "vec4 diffuseColor = vec4( diffuse, opacity );",
  587. "ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  588. "vec3 totalEmissiveRadiance = emissive;",
  589. ShaderChunk.logdepthbuf_fragment,
  590. ShaderChunk.map_fragment,
  591. ShaderChunk.color_fragment,
  592. ShaderChunk.alphamap_fragment,
  593. ShaderChunk.alphatest_fragment,
  594. ShaderChunk.specularmap_fragment,
  595. ShaderChunk.normal_fragment_begin,
  596. ShaderChunk.normal_fragment_maps,
  597. ShaderChunk.emissivemap_fragment,
  598. // accumulation
  599. ShaderChunk.lights_phong_fragment,
  600. ShaderChunk.lights_fragment_begin,
  601. ShaderChunk.lights_fragment_maps,
  602. ShaderChunk.lights_fragment_end,
  603. // modulation
  604. ShaderChunk.aomap_fragment,
  605. "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;",
  606. ShaderChunk.envmap_fragment,
  607. "gl_FragColor = vec4( outgoingLight, diffuseColor.a );",
  608. ShaderChunk.tonemapping_fragment,
  609. ShaderChunk.encodings_fragment,
  610. ShaderChunk.fog_fragment,
  611. ShaderChunk.premultiplied_alpha_fragment,
  612. ShaderChunk.dithering_fragment,
  613. "}",
  614. ].join( "\n" );
  615. this.setValues( parameters );
  616. }
  617. PackedPhongMaterial.prototype = Object.create( MeshPhongMaterial.prototype );
  618. export { GeometryCompressionUtils, PackedPhongMaterial };