GeometryPackingUtils.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /**
  2. * @author YY
  3. */
  4. import * as THREE from "../../../build/three.module.js";
  5. var GeometryPackingUtils = {
  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 = GeometryEncodingUtils.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 = GeometryEncodingUtils.octEncodeBest(array[idx], array[idx + 1], array[idx + 2]);
  35. let x = encoded[0] / (encoded[0] < 0 ? 128 : 127)
  36. let y = encoded[1] / (encoded[1] < 0 ? 128 : 127)
  37. if ( Math.abs(array[idx + 2]) == 0 ){
  38. console.log([ array[idx], array[idx + 1], array[idx + 2] ], encoded,
  39. x,
  40. y,
  41. 1-Math.abs(x)-Math.abs(y),
  42. (1-Math.abs(x)-Math.abs(y))*127,
  43. )
  44. }
  45. result[idx / 3 * 2 + 0] = encoded[0];
  46. result[idx / 3 * 2 + 1] = encoded[1];
  47. }
  48. mesh.geometry.setAttribute('normal', new THREE.BufferAttribute(result, 2, true));
  49. } else if (encodeMethod == "DEFAULT") {
  50. result = new Uint8Array(count * 3);
  51. for (let idx = 0; idx < array.length; idx += 3) {
  52. let encoded;
  53. encoded = GeometryEncodingUtils.defaultEncode(array[idx], array[idx + 1], array[idx + 2]);
  54. let decoded = GeometryEncodingUtils.defaultDecode(encoded);
  55. 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;
  56. Math.abs(array[idx + 2]) < 0.05 && console.log([array[idx], array[idx + 1], array[idx + 2]], encoded, decoded, angle)
  57. result[idx + 0] = encoded[0];
  58. result[idx + 1] = encoded[1];
  59. result[idx + 2] = encoded[2];
  60. }
  61. mesh.geometry.setAttribute('normal', new THREE.BufferAttribute(result, 3, true));
  62. } else {
  63. console.error("Unrecognized encoding method, should be `BASIC` or `OCT`. ");
  64. }
  65. mesh.geometry.attributes.normal.needsUpdate = true;
  66. mesh.geometry.attributes.normal.isPacked = true;
  67. mesh.geometry.attributes.normal.packingMethod = encodeMethod;
  68. // modify material
  69. if (!(mesh.material instanceof PackedPhongMaterial)) {
  70. mesh.material = new PackedPhongMaterial().copy(mesh.material);
  71. }
  72. if (encodeMethod == "BASIC") {
  73. mesh.material.defines.USE_PACKED_NORMAL = 0;
  74. }
  75. if (encodeMethod == "OCT") {
  76. mesh.material.defines.USE_PACKED_NORMAL = 1;
  77. }
  78. if (encodeMethod == "DEFAULT") {
  79. mesh.material.defines.USE_PACKED_NORMAL = 2;
  80. }
  81. },
  82. packPositions: function (mesh) {
  83. if (!mesh.geometry) {
  84. console.error("Mesh must contain geometry property. ");
  85. }
  86. let position = mesh.geometry.attributes.position;
  87. if (!position) {
  88. console.error("Geometry must contain position attribute. ");
  89. }
  90. if (position.isPacked) return;
  91. if (position.itemSize != 3) {
  92. console.error("position.itemSize is not 3, which cannot be packed. ");
  93. }
  94. let array = position.array;
  95. let count = position.count;
  96. let quantized = new Uint16Array(array.length);
  97. let decodeMat4 = new THREE.Matrix4();
  98. let min = new Float32Array(3);
  99. let max = new Float32Array(3);
  100. min[0] = min[1] = min[2] = Number.MAX_VALUE;
  101. max[0] = max[1] = max[2] = -Number.MAX_VALUE;
  102. for (let i = 0; i < array.length; i += 3) {
  103. min[0] = Math.min(min[0], array[i + 0]);
  104. min[1] = Math.min(min[1], array[i + 1]);
  105. min[2] = Math.min(min[2], array[i + 2]);
  106. max[0] = Math.max(max[0], array[i + 0]);
  107. max[1] = Math.max(max[1], array[i + 1]);
  108. max[2] = Math.max(max[2], array[i + 2]);
  109. }
  110. decodeMat4.scale(new THREE.Vector3(
  111. (max[0] - min[0]) / 65535,
  112. (max[1] - min[1]) / 65535,
  113. (max[2] - min[2]) / 65535
  114. ));
  115. decodeMat4.elements[12] = min[0];
  116. decodeMat4.elements[13] = min[1];
  117. decodeMat4.elements[14] = min[2];
  118. decodeMat4.transpose();
  119. let multiplier = new Float32Array([
  120. max[0] !== min[0] ? 65535 / (max[0] - min[0]) : 0,
  121. max[1] !== min[1] ? 65535 / (max[1] - min[1]) : 0,
  122. max[2] !== min[2] ? 65535 / (max[2] - min[2]) : 0
  123. ]);
  124. for (let i = 0; i < array.length; i += 3) {
  125. quantized[i + 0] = Math.floor((array[i + 0] - min[0]) * multiplier[0]);
  126. quantized[i + 1] = Math.floor((array[i + 1] - min[1]) * multiplier[1]);
  127. quantized[i + 2] = Math.floor((array[i + 2] - min[2]) * multiplier[2]);
  128. }
  129. // IMPORTANT: calculate original geometry bounding info first, before updating packed positions
  130. if (mesh.geometry.boundingBox == null) mesh.geometry.computeBoundingBox();
  131. if (mesh.geometry.boundingSphere == null) mesh.geometry.computeBoundingSphere();
  132. mesh.geometry.setAttribute('position', new THREE.BufferAttribute(quantized, 3));
  133. mesh.geometry.attributes.position.isPacked = true;
  134. mesh.geometry.attributes.position.needsUpdate = true;
  135. // modify material
  136. if (!(mesh.material instanceof PackedPhongMaterial)) {
  137. mesh.material = new PackedPhongMaterial().copy(mesh.material);
  138. }
  139. mesh.material.defines.USE_PACKED_POSITION = 0;
  140. mesh.material.uniforms.quantizeMat.value = decodeMat4;
  141. mesh.material.uniforms.quantizeMat.needsUpdate = true;
  142. }
  143. };
  144. /**
  145. *
  146. * Encoding functions: Default, Basic, OCT
  147. *
  148. */
  149. var GeometryEncodingUtils = {
  150. defaultEncode: function (x, y, z) {
  151. let tmpx = parseInt((x + 1) * 0.5 * 255);
  152. let tmpy = parseInt((y + 1) * 0.5 * 255);
  153. let tmpz = parseInt((z + 1) * 0.5 * 255);
  154. return new Uint8Array([tmpx, tmpy, tmpz]);
  155. },
  156. defaultDecode: function (array) {
  157. return [
  158. ((array[0] / 255) * 2.0) - 1.0,
  159. ((array[1] / 255) * 2.0) - 1.0,
  160. ((array[2] / 255) * 2.0) - 1.0,
  161. ]
  162. },
  163. // for `Basic` encoding
  164. uInt16Encode: function (x, y, z) {
  165. let normal0 = parseInt(0.5 * (1.0 + Math.atan2(y, x) / Math.PI) * 65535);
  166. let normal1 = parseInt(0.5 * (1.0 + z) * 65535);
  167. return new Uint16Array([normal0, normal1]);
  168. },
  169. // for `OCT` encoding
  170. octEncodeBest: function (x, y, z) {
  171. var oct, dec, best, currentCos, bestCos;
  172. // Test various combinations of ceil and floor
  173. // to minimize rounding errors
  174. best = oct = octEncodeVec3(x, y, z, "floor", "floor");
  175. dec = octDecodeVec2(oct);
  176. currentCos = bestCos = dot(x, y, z, dec);
  177. oct = octEncodeVec3(x, y, z, "ceil", "floor");
  178. dec = octDecodeVec2(oct);
  179. currentCos = dot(x, y, z, dec);
  180. if (currentCos > bestCos) {
  181. best = oct;
  182. bestCos = currentCos;
  183. }
  184. oct = octEncodeVec3(x, y, z, "floor", "ceil");
  185. dec = octDecodeVec2(oct);
  186. currentCos = dot(x, y, z, dec);
  187. if (currentCos > bestCos) {
  188. best = oct;
  189. bestCos = currentCos;
  190. }
  191. oct = octEncodeVec3(x, y, z, "ceil", "ceil");
  192. dec = octDecodeVec2(oct);
  193. currentCos = dot(x, y, z, dec);
  194. if (currentCos > bestCos) {
  195. best = oct;
  196. bestCos = currentCos;
  197. }
  198. var angle = Math.acos(bestCos) * 180 / Math.PI;
  199. // if (Math.abs(z) < 0.05) {
  200. // console.log(angle)
  201. // console.log([x, y, z], octDecodeVec2(octEncodeVec3(x, y, z, "floor", "floor")))
  202. // console.log([x, y, z], octDecodeVec2(octEncodeVec3(x, y, z, "ceil", "floor")))
  203. // console.log([x, y, z], octDecodeVec2(octEncodeVec3(x, y, z, "floor", "ceil")))
  204. // console.log([x, y, z], octDecodeVec2(octEncodeVec3(x, y, z, "ceil", "ceil")))
  205. // }
  206. return best;
  207. function octEncodeVec3(x, y, z, xfunc, yfunc) {
  208. var x = x / (Math.abs(x) + Math.abs(y) + Math.abs(z));
  209. var y = y / (Math.abs(x) + Math.abs(y) + Math.abs(z));
  210. if (z < 0) {
  211. var tempx = x;
  212. var tempy = y;
  213. tempx = (1 - Math.abs(y)) * (x >= 0 ? 1 : -1);
  214. tempy = (1 - Math.abs(x)) * (y >= 0 ? 1 : -1);
  215. x = tempx;
  216. y = tempy;
  217. var diff = 1-Math.abs(x)-Math.abs(y);
  218. if (diff > 0){
  219. diff += 0.001
  220. x += x > 0 ? diff / 2 : -diff / 2;
  221. y += y > 0 ? diff / 2 : -diff / 2;
  222. }
  223. console.log("z < 0", `z:${z}`, 1-Math.abs(x)-Math.abs(y))
  224. }
  225. return new Int8Array([
  226. Math[xfunc](x * 127.5 + (x < 0 ? 1 : 0)),
  227. Math[yfunc](y * 127.5 + (y < 0 ? 1 : 0))
  228. ]);
  229. // return new Int8Array([
  230. // parseInt(x * 127.5 + (x < 0 ? 1 : 0)),
  231. // parseInt(y * 127.5 + (y < 0 ? 1 : 0))
  232. // ]);
  233. }
  234. function octDecodeVec2(oct) {
  235. var x = oct[0];
  236. var y = oct[1];
  237. x /= x < 0 ? 127 : 128;
  238. y /= y < 0 ? 127 : 128;
  239. var z = 1 - Math.abs(x) - Math.abs(y);
  240. if (z < 0) {
  241. var tmpx = x;
  242. x = (1 - Math.abs(y)) * (x >= 0 ? 1 : -1);
  243. y = (1 - Math.abs(tmpx)) * (y >= 0 ? 1 : -1);
  244. }
  245. var length = Math.sqrt(x * x + y * y + z * z);
  246. return [
  247. x / length,
  248. y / length,
  249. z / length
  250. ];
  251. }
  252. function dot(x, y, z, vec3) {
  253. return x * vec3[0] + y * vec3[1] + z * vec3[2];
  254. }
  255. }
  256. };
  257. /**
  258. * PackedPhongMaterial inherited from THREE.MeshPhongMaterial
  259. *
  260. * @param {*} parameters
  261. */
  262. function PackedPhongMaterial(parameters) {
  263. THREE.MeshPhongMaterial.call(this);
  264. this.defines = {};
  265. this.type = 'PackedPhongMaterial';
  266. this.uniforms = PackedPhongShader.uniforms;
  267. this.vertexShader = PackedPhongShader.vertexShader;
  268. this.fragmentShader = PackedPhongShader.fragmentShader;
  269. this.setValues(parameters);
  270. }
  271. var PackedPhongShader = {
  272. uniforms: THREE.UniformsUtils.merge([
  273. THREE.ShaderLib.phong.uniforms,
  274. {
  275. quantizeMat: { value: null }
  276. }
  277. ]),
  278. vertexShader: [
  279. "#define PHONG",
  280. "varying vec3 vViewPosition;",
  281. "#ifndef FLAT_SHADED",
  282. "varying vec3 vNormal;",
  283. "#endif",
  284. THREE.ShaderChunk.common,
  285. THREE.ShaderChunk.uv_pars_vertex,
  286. THREE.ShaderChunk.uv2_pars_vertex,
  287. THREE.ShaderChunk.displacementmap_pars_vertex,
  288. THREE.ShaderChunk.envmap_pars_vertex,
  289. THREE.ShaderChunk.color_pars_vertex,
  290. THREE.ShaderChunk.fog_pars_vertex,
  291. THREE.ShaderChunk.morphtarget_pars_vertex,
  292. THREE.ShaderChunk.skinning_pars_vertex,
  293. THREE.ShaderChunk.shadowmap_pars_vertex,
  294. THREE.ShaderChunk.logdepthbuf_pars_vertex,
  295. THREE.ShaderChunk.clipping_planes_pars_vertex,
  296. `#ifdef USE_PACKED_NORMAL
  297. vec3 basicDecode(vec3 packedNormal)
  298. {
  299. float x = packedNormal.x * 2.0 - 1.0;
  300. float y = packedNormal.y * 2.0 - 1.0;
  301. vec2 scth = vec2(sin(x * PI), cos(x * PI));
  302. vec2 scphi = vec2(sqrt(1.0 - y * y), y);
  303. return normalize( vec3(scth.y * scphi.x, scth.x * scphi.x, scphi.y) );
  304. }
  305. vec3 octDecode(vec3 packedNormal)
  306. {
  307. // packedNormal = packedNormal / 127.0;
  308. vec3 v = vec3(packedNormal.xy, 1.0 - abs(packedNormal.x) - abs(packedNormal.y));
  309. if (v.z < 0.0)
  310. {
  311. v.xy = (1.0 - abs(v.yx)) * vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0);
  312. }
  313. return normalize(v);
  314. }
  315. vec3 defaultDecode(vec3 packedNormal)
  316. {
  317. vec3 v = (packedNormal * 2.0) - 1.0;
  318. return normalize(v);
  319. }
  320. #endif`,
  321. `#ifdef USE_PACKED_POSITION
  322. #if USE_PACKED_POSITION == 0
  323. uniform mat4 quantizeMat;
  324. #endif
  325. #endif`,
  326. "void main() {",
  327. THREE.ShaderChunk.uv_vertex,
  328. THREE.ShaderChunk.uv2_vertex,
  329. THREE.ShaderChunk.color_vertex,
  330. THREE.ShaderChunk.beginnormal_vertex,
  331. `#ifdef USE_PACKED_NORMAL
  332. #if USE_PACKED_NORMAL == 0
  333. objectNormal = basicDecode(objectNormal);
  334. #endif
  335. #if USE_PACKED_NORMAL == 1
  336. objectNormal = octDecode(objectNormal);
  337. #endif
  338. #if USE_PACKED_NORMAL == 2
  339. objectNormal = defaultDecode(objectNormal);
  340. #endif
  341. #endif
  342. #ifdef USE_TANGENT
  343. vec3 objectTangent = vec3( tangent.xyz );
  344. #endif
  345. `,
  346. THREE.ShaderChunk.morphnormal_vertex,
  347. THREE.ShaderChunk.skinbase_vertex,
  348. THREE.ShaderChunk.skinnormal_vertex,
  349. THREE.ShaderChunk.defaultnormal_vertex,
  350. "#ifndef FLAT_SHADED",
  351. "vNormal = normalize( transformedNormal );",
  352. "#endif",
  353. THREE.ShaderChunk.begin_vertex,
  354. `#ifdef USE_PACKED_POSITION
  355. #if USE_PACKED_POSITION == 0
  356. transformed = ( vec4(transformed, 1.0) * quantizeMat ).xyz;
  357. #endif
  358. #endif`,
  359. THREE.ShaderChunk.morphtarget_vertex,
  360. THREE.ShaderChunk.skinning_vertex,
  361. THREE.ShaderChunk.displacementmap_vertex,
  362. THREE.ShaderChunk.project_vertex,
  363. THREE.ShaderChunk.logdepthbuf_vertex,
  364. THREE.ShaderChunk.clipping_planes_vertex,
  365. "vViewPosition = - mvPosition.xyz;",
  366. THREE.ShaderChunk.worldpos_vertex,
  367. THREE.ShaderChunk.envmap_vertex,
  368. THREE.ShaderChunk.shadowmap_vertex,
  369. THREE.ShaderChunk.fog_vertex,
  370. "}",
  371. ].join("\n"),
  372. fragmentShader: [
  373. "#define PHONG",
  374. "uniform vec3 diffuse;",
  375. "uniform vec3 emissive;",
  376. "uniform vec3 specular;",
  377. "uniform float shininess;",
  378. "uniform float opacity;",
  379. THREE.ShaderChunk.common,
  380. THREE.ShaderChunk.packing,
  381. THREE.ShaderChunk.dithering_pars_fragment,
  382. THREE.ShaderChunk.color_pars_fragment,
  383. THREE.ShaderChunk.uv_pars_fragment,
  384. THREE.ShaderChunk.uv2_pars_fragment,
  385. THREE.ShaderChunk.map_pars_fragment,
  386. THREE.ShaderChunk.alphamap_pars_fragment,
  387. THREE.ShaderChunk.aomap_pars_fragment,
  388. THREE.ShaderChunk.lightmap_pars_fragment,
  389. THREE.ShaderChunk.emissivemap_pars_fragment,
  390. THREE.ShaderChunk.envmap_common_pars_fragment,
  391. THREE.ShaderChunk.envmap_pars_fragment,
  392. THREE.ShaderChunk.cube_uv_reflection_fragment,
  393. THREE.ShaderChunk.fog_pars_fragment,
  394. THREE.ShaderChunk.bsdfs,
  395. THREE.ShaderChunk.lights_pars_begin,
  396. THREE.ShaderChunk.lights_phong_pars_fragment,
  397. THREE.ShaderChunk.shadowmap_pars_fragment,
  398. THREE.ShaderChunk.bumpmap_pars_fragment,
  399. THREE.ShaderChunk.normalmap_pars_fragment,
  400. THREE.ShaderChunk.specularmap_pars_fragment,
  401. THREE.ShaderChunk.logdepthbuf_pars_fragment,
  402. THREE.ShaderChunk.clipping_planes_pars_fragment,
  403. "void main() {",
  404. THREE.ShaderChunk.clipping_planes_fragment,
  405. "vec4 diffuseColor = vec4( diffuse, opacity );",
  406. "ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  407. "vec3 totalEmissiveRadiance = emissive;",
  408. THREE.ShaderChunk.logdepthbuf_fragment,
  409. THREE.ShaderChunk.map_fragment,
  410. THREE.ShaderChunk.color_fragment,
  411. THREE.ShaderChunk.alphamap_fragment,
  412. THREE.ShaderChunk.alphatest_fragment,
  413. THREE.ShaderChunk.specularmap_fragment,
  414. THREE.ShaderChunk.normal_fragment_begin,
  415. THREE.ShaderChunk.normal_fragment_maps,
  416. THREE.ShaderChunk.emissivemap_fragment,
  417. // accumulation
  418. THREE.ShaderChunk.lights_phong_fragment,
  419. THREE.ShaderChunk.lights_fragment_begin,
  420. THREE.ShaderChunk.lights_fragment_maps,
  421. THREE.ShaderChunk.lights_fragment_end,
  422. // modulation
  423. THREE.ShaderChunk.aomap_fragment,
  424. "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;",
  425. THREE.ShaderChunk.envmap_fragment,
  426. "gl_FragColor = vec4( outgoingLight, diffuseColor.a );",
  427. THREE.ShaderChunk.tonemapping_fragment,
  428. THREE.ShaderChunk.encodings_fragment,
  429. THREE.ShaderChunk.fog_fragment,
  430. THREE.ShaderChunk.premultiplied_alpha_fragment,
  431. THREE.ShaderChunk.dithering_fragment,
  432. "}",
  433. ].join("\n")
  434. };
  435. PackedPhongMaterial.prototype = Object.create(THREE.MeshPhongMaterial.prototype);
  436. export { GeometryPackingUtils, PackedPhongMaterial };