GeometryCompressionUtils.js 26 KB

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