ModelExperimentalPrimitive.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import Check from "../../Core/Check.js";
  2. import defaultValue from "../../Core/defaultValue.js";
  3. import defined from "../../Core/defined.js";
  4. import PrimitiveType from "../../Core/PrimitiveType.js";
  5. import AlphaPipelineStage from "./AlphaPipelineStage.js";
  6. import BatchTexturePipelineStage from "./BatchTexturePipelineStage.js";
  7. import CustomShaderMode from "./CustomShaderMode.js";
  8. import CustomShaderPipelineStage from "./CustomShaderPipelineStage.js";
  9. import FeatureIdPipelineStage from "./FeatureIdPipelineStage.js";
  10. import CPUStylingPipelineStage from "./CPUStylingPipelineStage.js";
  11. import DequantizationPipelineStage from "./DequantizationPipelineStage.js";
  12. import GeometryPipelineStage from "./GeometryPipelineStage.js";
  13. import LightingPipelineStage from "./LightingPipelineStage.js";
  14. import MaterialPipelineStage from "./MaterialPipelineStage.js";
  15. import MetadataPipelineStage from "./MetadataPipelineStage.js";
  16. import ModelExperimentalUtility from "./ModelExperimentalUtility.js";
  17. import MorphTargetsPipelineStage from "./MorphTargetsPipelineStage.js";
  18. import PickingPipelineStage from "./PickingPipelineStage.js";
  19. import PointCloudAttenuationPipelineStage from "./PointCloudAttenuationPipelineStage.js";
  20. import SelectedFeatureIdPipelineStage from "./SelectedFeatureIdPipelineStage.js";
  21. import SkinningPipelineStage from "./SkinningPipelineStage.js";
  22. /**
  23. * In memory representation of a single primitive, that is, a primitive
  24. * and its corresponding mesh.
  25. *
  26. * @param {Object} options An object containing the following options:
  27. * @param {ModelComponents.Primitive} options.primitive The primitive component.
  28. * @param {ModelComponents.Node} options.node The node that this primitive belongs to.
  29. * @param {ModelExperimental} options.model The {@link ModelExperimental} this primitive belongs to.
  30. *
  31. * @alias ModelExperimentalPrimitive
  32. * @constructor
  33. *
  34. * @private
  35. */
  36. export default function ModelExperimentalPrimitive(options) {
  37. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  38. const primitive = options.primitive;
  39. const node = options.node;
  40. const model = options.model;
  41. //>>includeStart('debug', pragmas.debug);
  42. Check.typeOf.object("options.primitive", primitive);
  43. Check.typeOf.object("options.node", node);
  44. Check.typeOf.object("options.model", model);
  45. //>>includeEnd('debug');
  46. /**
  47. * The primitive component associated with this primitive.
  48. *
  49. * @type {ModelComponents.Primitive}
  50. *
  51. * @private
  52. */
  53. this.primitive = primitive;
  54. /**
  55. * A reference to the node this primitive belongs to.
  56. *
  57. * @type {ModelComponents.Node}
  58. *
  59. * @private
  60. */
  61. this.node = node;
  62. /**
  63. * A reference to the model
  64. *
  65. * @type {ModelExperimental}
  66. *
  67. * @private
  68. */
  69. this.model = model;
  70. /**
  71. * Pipeline stages to apply to this primitive. This
  72. * is an array of classes, each with a static method called
  73. * <code>process()</code>
  74. *
  75. * @type {Object[]}
  76. * @readonly
  77. *
  78. * @private
  79. */
  80. this.pipelineStages = [];
  81. /**
  82. * The generated {@link DrawCommand}s associated with this primitive.
  83. *
  84. * @type {DrawCommand[]}
  85. *
  86. * @private
  87. */
  88. this.drawCommands = [];
  89. /**
  90. * The bounding sphere of this primitive (in object-space).
  91. *
  92. * @type {BoundingSphere}
  93. *
  94. * @private
  95. */
  96. this.boundingSphere = undefined;
  97. /**
  98. * Update stages to apply to this primitive.
  99. *
  100. * @private
  101. */
  102. this.updateStages = [];
  103. this.configurePipeline();
  104. }
  105. /**
  106. * Configure the primitive pipeline stages. If the pipeline needs to be re-run, call
  107. * this method again to ensure the correct sequence of pipeline stages are
  108. * used.
  109. *
  110. * @private
  111. */
  112. ModelExperimentalPrimitive.prototype.configurePipeline = function () {
  113. const pipelineStages = this.pipelineStages;
  114. pipelineStages.length = 0;
  115. const primitive = this.primitive;
  116. const node = this.node;
  117. const model = this.model;
  118. const customShader = model.customShader;
  119. const hasMorphTargets =
  120. defined(primitive.morphTargets) && primitive.morphTargets.length > 0;
  121. const hasSkinning = defined(node.skin);
  122. const hasCustomShader = defined(customShader);
  123. const hasCustomFragmentShader =
  124. hasCustomShader && defined(customShader.fragmentShaderText);
  125. const materialsEnabled =
  126. !hasCustomFragmentShader ||
  127. customShader.mode !== CustomShaderMode.REPLACE_MATERIAL;
  128. const hasQuantization = ModelExperimentalUtility.hasQuantizedAttributes(
  129. primitive.attributes
  130. );
  131. const pointCloudShading = model.pointCloudShading;
  132. const hasAttenuation =
  133. defined(pointCloudShading) && pointCloudShading.attenuation;
  134. const featureIdFlags = inspectFeatureIds(model, node, primitive);
  135. // Start of pipeline -----------------------------------------------------
  136. pipelineStages.push(GeometryPipelineStage);
  137. if (hasMorphTargets) {
  138. pipelineStages.push(MorphTargetsPipelineStage);
  139. }
  140. if (hasSkinning) {
  141. pipelineStages.push(SkinningPipelineStage);
  142. }
  143. if (hasAttenuation && primitive.primitiveType === PrimitiveType.POINTS) {
  144. pipelineStages.push(PointCloudAttenuationPipelineStage);
  145. }
  146. if (hasQuantization) {
  147. pipelineStages.push(DequantizationPipelineStage);
  148. }
  149. if (materialsEnabled) {
  150. pipelineStages.push(MaterialPipelineStage);
  151. }
  152. // These stages are always run to ensure structs
  153. // are declared to avoid compilation errors.
  154. pipelineStages.push(FeatureIdPipelineStage);
  155. pipelineStages.push(MetadataPipelineStage);
  156. if (featureIdFlags.hasPropertyTable) {
  157. pipelineStages.push(SelectedFeatureIdPipelineStage);
  158. pipelineStages.push(BatchTexturePipelineStage);
  159. pipelineStages.push(CPUStylingPipelineStage);
  160. }
  161. if (hasCustomShader) {
  162. pipelineStages.push(CustomShaderPipelineStage);
  163. }
  164. pipelineStages.push(LightingPipelineStage);
  165. if (model.allowPicking) {
  166. pipelineStages.push(PickingPipelineStage);
  167. }
  168. pipelineStages.push(AlphaPipelineStage);
  169. return;
  170. };
  171. function inspectFeatureIds(model, node, primitive) {
  172. let featureIds;
  173. // Check instances first, as this is the most specific type of
  174. // feature ID
  175. if (defined(node.instances)) {
  176. featureIds = ModelExperimentalUtility.getFeatureIdsByLabel(
  177. node.instances.featureIds,
  178. model.instanceFeatureIdLabel
  179. );
  180. if (defined(featureIds)) {
  181. return {
  182. hasFeatureIds: true,
  183. hasPropertyTable: defined(featureIds.propertyTableId),
  184. };
  185. }
  186. }
  187. featureIds = ModelExperimentalUtility.getFeatureIdsByLabel(
  188. primitive.featureIds,
  189. model.featureIdLabel
  190. );
  191. if (defined(featureIds)) {
  192. return {
  193. hasFeatureIds: true,
  194. hasPropertyTable: defined(featureIds.propertyTableId),
  195. };
  196. }
  197. return {
  198. hasFeatureIds: false,
  199. hasPropertyTable: false,
  200. };
  201. }