ShaderMaterial.js 3.1 KB
Newer Older
B
bentok 已提交
1 2
import { Material } from './Material.js';
import { UniformsUtils } from '../renderers/shaders/UniformsUtils.js';
R
Rich Harris 已提交
3

4 5 6 7
/**
 * @author alteredq / http://alteredqualia.com/
 *
 * parameters = {
8
 *  defines: { "label" : "value" },
9
 *  uniforms: { "parameter1": { value: 1.0 }, "parameter2": { value2: 2 } },
10
 *
11 12
 *  fragmentShader: <string>,
 *  vertexShader: <string>,
13
 *
14
 *  wireframe: <boolean>,
15
 *  wireframeLinewidth: <float>,
16
 *
17
 *  lights: <bool>,
18
 *
19
 *  skinning: <bool>,
20
 *  morphTargets: <bool>,
M
Mr.doob 已提交
21
 *  morphNormals: <bool>
22 23 24
 * }
 */

M
Mr.doob 已提交
25
function ShaderMaterial( parameters ) {
26

R
Rich Harris 已提交
27
	Material.call( this );
28

29 30
	this.type = 'ShaderMaterial';

31
	this.defines = {};
M
Mr.doob 已提交
32
	this.uniforms = {};
33

M
Mr.doob 已提交
34 35 36
	this.vertexShader = 'void main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}';
	this.fragmentShader = 'void main() {\n\tgl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );\n}';

37 38
	this.linewidth = 1;

M
Mr.doob 已提交
39 40
	this.wireframe = false;
	this.wireframeLinewidth = 1;
41

M
Mr.doob 已提交
42 43
	this.fog = false; // set to use scene fog
	this.lights = false; // set to use scene lights
T
tschw 已提交
44
	this.clipping = false; // set to use user-defined clipping planes
45

46
	this.skinning = false; // set to use skinning attribute streams
M
Mr.doob 已提交
47 48
	this.morphTargets = false; // set to use morph targets
	this.morphNormals = false; // set to use morph normals
49

50 51 52 53 54 55
	this.extensions = {
		derivatives: false, // set to use derivatives
		fragDepth: false, // set to use fragment depth values
		drawBuffers: false, // set to use draw buffers
		shaderTextureLOD: false // set to use shader texture LOD
	};
56

M
Mr.doob 已提交
57 58
	// When rendered geometry doesn't include these attributes but the material does,
	// use these default values in WebGL. This avoids errors when buffer data is missing.
59
	this.defaultAttributeValues = {
60 61 62
		'color': [ 1, 1, 1 ],
		'uv': [ 0, 0 ],
		'uv2': [ 0, 0 ]
63 64
	};

65
	this.index0AttributeName = undefined;
66
	this.uniformsNeedUpdate = false;
67

68 69
	if ( parameters !== undefined ) {

70
		if ( parameters.attributes !== undefined ) {
71

72
			console.error( 'THREE.ShaderMaterial: attributes should now be defined in THREE.BufferGeometry instead.' );
73 74 75 76 77 78

		}

		this.setValues( parameters );

	}
79

M
Mr.doob 已提交
80
}
81

R
Rich Harris 已提交
82 83
ShaderMaterial.prototype = Object.create( Material.prototype );
ShaderMaterial.prototype.constructor = ShaderMaterial;
L
libra guest 已提交
84

85 86
ShaderMaterial.prototype.isShaderMaterial = true;

R
Rich Harris 已提交
87
ShaderMaterial.prototype.copy = function ( source ) {
88

R
Rich Harris 已提交
89
	Material.prototype.copy.call( this, source );
90

91 92
	this.fragmentShader = source.fragmentShader;
	this.vertexShader = source.vertexShader;
93

94
	this.uniforms = UniformsUtils.clone( source.uniforms );
95

96
	this.defines = source.defines;
97

98 99
	this.wireframe = source.wireframe;
	this.wireframeLinewidth = source.wireframeLinewidth;
100

101
	this.lights = source.lights;
T
tschw 已提交
102
	this.clipping = source.clipping;
103

104 105
	this.skinning = source.skinning;

106 107
	this.morphTargets = source.morphTargets;
	this.morphNormals = source.morphNormals;
108

109
	this.extensions = source.extensions;
M
Mr.doob 已提交
110

111
	return this;
L
libra guest 已提交
112 113

};
114

R
Rich Harris 已提交
115
ShaderMaterial.prototype.toJSON = function ( meta ) {
116

R
Rich Harris 已提交
117
	var data = Material.prototype.toJSON.call( this, meta );
118

119 120 121
	data.uniforms = this.uniforms;
	data.vertexShader = this.vertexShader;
	data.fragmentShader = this.fragmentShader;
122 123 124 125

	return data;

};
R
Rich Harris 已提交
126 127


128
export { ShaderMaterial };