GeometryCompressionUtils.js 22 KB

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