GeometryCompressionUtils.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. class 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. static compressNormals( 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 = 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 = 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 = 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 = 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. static compressPositions( 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 = 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. static compressUvs( 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 = 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 = 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. }
  192. class EncodingFuncs {
  193. static defaultEncode( x, y, z, bytes ) {
  194. if ( bytes == 1 ) {
  195. const tmpx = Math.round( ( x + 1 ) * 0.5 * 255 );
  196. const tmpy = Math.round( ( y + 1 ) * 0.5 * 255 );
  197. const tmpz = Math.round( ( z + 1 ) * 0.5 * 255 );
  198. return new Uint8Array( [ tmpx, tmpy, tmpz ] );
  199. } else if ( bytes == 2 ) {
  200. const tmpx = Math.round( ( x + 1 ) * 0.5 * 65535 );
  201. const tmpy = Math.round( ( y + 1 ) * 0.5 * 65535 );
  202. const tmpz = Math.round( ( z + 1 ) * 0.5 * 65535 );
  203. return new Uint16Array( [ tmpx, tmpy, tmpz ] );
  204. } else {
  205. console.error( 'number of bytes must be 1 or 2' );
  206. }
  207. }
  208. static defaultDecode( array, bytes ) {
  209. if ( bytes == 1 ) {
  210. return [ array[ 0 ] / 255 * 2.0 - 1.0, array[ 1 ] / 255 * 2.0 - 1.0, array[ 2 ] / 255 * 2.0 - 1.0 ];
  211. } else if ( bytes == 2 ) {
  212. return [ array[ 0 ] / 65535 * 2.0 - 1.0, array[ 1 ] / 65535 * 2.0 - 1.0, array[ 2 ] / 65535 * 2.0 - 1.0 ];
  213. } else {
  214. console.error( 'number of bytes must be 1 or 2' );
  215. }
  216. } // for `Angles` encoding
  217. static anglesEncode( 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. } // for `Octahedron` encoding
  222. static octEncodeBest( x, y, z, bytes ) {
  223. let oct, dec, best, currentCos, bestCos; // Test various combinations of ceil and floor
  224. // to minimize rounding errors
  225. best = oct = octEncodeVec3( x, y, z, 'floor', 'floor' );
  226. dec = octDecodeVec2( oct );
  227. bestCos = dot( x, y, z, dec );
  228. oct = octEncodeVec3( x, y, z, 'ceil', 'floor' );
  229. dec = octDecodeVec2( oct );
  230. currentCos = dot( x, y, z, dec );
  231. if ( currentCos > bestCos ) {
  232. best = oct;
  233. bestCos = currentCos;
  234. }
  235. oct = octEncodeVec3( x, y, z, 'floor', 'ceil' );
  236. dec = octDecodeVec2( oct );
  237. currentCos = dot( x, y, z, dec );
  238. if ( currentCos > bestCos ) {
  239. best = oct;
  240. bestCos = currentCos;
  241. }
  242. oct = octEncodeVec3( x, y, z, 'ceil', 'ceil' );
  243. dec = octDecodeVec2( oct );
  244. currentCos = dot( x, y, z, dec );
  245. if ( currentCos > bestCos ) {
  246. best = oct;
  247. }
  248. return best;
  249. function octEncodeVec3( x0, y0, z0, xfunc, yfunc ) {
  250. let x = x0 / ( Math.abs( x0 ) + Math.abs( y0 ) + Math.abs( z0 ) );
  251. let y = y0 / ( Math.abs( x0 ) + Math.abs( y0 ) + Math.abs( z0 ) );
  252. if ( z < 0 ) {
  253. const tempx = ( 1 - Math.abs( y ) ) * ( x >= 0 ? 1 : - 1 );
  254. const tempy = ( 1 - Math.abs( x ) ) * ( y >= 0 ? 1 : - 1 );
  255. x = tempx;
  256. y = tempy;
  257. let diff = 1 - Math.abs( x ) - Math.abs( y );
  258. if ( diff > 0 ) {
  259. diff += 0.001;
  260. x += x > 0 ? diff / 2 : - diff / 2;
  261. y += y > 0 ? diff / 2 : - diff / 2;
  262. }
  263. }
  264. if ( bytes == 1 ) {
  265. return new Int8Array( [ Math[ xfunc ]( x * 127.5 + ( x < 0 ? 1 : 0 ) ), Math[ yfunc ]( y * 127.5 + ( y < 0 ? 1 : 0 ) ) ] );
  266. }
  267. if ( bytes == 2 ) {
  268. return new Int16Array( [ Math[ xfunc ]( x * 32767.5 + ( x < 0 ? 1 : 0 ) ), Math[ yfunc ]( y * 32767.5 + ( y < 0 ? 1 : 0 ) ) ] );
  269. }
  270. }
  271. function octDecodeVec2( oct ) {
  272. let x = oct[ 0 ];
  273. let y = oct[ 1 ];
  274. if ( bytes == 1 ) {
  275. x /= x < 0 ? 127 : 128;
  276. y /= y < 0 ? 127 : 128;
  277. } else if ( bytes == 2 ) {
  278. x /= x < 0 ? 32767 : 32768;
  279. y /= y < 0 ? 32767 : 32768;
  280. }
  281. const z = 1 - Math.abs( x ) - Math.abs( y );
  282. if ( z < 0 ) {
  283. const tmpx = x;
  284. x = ( 1 - Math.abs( y ) ) * ( x >= 0 ? 1 : - 1 );
  285. y = ( 1 - Math.abs( tmpx ) ) * ( y >= 0 ? 1 : - 1 );
  286. }
  287. const length = Math.sqrt( x * x + y * y + z * z );
  288. return [ x / length, y / length, z / length ];
  289. }
  290. function dot( x, y, z, vec3 ) {
  291. return x * vec3[ 0 ] + y * vec3[ 1 ] + z * vec3[ 2 ];
  292. }
  293. }
  294. static quantizedEncode( array, bytes ) {
  295. let quantized, segments;
  296. if ( bytes == 1 ) {
  297. quantized = new Uint8Array( array.length );
  298. segments = 255;
  299. } else if ( bytes == 2 ) {
  300. quantized = new Uint16Array( array.length );
  301. segments = 65535;
  302. } else {
  303. console.error( 'number of bytes error! ' );
  304. }
  305. const decodeMat = new THREE.Matrix4();
  306. const min = new Float32Array( 3 );
  307. const max = new Float32Array( 3 );
  308. min[ 0 ] = min[ 1 ] = min[ 2 ] = Number.MAX_VALUE;
  309. max[ 0 ] = max[ 1 ] = max[ 2 ] = - Number.MAX_VALUE;
  310. for ( let i = 0; i < array.length; i += 3 ) {
  311. min[ 0 ] = Math.min( min[ 0 ], array[ i + 0 ] );
  312. min[ 1 ] = Math.min( min[ 1 ], array[ i + 1 ] );
  313. min[ 2 ] = Math.min( min[ 2 ], array[ i + 2 ] );
  314. max[ 0 ] = Math.max( max[ 0 ], array[ i + 0 ] );
  315. max[ 1 ] = Math.max( max[ 1 ], array[ i + 1 ] );
  316. max[ 2 ] = Math.max( max[ 2 ], array[ i + 2 ] );
  317. }
  318. decodeMat.scale( new THREE.Vector3( ( max[ 0 ] - min[ 0 ] ) / segments, ( max[ 1 ] - min[ 1 ] ) / segments, ( max[ 2 ] - min[ 2 ] ) / segments ) );
  319. decodeMat.elements[ 12 ] = min[ 0 ];
  320. decodeMat.elements[ 13 ] = min[ 1 ];
  321. decodeMat.elements[ 14 ] = min[ 2 ];
  322. decodeMat.transpose();
  323. 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 ] );
  324. for ( let i = 0; i < array.length; i += 3 ) {
  325. quantized[ i + 0 ] = Math.floor( ( array[ i + 0 ] - min[ 0 ] ) * multiplier[ 0 ] );
  326. quantized[ i + 1 ] = Math.floor( ( array[ i + 1 ] - min[ 1 ] ) * multiplier[ 1 ] );
  327. quantized[ i + 2 ] = Math.floor( ( array[ i + 2 ] - min[ 2 ] ) * multiplier[ 2 ] );
  328. }
  329. return {
  330. quantized: quantized,
  331. decodeMat: decodeMat
  332. };
  333. }
  334. static quantizedEncodeUV( array, bytes ) {
  335. let quantized, segments;
  336. if ( bytes == 1 ) {
  337. quantized = new Uint8Array( array.length );
  338. segments = 255;
  339. } else if ( bytes == 2 ) {
  340. quantized = new Uint16Array( array.length );
  341. segments = 65535;
  342. } else {
  343. console.error( 'number of bytes error! ' );
  344. }
  345. const decodeMat = new THREE.Matrix3();
  346. const min = new Float32Array( 2 );
  347. const max = new Float32Array( 2 );
  348. min[ 0 ] = min[ 1 ] = Number.MAX_VALUE;
  349. max[ 0 ] = max[ 1 ] = - Number.MAX_VALUE;
  350. for ( let i = 0; i < array.length; i += 2 ) {
  351. min[ 0 ] = Math.min( min[ 0 ], array[ i + 0 ] );
  352. min[ 1 ] = Math.min( min[ 1 ], array[ i + 1 ] );
  353. max[ 0 ] = Math.max( max[ 0 ], array[ i + 0 ] );
  354. max[ 1 ] = Math.max( max[ 1 ], array[ i + 1 ] );
  355. }
  356. decodeMat.scale( ( max[ 0 ] - min[ 0 ] ) / segments, ( max[ 1 ] - min[ 1 ] ) / segments );
  357. decodeMat.elements[ 6 ] = min[ 0 ];
  358. decodeMat.elements[ 7 ] = min[ 1 ];
  359. decodeMat.transpose();
  360. const multiplier = new Float32Array( [ max[ 0 ] !== min[ 0 ] ? segments / ( max[ 0 ] - min[ 0 ] ) : 0, max[ 1 ] !== min[ 1 ] ? segments / ( max[ 1 ] - min[ 1 ] ) : 0 ] );
  361. for ( let i = 0; i < array.length; i += 2 ) {
  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. }
  365. return {
  366. quantized: quantized,
  367. decodeMat: decodeMat
  368. };
  369. }
  370. }
  371. /**
  372. * `PackedPhongMaterial` inherited from THREE.MeshPhongMaterial
  373. *
  374. * @param {Object} parameters
  375. */
  376. class PackedPhongMaterial extends THREE.MeshPhongMaterial {
  377. constructor( parameters ) {
  378. super();
  379. this.defines = {};
  380. this.type = 'PackedPhongMaterial';
  381. this.uniforms = THREE.UniformsUtils.merge( [ THREE.ShaderLib.phong.uniforms, {
  382. quantizeMatPos: {
  383. value: null
  384. },
  385. quantizeMatUV: {
  386. value: null
  387. }
  388. } ] );
  389. 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
  390. #if USE_PACKED_NORMAL == 0
  391. vec3 decodeNormal(vec3 packedNormal)
  392. {
  393. float x = packedNormal.x * 2.0 - 1.0;
  394. float y = packedNormal.y * 2.0 - 1.0;
  395. vec2 scth = vec2(sin(x * PI), cos(x * PI));
  396. vec2 scphi = vec2(sqrt(1.0 - y * y), y);
  397. return normalize( vec3(scth.y * scphi.x, scth.x * scphi.x, scphi.y) );
  398. }
  399. #endif
  400. #if USE_PACKED_NORMAL == 1
  401. vec3 decodeNormal(vec3 packedNormal)
  402. {
  403. vec3 v = vec3(packedNormal.xy, 1.0 - abs(packedNormal.x) - abs(packedNormal.y));
  404. if (v.z < 0.0)
  405. {
  406. v.xy = (1.0 - abs(v.yx)) * vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0);
  407. }
  408. return normalize(v);
  409. }
  410. #endif
  411. #if USE_PACKED_NORMAL == 2
  412. vec3 decodeNormal(vec3 packedNormal)
  413. {
  414. vec3 v = (packedNormal * 2.0) - 1.0;
  415. return normalize(v);
  416. }
  417. #endif
  418. #endif`, `#ifdef USE_PACKED_POSITION
  419. #if USE_PACKED_POSITION == 0
  420. uniform mat4 quantizeMatPos;
  421. #endif
  422. #endif`, `#ifdef USE_PACKED_UV
  423. #if USE_PACKED_UV == 1
  424. uniform mat3 quantizeMatUV;
  425. #endif
  426. #endif`, `#ifdef USE_PACKED_UV
  427. #if USE_PACKED_UV == 0
  428. vec2 decodeUV(vec2 packedUV)
  429. {
  430. vec2 uv = (packedUV * 2.0) - 1.0;
  431. return uv;
  432. }
  433. #endif
  434. #if USE_PACKED_UV == 1
  435. vec2 decodeUV(vec2 packedUV)
  436. {
  437. vec2 uv = ( vec3(packedUV, 1.0) * quantizeMatUV ).xy;
  438. return uv;
  439. }
  440. #endif
  441. #endif`, 'void main() {', THREE.ShaderChunk.uv_vertex, `#ifdef USE_UV
  442. #ifdef USE_PACKED_UV
  443. vUv = decodeUV(vUv);
  444. #endif
  445. #endif`, THREE.ShaderChunk.uv2_vertex, THREE.ShaderChunk.color_vertex, THREE.ShaderChunk.beginnormal_vertex, `#ifdef USE_PACKED_NORMAL
  446. objectNormal = decodeNormal(objectNormal);
  447. #endif
  448. #ifdef USE_TANGENT
  449. vec3 objectTangent = vec3( tangent.xyz );
  450. #endif
  451. `, 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
  452. #if USE_PACKED_POSITION == 0
  453. transformed = ( vec4(transformed, 1.0) * quantizeMatPos ).xyz;
  454. #endif
  455. #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.
  456. 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
  457. THREE.ShaderChunk.lights_phong_fragment, THREE.ShaderChunk.lights_fragment_begin, THREE.ShaderChunk.lights_fragment_maps, THREE.ShaderChunk.lights_fragment_end, // modulation
  458. 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' );
  459. this.setValues( parameters );
  460. }
  461. }
  462. THREE.GeometryCompressionUtils = GeometryCompressionUtils;
  463. THREE.PackedPhongMaterial = PackedPhongMaterial;
  464. } )();