ShaderMaterial.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. * parameters = {
  5. * defines: { "label" : "value" },
  6. * uniforms: { "parameter1": { type: "f", value: 1.0 }, "parameter2": { type: "i" value2: 2 } },
  7. *
  8. * fragmentShader: <string>,
  9. * vertexShader: <string>,
  10. *
  11. * shading: THREE.SmoothShading,
  12. * blending: THREE.NormalBlending,
  13. * depthTest: <bool>,
  14. * depthWrite: <bool>,
  15. *
  16. * wireframe: <boolean>,
  17. * wireframeLinewidth: <float>,
  18. *
  19. * lights: <bool>,
  20. *
  21. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  22. *
  23. * skinning: <bool>,
  24. * morphTargets: <bool>,
  25. * morphNormals: <bool>,
  26. *
  27. * fog: <bool>
  28. * }
  29. */
  30. THREE.ShaderMaterial = function ( parameters ) {
  31. THREE.Material.call( this );
  32. this.type = 'ShaderMaterial';
  33. this.defines = {};
  34. this.uniforms = {};
  35. this.attributes = [];
  36. this.vertexShader = 'void main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}';
  37. this.fragmentShader = 'void main() {\n\tgl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );\n}';
  38. this.shading = THREE.SmoothShading;
  39. this.linewidth = 1;
  40. this.wireframe = false;
  41. this.wireframeLinewidth = 1;
  42. this.fog = false; // set to use scene fog
  43. this.lights = false; // set to use scene lights
  44. this.vertexColors = THREE.NoColors; // set to use "color" attribute stream
  45. this.skinning = false; // set to use skinning attribute streams
  46. this.morphTargets = false; // set to use morph targets
  47. this.morphNormals = false; // set to use morph normals
  48. this.derivatives = false; // set to use derivatives
  49. // When rendered geometry doesn't include these attributes but the material does,
  50. // use these default values in WebGL. This avoids errors when buffer data is missing.
  51. this.defaultAttributeValues = {
  52. 'color': [ 1, 1, 1 ],
  53. 'uv': [ 0, 0 ],
  54. 'uv2': [ 0, 0 ]
  55. };
  56. this.index0AttributeName = undefined;
  57. if ( parameters !== undefined ) {
  58. if ( parameters.attributes !== undefined && Array.isArray( parameters.attributes ) === false ) {
  59. console.warn( 'THREE.ShaderMaterial: attributes should now be an array of attribute names.' );
  60. parameters.attributes = Object.keys( parameters.attributes );
  61. }
  62. this.setValues( parameters );
  63. }
  64. };
  65. THREE.ShaderMaterial.prototype = Object.create( THREE.Material.prototype );
  66. THREE.ShaderMaterial.prototype.constructor = THREE.ShaderMaterial;
  67. THREE.ShaderMaterial.prototype.clone = function () {
  68. var material = new THREE.ShaderMaterial();
  69. return material.copy( this );
  70. };
  71. THREE.ShaderMaterial.prototype.copy = function ( source ) {
  72. THREE.Material.prototype.copy.call( this, source );
  73. this.fragmentShader = source.fragmentShader;
  74. this.vertexShader = source.vertexShader;
  75. this.uniforms = THREE.UniformsUtils.clone( source.uniforms );
  76. this.attributes = source.attributes;
  77. this.defines = source.defines;
  78. this.shading = source.shading;
  79. this.wireframe = source.wireframe;
  80. this.wireframeLinewidth = source.wireframeLinewidth;
  81. this.fog = source.fog;
  82. this.lights = source.lights;
  83. this.vertexColors = source.vertexColors;
  84. this.skinning = source.skinning;
  85. this.morphTargets = source.morphTargets;
  86. this.morphNormals = source.morphNormals;
  87. return this;
  88. };
  89. THREE.ShaderMaterial.prototype.toJSON = function ( meta ) {
  90. var data = THREE.Material.prototype.toJSON.call( this, meta );
  91. data.uniforms = this.uniforms;
  92. data.attributes = this.attributes;
  93. data.vertexShader = this.vertexShader;
  94. data.fragmentShader = this.fragmentShader;
  95. return data;
  96. };