|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +import defined from "../Core/defined.js"; |
| 4 | +import Cartesian3 from "../Core/Cartesian3.js"; |
| 5 | +import BufferPoint from "./BufferPoint.js"; |
| 6 | +import Buffer from "../Renderer/Buffer.js"; |
| 7 | +import BufferUsage from "../Renderer/BufferUsage.js"; |
| 8 | +import VertexArray from "../Renderer/VertexArray.js"; |
| 9 | +import ComponentDatatype from "../Core/ComponentDatatype.js"; |
| 10 | +import RenderState from "../Renderer/RenderState.js"; |
| 11 | +import BlendingState from "./BlendingState.js"; |
| 12 | +import Color from "../Core/Color.js"; |
| 13 | +import ShaderSource from "../Renderer/ShaderSource.js"; |
| 14 | +import ShaderProgram from "../Renderer/ShaderProgram.js"; |
| 15 | +import DrawCommand from "../Renderer/DrawCommand.js"; |
| 16 | +import Pass from "../Renderer/Pass.js"; |
| 17 | +import PrimitiveType from "../Core/PrimitiveType.js"; |
| 18 | +import BufferPointCollectionVS from "../Shaders/BufferPointCollectionVS.js"; |
| 19 | +import BufferPointCollectionFS from "../Shaders/BufferPointCollectionFS.js"; |
| 20 | +import EncodedCartesian3 from "../Core/EncodedCartesian3.js"; |
| 21 | +import AttributeCompression from "../Core/AttributeCompression.js"; |
| 22 | + |
| 23 | +/** @import FrameState from "./FrameState.js"; */ |
| 24 | +/** @import BufferPointCollection from "./BufferPointCollection.js"; */ |
| 25 | +/** @import {TypedArray, TypedArrayConstructor} from "../Core/globalTypes.js"; */ |
| 26 | + |
| 27 | +/** @type {{positionHighAndShow: number, positionLowAndColor: number}} */ |
| 28 | +const BufferPointAttributeLocations = { |
| 29 | + /** @type {number} */ |
| 30 | + positionHighAndShow: 0, |
| 31 | + /** @type {number} */ |
| 32 | + positionLowAndColor: 1, |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * @typedef {object} BufferPointRenderContext |
| 37 | + * @property {VertexArray} [vertexArray] |
| 38 | + * @property {Record<number, TypedArray>} [attributeArrays] |
| 39 | + * @property {RenderState} [renderState] |
| 40 | + * @property {ShaderProgram} [shaderProgram] |
| 41 | + * @property {object} [uniformMap] |
| 42 | + * @ignore |
| 43 | + */ |
| 44 | + |
| 45 | +/** |
| 46 | + * @param {BufferPointCollection} collection |
| 47 | + * @param {FrameState} frameState |
| 48 | + * @param {BufferPointRenderContext} [renderContext] |
| 49 | + * @returns {BufferPointRenderContext} |
| 50 | + * @ignore |
| 51 | + */ |
| 52 | +function renderBufferPointCollection(collection, frameState, renderContext) { |
| 53 | + const context = frameState.context; |
| 54 | + renderContext = renderContext || {}; |
| 55 | + |
| 56 | + if (!defined(renderContext.attributeArrays)) { |
| 57 | + const featureCountMax = collection.primitiveCountMax; |
| 58 | + const positionHighAndShowArray = new Float32Array(featureCountMax * 4); |
| 59 | + const positionLowAndColorArray = new Float32Array(featureCountMax * 4); |
| 60 | + |
| 61 | + renderContext.attributeArrays = { |
| 62 | + [BufferPointAttributeLocations.positionHighAndShow]: |
| 63 | + positionHighAndShowArray, |
| 64 | + [BufferPointAttributeLocations.positionLowAndColor]: |
| 65 | + positionLowAndColorArray, |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + if (collection._dirtyCount > 0) { |
| 70 | + const point = new BufferPoint(); |
| 71 | + const color = new Color(); |
| 72 | + const cartesian = new Cartesian3(); |
| 73 | + const encodedCartesian = new EncodedCartesian3(); |
| 74 | + |
| 75 | + const positionHighAndShowArray = |
| 76 | + renderContext.attributeArrays[ |
| 77 | + BufferPointAttributeLocations.positionHighAndShow |
| 78 | + ]; |
| 79 | + const positionLowAndColorArray = |
| 80 | + renderContext.attributeArrays[ |
| 81 | + BufferPointAttributeLocations.positionLowAndColor |
| 82 | + ]; |
| 83 | + |
| 84 | + const { _dirtyOffset, _dirtyCount } = collection; |
| 85 | + |
| 86 | + for (let i = _dirtyOffset, il = _dirtyOffset + _dirtyCount; i < il; i++) { |
| 87 | + collection.get(i, point); |
| 88 | + |
| 89 | + if (!point._dirty) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + point.getPosition(cartesian); |
| 94 | + EncodedCartesian3.fromCartesian(cartesian, encodedCartesian); |
| 95 | + |
| 96 | + point.getColor(color); |
| 97 | + |
| 98 | + positionHighAndShowArray[i * 4] = encodedCartesian.high.x; |
| 99 | + positionHighAndShowArray[i * 4 + 1] = encodedCartesian.high.y; |
| 100 | + positionHighAndShowArray[i * 4 + 2] = encodedCartesian.high.z; |
| 101 | + positionHighAndShowArray[i * 4 + 3] = point.show ? 1 : 0; |
| 102 | + |
| 103 | + positionLowAndColorArray[i * 4] = encodedCartesian.low.x; |
| 104 | + positionLowAndColorArray[i * 4 + 1] = encodedCartesian.low.y; |
| 105 | + positionLowAndColorArray[i * 4 + 2] = encodedCartesian.low.z; |
| 106 | + positionLowAndColorArray[i * 4 + 3] = |
| 107 | + AttributeCompression.encodeRGB8(color); |
| 108 | + |
| 109 | + point._dirty = false; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (!defined(renderContext.vertexArray)) { |
| 114 | + const attributeArrays = renderContext.attributeArrays; |
| 115 | + |
| 116 | + const positionHighBuffer = Buffer.createVertexBuffer({ |
| 117 | + typedArray: |
| 118 | + attributeArrays[BufferPointAttributeLocations.positionHighAndShow], |
| 119 | + context, |
| 120 | + // @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203. |
| 121 | + usage: BufferUsage.STATIC_DRAW, |
| 122 | + }); |
| 123 | + |
| 124 | + const positionLowBuffer = Buffer.createVertexBuffer({ |
| 125 | + typedArray: |
| 126 | + attributeArrays[BufferPointAttributeLocations.positionLowAndColor], |
| 127 | + context, |
| 128 | + // @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203. |
| 129 | + usage: BufferUsage.STATIC_DRAW, |
| 130 | + }); |
| 131 | + |
| 132 | + renderContext.vertexArray = new VertexArray({ |
| 133 | + context, |
| 134 | + attributes: [ |
| 135 | + { |
| 136 | + index: BufferPointAttributeLocations.positionHighAndShow, |
| 137 | + vertexBuffer: positionHighBuffer, |
| 138 | + componentDatatype: ComponentDatatype.FLOAT, |
| 139 | + componentsPerAttribute: 4, |
| 140 | + }, |
| 141 | + { |
| 142 | + index: BufferPointAttributeLocations.positionLowAndColor, |
| 143 | + vertexBuffer: positionLowBuffer, |
| 144 | + componentDatatype: ComponentDatatype.FLOAT, |
| 145 | + componentsPerAttribute: 4, |
| 146 | + }, |
| 147 | + ], |
| 148 | + }); |
| 149 | + } else if (collection._dirtyCount > 0) { |
| 150 | + const { positionHighAndShow, positionLowAndColor } = |
| 151 | + BufferPointAttributeLocations; |
| 152 | + renderContext.vertexArray.copyAttributeFromRange( |
| 153 | + positionHighAndShow, |
| 154 | + renderContext.attributeArrays[positionHighAndShow], |
| 155 | + collection._dirtyOffset, |
| 156 | + collection._dirtyCount, |
| 157 | + ); |
| 158 | + renderContext.vertexArray.copyAttributeFromRange( |
| 159 | + positionLowAndColor, |
| 160 | + renderContext.attributeArrays[positionLowAndColor], |
| 161 | + collection._dirtyOffset, |
| 162 | + collection._dirtyCount, |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + if (!defined(renderContext.renderState)) { |
| 167 | + renderContext.renderState = RenderState.fromCache({ |
| 168 | + blending: BlendingState.DISABLED, |
| 169 | + depthMask: false, |
| 170 | + depthTest: { enabled: true }, |
| 171 | + polygonOffset: { enabled: false }, |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + if (!defined(renderContext.uniformMap)) { |
| 176 | + renderContext.uniformMap = {}; |
| 177 | + } |
| 178 | + |
| 179 | + if (!defined(renderContext.shaderProgram)) { |
| 180 | + const vertexShaderSource = new ShaderSource({ |
| 181 | + sources: [BufferPointCollectionVS], |
| 182 | + }); |
| 183 | + |
| 184 | + const fragmentShaderSource = new ShaderSource({ |
| 185 | + sources: [BufferPointCollectionFS], |
| 186 | + }); |
| 187 | + |
| 188 | + renderContext.shaderProgram = ShaderProgram.fromCache({ |
| 189 | + context, |
| 190 | + vertexShaderSource, |
| 191 | + fragmentShaderSource, |
| 192 | + attributeLocations: BufferPointAttributeLocations, |
| 193 | + }); |
| 194 | + } |
| 195 | + |
| 196 | + const command = new DrawCommand({ |
| 197 | + primitiveType: PrimitiveType.POINTS, |
| 198 | + pass: Pass.OPAQUE, |
| 199 | + |
| 200 | + vertexArray: renderContext.vertexArray, |
| 201 | + renderState: renderContext.renderState, |
| 202 | + shaderProgram: renderContext.shaderProgram, |
| 203 | + uniformMap: renderContext.uniformMap, |
| 204 | + |
| 205 | + owner: collection, |
| 206 | + count: collection.primitiveCount, |
| 207 | + boundingVolume: collection.boundingVolume, |
| 208 | + debugShowBoundingVolume: collection.debugShowBoundingVolume, |
| 209 | + }); |
| 210 | + |
| 211 | + frameState.commandList.push(command); |
| 212 | + |
| 213 | + collection._dirtyCount = 0; |
| 214 | + collection._dirtyOffset = 0; |
| 215 | + |
| 216 | + return renderContext; |
| 217 | +} |
| 218 | + |
| 219 | +export default renderBufferPointCollection; |
0 commit comments