GeometryCompressionUtils.js 21 KB

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