createTangentSpaceDebugPrimitive.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * @exports createTangentSpaceDebugPrimitive
  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. var instances = [];
  34. var 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. var attributes = geometry.attributes;
  46. var modelMatrix = Matrix4.clone(defaultValue(options.modelMatrix, Matrix4.IDENTITY));
  47. var length = defaultValue(options.length, 10000.0);
  48. if (defined(attributes.normal)) {
  49. instances.push(new GeometryInstance({
  50. geometry : GeometryPipeline.createLineSegmentsForVectors(geometry, 'normal', length),
  51. attributes : {
  52. color : new ColorGeometryInstanceAttribute(1.0, 0.0, 0.0, 1.0)
  53. },
  54. modelMatrix : modelMatrix
  55. }));
  56. }
  57. if (defined(attributes.tangent)) {
  58. instances.push(new GeometryInstance({
  59. geometry : GeometryPipeline.createLineSegmentsForVectors(geometry, 'tangent', length),
  60. attributes : {
  61. color : new ColorGeometryInstanceAttribute(0.0, 1.0, 0.0, 1.0)
  62. },
  63. modelMatrix : modelMatrix
  64. }));
  65. }
  66. if (defined(attributes.bitangent)) {
  67. instances.push(new GeometryInstance({
  68. geometry : GeometryPipeline.createLineSegmentsForVectors(geometry, 'bitangent', length),
  69. attributes : {
  70. color : new ColorGeometryInstanceAttribute(0.0, 0.0, 1.0, 1.0)
  71. },
  72. modelMatrix : modelMatrix
  73. }));
  74. }
  75. if (instances.length > 0) {
  76. return new Primitive({
  77. asynchronous : false,
  78. geometryInstances : instances,
  79. appearance : new PerInstanceColorAppearance({
  80. flat : true,
  81. translucent : false
  82. })
  83. });
  84. }
  85. return undefined;
  86. }
  87. export default createTangentSpaceDebugPrimitive;