createTangentSpaceDebugPrimitive.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import ColorGeometryInstanceAttribute from "../Core/ColorGeometryInstanceAttribute.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defined from "../Core/defined.js";
  4. import DeveloperError from "../Core/DeveloperError.js";
  5. import GeometryInstance from "../Core/GeometryInstance.js";
  6. import GeometryPipeline from "../Core/GeometryPipeline.js";
  7. import Matrix4 from "../Core/Matrix4.js";
  8. import PerInstanceColorAppearance from "./PerInstanceColorAppearance.js";
  9. import Primitive from "./Primitive.js";
  10. /**
  11. * Creates a {@link Primitive} to visualize well-known vector vertex attributes:
  12. * <code>normal</code>, <code>tangent</code>, and <code>bitangent</code>. Normal
  13. * is red; tangent is green; and bitangent is blue. If an attribute is not
  14. * present, it is not drawn.
  15. *
  16. * @function
  17. *
  18. * @param {Object} options Object with the following properties:
  19. * @param {Geometry} options.geometry The <code>Geometry</code> instance with the attribute.
  20. * @param {Number} [options.length=10000.0] The length of each line segment in meters. This can be negative to point the vector in the opposite direction.
  21. * @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The model matrix that transforms to transform the geometry from model to world coordinates.
  22. * @returns {Primitive} A new <code>Primitive</code> instance with geometry for the vectors.
  23. *
  24. * @example
  25. * scene.primitives.add(Cesium.createTangentSpaceDebugPrimitive({
  26. * geometry : instance.geometry,
  27. * length : 100000.0,
  28. * modelMatrix : instance.modelMatrix
  29. * }));
  30. */
  31. function createTangentSpaceDebugPrimitive(options) {
  32. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  33. const instances = [];
  34. let geometry = options.geometry;
  35. //>>includeStart('debug', pragmas.debug);
  36. if (!defined(geometry)) {
  37. throw new DeveloperError("options.geometry is required.");
  38. }
  39. //>>includeEnd('debug');
  40. if (!defined(geometry.attributes) || !defined(geometry.primitiveType)) {
  41. // to create the debug lines, we need the computed attributes.
  42. // compute them if they are undefined.
  43. geometry = geometry.constructor.createGeometry(geometry);
  44. }
  45. const attributes = geometry.attributes;
  46. const modelMatrix = Matrix4.clone(
  47. defaultValue(options.modelMatrix, Matrix4.IDENTITY)
  48. );
  49. const length = defaultValue(options.length, 10000.0);
  50. if (defined(attributes.normal)) {
  51. instances.push(
  52. new GeometryInstance({
  53. geometry: GeometryPipeline.createLineSegmentsForVectors(
  54. geometry,
  55. "normal",
  56. length
  57. ),
  58. attributes: {
  59. color: new ColorGeometryInstanceAttribute(1.0, 0.0, 0.0, 1.0),
  60. },
  61. modelMatrix: modelMatrix,
  62. })
  63. );
  64. }
  65. if (defined(attributes.tangent)) {
  66. instances.push(
  67. new GeometryInstance({
  68. geometry: GeometryPipeline.createLineSegmentsForVectors(
  69. geometry,
  70. "tangent",
  71. length
  72. ),
  73. attributes: {
  74. color: new ColorGeometryInstanceAttribute(0.0, 1.0, 0.0, 1.0),
  75. },
  76. modelMatrix: modelMatrix,
  77. })
  78. );
  79. }
  80. if (defined(attributes.bitangent)) {
  81. instances.push(
  82. new GeometryInstance({
  83. geometry: GeometryPipeline.createLineSegmentsForVectors(
  84. geometry,
  85. "bitangent",
  86. length
  87. ),
  88. attributes: {
  89. color: new ColorGeometryInstanceAttribute(0.0, 0.0, 1.0, 1.0),
  90. },
  91. modelMatrix: modelMatrix,
  92. })
  93. );
  94. }
  95. if (instances.length > 0) {
  96. return new Primitive({
  97. asynchronous: false,
  98. geometryInstances: instances,
  99. appearance: new PerInstanceColorAppearance({
  100. flat: true,
  101. translucent: false,
  102. }),
  103. });
  104. }
  105. return undefined;
  106. }
  107. export default createTangentSpaceDebugPrimitive;