ShaderMaterial.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { Material } from './Material.js';
  2. import { UniformsUtils } from '../renderers/shaders/UniformsUtils.js';
  3. /**
  4. * @author alteredq / http://alteredqualia.com/
  5. *
  6. * parameters = {
  7. * defines: { "label" : "value" },
  8. * uniforms: { "parameter1": { value: 1.0 }, "parameter2": { value2: 2 } },
  9. *
  10. * fragmentShader: <string>,
  11. * vertexShader: <string>,
  12. *
  13. * wireframe: <boolean>,
  14. * wireframeLinewidth: <float>,
  15. *
  16. * lights: <bool>,
  17. *
  18. * skinning: <bool>,
  19. * morphTargets: <bool>,
  20. * morphNormals: <bool>
  21. * }
  22. */
  23. function ShaderMaterial( parameters ) {
  24. Material.call( this );
  25. this.type = 'ShaderMaterial';
  26. this.defines = {};
  27. this.uniforms = {};
  28. this.vertexShader = 'void main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}';
  29. this.fragmentShader = 'void main() {\n\tgl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );\n}';
  30. this.linewidth = 1;
  31. this.wireframe = false;
  32. this.wireframeLinewidth = 1;
  33. this.fog = false; // set to use scene fog
  34. this.lights = false; // set to use scene lights
  35. this.clipping = false; // set to use user-defined clipping planes
  36. this.skinning = false; // set to use skinning attribute streams
  37. this.morphTargets = false; // set to use morph targets
  38. this.morphNormals = false; // set to use morph normals
  39. this.extensions = {
  40. derivatives: false, // set to use derivatives
  41. fragDepth: false, // set to use fragment depth values
  42. drawBuffers: false, // set to use draw buffers
  43. shaderTextureLOD: false // set to use shader texture LOD
  44. };
  45. // When rendered geometry doesn't include these attributes but the material does,
  46. // use these default values in WebGL. This avoids errors when buffer data is missing.
  47. this.defaultAttributeValues = {
  48. 'color': [ 1, 1, 1 ],
  49. 'uv': [ 0, 0 ],
  50. 'uv2': [ 0, 0 ]
  51. };
  52. this.index0AttributeName = undefined;
  53. if ( parameters !== undefined ) {
  54. if ( parameters.attributes !== undefined ) {
  55. console.error( 'THREE.ShaderMaterial: attributes should now be defined in THREE.BufferGeometry instead.' );
  56. }
  57. this.setValues( parameters );
  58. }
  59. }
  60. ShaderMaterial.prototype = Object.create( Material.prototype );
  61. ShaderMaterial.prototype.constructor = ShaderMaterial;
  62. ShaderMaterial.prototype.isShaderMaterial = true;
  63. ShaderMaterial.prototype.copy = function ( source ) {
  64. Material.prototype.copy.call( this, source );
  65. this.fragmentShader = source.fragmentShader;
  66. this.vertexShader = source.vertexShader;
  67. this.uniforms = UniformsUtils.clone( source.uniforms );
  68. this.defines = source.defines;
  69. this.wireframe = source.wireframe;
  70. this.wireframeLinewidth = source.wireframeLinewidth;
  71. this.lights = source.lights;
  72. this.clipping = source.clipping;
  73. this.skinning = source.skinning;
  74. this.morphTargets = source.morphTargets;
  75. this.morphNormals = source.morphNormals;
  76. this.extensions = source.extensions;
  77. return this;
  78. };
  79. ShaderMaterial.prototype.toJSON = function ( meta ) {
  80. var data = Material.prototype.toJSON.call( this, meta );
  81. data.uniforms = this.uniforms;
  82. data.vertexShader = this.vertexShader;
  83. data.fragmentShader = this.fragmentShader;
  84. return data;
  85. };
  86. export { ShaderMaterial };
粤ICP备19079148号