GeometryCompressionUtils.js 24 KB

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