UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

78 lines (77 loc) 2.34 kB
import ProgressiveQuickSort from "../ProgressiveQuickSort.mjs"; import glmatrix from "../../../../claygl/src/glmatrix/index.mjs"; var vec3 = glmatrix.vec3; var verticesSortMixin = { needsSortVertices: function() { return this.sortVertices; }, needsSortVerticesProgressively: function() { return this.needsSortVertices() && this.vertexCount >= 2e4; }, doSortVertices: function(cameraPos, frame) { var indices = this.indices; var p = vec3.create(); if (!indices) { indices = this.indices = this.vertexCount > 65535 ? new Uint32Array(this.vertexCount) : new Uint16Array(this.vertexCount); for (var i = 0; i < indices.length; i++) { indices[i] = i; } } if (frame === 0) { var posAttr = this.attributes.position; var cameraPos = cameraPos.array; var noneCount = 0; if (!this._zList || this._zList.length !== this.vertexCount) { this._zList = new Float32Array(this.vertexCount); } var firstZ; for (var i = 0; i < this.vertexCount; i++) { posAttr.get(i, p); var z = vec3.sqrDist(p, cameraPos); if (isNaN(z)) { z = 1e7; noneCount++; } if (i === 0) { firstZ = z; z = 0; } else { z = z - firstZ; } this._zList[i] = z; } this._noneCount = noneCount; } if (this.vertexCount < 2e4) { if (frame === 0) { this._simpleSort(this._noneCount / this.vertexCount > 0.05); } } else { for (var i = 0; i < 3; i++) { this._progressiveQuickSort(frame * 3 + i); } } this.dirtyIndices(); }, _simpleSort: function(useNativeQuickSort) { var zList = this._zList; var indices = this.indices; function compare(a, b) { return zList[b] - zList[a]; } if (useNativeQuickSort) { Array.prototype.sort.call(indices, compare); } else { ProgressiveQuickSort.sort(indices, compare, 0, indices.length - 1); } }, _progressiveQuickSort: function(frame) { var zList = this._zList; var indices = this.indices; this._quickSort = this._quickSort || new ProgressiveQuickSort(); this._quickSort.step(indices, function(a, b) { return zList[b] - zList[a]; }, frame); } }; export { verticesSortMixin as default };