DebugAppearance.js 8.5 KB

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