PrimitiveRenderResources.js 6.1 KB

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