PrimitiveRenderResources.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import BoundingSphere from "../../Core/BoundingSphere.js";
  2. import Cartesian3 from "../../Core/Cartesian3.js";
  3. import Check from "../../Core/Check.js";
  4. import clone from "../../Core/clone.js";
  5. import defined from "../../Core/defined.js";
  6. import ModelUtility from "./ModelUtility.js";
  7. import ModelLightingOptions from "./ModelLightingOptions.js";
  8. /**
  9. * Each node may have many mesh primitives. Most model pipeline stages operate
  10. * at the primitive level. Again, properties are inherited from the parent.
  11. *
  12. * @param {NodeRenderResources} nodeRenderResources The node resources to inherit from
  13. * @param {ModelRuntimePrimitive} runtimePrimitive The primitive.
  14. *
  15. * @private
  16. */
  17. function PrimitiveRenderResources(nodeRenderResources, runtimePrimitive) {
  18. //>>includeStart('debug', pragmas.debug);
  19. Check.typeOf.object("nodeRenderResources", nodeRenderResources);
  20. Check.typeOf.object("runtimePrimitive", runtimePrimitive);
  21. //>>includeEnd('debug');
  22. // Properties inherited from NodeRenderResources.
  23. /**
  24. * A reference to the model. Inherited from the node render resources.
  25. *
  26. * @type {Model}
  27. * @readonly
  28. *
  29. * @private
  30. */
  31. this.model = nodeRenderResources.model;
  32. /**
  33. * A reference to the runtime node. Inherited from the node render resources.
  34. *
  35. * @type {ModelRuntimeNode}
  36. * @readonly
  37. *
  38. * @private
  39. */
  40. this.runtimeNode = nodeRenderResources.runtimeNode;
  41. /**
  42. * The vertex attributes. This is shallow cloned from the node render
  43. * resources as the primitive will add additional properties.
  44. *
  45. * @type {Object[]}
  46. * @readonly
  47. *
  48. * @private
  49. */
  50. this.attributes = nodeRenderResources.attributes.slice();
  51. /**
  52. * The index to give to the next vertex attribute added to the attributes
  53. * array. POSITION takes index 0. Inherited from the node render resources.
  54. *
  55. * @type {number}
  56. *
  57. * @private
  58. */
  59. this.attributeIndex = nodeRenderResources.attributeIndex;
  60. /**
  61. * The set index to assign to feature ID vertex attribute(s) created from the
  62. * offset/repeat in the feature ID attribute. Inherited from the node render
  63. * resources.
  64. *
  65. * @type {number}
  66. *
  67. * @private
  68. */
  69. this.featureIdVertexAttributeSetIndex =
  70. nodeRenderResources.featureIdVertexAttributeSetIndex;
  71. /**
  72. * A dictionary mapping uniform name to functions that return the uniform
  73. * values. Inherited from the node render resources.
  74. *
  75. * @type {Object<string, Function>}
  76. * @readonly
  77. *
  78. * @private
  79. */
  80. this.uniformMap = clone(nodeRenderResources.uniformMap);
  81. /**
  82. * Options for configuring the alpha stage such as pass and alpha cutoff.
  83. * Inherited from the node render resources.
  84. *
  85. * @type {ModelAlphaOptions}
  86. * @readonly
  87. *
  88. * @private
  89. */
  90. this.alphaOptions = clone(nodeRenderResources.alphaOptions);
  91. /**
  92. * An object storing options for creating a {@link RenderState}.
  93. * The pipeline stages simply set the options; the actual render state
  94. * is created when the {@link DrawCommand} is constructed. Inherited from
  95. * the node render resources.
  96. *
  97. * @type {object}
  98. * @readonly
  99. *
  100. * @private
  101. */
  102. this.renderStateOptions = clone(nodeRenderResources.renderStateOptions, true);
  103. /**
  104. * Whether the model has a silhouette. This value indicates what draw commands
  105. * are needed. Inherited from the node render resources.
  106. *
  107. * @type {boolean}
  108. * @readonly
  109. *
  110. * @private
  111. */
  112. this.hasSilhouette = nodeRenderResources.hasSilhouette;
  113. /**
  114. * Whether the model is part of a tileset that uses the skipLevelOfDetail
  115. * optimization. This value indicates what draw commands are needed.
  116. * Inherited from the node render resources.
  117. *
  118. * @type {boolean}
  119. * @readonly
  120. *
  121. * @private
  122. */
  123. this.hasSkipLevelOfDetail = nodeRenderResources.hasSkipLevelOfDetail;
  124. /**
  125. * An object used to build a shader incrementally. This is cloned from the
  126. * node render resources because each primitive can compute a different shader.
  127. *
  128. * @type {ShaderBuilder}
  129. * @readonly
  130. *
  131. * @private
  132. */
  133. this.shaderBuilder = nodeRenderResources.shaderBuilder.clone();
  134. /**
  135. * The number of instances. Default is 0, if instancing is not used.
  136. * Inherited from the node render resources.
  137. *
  138. * @type {number}
  139. * @readonly
  140. *
  141. * @private
  142. */
  143. this.instanceCount = nodeRenderResources.instanceCount;
  144. // Other properties
  145. /**
  146. * A reference to the runtime primitive.
  147. *
  148. * @type {ModelRuntimePrimitive}
  149. * @readonly
  150. *
  151. * @private
  152. */
  153. this.runtimePrimitive = runtimePrimitive;
  154. /**
  155. * The primitive associated with the render resources.
  156. *
  157. * @type {ModelComponents.Primitive}
  158. * @readonly
  159. *
  160. * @private
  161. */
  162. const primitive = runtimePrimitive.primitive;
  163. /**
  164. * The number of indices in the primitive. The interpretation of this
  165. * depends on the primitive type.
  166. *
  167. * @type {number}
  168. * @readonly
  169. *
  170. * @private
  171. */
  172. this.count = defined(primitive.indices)
  173. ? primitive.indices.count
  174. : ModelUtility.getAttributeBySemantic(primitive, "POSITION").count;
  175. /**
  176. * Whether or not this primitive has a property table for storing metadata.
  177. * When present, picking and styling can use this. This value is set by
  178. * SelectedFeatureIdPipelineStage.
  179. *
  180. * @type {boolean}
  181. * @default false
  182. *
  183. * @private
  184. */
  185. this.hasPropertyTable = false;
  186. /**
  187. * The indices for this primitive.
  188. *
  189. * @type {ModelComponents.Indices}
  190. * @readonly
  191. *
  192. * @private
  193. */
  194. this.indices = primitive.indices;
  195. /**
  196. * Additional index buffer for wireframe mode (if enabled). This value is set
  197. * by WireframePipelineStage.
  198. *
  199. * @type {Buffer}
  200. * @readonly
  201. *
  202. * @private
  203. */
  204. this.wireframeIndexBuffer = undefined;
  205. /**
  206. * The primitive type such as TRIANGLES or POINTS.
  207. *
  208. * @type {PrimitiveType}
  209. * @readonly
  210. *
  211. * @private
  212. */
  213. this.primitiveType = primitive.primitiveType;
  214. const positionMinMax = ModelUtility.getPositionMinMax(
  215. primitive,
  216. this.runtimeNode.instancingTranslationMin,
  217. this.runtimeNode.instancingTranslationMax
  218. );
  219. /**
  220. * The minimum position value for this primitive.
  221. *
  222. * @type {Cartesian3}
  223. * @readonly
  224. *
  225. * @private
  226. */
  227. this.positionMin = Cartesian3.clone(positionMinMax.min, new Cartesian3());
  228. /**
  229. * The maximum position value for this primitive.
  230. *
  231. * @type {Cartesian3}
  232. * @readonly
  233. *
  234. * @private
  235. */
  236. this.positionMax = Cartesian3.clone(positionMinMax.max, new Cartesian3());
  237. /**
  238. * The bounding sphere that contains all the vertices in this primitive.
  239. *
  240. * @type {BoundingSphere}
  241. * @readonly
  242. *
  243. * @private
  244. */
  245. this.boundingSphere = BoundingSphere.fromCornerPoints(
  246. this.positionMin,
  247. this.positionMax,
  248. new BoundingSphere()
  249. );
  250. /**
  251. * Options for configuring the lighting stage, such as selecting between
  252. * unlit and PBR shading.
  253. *
  254. * @type {ModelLightingOptions}
  255. * @readonly
  256. *
  257. * @private
  258. */
  259. this.lightingOptions = new ModelLightingOptions();
  260. /**
  261. * The shader variable to use for picking. If picking is enabled, this value
  262. * is set by PickingPipelineStage.
  263. *
  264. * @type {string}
  265. *
  266. * @private
  267. */
  268. this.pickId = undefined;
  269. }
  270. export default PrimitiveRenderResources;