GeometryPackingUtils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. } else if (encodeMethod == "OCT") {
  24. result = new Int8Array(count * 2);
  25. } else {
  26. console.error("Unrecognized encoding method, should be `BASIC` or `OCT`. ");
  27. }
  28. for (let idx = 0; idx < array.length; idx += 3) {
  29. let encoded;
  30. if (encodeMethod == "BASIC") {
  31. encoded = uInt16Encode(array[idx], array[idx + 1], array[idx + 2]);
  32. result[idx / 3 * 2 + 0] = encoded[0];
  33. result[idx / 3 * 2 + 1] = encoded[1];
  34. } else if (encodeMethod == "OCT") {
  35. var oct, dec, best, currentCos, bestCos;
  36. encoded = octEncodeBest(array[idx], array[idx + 1], array[idx + 2]);
  37. result[idx / 3 * 2 + 0] = encoded[0];
  38. result[idx / 3 * 2 + 1] = encoded[1];
  39. }
  40. }
  41. mesh.geometry.setAttribute('normal', new THREE.BufferAttribute(result, 2, true));
  42. mesh.geometry.attributes.normal.isPacked = true;
  43. mesh.geometry.attributes.normal.packingMethod = encodeMethod;
  44. // modify material
  45. mesh.material = new PackedPhongMaterial().copy(mesh.material);
  46. Object.defineProperty(mesh.material.defines, 'USE_PACKED_NORMAL', { value: "" });
  47. if (encodeMethod == "BASIC"){
  48. mesh.material.defines.USE_PACKED_NORMAL = 0;
  49. }
  50. if (encodeMethod == "OCT"){
  51. mesh.material.defines.USE_PACKED_NORMAL = 1;
  52. }
  53. /**
  54. *
  55. * Encoding functions: Basic, OCT
  56. *
  57. */
  58. // for `Basic` encoding
  59. function uInt16Encode(x, y, z) {
  60. let normal0 = parseInt(0.5 * (1.0 + Math.atan2(y, x) / Math.PI) * 65535);
  61. let normal1 = parseInt(0.5 * (1.0 + z) * 65535);
  62. return new Uint16Array([normal0, normal1]);
  63. }
  64. // for `OCT` encoding
  65. function octEncodeBest(x, y, z) {
  66. // Test various combinations of ceil and floor
  67. // to minimize rounding errors
  68. best = oct = octEncodeVec3(x, y, z, "floor", "floor");
  69. dec = octDecodeVec2(oct);
  70. currentCos = bestCos = dot(x, y, z, dec);
  71. oct = octEncodeVec3(x, y, z, "ceil", "floor");
  72. dec = octDecodeVec2(oct);
  73. currentCos = dot(x, y, z, dec);
  74. if (currentCos > bestCos) {
  75. best = oct;
  76. bestCos = currentCos;
  77. }
  78. oct = octEncodeVec3(x, y, z, "floor", "ceil");
  79. dec = octDecodeVec2(oct);
  80. currentCos = dot(x, y, z, dec);
  81. if (currentCos > bestCos) {
  82. best = oct;
  83. bestCos = currentCos;
  84. }
  85. oct = octEncodeVec3(x, y, z, "ceil", "ceil");
  86. dec = octDecodeVec2(oct);
  87. currentCos = dot(x, y, z, dec);
  88. if (currentCos > bestCos) {
  89. best = oct;
  90. bestCos = currentCos;
  91. }
  92. return best;
  93. }
  94. function octEncodeVec3(x, y, z, xfunc, yfunc) {
  95. var x = x / (Math.abs(x) + Math.abs(y) + Math.abs(z));
  96. var y = y / (Math.abs(x) + Math.abs(y) + Math.abs(z));
  97. if (z < 0) {
  98. var tempx = x;
  99. var tempy = y;
  100. tempx = (1 - Math.abs(y)) * (x >= 0 ? 1 : -1);
  101. tempy = (1 - Math.abs(x)) * (y >= 0 ? 1 : -1);
  102. x = tempx;
  103. y = tempy;
  104. }
  105. return new Int8Array([
  106. Math[xfunc](x * 127.5 + (x < 0 ? -1 : 0)),
  107. Math[yfunc](y * 127.5 + (y < 0 ? -1 : 0))
  108. ]);
  109. }
  110. function octDecodeVec2(oct) {
  111. var x = oct[0];
  112. var y = oct[1];
  113. x /= x < 0 ? 127 : 128;
  114. y /= y < 0 ? 127 : 128;
  115. var z = 1 - Math.abs(x) - Math.abs(y);
  116. if (z < 0) {
  117. x = (1 - Math.abs(y)) * (x >= 0 ? 1 : -1);
  118. y = (1 - Math.abs(x)) * (y >= 0 ? 1 : -1);
  119. }
  120. var length = Math.sqrt(x * x + y * y + z * z);
  121. return [
  122. x / length,
  123. y / length,
  124. z / length
  125. ];
  126. }
  127. function dot(x, y, z, vec3) {
  128. return x * vec3[0] + y * vec3[1] + z * vec3[2];
  129. }
  130. },
  131. changeShaderChunk: function () {
  132. THREE.ShaderChunk.beginnormal_vertex = `
  133. vec3 objectNormal = vec3( normal );
  134. #ifdef USE_TANGENT
  135. vec3 objectTangent = vec3( tangent.xyz );
  136. #endif
  137. #ifdef USE_PACKED_NORMAL
  138. #ifdef USE_PACKED_NORMAL == 0
  139. float x = objectNormal.x * 2.0 - 1.0;
  140. float y = objectNormal.y * 2.0 - 1.0;
  141. vec2 scth = vec2(sin(x * PI), cos(x * PI));
  142. vec2 scphi = vec2(sqrt(1.0 - y * y), y);
  143. objectNormal = normalize( vec3(scth.y * scphi.x, scth.x * scphi.x, scphi.y) );
  144. objectNormal = vec3(0.0, 0.0, 1.0);
  145. #endif
  146. #ifdef USE_PACKED_NORMAL == 1
  147. float x = objectNormal.x;
  148. float y = objectNormal.y;
  149. float z = 1.0 - abs(x) - abs(y);
  150. if (z < 0.0) {
  151. x = (1.0 - abs(y)) * (x >= 0.0 ? 1.0 : -1.0);
  152. y = (1.0 - abs(x)) * (y >= 0.0 ? 1.0 : -1.0);
  153. }
  154. float length = sqrt(x * x + y * y + z * z);
  155. objectNormal = normalize( vec3(x / length, y / length, z / length) );
  156. #endif
  157. #endif
  158. `;
  159. }
  160. };
  161. function PackedPhongMaterial(parameters) {
  162. THREE.MeshPhongMaterial.call(this);
  163. this.defines = { 'USE_PACKED_NORMAL': '' };
  164. this.type = 'PackedPhongMaterial'; // IMPORTANT: DO NOT CHANGE THIS TYPE
  165. this.uniforms = PackedPhongShader.uniforms;
  166. this.vertexShader = PackedPhongShader.vertexShader;
  167. this.fragmentShader = PackedPhongShader.fragmentShader;
  168. this.setValues(parameters);
  169. }
  170. var PackedPhongShader = {
  171. uniforms: THREE.UniformsUtils.merge([
  172. THREE.ShaderLib.phong.uniforms,
  173. // {
  174. // packedNormal: { value: null }
  175. // }
  176. ]),
  177. vertexShader: [
  178. "#define PHONG",
  179. "varying vec3 vViewPosition;",
  180. "#ifndef FLAT_SHADED",
  181. "varying vec3 vNormal;",
  182. "#endif",
  183. THREE.ShaderChunk.common,
  184. THREE.ShaderChunk.uv_pars_vertex,
  185. THREE.ShaderChunk.uv2_pars_vertex,
  186. THREE.ShaderChunk.displacementmap_pars_vertex,
  187. THREE.ShaderChunk.envmap_pars_vertex,
  188. THREE.ShaderChunk.color_pars_vertex,
  189. THREE.ShaderChunk.fog_pars_vertex,
  190. THREE.ShaderChunk.morphtarget_pars_vertex,
  191. THREE.ShaderChunk.skinning_pars_vertex,
  192. THREE.ShaderChunk.shadowmap_pars_vertex,
  193. THREE.ShaderChunk.logdepthbuf_pars_vertex,
  194. THREE.ShaderChunk.clipping_planes_pars_vertex,
  195. `#ifdef USE_PACKED_NORMAL
  196. vec3 basicDecode(vec3 packedNormal)
  197. {
  198. float x = packedNormal.x * 2.0 - 1.0;
  199. float y = packedNormal.y * 2.0 - 1.0;
  200. vec2 scth = vec2(sin(x * PI), cos(x * PI));
  201. vec2 scphi = vec2(sqrt(1.0 - y * y), y);
  202. return normalize( vec3(scth.y * scphi.x, scth.x * scphi.x, scphi.y) );
  203. }
  204. vec3 octDecode(vec3 packedNormal)
  205. {
  206. // float x = packedNormal.x / packedNormal.x < 0.0 ? 127.0 : 128.0;
  207. // float y = packedNormal.y / packedNormal.y < 0.0 ? 127.0 : 128.0;
  208. float x = packedNormal.x;
  209. float y = packedNormal.y;
  210. float z = 1.0 - abs(x) - abs(y);
  211. if (z < 0.0) {
  212. x = (1.0 - abs(y)) * (x >= 0.0 ? 1.0 : -1.0);
  213. y = (1.0 - abs(x)) * (y >= 0.0 ? 1.0 : -1.0);
  214. }
  215. float length = sqrt(x * x + y * y + z * z);
  216. return normalize( vec3(x / length, y / length, z / length) );
  217. }
  218. #endif`,
  219. "void main() {",
  220. THREE.ShaderChunk.uv_vertex,
  221. THREE.ShaderChunk.uv2_vertex,
  222. THREE.ShaderChunk.color_vertex,
  223. THREE.ShaderChunk.beginnormal_vertex,
  224. `#ifdef USE_PACKED_NORMAL
  225. #if USE_PACKED_NORMAL == 0
  226. objectNormal = basicDecode(objectNormal);
  227. #endif
  228. #if USE_PACKED_NORMAL == 1
  229. objectNormal = octDecode(objectNormal);
  230. #endif
  231. #endif
  232. #ifdef USE_TANGENT
  233. vec3 objectTangent = vec3( tangent.xyz );
  234. #endif
  235. `,
  236. THREE.ShaderChunk.morphnormal_vertex,
  237. THREE.ShaderChunk.skinbase_vertex,
  238. THREE.ShaderChunk.skinnormal_vertex,
  239. THREE.ShaderChunk.defaultnormal_vertex,
  240. "#ifndef FLAT_SHADED",
  241. "vNormal = normalize( transformedNormal );",
  242. "#endif",
  243. THREE.ShaderChunk.begin_vertex,
  244. THREE.ShaderChunk.morphtarget_vertex,
  245. THREE.ShaderChunk.skinning_vertex,
  246. THREE.ShaderChunk.displacementmap_vertex,
  247. THREE.ShaderChunk.project_vertex,
  248. THREE.ShaderChunk.logdepthbuf_vertex,
  249. THREE.ShaderChunk.clipping_planes_vertex,
  250. "vViewPosition = - mvPosition.xyz;",
  251. THREE.ShaderChunk.worldpos_vertex,
  252. THREE.ShaderChunk.envmap_vertex,
  253. THREE.ShaderChunk.shadowmap_vertex,
  254. THREE.ShaderChunk.fog_vertex,
  255. "}",
  256. ].join("\n"),
  257. fragmentShader: [
  258. "#define PHONG",
  259. "uniform vec3 diffuse;",
  260. "uniform vec3 emissive;",
  261. "uniform vec3 specular;",
  262. "uniform float shininess;",
  263. "uniform float opacity;",
  264. THREE.ShaderChunk.common,
  265. THREE.ShaderChunk.packing,
  266. THREE.ShaderChunk.dithering_pars_fragment,
  267. THREE.ShaderChunk.color_pars_fragment,
  268. THREE.ShaderChunk.uv_pars_fragment,
  269. THREE.ShaderChunk.uv2_pars_fragment,
  270. THREE.ShaderChunk.map_pars_fragment,
  271. THREE.ShaderChunk.alphamap_pars_fragment,
  272. THREE.ShaderChunk.aomap_pars_fragment,
  273. THREE.ShaderChunk.lightmap_pars_fragment,
  274. THREE.ShaderChunk.emissivemap_pars_fragment,
  275. THREE.ShaderChunk.envmap_common_pars_fragment,
  276. THREE.ShaderChunk.envmap_pars_fragment,
  277. THREE.ShaderChunk.cube_uv_reflection_fragment,
  278. THREE.ShaderChunk.fog_pars_fragment,
  279. THREE.ShaderChunk.bsdfs,
  280. THREE.ShaderChunk.lights_pars_begin,
  281. THREE.ShaderChunk.lights_phong_pars_fragment,
  282. THREE.ShaderChunk.shadowmap_pars_fragment,
  283. THREE.ShaderChunk.bumpmap_pars_fragment,
  284. THREE.ShaderChunk.normalmap_pars_fragment,
  285. THREE.ShaderChunk.specularmap_pars_fragment,
  286. THREE.ShaderChunk.logdepthbuf_pars_fragment,
  287. THREE.ShaderChunk.clipping_planes_pars_fragment,
  288. "void main() {",
  289. THREE.ShaderChunk.clipping_planes_fragment,
  290. "vec4 diffuseColor = vec4( diffuse, opacity );",
  291. "ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  292. "vec3 totalEmissiveRadiance = emissive;",
  293. THREE.ShaderChunk.logdepthbuf_fragment,
  294. THREE.ShaderChunk.map_fragment,
  295. THREE.ShaderChunk.color_fragment,
  296. THREE.ShaderChunk.alphamap_fragment,
  297. THREE.ShaderChunk.alphatest_fragment,
  298. THREE.ShaderChunk.specularmap_fragment,
  299. THREE.ShaderChunk.normal_fragment_begin,
  300. THREE.ShaderChunk.normal_fragment_maps,
  301. THREE.ShaderChunk.emissivemap_fragment,
  302. // accumulation
  303. THREE.ShaderChunk.lights_phong_fragment,
  304. THREE.ShaderChunk.lights_fragment_begin,
  305. THREE.ShaderChunk.lights_fragment_maps,
  306. THREE.ShaderChunk.lights_fragment_end,
  307. // modulation
  308. THREE.ShaderChunk.aomap_fragment,
  309. "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;",
  310. THREE.ShaderChunk.envmap_fragment,
  311. "gl_FragColor = vec4( outgoingLight, diffuseColor.a );",
  312. THREE.ShaderChunk.tonemapping_fragment,
  313. THREE.ShaderChunk.encodings_fragment,
  314. THREE.ShaderChunk.fog_fragment,
  315. THREE.ShaderChunk.premultiplied_alpha_fragment,
  316. THREE.ShaderChunk.dithering_fragment,
  317. "}",
  318. ].join("\n")
  319. };
  320. PackedPhongMaterial.prototype = Object.create(THREE.MeshPhongMaterial.prototype);
  321. export { GeometryPackingUtils, PackedPhongMaterial };