createTangentSpaceDebugPrimitive.js 4.1 KB

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