GeometryCompressionUtils.js 25 KB

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