GeometryCompressionUtils.js 24 KB

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