GeometryCompressionUtils.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. ( function () {
  2. /**
  3. * Octahedron and Quantization encodings based on work by:
  4. *
  5. * @link https://github.com/tsherif/mesh-quantization-example
  6. *
  7. */
  8. var GeometryCompressionUtils = {
  9. /**
  10. * Make the input mesh.geometry's normal attribute encoded and compressed by 3 different methods.
  11. * Also will change the mesh.material to `PackedPhongMaterial` which let the vertex shader program decode the normal data.
  12. *
  13. * @param {THREE.Mesh} mesh
  14. * @param {String} encodeMethod "DEFAULT" || "OCT1Byte" || "OCT2Byte" || "ANGLES"
  15. *
  16. */
  17. compressNormals: function ( mesh, encodeMethod ) {
  18. if ( ! mesh.geometry ) {
  19. console.error( 'Mesh must contain geometry. ' );
  20. }
  21. const normal = mesh.geometry.attributes.normal;
  22. if ( ! normal ) {
  23. console.error( 'Geometry must contain normal attribute. ' );
  24. }
  25. if ( normal.isPacked ) return;
  26. if ( normal.itemSize != 3 ) {
  27. console.error( 'normal.itemSize is not 3, which cannot be encoded. ' );
  28. }
  29. const array = normal.array;
  30. const count = normal.count;
  31. let result;
  32. if ( encodeMethod == 'DEFAULT' ) {
  33. // TODO: Add 1 byte to the result, making the encoded length to be 4 bytes.
  34. result = new Uint8Array( count * 3 );
  35. for ( let idx = 0; idx < array.length; idx += 3 ) {
  36. const encoded = this.EncodingFuncs.defaultEncode( array[ idx ], array[ idx + 1 ], array[ idx + 2 ], 1 );
  37. result[ idx + 0 ] = encoded[ 0 ];
  38. result[ idx + 1 ] = encoded[ 1 ];
  39. result[ idx + 2 ] = encoded[ 2 ];
  40. }
  41. mesh.geometry.setAttribute( 'normal', new THREE.BufferAttribute( result, 3, true ) );
  42. mesh.geometry.attributes.normal.bytes = result.length * 1;
  43. } else if ( encodeMethod == 'OCT1Byte' ) {
  44. /**
  45. * It is not recommended to use 1-byte octahedron normals encoding unless you want to extremely reduce the memory usage
  46. * 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
  47. * Please refer to @zeux 's comments in https://github.com/mrdoob/three.js/pull/18208
  48. */
  49. result = new Int8Array( count * 2 );
  50. for ( let idx = 0; idx < array.length; idx += 3 ) {
  51. const encoded = this.EncodingFuncs.octEncodeBest( array[ idx ], array[ idx + 1 ], array[ idx + 2 ], 1 );
  52. result[ idx / 3 * 2 + 0 ] = encoded[ 0 ];
  53. result[ idx / 3 * 2 + 1 ] = encoded[ 1 ];
  54. }
  55. mesh.geometry.setAttribute( 'normal', new THREE.BufferAttribute( result, 2, true ) );
  56. mesh.geometry.attributes.normal.bytes = result.length * 1;
  57. } else if ( encodeMethod == 'OCT2Byte' ) {
  58. result = new Int16Array( 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 ], 2 );
  61. result[ idx / 3 * 2 + 0 ] = encoded[ 0 ];
  62. result[ idx / 3 * 2 + 1 ] = encoded[ 1 ];
  63. }
  64. mesh.geometry.setAttribute( 'normal', new THREE.BufferAttribute( result, 2, true ) );
  65. mesh.geometry.attributes.normal.bytes = result.length * 2;
  66. } else if ( encodeMethod == 'ANGLES' ) {
  67. result = new Uint16Array( count * 2 );
  68. for ( let idx = 0; idx < array.length; idx += 3 ) {
  69. const 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; // modify material
  81. if ( ! ( mesh.material instanceof PackedPhongMaterial ) ) {
  82. mesh.material = new PackedPhongMaterial().copy( mesh.material );
  83. }
  84. if ( encodeMethod == 'ANGLES' ) {
  85. mesh.material.defines.USE_PACKED_NORMAL = 0;
  86. }
  87. if ( encodeMethod == 'OCT1Byte' ) {
  88. mesh.material.defines.USE_PACKED_NORMAL = 1;
  89. }
  90. if ( encodeMethod == 'OCT2Byte' ) {
  91. mesh.material.defines.USE_PACKED_NORMAL = 1;
  92. }
  93. if ( encodeMethod == 'DEFAULT' ) {
  94. mesh.material.defines.USE_PACKED_NORMAL = 2;
  95. }
  96. },
  97. /**
  98. * Make the input mesh.geometry's position attribute encoded and compressed.
  99. * Also will change the mesh.material to `PackedPhongMaterial` which let the vertex shader program decode the position data.
  100. *
  101. * @param {THREE.Mesh} mesh
  102. *
  103. */
  104. compressPositions: function ( mesh ) {
  105. if ( ! mesh.geometry ) {
  106. console.error( 'Mesh must contain geometry. ' );
  107. }
  108. const position = mesh.geometry.attributes.position;
  109. if ( ! position ) {
  110. console.error( 'Geometry must contain position attribute. ' );
  111. }
  112. if ( position.isPacked ) return;
  113. if ( position.itemSize != 3 ) {
  114. console.error( 'position.itemSize is not 3, which cannot be packed. ' );
  115. }
  116. const array = position.array;
  117. const encodingBytes = 2;
  118. const result = this.EncodingFuncs.quantizedEncode( array, encodingBytes );
  119. const quantized = result.quantized;
  120. const decodeMat = result.decodeMat; // IMPORTANT: calculate original geometry bounding info first, before updating packed positions
  121. if ( mesh.geometry.boundingBox == null ) mesh.geometry.computeBoundingBox();
  122. if ( mesh.geometry.boundingSphere == null ) mesh.geometry.computeBoundingSphere();
  123. mesh.geometry.setAttribute( 'position', new THREE.BufferAttribute( quantized, 3 ) );
  124. mesh.geometry.attributes.position.isPacked = true;
  125. mesh.geometry.attributes.position.needsUpdate = true;
  126. mesh.geometry.attributes.position.bytes = quantized.length * encodingBytes; // modify material
  127. if ( ! ( mesh.material instanceof PackedPhongMaterial ) ) {
  128. mesh.material = new PackedPhongMaterial().copy( mesh.material );
  129. }
  130. mesh.material.defines.USE_PACKED_POSITION = 0;
  131. mesh.material.uniforms.quantizeMatPos.value = decodeMat;
  132. mesh.material.uniforms.quantizeMatPos.needsUpdate = true;
  133. },
  134. /**
  135. * Make the input mesh.geometry's uv attribute encoded and compressed.
  136. * Also will change the mesh.material to `PackedPhongMaterial` which let the vertex shader program decode the uv data.
  137. *
  138. * @param {THREE.Mesh} mesh
  139. *
  140. */
  141. compressUvs: function ( mesh ) {
  142. if ( ! mesh.geometry ) {
  143. console.error( 'Mesh must contain geometry property. ' );
  144. }
  145. const uvs = mesh.geometry.attributes.uv;
  146. if ( ! uvs ) {
  147. console.error( 'Geometry must contain uv attribute. ' );
  148. }
  149. if ( uvs.isPacked ) return;
  150. const range = {
  151. min: Infinity,
  152. max: - Infinity
  153. };
  154. const 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. const 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. const tmpx = Math.round( ( x + 1 ) * 0.5 * 255 );
  195. const tmpy = Math.round( ( y + 1 ) * 0.5 * 255 );
  196. const tmpz = Math.round( ( z + 1 ) * 0.5 * 255 );
  197. return new Uint8Array( [ tmpx, tmpy, tmpz ] );
  198. } else if ( bytes == 2 ) {
  199. const tmpx = Math.round( ( x + 1 ) * 0.5 * 65535 );
  200. const tmpy = Math.round( ( y + 1 ) * 0.5 * 65535 );
  201. const 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 [ array[ 0 ] / 255 * 2.0 - 1.0, array[ 1 ] / 255 * 2.0 - 1.0, array[ 2 ] / 255 * 2.0 - 1.0 ];
  210. } else if ( bytes == 2 ) {
  211. return [ array[ 0 ] / 65535 * 2.0 - 1.0, array[ 1 ] / 65535 * 2.0 - 1.0, array[ 2 ] / 65535 * 2.0 - 1.0 ];
  212. } else {
  213. console.error( 'number of bytes must be 1 or 2' );
  214. }
  215. },
  216. // for `Angles` encoding
  217. anglesEncode: function ( x, y, z ) {
  218. const normal0 = parseInt( 0.5 * ( 1.0 + Math.atan2( y, x ) / Math.PI ) * 65535 );
  219. const normal1 = parseInt( 0.5 * ( 1.0 + z ) * 65535 );
  220. return new Uint16Array( [ normal0, normal1 ] );
  221. },
  222. // for `Octahedron` encoding
  223. octEncodeBest: function ( x, y, z, bytes ) {
  224. var oct, dec, best, currentCos, bestCos; // Test various combinations of ceil and floor
  225. // to minimize rounding errors
  226. best = oct = octEncodeVec3( x, y, z, 'floor', 'floor' );
  227. dec = octDecodeVec2( oct );
  228. bestCos = dot( x, y, z, dec );
  229. oct = octEncodeVec3( x, y, z, 'ceil', 'floor' );
  230. dec = octDecodeVec2( oct );
  231. currentCos = dot( x, y, z, dec );
  232. if ( currentCos > bestCos ) {
  233. best = oct;
  234. bestCos = currentCos;
  235. }
  236. oct = octEncodeVec3( x, y, z, 'floor', 'ceil' );
  237. dec = octDecodeVec2( oct );
  238. currentCos = dot( x, y, z, dec );
  239. if ( currentCos > bestCos ) {
  240. best = oct;
  241. bestCos = currentCos;
  242. }
  243. oct = octEncodeVec3( x, y, z, 'ceil', 'ceil' );
  244. dec = octDecodeVec2( oct );
  245. currentCos = dot( x, y, z, dec );
  246. if ( currentCos > bestCos ) {
  247. best = oct;
  248. }
  249. return best;
  250. function octEncodeVec3( x0, y0, z0, xfunc, yfunc ) {
  251. var x = x0 / ( Math.abs( x0 ) + Math.abs( y0 ) + Math.abs( z0 ) );
  252. var y = y0 / ( Math.abs( x0 ) + Math.abs( y0 ) + Math.abs( z0 ) );
  253. if ( z < 0 ) {
  254. var tempx = ( 1 - Math.abs( y ) ) * ( x >= 0 ? 1 : - 1 );
  255. var tempy = ( 1 - Math.abs( x ) ) * ( y >= 0 ? 1 : - 1 );
  256. x = tempx;
  257. y = tempy;
  258. var diff = 1 - Math.abs( x ) - Math.abs( y );
  259. if ( diff > 0 ) {
  260. diff += 0.001;
  261. x += x > 0 ? diff / 2 : - diff / 2;
  262. y += y > 0 ? diff / 2 : - diff / 2;
  263. }
  264. }
  265. if ( bytes == 1 ) {
  266. return new Int8Array( [ Math[ xfunc ]( x * 127.5 + ( x < 0 ? 1 : 0 ) ), Math[ yfunc ]( y * 127.5 + ( y < 0 ? 1 : 0 ) ) ] );
  267. }
  268. if ( bytes == 2 ) {
  269. return new Int16Array( [ Math[ xfunc ]( x * 32767.5 + ( x < 0 ? 1 : 0 ) ), Math[ yfunc ]( y * 32767.5 + ( y < 0 ? 1 : 0 ) ) ] );
  270. }
  271. }
  272. function octDecodeVec2( oct ) {
  273. var x = oct[ 0 ];
  274. var y = oct[ 1 ];
  275. if ( bytes == 1 ) {
  276. x /= x < 0 ? 127 : 128;
  277. y /= y < 0 ? 127 : 128;
  278. } else if ( bytes == 2 ) {
  279. x /= x < 0 ? 32767 : 32768;
  280. y /= y < 0 ? 32767 : 32768;
  281. }
  282. var z = 1 - Math.abs( x ) - Math.abs( y );
  283. if ( z < 0 ) {
  284. var tmpx = x;
  285. x = ( 1 - Math.abs( y ) ) * ( x >= 0 ? 1 : - 1 );
  286. y = ( 1 - Math.abs( tmpx ) ) * ( y >= 0 ? 1 : - 1 );
  287. }
  288. var length = Math.sqrt( x * x + y * y + z * z );
  289. return [ x / length, y / length, z / length ];
  290. }
  291. function dot( x, y, z, vec3 ) {
  292. return x * vec3[ 0 ] + y * vec3[ 1 ] + z * vec3[ 2 ];
  293. }
  294. },
  295. quantizedEncode: function ( array, bytes ) {
  296. let quantized, segments;
  297. if ( bytes == 1 ) {
  298. quantized = new Uint8Array( array.length );
  299. segments = 255;
  300. } else if ( bytes == 2 ) {
  301. quantized = new Uint16Array( array.length );
  302. segments = 65535;
  303. } else {
  304. console.error( 'number of bytes error! ' );
  305. }
  306. const decodeMat = new THREE.Matrix4();
  307. const min = new Float32Array( 3 );
  308. const max = new Float32Array( 3 );
  309. min[ 0 ] = min[ 1 ] = min[ 2 ] = Number.MAX_VALUE;
  310. max[ 0 ] = max[ 1 ] = max[ 2 ] = - Number.MAX_VALUE;
  311. for ( let i = 0; i < array.length; i += 3 ) {
  312. min[ 0 ] = Math.min( min[ 0 ], array[ i + 0 ] );
  313. min[ 1 ] = Math.min( min[ 1 ], array[ i + 1 ] );
  314. min[ 2 ] = Math.min( min[ 2 ], array[ i + 2 ] );
  315. max[ 0 ] = Math.max( max[ 0 ], array[ i + 0 ] );
  316. max[ 1 ] = Math.max( max[ 1 ], array[ i + 1 ] );
  317. max[ 2 ] = Math.max( max[ 2 ], array[ i + 2 ] );
  318. }
  319. decodeMat.scale( new THREE.Vector3( ( max[ 0 ] - min[ 0 ] ) / segments, ( max[ 1 ] - min[ 1 ] ) / segments, ( max[ 2 ] - min[ 2 ] ) / segments ) );
  320. decodeMat.elements[ 12 ] = min[ 0 ];
  321. decodeMat.elements[ 13 ] = min[ 1 ];
  322. decodeMat.elements[ 14 ] = min[ 2 ];
  323. decodeMat.transpose();
  324. const multiplier = new Float32Array( [ max[ 0 ] !== min[ 0 ] ? segments / ( max[ 0 ] - min[ 0 ] ) : 0, max[ 1 ] !== min[ 1 ] ? segments / ( max[ 1 ] - min[ 1 ] ) : 0, max[ 2 ] !== min[ 2 ] ? segments / ( max[ 2 ] - min[ 2 ] ) : 0 ] );
  325. for ( let i = 0; i < array.length; i += 3 ) {
  326. quantized[ i + 0 ] = Math.floor( ( array[ i + 0 ] - min[ 0 ] ) * multiplier[ 0 ] );
  327. quantized[ i + 1 ] = Math.floor( ( array[ i + 1 ] - min[ 1 ] ) * multiplier[ 1 ] );
  328. quantized[ i + 2 ] = Math.floor( ( array[ i + 2 ] - min[ 2 ] ) * multiplier[ 2 ] );
  329. }
  330. return {
  331. quantized: quantized,
  332. decodeMat: decodeMat
  333. };
  334. },
  335. quantizedEncodeUV: function ( array, bytes ) {
  336. let quantized, segments;
  337. if ( bytes == 1 ) {
  338. quantized = new Uint8Array( array.length );
  339. segments = 255;
  340. } else if ( bytes == 2 ) {
  341. quantized = new Uint16Array( array.length );
  342. segments = 65535;
  343. } else {
  344. console.error( 'number of bytes error! ' );
  345. }
  346. const decodeMat = new THREE.Matrix3();
  347. const min = new Float32Array( 2 );
  348. const max = new Float32Array( 2 );
  349. min[ 0 ] = min[ 1 ] = Number.MAX_VALUE;
  350. max[ 0 ] = max[ 1 ] = - Number.MAX_VALUE;
  351. for ( let i = 0; i < array.length; i += 2 ) {
  352. min[ 0 ] = Math.min( min[ 0 ], array[ i + 0 ] );
  353. min[ 1 ] = Math.min( min[ 1 ], array[ i + 1 ] );
  354. max[ 0 ] = Math.max( max[ 0 ], array[ i + 0 ] );
  355. max[ 1 ] = Math.max( max[ 1 ], array[ i + 1 ] );
  356. }
  357. decodeMat.scale( ( max[ 0 ] - min[ 0 ] ) / segments, ( max[ 1 ] - min[ 1 ] ) / segments );
  358. decodeMat.elements[ 6 ] = min[ 0 ];
  359. decodeMat.elements[ 7 ] = min[ 1 ];
  360. decodeMat.transpose();
  361. const multiplier = new Float32Array( [ max[ 0 ] !== min[ 0 ] ? segments / ( max[ 0 ] - min[ 0 ] ) : 0, max[ 1 ] !== min[ 1 ] ? segments / ( max[ 1 ] - min[ 1 ] ) : 0 ] );
  362. for ( let i = 0; i < array.length; i += 2 ) {
  363. quantized[ i + 0 ] = Math.floor( ( array[ i + 0 ] - min[ 0 ] ) * multiplier[ 0 ] );
  364. quantized[ i + 1 ] = Math.floor( ( array[ i + 1 ] - min[ 1 ] ) * multiplier[ 1 ] );
  365. }
  366. return {
  367. quantized: quantized,
  368. decodeMat: decodeMat
  369. };
  370. }
  371. }
  372. };
  373. /**
  374. * `PackedPhongMaterial` inherited from THREE.MeshPhongMaterial
  375. *
  376. * @param {Object} parameters
  377. */
  378. class PackedPhongMaterial extends THREE.MeshPhongMaterial {
  379. constructor( parameters ) {
  380. super();
  381. this.defines = {};
  382. this.type = 'PackedPhongMaterial';
  383. this.uniforms = THREE.UniformsUtils.merge( [ THREE.ShaderLib.phong.uniforms, {
  384. quantizeMatPos: {
  385. value: null
  386. },
  387. quantizeMatUV: {
  388. value: null
  389. }
  390. } ] );
  391. this.vertexShader = [ '#define PHONG', 'varying vec3 vViewPosition;', '#ifndef FLAT_SHADED', 'varying vec3 vNormal;', '#endif', THREE.ShaderChunk.common, THREE.ShaderChunk.uv_pars_vertex, THREE.ShaderChunk.uv2_pars_vertex, THREE.ShaderChunk.displacementmap_pars_vertex, THREE.ShaderChunk.envmap_pars_vertex, THREE.ShaderChunk.color_pars_vertex, THREE.ShaderChunk.fog_pars_vertex, THREE.ShaderChunk.morphtarget_pars_vertex, THREE.ShaderChunk.skinning_pars_vertex, THREE.ShaderChunk.shadowmap_pars_vertex, THREE.ShaderChunk.logdepthbuf_pars_vertex, THREE.ShaderChunk.clipping_planes_pars_vertex, `#ifdef USE_PACKED_NORMAL
  392. #if USE_PACKED_NORMAL == 0
  393. vec3 decodeNormal(vec3 packedNormal)
  394. {
  395. float x = packedNormal.x * 2.0 - 1.0;
  396. float y = packedNormal.y * 2.0 - 1.0;
  397. vec2 scth = vec2(sin(x * PI), cos(x * PI));
  398. vec2 scphi = vec2(sqrt(1.0 - y * y), y);
  399. return normalize( vec3(scth.y * scphi.x, scth.x * scphi.x, scphi.y) );
  400. }
  401. #endif
  402. #if USE_PACKED_NORMAL == 1
  403. vec3 decodeNormal(vec3 packedNormal)
  404. {
  405. vec3 v = vec3(packedNormal.xy, 1.0 - abs(packedNormal.x) - abs(packedNormal.y));
  406. if (v.z < 0.0)
  407. {
  408. v.xy = (1.0 - abs(v.yx)) * vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0);
  409. }
  410. return normalize(v);
  411. }
  412. #endif
  413. #if USE_PACKED_NORMAL == 2
  414. vec3 decodeNormal(vec3 packedNormal)
  415. {
  416. vec3 v = (packedNormal * 2.0) - 1.0;
  417. return normalize(v);
  418. }
  419. #endif
  420. #endif`, `#ifdef USE_PACKED_POSITION
  421. #if USE_PACKED_POSITION == 0
  422. uniform mat4 quantizeMatPos;
  423. #endif
  424. #endif`, `#ifdef USE_PACKED_UV
  425. #if USE_PACKED_UV == 1
  426. uniform mat3 quantizeMatUV;
  427. #endif
  428. #endif`, `#ifdef USE_PACKED_UV
  429. #if USE_PACKED_UV == 0
  430. vec2 decodeUV(vec2 packedUV)
  431. {
  432. vec2 uv = (packedUV * 2.0) - 1.0;
  433. return uv;
  434. }
  435. #endif
  436. #if USE_PACKED_UV == 1
  437. vec2 decodeUV(vec2 packedUV)
  438. {
  439. vec2 uv = ( vec3(packedUV, 1.0) * quantizeMatUV ).xy;
  440. return uv;
  441. }
  442. #endif
  443. #endif`, 'void main() {', THREE.ShaderChunk.uv_vertex, `#ifdef USE_UV
  444. #ifdef USE_PACKED_UV
  445. vUv = decodeUV(vUv);
  446. #endif
  447. #endif`, THREE.ShaderChunk.uv2_vertex, THREE.ShaderChunk.color_vertex, THREE.ShaderChunk.beginnormal_vertex, `#ifdef USE_PACKED_NORMAL
  448. objectNormal = decodeNormal(objectNormal);
  449. #endif
  450. #ifdef USE_TANGENT
  451. vec3 objectTangent = vec3( tangent.xyz );
  452. #endif
  453. `, THREE.ShaderChunk.morphnormal_vertex, THREE.ShaderChunk.skinbase_vertex, THREE.ShaderChunk.skinnormal_vertex, THREE.ShaderChunk.defaultnormal_vertex, '#ifndef FLAT_SHADED', ' vNormal = normalize( transformedNormal );', '#endif', THREE.ShaderChunk.begin_vertex, `#ifdef USE_PACKED_POSITION
  454. #if USE_PACKED_POSITION == 0
  455. transformed = ( vec4(transformed, 1.0) * quantizeMatPos ).xyz;
  456. #endif
  457. #endif`, THREE.ShaderChunk.morphtarget_vertex, THREE.ShaderChunk.skinning_vertex, THREE.ShaderChunk.displacementmap_vertex, THREE.ShaderChunk.project_vertex, THREE.ShaderChunk.logdepthbuf_vertex, THREE.ShaderChunk.clipping_planes_vertex, 'vViewPosition = - mvPosition.xyz;', THREE.ShaderChunk.worldpos_vertex, THREE.ShaderChunk.envmap_vertex, THREE.ShaderChunk.shadowmap_vertex, THREE.ShaderChunk.fog_vertex, '}' ].join( '\n' ); // Use the original THREE.MeshPhongMaterial's fragmentShader.
  458. this.fragmentShader = [ '#define PHONG', 'uniform vec3 diffuse;', 'uniform vec3 emissive;', 'uniform vec3 specular;', 'uniform float shininess;', 'uniform float opacity;', THREE.ShaderChunk.common, THREE.ShaderChunk.packing, THREE.ShaderChunk.dithering_pars_fragment, THREE.ShaderChunk.color_pars_fragment, THREE.ShaderChunk.uv_pars_fragment, THREE.ShaderChunk.uv2_pars_fragment, THREE.ShaderChunk.map_pars_fragment, THREE.ShaderChunk.alphamap_pars_fragment, THREE.ShaderChunk.aomap_pars_fragment, THREE.ShaderChunk.lightmap_pars_fragment, THREE.ShaderChunk.emissivemap_pars_fragment, THREE.ShaderChunk.envmap_common_pars_fragment, THREE.ShaderChunk.envmap_pars_fragment, THREE.ShaderChunk.cube_uv_reflection_fragment, THREE.ShaderChunk.fog_pars_fragment, THREE.ShaderChunk.bsdfs, THREE.ShaderChunk.lights_pars_begin, THREE.ShaderChunk.lights_phong_pars_fragment, THREE.ShaderChunk.shadowmap_pars_fragment, THREE.ShaderChunk.bumpmap_pars_fragment, THREE.ShaderChunk.normalmap_pars_fragment, THREE.ShaderChunk.specularmap_pars_fragment, THREE.ShaderChunk.logdepthbuf_pars_fragment, THREE.ShaderChunk.clipping_planes_pars_fragment, 'void main() {', THREE.ShaderChunk.clipping_planes_fragment, 'vec4 diffuseColor = vec4( diffuse, opacity );', 'ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );', 'vec3 totalEmissiveRadiance = emissive;', THREE.ShaderChunk.logdepthbuf_fragment, THREE.ShaderChunk.map_fragment, THREE.ShaderChunk.color_fragment, THREE.ShaderChunk.alphamap_fragment, THREE.ShaderChunk.alphatest_fragment, THREE.ShaderChunk.specularmap_fragment, THREE.ShaderChunk.normal_fragment_begin, THREE.ShaderChunk.normal_fragment_maps, THREE.ShaderChunk.emissivemap_fragment, // accumulation
  459. THREE.ShaderChunk.lights_phong_fragment, THREE.ShaderChunk.lights_fragment_begin, THREE.ShaderChunk.lights_fragment_maps, THREE.ShaderChunk.lights_fragment_end, // modulation
  460. THREE.ShaderChunk.aomap_fragment, 'vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;', THREE.ShaderChunk.envmap_fragment, 'gl_FragColor = vec4( outgoingLight, diffuseColor.a );', THREE.ShaderChunk.tonemapping_fragment, THREE.ShaderChunk.encodings_fragment, THREE.ShaderChunk.fog_fragment, THREE.ShaderChunk.premultiplied_alpha_fragment, THREE.ShaderChunk.dithering_fragment, '}' ].join( '\n' );
  461. this.setValues( parameters );
  462. }
  463. }
  464. THREE.GeometryCompressionUtils = GeometryCompressionUtils;
  465. THREE.PackedPhongMaterial = PackedPhongMaterial;
  466. } )();