GeometryCompressionUtils.js 22 KB

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