GeometryCompressionUtils.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /**
  2. * @author LeonYuanYao / https://github.com/LeonYuanYao
  3. *
  4. * Octahedron and Quantization encodings based on work by:
  5. * @auther Tarek Sherif @tsherif
  6. * @link https://github.com/tsherif/mesh-quantization-example
  7. *
  8. */
  9. import {
  10. BufferAttribute,
  11. Matrix3,
  12. Matrix4,
  13. MeshPhongMaterial,
  14. ShaderChunk,
  15. ShaderLib,
  16. UniformsUtils,
  17. Vector3
  18. } from "../../../build/three.module.js";
  19. var GeometryCompressionUtils = {
  20. /**
  21. * Make the input mesh.geometry's normal attribute encoded and compressed by 3 different methods.
  22. * Also will change the mesh.material to `PackedPhongMaterial` which let the vertex shader program decode the normal data.
  23. *
  24. * @param {THREE.Mesh} mesh
  25. * @param {String} encodeMethod "DEFAULT" || "OCT1Byte" || "OCT2Byte" || "ANGLES"
  26. *
  27. */
  28. compressNormals: function ( mesh, encodeMethod ) {
  29. if ( ! mesh.geometry ) {
  30. console.error( "Mesh must contain geometry. " );
  31. }
  32. let normal = mesh.geometry.attributes.normal;
  33. if ( ! normal ) {
  34. console.error( "Geometry must contain normal attribute. " );
  35. }
  36. if ( normal.isPacked ) return;
  37. if ( normal.itemSize != 3 ) {
  38. console.error( "normal.itemSize is not 3, which cannot be encoded. " );
  39. }
  40. let array = normal.array;
  41. let count = normal.count;
  42. let result;
  43. if ( encodeMethod == "DEFAULT" ) {
  44. result = new Uint8Array( count * 3 );
  45. for ( let idx = 0; idx < array.length; idx += 3 ) {
  46. let encoded;
  47. encoded = this.EncodingFuncs.defaultEncode( array[ idx ], array[ idx + 1 ], array[ idx + 2 ], 1 );
  48. result[ idx + 0 ] = encoded[ 0 ];
  49. result[ idx + 1 ] = encoded[ 1 ];
  50. result[ idx + 2 ] = encoded[ 2 ];
  51. }
  52. mesh.geometry.setAttribute( 'normal', new BufferAttribute( result, 3, true ) );
  53. mesh.geometry.attributes.normal.bytes = result.length * 1;
  54. } else if ( encodeMethod == "OCT1Byte" ) {
  55. result = new Int8Array( count * 2 );
  56. for ( let idx = 0; idx < array.length; idx += 3 ) {
  57. let encoded;
  58. encoded = this.EncodingFuncs.octEncodeBest( array[ idx ], array[ idx + 1 ], array[ idx + 2 ], 1 );
  59. result[ idx / 3 * 2 + 0 ] = encoded[ 0 ];
  60. result[ idx / 3 * 2 + 1 ] = encoded[ 1 ];
  61. }
  62. mesh.geometry.setAttribute( 'normal', new BufferAttribute( result, 2, true ) );
  63. mesh.geometry.attributes.normal.bytes = result.length * 1;
  64. } else if ( encodeMethod == "OCT2Byte" ) {
  65. result = new Int16Array( count * 2 );
  66. for ( let idx = 0; idx < array.length; idx += 3 ) {
  67. let encoded;
  68. encoded = this.EncodingFuncs.octEncodeBest( array[ idx ], array[ idx + 1 ], array[ idx + 2 ], 2 );
  69. result[ idx / 3 * 2 + 0 ] = encoded[ 0 ];
  70. result[ idx / 3 * 2 + 1 ] = encoded[ 1 ];
  71. }
  72. mesh.geometry.setAttribute( 'normal', new BufferAttribute( result, 2, true ) );
  73. mesh.geometry.attributes.normal.bytes = result.length * 2;
  74. } else if ( encodeMethod == "ANGLES" ) {
  75. result = new Uint16Array( count * 2 );
  76. for ( let idx = 0; idx < array.length; idx += 3 ) {
  77. let encoded;
  78. 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. let 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. let array = position.array;
  127. let encodingBytes = 2;
  128. let result = this.EncodingFuncs.quantizedEncode( array, encodingBytes );
  129. let quantized = result.quantized;
  130. let 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. let uvs = mesh.geometry.attributes.uv;
  158. if ( ! uvs ) {
  159. console.error( "Geometry must contain uv attribute. " );
  160. }
  161. if ( uvs.isPacked ) return;
  162. let range = { min: Infinity, max: - Infinity };
  163. let 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. let 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. let tmpx = Math.round( ( x + 1 ) * 0.5 * 255 );
  204. let tmpy = Math.round( ( y + 1 ) * 0.5 * 255 );
  205. let tmpz = Math.round( ( z + 1 ) * 0.5 * 255 );
  206. return new Uint8Array( [ tmpx, tmpy, tmpz ] );
  207. } else if ( bytes == 2 ) {
  208. let tmpx = Math.round( ( x + 1 ) * 0.5 * 65535 );
  209. let tmpy = Math.round( ( y + 1 ) * 0.5 * 65535 );
  210. let 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. let normal0 = parseInt( 0.5 * ( 1.0 + Math.atan2( y, x ) / Math.PI ) * 65535 );
  236. let normal1 = parseInt( 0.5 * ( 1.0 + z ) * 65535 );
  237. return new Uint16Array( [ normal0, normal1 ] );
  238. },
  239. // for `OCT` 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. currentCos = 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. bestCos = currentCos;
  267. }
  268. return best;
  269. function octEncodeVec3( x0, y0, z0, xfunc, yfunc ) {
  270. var x = x0 / ( Math.abs( x0 ) + Math.abs( y0 ) + Math.abs( z0 ) );
  271. var y = y0 / ( Math.abs( x0 ) + Math.abs( y0 ) + Math.abs( z0 ) );
  272. if ( z < 0 ) {
  273. var tempx = x;
  274. var tempy = y;
  275. tempx = ( 1 - Math.abs( y ) ) * ( x >= 0 ? 1 : - 1 );
  276. tempy = ( 1 - Math.abs( x ) ) * ( y >= 0 ? 1 : - 1 );
  277. x = tempx;
  278. y = tempy;
  279. var diff = 1 - Math.abs( x ) - Math.abs( y );
  280. if ( diff > 0 ) {
  281. diff += 0.001;
  282. x += x > 0 ? diff / 2 : - diff / 2;
  283. y += y > 0 ? diff / 2 : - diff / 2;
  284. }
  285. }
  286. if ( bytes == 1 ) {
  287. return new Int8Array( [
  288. Math[ xfunc ]( x * 127.5 + ( x < 0 ? 1 : 0 ) ),
  289. Math[ yfunc ]( y * 127.5 + ( y < 0 ? 1 : 0 ) )
  290. ] );
  291. }
  292. if ( bytes == 2 ) {
  293. return new Int16Array( [
  294. Math[ xfunc ]( x * 32767.5 + ( x < 0 ? 1 : 0 ) ),
  295. Math[ yfunc ]( y * 32767.5 + ( y < 0 ? 1 : 0 ) )
  296. ] );
  297. }
  298. }
  299. function octDecodeVec2( oct ) {
  300. var x = oct[ 0 ];
  301. var y = oct[ 1 ];
  302. if ( bytes == 1 ) {
  303. x /= x < 0 ? 127 : 128;
  304. y /= y < 0 ? 127 : 128;
  305. } else if ( bytes == 2 ) {
  306. x /= x < 0 ? 32767 : 32768;
  307. y /= y < 0 ? 32767 : 32768;
  308. }
  309. var z = 1 - Math.abs( x ) - Math.abs( y );
  310. if ( z < 0 ) {
  311. var tmpx = x;
  312. x = ( 1 - Math.abs( y ) ) * ( x >= 0 ? 1 : - 1 );
  313. y = ( 1 - Math.abs( tmpx ) ) * ( y >= 0 ? 1 : - 1 );
  314. }
  315. var length = Math.sqrt( x * x + y * y + z * z );
  316. return [
  317. x / length,
  318. y / length,
  319. z / length
  320. ];
  321. }
  322. function dot( x, y, z, vec3 ) {
  323. return x * vec3[ 0 ] + y * vec3[ 1 ] + z * vec3[ 2 ];
  324. }
  325. },
  326. quantizedEncode: function ( array, bytes ) {
  327. let quantized, segments;
  328. if ( bytes == 1 ) {
  329. quantized = new Uint8Array( array.length );
  330. segments = 255;
  331. } else if ( bytes == 2 ) {
  332. quantized = new Uint16Array( array.length );
  333. segments = 65535;
  334. } else {
  335. console.error( "number of bytes error! " );
  336. }
  337. let decodeMat = new Matrix4();
  338. let min = new Float32Array( 3 );
  339. let max = new Float32Array( 3 );
  340. min[ 0 ] = min[ 1 ] = min[ 2 ] = Number.MAX_VALUE;
  341. max[ 0 ] = max[ 1 ] = max[ 2 ] = - Number.MAX_VALUE;
  342. for ( let i = 0; i < array.length; i += 3 ) {
  343. min[ 0 ] = Math.min( min[ 0 ], array[ i + 0 ] );
  344. min[ 1 ] = Math.min( min[ 1 ], array[ i + 1 ] );
  345. min[ 2 ] = Math.min( min[ 2 ], array[ i + 2 ] );
  346. max[ 0 ] = Math.max( max[ 0 ], array[ i + 0 ] );
  347. max[ 1 ] = Math.max( max[ 1 ], array[ i + 1 ] );
  348. max[ 2 ] = Math.max( max[ 2 ], array[ i + 2 ] );
  349. }
  350. decodeMat.scale( new Vector3(
  351. ( max[ 0 ] - min[ 0 ] ) / segments,
  352. ( max[ 1 ] - min[ 1 ] ) / segments,
  353. ( max[ 2 ] - min[ 2 ] ) / segments
  354. ) );
  355. decodeMat.elements[ 12 ] = min[ 0 ];
  356. decodeMat.elements[ 13 ] = min[ 1 ];
  357. decodeMat.elements[ 14 ] = min[ 2 ];
  358. decodeMat.transpose();
  359. let multiplier = new Float32Array( [
  360. max[ 0 ] !== min[ 0 ] ? segments / ( max[ 0 ] - min[ 0 ] ) : 0,
  361. max[ 1 ] !== min[ 1 ] ? segments / ( max[ 1 ] - min[ 1 ] ) : 0,
  362. max[ 2 ] !== min[ 2 ] ? segments / ( max[ 2 ] - min[ 2 ] ) : 0
  363. ] );
  364. for ( let i = 0; i < array.length; i += 3 ) {
  365. quantized[ i + 0 ] = Math.floor( ( array[ i + 0 ] - min[ 0 ] ) * multiplier[ 0 ] );
  366. quantized[ i + 1 ] = Math.floor( ( array[ i + 1 ] - min[ 1 ] ) * multiplier[ 1 ] );
  367. quantized[ i + 2 ] = Math.floor( ( array[ i + 2 ] - min[ 2 ] ) * multiplier[ 2 ] );
  368. }
  369. return {
  370. quantized: quantized,
  371. decodeMat: decodeMat
  372. };
  373. },
  374. quantizedEncodeUV: function ( array, bytes ) {
  375. let quantized, segments;
  376. if ( bytes == 1 ) {
  377. quantized = new Uint8Array( array.length );
  378. segments = 255;
  379. } else if ( bytes == 2 ) {
  380. quantized = new Uint16Array( array.length );
  381. segments = 65535;
  382. } else {
  383. console.error( "number of bytes error! " );
  384. }
  385. let decodeMat = new Matrix3();
  386. let min = new Float32Array( 2 );
  387. let max = new Float32Array( 2 );
  388. min[ 0 ] = min[ 1 ] = Number.MAX_VALUE;
  389. max[ 0 ] = max[ 1 ] = - Number.MAX_VALUE;
  390. for ( let i = 0; i < array.length; i += 2 ) {
  391. min[ 0 ] = Math.min( min[ 0 ], array[ i + 0 ] );
  392. min[ 1 ] = Math.min( min[ 1 ], array[ i + 1 ] );
  393. max[ 0 ] = Math.max( max[ 0 ], array[ i + 0 ] );
  394. max[ 1 ] = Math.max( max[ 1 ], array[ i + 1 ] );
  395. }
  396. decodeMat.scale(
  397. ( max[ 0 ] - min[ 0 ] ) / segments,
  398. ( max[ 1 ] - min[ 1 ] ) / segments
  399. );
  400. decodeMat.elements[ 6 ] = min[ 0 ];
  401. decodeMat.elements[ 7 ] = min[ 1 ];
  402. decodeMat.transpose();
  403. let multiplier = new Float32Array( [
  404. max[ 0 ] !== min[ 0 ] ? segments / ( max[ 0 ] - min[ 0 ] ) : 0,
  405. max[ 1 ] !== min[ 1 ] ? segments / ( max[ 1 ] - min[ 1 ] ) : 0
  406. ] );
  407. for ( let i = 0; i < array.length; i += 2 ) {
  408. quantized[ i + 0 ] = Math.floor( ( array[ i + 0 ] - min[ 0 ] ) * multiplier[ 0 ] );
  409. quantized[ i + 1 ] = Math.floor( ( array[ i + 1 ] - min[ 1 ] ) * multiplier[ 1 ] );
  410. }
  411. return {
  412. quantized: quantized,
  413. decodeMat: decodeMat
  414. };
  415. }
  416. }
  417. };
  418. /**
  419. * `PackedPhongMaterial` inherited from THREE.MeshPhongMaterial
  420. *
  421. * @param {Object} parameters
  422. */
  423. function PackedPhongMaterial( parameters ) {
  424. MeshPhongMaterial.call( this );
  425. this.defines = {};
  426. this.type = 'PackedPhongMaterial';
  427. this.uniforms = UniformsUtils.merge( [
  428. ShaderLib.phong.uniforms,
  429. {
  430. quantizeMatPos: { value: null },
  431. quantizeMatUV: { value: null }
  432. }
  433. ] );
  434. this.vertexShader = [
  435. "#define PHONG",
  436. "varying vec3 vViewPosition;",
  437. "#ifndef FLAT_SHADED",
  438. "varying vec3 vNormal;",
  439. "#endif",
  440. ShaderChunk.common,
  441. ShaderChunk.uv_pars_vertex,
  442. ShaderChunk.uv2_pars_vertex,
  443. ShaderChunk.displacementmap_pars_vertex,
  444. ShaderChunk.envmap_pars_vertex,
  445. ShaderChunk.color_pars_vertex,
  446. ShaderChunk.fog_pars_vertex,
  447. ShaderChunk.morphtarget_pars_vertex,
  448. ShaderChunk.skinning_pars_vertex,
  449. ShaderChunk.shadowmap_pars_vertex,
  450. ShaderChunk.logdepthbuf_pars_vertex,
  451. ShaderChunk.clipping_planes_pars_vertex,
  452. `#ifdef USE_PACKED_NORMAL
  453. #if USE_PACKED_NORMAL == 0
  454. vec3 decodeNormal(vec3 packedNormal)
  455. {
  456. float x = packedNormal.x * 2.0 - 1.0;
  457. float y = packedNormal.y * 2.0 - 1.0;
  458. vec2 scth = vec2(sin(x * PI), cos(x * PI));
  459. vec2 scphi = vec2(sqrt(1.0 - y * y), y);
  460. return normalize( vec3(scth.y * scphi.x, scth.x * scphi.x, scphi.y) );
  461. }
  462. #endif
  463. #if USE_PACKED_NORMAL == 1
  464. vec3 decodeNormal(vec3 packedNormal)
  465. {
  466. vec3 v = vec3(packedNormal.xy, 1.0 - abs(packedNormal.x) - abs(packedNormal.y));
  467. if (v.z < 0.0)
  468. {
  469. v.xy = (1.0 - abs(v.yx)) * vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0);
  470. }
  471. return normalize(v);
  472. }
  473. #endif
  474. #if USE_PACKED_NORMAL == 2
  475. vec3 decodeNormal(vec3 packedNormal)
  476. {
  477. vec3 v = (packedNormal * 2.0) - 1.0;
  478. return normalize(v);
  479. }
  480. #endif
  481. #endif`,
  482. `#ifdef USE_PACKED_POSITION
  483. #if USE_PACKED_POSITION == 0
  484. uniform mat4 quantizeMatPos;
  485. #endif
  486. #endif`,
  487. `#ifdef USE_PACKED_UV
  488. #if USE_PACKED_UV == 1
  489. uniform mat3 quantizeMatUV;
  490. #endif
  491. #endif`,
  492. `#ifdef USE_PACKED_UV
  493. #if USE_PACKED_UV == 0
  494. vec2 decodeUV(vec2 packedUV)
  495. {
  496. vec2 uv = (packedUV * 2.0) - 1.0;
  497. return uv;
  498. }
  499. #endif
  500. #if USE_PACKED_UV == 1
  501. vec2 decodeUV(vec2 packedUV)
  502. {
  503. vec2 uv = ( vec3(packedUV, 1.0) * quantizeMatUV ).xy;
  504. return uv;
  505. }
  506. #endif
  507. #endif`,
  508. "void main() {",
  509. ShaderChunk.uv_vertex,
  510. `#ifdef USE_UV
  511. #ifdef USE_PACKED_UV
  512. vUv = decodeUV(vUv);
  513. #endif
  514. #endif`,
  515. ShaderChunk.uv2_vertex,
  516. ShaderChunk.color_vertex,
  517. ShaderChunk.beginnormal_vertex,
  518. `#ifdef USE_PACKED_NORMAL
  519. objectNormal = decodeNormal(objectNormal);
  520. #endif
  521. #ifdef USE_TANGENT
  522. vec3 objectTangent = vec3( tangent.xyz );
  523. #endif
  524. `,
  525. ShaderChunk.morphnormal_vertex,
  526. ShaderChunk.skinbase_vertex,
  527. ShaderChunk.skinnormal_vertex,
  528. ShaderChunk.defaultnormal_vertex,
  529. "#ifndef FLAT_SHADED",
  530. " vNormal = normalize( transformedNormal );",
  531. "#endif",
  532. ShaderChunk.begin_vertex,
  533. `#ifdef USE_PACKED_POSITION
  534. #if USE_PACKED_POSITION == 0
  535. transformed = ( vec4(transformed, 1.0) * quantizeMatPos ).xyz;
  536. #endif
  537. #endif`,
  538. ShaderChunk.morphtarget_vertex,
  539. ShaderChunk.skinning_vertex,
  540. ShaderChunk.displacementmap_vertex,
  541. ShaderChunk.project_vertex,
  542. ShaderChunk.logdepthbuf_vertex,
  543. ShaderChunk.clipping_planes_vertex,
  544. "vViewPosition = - mvPosition.xyz;",
  545. ShaderChunk.worldpos_vertex,
  546. ShaderChunk.envmap_vertex,
  547. ShaderChunk.shadowmap_vertex,
  548. ShaderChunk.fog_vertex,
  549. "}",
  550. ].join( "\n" );
  551. this.fragmentShader = [
  552. "#define PHONG",
  553. "uniform vec3 diffuse;",
  554. "uniform vec3 emissive;",
  555. "uniform vec3 specular;",
  556. "uniform float shininess;",
  557. "uniform float opacity;",
  558. ShaderChunk.common,
  559. ShaderChunk.packing,
  560. ShaderChunk.dithering_pars_fragment,
  561. ShaderChunk.color_pars_fragment,
  562. ShaderChunk.uv_pars_fragment,
  563. ShaderChunk.uv2_pars_fragment,
  564. ShaderChunk.map_pars_fragment,
  565. ShaderChunk.alphamap_pars_fragment,
  566. ShaderChunk.aomap_pars_fragment,
  567. ShaderChunk.lightmap_pars_fragment,
  568. ShaderChunk.emissivemap_pars_fragment,
  569. ShaderChunk.envmap_common_pars_fragment,
  570. ShaderChunk.envmap_pars_fragment,
  571. ShaderChunk.cube_uv_reflection_fragment,
  572. ShaderChunk.fog_pars_fragment,
  573. ShaderChunk.bsdfs,
  574. ShaderChunk.lights_pars_begin,
  575. ShaderChunk.lights_phong_pars_fragment,
  576. ShaderChunk.shadowmap_pars_fragment,
  577. ShaderChunk.bumpmap_pars_fragment,
  578. ShaderChunk.normalmap_pars_fragment,
  579. ShaderChunk.specularmap_pars_fragment,
  580. ShaderChunk.logdepthbuf_pars_fragment,
  581. ShaderChunk.clipping_planes_pars_fragment,
  582. "void main() {",
  583. ShaderChunk.clipping_planes_fragment,
  584. "vec4 diffuseColor = vec4( diffuse, opacity );",
  585. "ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  586. "vec3 totalEmissiveRadiance = emissive;",
  587. ShaderChunk.logdepthbuf_fragment,
  588. ShaderChunk.map_fragment,
  589. ShaderChunk.color_fragment,
  590. ShaderChunk.alphamap_fragment,
  591. ShaderChunk.alphatest_fragment,
  592. ShaderChunk.specularmap_fragment,
  593. ShaderChunk.normal_fragment_begin,
  594. ShaderChunk.normal_fragment_maps,
  595. ShaderChunk.emissivemap_fragment,
  596. // accumulation
  597. ShaderChunk.lights_phong_fragment,
  598. ShaderChunk.lights_fragment_begin,
  599. ShaderChunk.lights_fragment_maps,
  600. ShaderChunk.lights_fragment_end,
  601. // modulation
  602. ShaderChunk.aomap_fragment,
  603. "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;",
  604. ShaderChunk.envmap_fragment,
  605. "gl_FragColor = vec4( outgoingLight, diffuseColor.a );",
  606. ShaderChunk.tonemapping_fragment,
  607. ShaderChunk.encodings_fragment,
  608. ShaderChunk.fog_fragment,
  609. ShaderChunk.premultiplied_alpha_fragment,
  610. ShaderChunk.dithering_fragment,
  611. "}",
  612. ].join( "\n" );
  613. this.setValues( parameters );
  614. }
  615. PackedPhongMaterial.prototype = Object.create( MeshPhongMaterial.prototype );
  616. export { GeometryCompressionUtils, PackedPhongMaterial };