1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 | 1× 1× 1× 1× 1× 839× 268× 2469× 2469× 2469× 2469× 2469× 2469× 549× 549× 4411× 4411× 4411× 549× 549× 1098× 1098× 8822× 8822× 8822× 8822× 8822× 1098× 1098× 822× 822× 822× 1× 168× 168× 168× 1× 671× 671× 671× 671× 671× 671× 5926× 15306× 671× 1× 69× 69× 69× 69× 69× 69× 69× 23× 23× 1× 274× 274× 274× 274× 210× 210× 210× 210× 210× 210× 210× 210× 210× 210× 210× 742× 742× 742× 742× 742× 742× 742× 742× 742× 210× 210× 40× 40× 130× 40× 40× 40× 40× 130× 130× 78× 227× 227× 227× 227× 227× 227× 2104× 6312× 227× 7× 101× 101× 101× 38× 63× 63× 63× 567× 567× 567× 199× 199× 63× 63× 35× 35× 35× 35× 35× 23× 23× 23× 69× 23× 23× 35× 35× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 69× 69× 69× 69× 69× 69× 69× 69× 69× 23× 23× 23× 23× 23× 23× 23× 23× 23× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 1540× 1540× 1540× 1540× 1540× 1540× 1540× 1540× 1540× 1540× 1540× 4620× 4620× 4620× 10× 1490× 1490× 1490× 1490× 1490× 10× 5× 5× 5× 5× 5× 5× 5× 5× 5× 5× 5× 5× 5× 5× 5× 5× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 5682× 5682× 5682× 5× 5× 5× 5× 5× 5× 5× 5× 5× 5× 5× 5× 5× 5× 1173× 1173× 5× 5× 5× 5× 5× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 1894× 5× 5× 5× 5× 1173× 1173× 1173× 1173× 1173× 1173× 1173× 1173× 1173× 1173× 1173× 1173× 5× 20× 20× 15× 15× 15× 15× 15× 15× 15× 45× 45× 45× 15× 15× 17046× 17046× 51138× 51138× 51138× 51138× 136368× 17046× 17046× 15× 5× 5× 5× 5× 5× 5× 5× 5× 5× 1894× 5682× 5682× 5× 210× 210× 210× 210× 210× 210× 210× 210× 210× 210× 210× 210× 210× 210× 210× 210× 1× 1× 1× 1× 1× 1× 1× 3× 3× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 820× 820× 820× 1× 1× 1× 1× 1× 1× 1× | import Base from './core/Base'; import glenum from './core/glenum'; import Cache from './core/Cache'; import vendor from './core/vendor'; import glMatrix from './dep/glmatrix'; import BoundingBox from './math/BoundingBox'; var vec3 = glMatrix.vec3; var mat4 = glMatrix.mat4; var vec3Create = vec3.create; var vec3Add = vec3.add; var vec3Set = vec3.set; function getArrayCtorByType (type) { return ({ 'byte': vendor.Int8Array, 'ubyte': vendor.Uint8Array, 'short': vendor.Int16Array, 'ushort': vendor.Uint16Array })[type] || vendor.Float32Array; } function makeAttrKey(attrName) { return 'attr_' + attrName; } /** * Geometry attribute * @alias clay.Geometry.Attribute * @constructor */ function Attribute(name, type, size, semantic) { /** * Attribute name * @type {string} */ this.name = name; /** * Attribute type * Possible values: * + `'byte'` * + `'ubyte'` * + `'short'` * + `'ushort'` * + `'float'` Most commonly used. * @type {string} */ this.type = type; /** * Size of attribute component. 1 - 4. * @type {number} */ this.size = size; /** * Semantic of this attribute. * Possible values: * + `'POSITION'` * + `'NORMAL'` * + `'BINORMAL'` * + `'TANGENT'` * + `'TEXCOORD'` * + `'TEXCOORD_0'` * + `'TEXCOORD_1'` * + `'COLOR'` * + `'JOINT'` * + `'WEIGHT'` * * In shader, attribute with same semantic will be automatically mapped. For example: * ```glsl * attribute vec3 pos: POSITION * ``` * will use the attribute value with semantic POSITION in geometry, no matter what name it used. * @type {string} */ this.semantic = semantic || ''; /** * Value of the attribute. * @type {TypedArray} */ this.value = null; // Init getter setter switch (size) { case 1: this.get = function (idx) { return this.value[idx]; }; this.set = function (idx, value) { this.value[idx] = value; }; // Copy from source to target this.copy = function (target, source) { this.value[target] = this.value[target]; }; break; case 2: this.get = function (idx, out) { var arr = this.value; out[0] = arr[idx * 2]; out[1] = arr[idx * 2 + 1]; return out; }; this.set = function (idx, val) { var arr = this.value; arr[idx * 2] = val[0]; arr[idx * 2 + 1] = val[1]; }; this.copy = function (target, source) { var arr = this.value; source *= 2; target *= 2; arr[target] = arr[source]; arr[target + 1] = arr[source + 1]; }; break; case 3: this.get = function (idx, out) { var idx3 = idx * 3; var arr = this.value; out[0] = arr[idx3]; out[1] = arr[idx3 + 1]; out[2] = arr[idx3 + 2]; return out; }; this.set = function (idx, val) { var idx3 = idx * 3; var arr = this.value; arr[idx3] = val[0]; arr[idx3 + 1] = val[1]; arr[idx3 + 2] = val[2]; }; this.copy = function (target, source) { var arr = this.value; source *= 3; target *= 3; arr[target] = arr[source]; arr[target + 1] = arr[source + 1]; arr[target + 2] = arr[source + 2]; }; break; case 4: this.get = function (idx, out) { var arr = this.value; var idx4 = idx * 4; out[0] = arr[idx4]; out[1] = arr[idx4 + 1]; out[2] = arr[idx4 + 2]; out[3] = arr[idx4 + 3]; return out; }; this.set = function (idx, val) { var arr = this.value; var idx4 = idx * 4; arr[idx4] = val[0]; arr[idx4 + 1] = val[1]; arr[idx4 + 2] = val[2]; arr[idx4 + 3] = val[3]; }; this.copy = function (target, source) { var arr = this.value; source *= 4; target *= 4; // copyWithin is extremely slow arr[target] = arr[source]; arr[target + 1] = arr[source + 1]; arr[target + 2] = arr[source + 2]; arr[target + 3] = arr[source + 3]; }; } } /** * Set item value at give index. Second parameter val is number if size is 1 * @function * @name clay.Geometry.Attribute#set * @param {number} idx * @param {number[]|number} val * @example * geometry.getAttribute('position').set(0, [1, 1, 1]); */ /** * Get item value at give index. Second parameter out is no need if size is 1 * @function * @name clay.Geometry.Attribute#set * @param {number} idx * @param {number[]} [out] * @example * geometry.getAttribute('position').get(0, out); */ /** * Initialize attribute with given vertex count * @param {number} nVertex */ Attribute.prototype.init = function (nVertex) { Eif (!this.value || this.value.length != nVertex * this.size) { var ArrayConstructor = getArrayCtorByType(this.type); this.value = new ArrayConstructor(nVertex * this.size); } }; /** * Initialize attribute with given array. Which can be 1 dimensional or 2 dimensional * @param {Array} array * @example * geometry.getAttribute('position').fromArray( * [-1, 0, 0, 1, 0, 0, 0, 1, 0] * ); * geometry.getAttribute('position').fromArray( * [ [-1, 0, 0], [1, 0, 0], [0, 1, 0] ] * ); */ Attribute.prototype.fromArray = function (array) { var ArrayConstructor = getArrayCtorByType(this.type); var value; // Convert 2d array to flat Eif (array[0] && (array[0].length)) { var n = 0; var size = this.size; value = new ArrayConstructor(array.length * size); for (var i = 0; i < array.length; i++) { for (var j = 0; j < size; j++) { value[n++] = array[i][j]; } } } else { value = new ArrayConstructor(array); } this.value = value; }; Attribute.prototype.clone = function(copyValue) { var ret = new Attribute(this.name, this.type, this.size, this.semantic); // FIXME if (copyValue) { console.warn('todo'); } return ret; }; function AttributeBuffer(name, type, buffer, size, semantic) { this.name = name; this.type = type; this.buffer = buffer; this.size = size; this.semantic = semantic; // To be set in mesh // symbol in the shader this.symbol = ''; // Needs remove flag this.needsRemove = false; } function IndicesBuffer(buffer) { this.buffer = buffer; this.count = 0; } /** * Geometry in ClayGL contains vertex attributes of mesh. These vertex attributes will be finally provided to the {@link clay.Shader}. * Different {@link clay.Shader} needs different attributes. Here is a list of attributes used in the builtin shaders. * * + position: `clay.basic`, `clay.lambert`, `clay.standard` * + texcoord0: `clay.basic`, `clay.lambert`, `clay.standard` * + color: `clay.basic`, `clay.lambert`, `clay.standard` * + weight: `clay.basic`, `clay.lambert`, `clay.standard` * + joint: `clay.basic`, `clay.lambert`, `clay.standard` * + normal: `clay.lambert`, `clay.standard` * + tangent: `clay.standard` * * #### Create a procedural geometry * * ClayGL provides a couple of builtin procedural geometries. Inlcuding: * * + {@link clay.geometry.Cube} * + {@link clay.geometry.Sphere} * + {@link clay.geometry.Plane} * + {@link clay.geometry.Cylinder} * + {@link clay.geometry.Cone} * + {@link clay.geometry.ParametricSurface} * * It's simple to create a basic geometry with these classes. * ```js var sphere = new clay.geometry.Sphere({ radius: 2 }); ``` * * #### Create the geometry data by yourself * * Usually the vertex attributes data are created by the {@link clay.loader.GLTF} or procedural geometries like {@link clay.geometry.Sphere}. * Besides these, you can create the data manually. Here is a simple example to create a triangle. ```js var TRIANGLE_POSITIONS = [ [-0.5, -0.5, 0], [0.5, -0.5, 0], [0, 0.5, 0] ]; var geometry = new clay.StaticGeometry(); // Add triangle vertices to position attribute. geometry.attributes.position.fromArray(TRIANGLE_POSITIONS); ``` * Then you can use the utility methods like `generateVertexNormals`, `generateTangents` to create the remaining necessary attributes. * * * #### Use with custom shaders * * If you wan't to write custom shaders. Don't forget to add SEMANTICS to these attributes. For example * ```glsl uniform mat4 worldViewProjection : WORLDVIEWPROJECTION; uniform mat4 worldInverseTranspose : WORLDINVERSETRANSPOSE; uniform mat4 world : WORLD; attribute vec3 position : POSITION; attribute vec2 texcoord : TEXCOORD_0; attribute vec3 normal : NORMAL; ``` * These `POSITION`, `TEXCOORD_0`, `NORMAL` are SEMANTICS which will map the attributes in shader to the attributes in the Geometry * * Available attributes SEMANTICS includes `POSITION`, `TEXCOORD_0`, `TEXCOORD_1` `NORMAL`, `TANGENT`, `COLOR`, `WEIGHT`, `JOINT`. * * * @constructor clay.Geometry * @extends clay.core.Base */ var Geometry = Base.extend(function () { return /** @lends clay.Geometry# */ { /** * Attributes of geometry. Including: * + `position` * + `texcoord0` * + `texcoord1` * + `normal` * + `tangent` * + `color` * + `weight` * + `joint` * + `barycentric` * * @type {Object.<string, clay.Geometry.Attribute>} */ attributes: { position: new Attribute('position', 'float', 3, 'POSITION'), texcoord0: new Attribute('texcoord0', 'float', 2, 'TEXCOORD_0'), texcoord1: new Attribute('texcoord1', 'float', 2, 'TEXCOORD_1'), normal: new Attribute('normal', 'float', 3, 'NORMAL'), tangent: new Attribute('tangent', 'float', 4, 'TANGENT'), color: new Attribute('color', 'float', 4, 'COLOR'), // Skinning attributes // Each vertex can be bind to 4 bones, because the // sum of weights is 1, so the weights is stored in vec3 and the last // can be calculated by 1-w.x-w.y-w.z weight: new Attribute('weight', 'float', 3, 'WEIGHT'), joint: new Attribute('joint', 'float', 4, 'JOINT'), // For wireframe display // http://codeflow.org/entries/2012/aug/02/easy-wireframe-display-with-barycentric-coordinates/ barycentric: new Attribute('barycentric', 'float', 3, null), }, /** * Calculated bounding box of geometry. * @type {clay.BoundingBox} */ boundingBox: null, /** * Indices of geometry. * @type {Uint16Array|Uint32Array} */ indices: null, /** * Is vertices data dynamically updated. * Attributes value can't be changed after first render if dyanmic is false. * @type {boolean} */ dynamic: true, _enabledAttributes: null, // PENDING // Init it here to avoid deoptimization when it's assigned in application dynamically __used: 0 }; }, function() { // Use cache this._cache = new Cache(); this._attributeList = Object.keys(this.attributes); this.__vaoCache = {}; }, /** @lends clay.Geometry.prototype */ { /** * Main attribute will be used to count vertex number * @type {string} */ mainAttribute: 'position', /** * User defined picking algorithm instead of default * triangle ray intersection * x, y are NDC. * ```typescript * (x, y, renderer, camera, renderable, out) => boolean * ``` * @type {?Function} */ pick: null, /** * User defined ray picking algorithm instead of default * triangle ray intersection * ```typescript * (ray: clay.Ray, renderable: clay.Renderable, out: Array) => boolean * ``` * @type {?Function} */ pickByRay: null, /** * Update boundingBox of Geometry */ updateBoundingBox: function () { var bbox = this.boundingBox; Iif (!bbox) { bbox = this.boundingBox = new BoundingBox(); } var posArr = this.attributes.position.value; Eif (posArr && posArr.length) { var min = bbox.min; var max = bbox.max; var minArr = min.array; var maxArr = max.array; vec3.set(minArr, posArr[0], posArr[1], posArr[2]); vec3.set(maxArr, posArr[0], posArr[1], posArr[2]); for (var i = 3; i < posArr.length;) { var x = posArr[i++]; var y = posArr[i++]; var z = posArr[i++]; if (x < minArr[0]) { minArr[0] = x; } Iif (y < minArr[1]) { minArr[1] = y; } if (z < minArr[2]) { minArr[2] = z; } if (x > maxArr[0]) { maxArr[0] = x; } if (y > maxArr[1]) { maxArr[1] = y; } if (z > maxArr[2]) { maxArr[2] = z; } } min._dirty = true; max._dirty = true; } }, /** * Mark attributes and indices in geometry needs to update. * Usually called after you change the data in attributes. */ dirty: function () { var enabledAttributes = this.getEnabledAttributes(); for (var i = 0; i < enabledAttributes.length; i++) { this.dirtyAttribute(enabledAttributes[i]); } this.dirtyIndices(); this._enabledAttributes = null; this._cache.dirty('any'); }, /** * Mark the indices needs to update. */ dirtyIndices: function () { this._cache.dirtyAll('indices'); }, /** * Mark the attributes needs to update. * @param {string} [attrName] */ dirtyAttribute: function (attrName) { this._cache.dirtyAll(makeAttrKey(attrName)); this._cache.dirtyAll('attributes'); }, /** * Get indices of triangle at given index. * @param {number} idx * @param {Array.<number>} out * @return {Array.<number>} */ getTriangleIndices: function (idx, out) { if (idx < this.triangleCount && idx >= 0) { if (!out) { out = vec3Create(); } var indices = this.indices; out[0] = indices[idx * 3]; out[1] = indices[idx * 3 + 1]; out[2] = indices[idx * 3 + 2]; return out; } }, /** * Set indices of triangle at given index. * @param {number} idx * @param {Array.<number>} arr */ setTriangleIndices: function (idx, arr) { var indices = this.indices; indices[idx * 3] = arr[0]; indices[idx * 3 + 1] = arr[1]; indices[idx * 3 + 2] = arr[2]; }, isUseIndices: function () { return !!this.indices; }, /** * Initialize indices from an array. * @param {Array} array */ initIndicesFromArray: function (array) { var value; var ArrayConstructor = this.vertexCount > 0xffff ? vendor.Uint32Array : vendor.Uint16Array; // Convert 2d array to flat Eif (array[0] && (array[0].length)) { var n = 0; var size = 3; value = new ArrayConstructor(array.length * size); for (var i = 0; i < array.length; i++) { for (var j = 0; j < size; j++) { value[n++] = array[i][j]; } } } else { value = new ArrayConstructor(array); } this.indices = value; }, /** * Create a new attribute * @param {string} name * @param {string} type * @param {number} size * @param {string} [semantic] */ createAttribute: function (name, type, size, semantic) { var attrib = new Attribute(name, type, size, semantic); if (this.attributes[name]) { this.removeAttribute(name); } this.attributes[name] = attrib; this._attributeList.push(name); return attrib; }, /** * Remove attribute * @param {string} name */ removeAttribute: function (name) { var attributeList = this._attributeList; var idx = attributeList.indexOf(name); if (idx >= 0) { attributeList.splice(idx, 1); delete this.attributes[name]; return true; } return false; }, /** * Get attribute * @param {string} name * @return {clay.Geometry.Attribute} */ getAttribute: function (name) { return this.attributes[name]; }, /** * Get enabled attributes name list * Attribute which has the same vertex number with position is treated as a enabled attribute * @return {string[]} */ getEnabledAttributes: function () { var enabledAttributes = this._enabledAttributes; var attributeList = this._attributeList; // Cache if (enabledAttributes) { return enabledAttributes; } var result = []; var nVertex = this.vertexCount; for (var i = 0; i < attributeList.length; i++) { var name = attributeList[i]; var attrib = this.attributes[name]; if (attrib.value) { Eif (attrib.value.length === nVertex * attrib.size) { result.push(name); } } } this._enabledAttributes = result; return result; }, getBufferChunks: function (renderer) { var cache = this._cache; cache.use(renderer.__uid__); var isAttributesDirty = cache.isDirty('attributes'); var isIndicesDirty = cache.isDirty('indices'); if (isAttributesDirty || isIndicesDirty) { this._updateBuffer(renderer.gl, isAttributesDirty, isIndicesDirty); var enabledAttributes = this.getEnabledAttributes(); for (var i = 0; i < enabledAttributes.length; i++) { cache.fresh(makeAttrKey(enabledAttributes[i])); } cache.fresh('attributes'); cache.fresh('indices'); } cache.fresh('any'); return cache.get('chunks'); }, _updateBuffer: function (_gl, isAttributesDirty, isIndicesDirty) { var cache = this._cache; var chunks = cache.get('chunks'); var firstUpdate = false; Eif (!chunks) { chunks = []; // Intialize chunks[0] = { attributeBuffers: [], indicesBuffer: null }; cache.put('chunks', chunks); firstUpdate = true; } var chunk = chunks[0]; var attributeBuffers = chunk.attributeBuffers; var indicesBuffer = chunk.indicesBuffer; Eif (isAttributesDirty || firstUpdate) { var attributeList = this.getEnabledAttributes(); var attributeBufferMap = {}; Iif (!firstUpdate) { for (var i = 0; i < attributeBuffers.length; i++) { attributeBufferMap[attributeBuffers[i].name] = attributeBuffers[i]; } } // FIXME If some attributes removed for (var k = 0; k < attributeList.length; k++) { var name = attributeList[k]; var attribute = this.attributes[name]; var bufferInfo; Iif (!firstUpdate) { bufferInfo = attributeBufferMap[name]; } var buffer; Iif (bufferInfo) { buffer = bufferInfo.buffer; } else { buffer = _gl.createBuffer(); } Eif (cache.isDirty(makeAttrKey(name))) { // Only update when they are dirty. // TODO: Use BufferSubData? _gl.bindBuffer(_gl.ARRAY_BUFFER, buffer); _gl.bufferData(_gl.ARRAY_BUFFER, attribute.value, this.dynamic ? glenum.DYNAMIC_DRAW : glenum.STATIC_DRAW); } attributeBuffers[k] = new AttributeBuffer(name, attribute.type, buffer, attribute.size, attribute.semantic); } // Remove unused attributes buffers. // PENDING for (var i = k; i < attributeBuffers.length; i++) { _gl.deleteBuffer(attributeBuffers[i].buffer); } attributeBuffers.length = k; } Eif (this.isUseIndices() && (isIndicesDirty || firstUpdate)) { Eif (!indicesBuffer) { indicesBuffer = new IndicesBuffer(_gl.createBuffer()); chunk.indicesBuffer = indicesBuffer; } indicesBuffer.count = this.indices.length; _gl.bindBuffer(_gl.ELEMENT_ARRAY_BUFFER, indicesBuffer.buffer); _gl.bufferData(_gl.ELEMENT_ARRAY_BUFFER, this.indices, this.dynamic ? glenum.DYNAMIC_DRAW : glenum.STATIC_DRAW); } }, /** * Generate normals per vertex. */ generateVertexNormals: function () { Iif (!this.vertexCount) { return; } var indices = this.indices; var attributes = this.attributes; var positions = attributes.position.value; var normals = attributes.normal.value; Eif (!normals || normals.length !== positions.length) { normals = attributes.normal.value = new vendor.Float32Array(positions.length); } else { // Reset for (var i = 0; i < normals.length; i++) { normals[i] = 0; } } var p1 = vec3Create(); var p2 = vec3Create(); var p3 = vec3Create(); var v21 = vec3Create(); var v32 = vec3Create(); var n = vec3Create(); var len = indices ? indices.length : this.vertexCount; var i1, i2, i3; for (var f = 0; f < len;) { Eif (indices) { i1 = indices[f++]; i2 = indices[f++]; i3 = indices[f++]; } else { i1 = f++; i2 = f++; i3 = f++; } vec3Set(p1, positions[i1*3], positions[i1*3+1], positions[i1*3+2]); vec3Set(p2, positions[i2*3], positions[i2*3+1], positions[i2*3+2]); vec3Set(p3, positions[i3*3], positions[i3*3+1], positions[i3*3+2]); vec3.sub(v21, p1, p2); vec3.sub(v32, p2, p3); vec3.cross(n, v21, v32); // Already be weighted by the triangle area for (var i = 0; i < 3; i++) { normals[i1*3+i] = normals[i1*3+i] + n[i]; normals[i2*3+i] = normals[i2*3+i] + n[i]; normals[i3*3+i] = normals[i3*3+i] + n[i]; } } for (var i = 0; i < normals.length;) { vec3Set(n, normals[i], normals[i+1], normals[i+2]); vec3.normalize(n, n); normals[i++] = n[0]; normals[i++] = n[1]; normals[i++] = n[2]; } this.dirty(); }, /** * Generate normals per face. */ generateFaceNormals: function () { Iif (!this.vertexCount) { return; } Eif (!this.isUniqueVertex()) { this.generateUniqueVertex(); } var indices = this.indices; var attributes = this.attributes; var positions = attributes.position.value; var normals = attributes.normal.value; var p1 = vec3Create(); var p2 = vec3Create(); var p3 = vec3Create(); var v21 = vec3Create(); var v32 = vec3Create(); var n = vec3Create(); Iif (!normals) { normals = attributes.normal.value = new Float32Array(positions.length); } var len = indices ? indices.length : this.vertexCount; var i1, i2, i3; for (var f = 0; f < len;) { Eif (indices) { i1 = indices[f++]; i2 = indices[f++]; i3 = indices[f++]; } else { i1 = f++; i2 = f++; i3 = f++; } vec3Set(p1, positions[i1*3], positions[i1*3+1], positions[i1*3+2]); vec3Set(p2, positions[i2*3], positions[i2*3+1], positions[i2*3+2]); vec3Set(p3, positions[i3*3], positions[i3*3+1], positions[i3*3+2]); vec3.sub(v21, p1, p2); vec3.sub(v32, p2, p3); vec3.cross(n, v21, v32); vec3.normalize(n, n); for (var i = 0; i < 3; i++) { normals[i1*3 + i] = n[i]; normals[i2*3 + i] = n[i]; normals[i3*3 + i] = n[i]; } } this.dirty(); }, /** * Generate tangents attributes. */ generateTangents: function () { Iif (!this.vertexCount) { return; } var nVertex = this.vertexCount; var attributes = this.attributes; Eif (!attributes.tangent.value) { attributes.tangent.value = new Float32Array(nVertex * 4); } var texcoords = attributes.texcoord0.value; var positions = attributes.position.value; var tangents = attributes.tangent.value; var normals = attributes.normal.value; Iif (!texcoords) { console.warn('Geometry without texcoords can\'t generate tangents.'); return; } var tan1 = []; var tan2 = []; for (var i = 0; i < nVertex; i++) { tan1[i] = [0.0, 0.0, 0.0]; tan2[i] = [0.0, 0.0, 0.0]; } var sdir = [0.0, 0.0, 0.0]; var tdir = [0.0, 0.0, 0.0]; var indices = this.indices; var len = indices ? indices.length : this.vertexCount; var i1, i2, i3; for (var i = 0; i < len;) { Eif (indices) { i1 = indices[i++]; i2 = indices[i++]; i3 = indices[i++]; } else { i1 = i++; i2 = i++; i3 = i++; } var st1s = texcoords[i1 * 2], st2s = texcoords[i2 * 2], st3s = texcoords[i3 * 2], st1t = texcoords[i1 * 2 + 1], st2t = texcoords[i2 * 2 + 1], st3t = texcoords[i3 * 2 + 1], p1x = positions[i1 * 3], p2x = positions[i2 * 3], p3x = positions[i3 * 3], p1y = positions[i1 * 3 + 1], p2y = positions[i2 * 3 + 1], p3y = positions[i3 * 3 + 1], p1z = positions[i1 * 3 + 2], p2z = positions[i2 * 3 + 2], p3z = positions[i3 * 3 + 2]; var x1 = p2x - p1x, x2 = p3x - p1x, y1 = p2y - p1y, y2 = p3y - p1y, z1 = p2z - p1z, z2 = p3z - p1z; var s1 = st2s - st1s, s2 = st3s - st1s, t1 = st2t - st1t, t2 = st3t - st1t; var r = 1.0 / (s1 * t2 - t1 * s2); sdir[0] = (t2 * x1 - t1 * x2) * r; sdir[1] = (t2 * y1 - t1 * y2) * r; sdir[2] = (t2 * z1 - t1 * z2) * r; tdir[0] = (s1 * x2 - s2 * x1) * r; tdir[1] = (s1 * y2 - s2 * y1) * r; tdir[2] = (s1 * z2 - s2 * z1) * r; vec3Add(tan1[i1], tan1[i1], sdir); vec3Add(tan1[i2], tan1[i2], sdir); vec3Add(tan1[i3], tan1[i3], sdir); vec3Add(tan2[i1], tan2[i1], tdir); vec3Add(tan2[i2], tan2[i2], tdir); vec3Add(tan2[i3], tan2[i3], tdir); } var tmp = vec3Create(); var nCrossT = vec3Create(); var n = vec3Create(); for (var i = 0; i < nVertex; i++) { n[0] = normals[i * 3]; n[1] = normals[i * 3 + 1]; n[2] = normals[i * 3 + 2]; var t = tan1[i]; // Gram-Schmidt orthogonalize vec3.scale(tmp, n, vec3.dot(n, t)); vec3.sub(tmp, t, tmp); vec3.normalize(tmp, tmp); // Calculate handedness. vec3.cross(nCrossT, n, t); tangents[i * 4] = tmp[0]; tangents[i * 4 + 1] = tmp[1]; tangents[i * 4 + 2] = tmp[2]; // PENDING can config ? tangents[i * 4 + 3] = vec3.dot(nCrossT, tan2[i]) < 0.0 ? -1.0 : 1.0; } this.dirty(); }, /** * If vertices are not shared by different indices. */ isUniqueVertex: function () { Eif (this.isUseIndices()) { return this.vertexCount === this.indices.length; } else { return true; } }, /** * Create a unique vertex for each index. */ generateUniqueVertex: function () { Iif (!this.vertexCount || !this.indices) { return; } Iif (this.indices.length > 0xffff) { this.indices = new vendor.Uint32Array(this.indices); } var attributes = this.attributes; var indices = this.indices; var attributeNameList = this.getEnabledAttributes(); var oldAttrValues = {}; for (var a = 0; a < attributeNameList.length; a++) { var name = attributeNameList[a]; oldAttrValues[name] = attributes[name].value; attributes[name].init(this.indices.length); } var cursor = 0; for (var i = 0; i < indices.length; i++) { var ii = indices[i]; for (var a = 0; a < attributeNameList.length; a++) { var name = attributeNameList[a]; var array = attributes[name].value; var size = attributes[name].size; for (var k = 0; k < size; k++) { array[cursor * size + k] = oldAttrValues[name][ii * size + k]; } } indices[i] = cursor; cursor++; } this.dirty(); }, /** * Generate barycentric coordinates for wireframe draw. */ generateBarycentric: function () { Iif (!this.vertexCount) { return; } Eif (!this.isUniqueVertex()) { this.generateUniqueVertex(); } var attributes = this.attributes; var array = attributes.barycentric.value; var indices = this.indices; // Already existed; Iif (array && array.length === indices.length * 3) { return; } array = attributes.barycentric.value = new Float32Array(indices.length * 3); for (var i = 0; i < (indices ? indices.length : this.vertexCount / 3);) { for (var j = 0; j < 3; j++) { var ii = indices ? indices[i++] : (i * 3 + j); array[ii * 3 + j] = 1; } } this.dirty(); }, /** * Apply transform to geometry attributes. * @param {clay.Matrix4} matrix */ applyTransform: function (matrix) { var attributes = this.attributes; var positions = attributes.position.value; var normals = attributes.normal.value; var tangents = attributes.tangent.value; matrix = matrix.array; // Normal Matrix var inverseTransposeMatrix = mat4.create(); mat4.invert(inverseTransposeMatrix, matrix); mat4.transpose(inverseTransposeMatrix, inverseTransposeMatrix); var vec3TransformMat4 = vec3.transformMat4; var vec3ForEach = vec3.forEach; vec3ForEach(positions, 3, 0, null, vec3TransformMat4, matrix); Eif (normals) { vec3ForEach(normals, 3, 0, null, vec3TransformMat4, inverseTransposeMatrix); } Iif (tangents) { vec3ForEach(tangents, 4, 0, null, vec3TransformMat4, inverseTransposeMatrix); } Eif (this.boundingBox) { this.updateBoundingBox(); } }, /** * Dispose geometry data in GL context. * @param {clay.Renderer} renderer */ dispose: function (renderer) { var cache = this._cache; cache.use(renderer.__uid__); var chunks = cache.get('chunks'); Eif (chunks) { for (var c = 0; c < chunks.length; c++) { var chunk = chunks[c]; for (var k = 0; k < chunk.attributeBuffers.length; k++) { var attribs = chunk.attributeBuffers[k]; renderer.gl.deleteBuffer(attribs.buffer); } Eif (chunk.indicesBuffer) { renderer.gl.deleteBuffer(chunk.indicesBuffer.buffer); } } } Eif (this.__vaoCache) { var vaoExt = renderer.getGLExtension('OES_vertex_array_object'); for (var id in this.__vaoCache) { var vao = this.__vaoCache[id].vao; Eif (vao) { vaoExt.deleteVertexArrayOES(vao); } } } this.__vaoCache = {}; cache.deleteContext(renderer.__uid__); } }); Eif (Object.defineProperty) { /** * @name clay.Geometry#vertexCount * @type {number} * @readOnly */ Object.defineProperty(Geometry.prototype, 'vertexCount', { enumerable: false, get: function () { var mainAttribute = this.attributes[this.mainAttribute]; Iif (!mainAttribute || !mainAttribute.value) { return 0; } return mainAttribute.value.length / mainAttribute.size; } }); /** * @name clay.Geometry#triangleCount * @type {number} * @readOnly */ Object.defineProperty(Geometry.prototype, 'triangleCount', { enumerable: false, get: function () { var indices = this.indices; if (!indices) { return 0; } else { return indices.length / 3; } } }); } Geometry.STATIC_DRAW = glenum.STATIC_DRAW; Geometry.DYNAMIC_DRAW = glenum.DYNAMIC_DRAW; Geometry.STREAM_DRAW = glenum.STREAM_DRAW; Geometry.AttributeBuffer = AttributeBuffer; Geometry.IndicesBuffer = IndicesBuffer; Geometry.Attribute = Attribute; export default Geometry; |