GeometryCompressionUtils.js 21 KB

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