DebugAppearance.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import defaultValue from '../Core/defaultValue.js';
  2. import defined from '../Core/defined.js';
  3. import defineProperties from '../Core/defineProperties.js';
  4. import DeveloperError from '../Core/DeveloperError.js';
  5. import Appearance from './Appearance.js';
  6. /**
  7. * Visualizes a vertex attribute by displaying it as a color for debugging.
  8. * <p>
  9. * Components for well-known unit-length vectors, i.e., <code>normal</code>,
  10. * <code>tangent</code>, and <code>bitangent</code>, are scaled and biased
  11. * from [-1.0, 1.0] to (-1.0, 1.0).
  12. * </p>
  13. *
  14. * @alias DebugAppearance
  15. * @constructor
  16. *
  17. * @param {Object} options Object with the following properties:
  18. * @param {String} options.attributeName The name of the attribute to visualize.
  19. * @param {Boolean} [options.perInstanceAttribute=false] Boolean that determines whether this attribute is a per-instance geometry attribute.
  20. * @param {String} [options.glslDatatype='vec3'] The GLSL datatype of the attribute. Supported datatypes are <code>float</code>, <code>vec2</code>, <code>vec3</code>, and <code>vec4</code>.
  21. * @param {String} [options.vertexShaderSource] Optional GLSL vertex shader source to override the default vertex shader.
  22. * @param {String} [options.fragmentShaderSource] Optional GLSL fragment shader source to override the default fragment shader.
  23. * @param {RenderState} [options.renderState] Optional render state to override the default render state.
  24. *
  25. * @exception {DeveloperError} options.glslDatatype must be float, vec2, vec3, or vec4.
  26. *
  27. * @example
  28. * var primitive = new Cesium.Primitive({
  29. * geometryInstances : // ...
  30. * appearance : new Cesium.DebugAppearance({
  31. * attributeName : 'normal'
  32. * })
  33. * });
  34. */
  35. function DebugAppearance(options) {
  36. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  37. var attributeName = options.attributeName;
  38. var perInstanceAttribute = options.perInstanceAttribute;
  39. //>>includeStart('debug', pragmas.debug);
  40. if (!defined(attributeName)) {
  41. throw new DeveloperError('options.attributeName is required.');
  42. }
  43. //>>includeEnd('debug');
  44. if (!defined(perInstanceAttribute)) {
  45. perInstanceAttribute = false;
  46. }
  47. var glslDatatype = defaultValue(options.glslDatatype, 'vec3');
  48. var varyingName = 'v_' + attributeName;
  49. var getColor;
  50. // Well-known normalized vector attributes in VertexFormat
  51. if ((attributeName === 'normal') || (attributeName === 'tangent') || (attributeName === 'bitangent')) {
  52. getColor = 'vec4 getColor() { return vec4((' + varyingName + ' + vec3(1.0)) * 0.5, 1.0); }\n';
  53. } else {
  54. // All other attributes, both well-known and custom
  55. if (attributeName === 'st') {
  56. glslDatatype = 'vec2';
  57. }
  58. switch(glslDatatype) {
  59. case 'float':
  60. getColor = 'vec4 getColor() { return vec4(vec3(' + varyingName + '), 1.0); }\n';
  61. break;
  62. case 'vec2':
  63. getColor = 'vec4 getColor() { return vec4(' + varyingName + ', 0.0, 1.0); }\n';
  64. break;
  65. case 'vec3':
  66. getColor = 'vec4 getColor() { return vec4(' + varyingName + ', 1.0); }\n';
  67. break;
  68. case 'vec4':
  69. getColor = 'vec4 getColor() { return ' + varyingName + '; }\n';
  70. break;
  71. //>>includeStart('debug', pragmas.debug);
  72. default:
  73. throw new DeveloperError('options.glslDatatype must be float, vec2, vec3, or vec4.');
  74. //>>includeEnd('debug');
  75. }
  76. }
  77. var vs =
  78. 'attribute vec3 position3DHigh;\n' +
  79. 'attribute vec3 position3DLow;\n' +
  80. 'attribute float batchId;\n' +
  81. (perInstanceAttribute ? '' : 'attribute ' + glslDatatype + ' ' + attributeName + ';\n') +
  82. 'varying ' + glslDatatype + ' ' + varyingName + ';\n' +
  83. 'void main()\n' +
  84. '{\n' +
  85. 'vec4 p = czm_translateRelativeToEye(position3DHigh, position3DLow);\n' +
  86. (perInstanceAttribute ?
  87. varyingName + ' = czm_batchTable_' + attributeName + '(batchId);\n' :
  88. varyingName + ' = ' + attributeName + ';\n') +
  89. 'gl_Position = czm_modelViewProjectionRelativeToEye * p;\n' +
  90. '}';
  91. var fs =
  92. 'varying ' + glslDatatype + ' ' + varyingName + ';\n' +
  93. getColor + '\n' +
  94. 'void main()\n' +
  95. '{\n' +
  96. 'gl_FragColor = getColor();\n' +
  97. '}';
  98. /**
  99. * This property is part of the {@link Appearance} interface, but is not
  100. * used by {@link DebugAppearance} since a fully custom fragment shader is used.
  101. *
  102. * @type Material
  103. *
  104. * @default undefined
  105. */
  106. this.material = undefined;
  107. /**
  108. * When <code>true</code>, the geometry is expected to appear translucent.
  109. *
  110. * @type {Boolean}
  111. *
  112. * @default false
  113. */
  114. this.translucent = defaultValue(options.translucent, false);
  115. this._vertexShaderSource = defaultValue(options.vertexShaderSource, vs);
  116. this._fragmentShaderSource = defaultValue(options.fragmentShaderSource, fs);
  117. this._renderState = Appearance.getDefaultRenderState(false, false, options.renderState);
  118. this._closed = defaultValue(options.closed, false);
  119. // Non-derived members
  120. this._attributeName = attributeName;
  121. this._glslDatatype = glslDatatype;
  122. }
  123. defineProperties(DebugAppearance.prototype, {
  124. /**
  125. * The GLSL source code for the vertex shader.
  126. *
  127. * @memberof DebugAppearance.prototype
  128. *
  129. * @type {String}
  130. * @readonly
  131. */
  132. vertexShaderSource : {
  133. get : function() {
  134. return this._vertexShaderSource;
  135. }
  136. },
  137. /**
  138. * The GLSL source code for the fragment shader. The full fragment shader
  139. * source is built procedurally taking into account the {@link DebugAppearance#material}.
  140. * Use {@link DebugAppearance#getFragmentShaderSource} to get the full source.
  141. *
  142. * @memberof DebugAppearance.prototype
  143. *
  144. * @type {String}
  145. * @readonly
  146. */
  147. fragmentShaderSource : {
  148. get : function() {
  149. return this._fragmentShaderSource;
  150. }
  151. },
  152. /**
  153. * The WebGL fixed-function state to use when rendering the geometry.
  154. *
  155. * @memberof DebugAppearance.prototype
  156. *
  157. * @type {Object}
  158. * @readonly
  159. */
  160. renderState : {
  161. get : function() {
  162. return this._renderState;
  163. }
  164. },
  165. /**
  166. * When <code>true</code>, the geometry is expected to be closed.
  167. *
  168. * @memberof DebugAppearance.prototype
  169. *
  170. * @type {Boolean}
  171. * @readonly
  172. *
  173. * @default false
  174. */
  175. closed : {
  176. get : function() {
  177. return this._closed;
  178. }
  179. },
  180. /**
  181. * The name of the attribute being visualized.
  182. *
  183. * @memberof DebugAppearance.prototype
  184. *
  185. * @type {String}
  186. * @readonly
  187. */
  188. attributeName : {
  189. get : function() {
  190. return this._attributeName;
  191. }
  192. },
  193. /**
  194. * The GLSL datatype of the attribute being visualized.
  195. *
  196. * @memberof DebugAppearance.prototype
  197. *
  198. * @type {String}
  199. * @readonly
  200. */
  201. glslDatatype : {
  202. get : function() {
  203. return this._glslDatatype;
  204. }
  205. }
  206. });
  207. /**
  208. * Returns the full GLSL fragment shader source, which for {@link DebugAppearance} is just
  209. * {@link DebugAppearance#fragmentShaderSource}.
  210. *
  211. * @function
  212. *
  213. * @returns {String} The full GLSL fragment shader source.
  214. */
  215. DebugAppearance.prototype.getFragmentShaderSource = Appearance.prototype.getFragmentShaderSource;
  216. /**
  217. * Determines if the geometry is translucent based on {@link DebugAppearance#translucent}.
  218. *
  219. * @function
  220. *
  221. * @returns {Boolean} <code>true</code> if the appearance is translucent.
  222. */
  223. DebugAppearance.prototype.isTranslucent = Appearance.prototype.isTranslucent;
  224. /**
  225. * Creates a render state. This is not the final render state instance; instead,
  226. * it can contain a subset of render state properties identical to the render state
  227. * created in the context.
  228. *
  229. * @function
  230. *
  231. * @returns {Object} The render state.
  232. */
  233. DebugAppearance.prototype.getRenderState = Appearance.prototype.getRenderState;
  234. export default DebugAppearance;