ShaderMaterial.js 3.0 KB

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