Material.js 5.4 KB
Newer Older
M
Mr.doob 已提交
1
/**
M
Mr.doob 已提交
2
 * @author mrdoob / http://mrdoob.com/
3
 * @author alteredq / http://alteredqualia.com/
M
Mr.doob 已提交
4 5
 */

M
Mr.doob 已提交
6
THREE.Material = function () {
7

M
Mr.doob 已提交
8 9
	Object.defineProperty( this, 'id', { value: THREE.MaterialIdCount ++ } );

10
	this.uuid = THREE.Math.generateUUID();
M
Mr.doob 已提交
11

12
	this.name = '';
13
	this.type = 'Material';
14

M
Mr.doob 已提交
15
	this.side = THREE.FrontSide;
16

M
Mr.doob 已提交
17 18
	this.opacity = 1;
	this.transparent = false;
19

M
Mr.doob 已提交
20
	this.blending = THREE.NormalBlending;
21

M
Mr.doob 已提交
22 23 24
	this.blendSrc = THREE.SrcAlphaFactor;
	this.blendDst = THREE.OneMinusSrcAlphaFactor;
	this.blendEquation = THREE.AddEquation;
25 26 27
	this.blendSrcAlpha = null;
	this.blendDstAlpha = null;
	this.blendEquationAlpha = null;
28

B
Ben Adams 已提交
29
	this.depthFunc = THREE.LessEqualDepth;
M
Mr.doob 已提交
30 31
	this.depthTest = true;
	this.depthWrite = true;
M
Mr.doob 已提交
32

33 34
	this.colorWrite = true;

M
Mr.doob 已提交
35 36 37
	this.polygonOffset = false;
	this.polygonOffsetFactor = 0;
	this.polygonOffsetUnits = 0;
38

M
Mr.doob 已提交
39
	this.alphaTest = 0;
40

41
	this.overdraw = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer
42

43 44
	this.visible = true;

45
	this._needsUpdate = true;
A
alteredq 已提交
46

M
Mr.doob 已提交
47 48
};

49
THREE.Material.prototype = {
M
Mr.doob 已提交
50

51
	constructor: THREE.Material,
M
Mr.doob 已提交
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66
	get needsUpdate () {

		return this._needsUpdate;

	},

	set needsUpdate ( value ) {

		if ( value === true ) this.update();

		this._needsUpdate = value;

	},

67
	setValues: function ( values ) {
M
Mr.doob 已提交
68

69
		if ( values === undefined ) return;
M
Mr.doob 已提交
70

71
		for ( var key in values ) {
72

73
			var newValue = values[ key ];
74

75 76
			if ( newValue === undefined ) {

77
				console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
78 79 80 81 82
				continue;

			}

			if ( key in this ) {
83

84
				var currentValue = this[ key ];
A
alteredq 已提交
85

86
				if ( currentValue instanceof THREE.Color ) {
A
alteredq 已提交
87

88
					currentValue.set( newValue );
A
alteredq 已提交
89

90
				} else if ( currentValue instanceof THREE.Vector3 && newValue instanceof THREE.Vector3 ) {
A
alteredq 已提交
91

92
					currentValue.copy( newValue );
A
alteredq 已提交
93

F
Fabian Lange 已提交
94
				} else if ( key === 'overdraw' ) {
95

D
dubejf 已提交
96
					// ensure overdraw is backwards-compatible with legacy boolean type
97
					this[ key ] = Number( newValue );
98

99
				} else {
A
alteredq 已提交
100

101
					this[ key ] = newValue;
A
alteredq 已提交
102

103
				}
A
alteredq 已提交
104 105

			}
M
Mr.doob 已提交
106

107
		}
M
Mr.doob 已提交
108

109
	},
110

111
	toJSON: function ( meta ) {
112

113 114 115 116 117 118
		var data = {
			metadata: {
				version: 4.4,
				type: 'Material',
				generator: 'Material.toJSON'
			}
B
brason 已提交
119
		};
120

121 122
		// standard Material serialization
		data.uuid = this.uuid;
M
Mr.doob 已提交
123
		data.type = this.type;
124
		if ( this.name !== '' ) data.name = this.name;
125

M
Mr.doob 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
		if ( this.color instanceof THREE.Color ) data.color = this.color.getHex();
		if ( this.emissive instanceof THREE.Color ) data.emissive = this.emissive.getHex();
		if ( this.specular instanceof THREE.Color ) data.specular = this.specular.getHex();
		if ( this.shininess !== undefined ) data.shininess = this.shininess;

		if ( this.map instanceof THREE.Texture ) data.map = this.map.toJSON( meta ).uuid;
		if ( this.alphaMap instanceof THREE.Texture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
		if ( this.lightMap instanceof THREE.Texture ) data.lightMap = this.lightMap.toJSON( meta ).uuid;
		if ( this.bumpMap instanceof THREE.Texture ) {
			data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
			data.bumpScale = this.bumpScale;
		}
		if ( this.normalMap instanceof THREE.Texture ) {
			data.normalMap = this.normalMap.toJSON( meta ).uuid;
			data.normalScale = this.normalScale; // Removed for now, causes issue in editor ui.js
		}
		if ( this.specularMap instanceof THREE.Texture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
		if ( this.envMap instanceof THREE.Texture ) {
			data.envMap = this.envMap.toJSON( meta ).uuid;
			data.reflectivity = this.reflectivity; // Scale behind envMap
		}

		if ( this.size !== undefined ) data.size = this.size;
		if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;

		if ( this.vertexColors !== undefined && this.vertexColors !== THREE.NoColors ) data.vertexColors = this.vertexColors;
		if ( this.shading !== undefined && this.shading !== THREE.SmoothShading ) data.shading = this.shading;
		if ( this.blending !== undefined && this.blending !== THREE.NormalBlending ) data.blending = this.blending;
		if ( this.side !== undefined && this.side !== THREE.FrontSide ) data.side = this.side;

156
		if ( this.opacity < 1 ) data.opacity = this.opacity;
M
Mr.doob 已提交
157 158
		if ( this.transparent === true ) data.transparent = this.transparent;
		if ( this.wireframe === true ) data.wireframe = this.wireframe;
159

160
		return data;
161 162 163

	},

164
	clone: function () {
M
Mr.doob 已提交
165

166
		var material = new THREE.Material();
167
		return material._copyFrom( this );
168 169 170

	},

171
	_copyFrom: function ( source ) {
M
Mr.doob 已提交
172

173
		this.name = source.name;
174

175
		this.side = source.side;
176

177 178
		this.opacity = source.opacity;
		this.transparent = source.transparent;
179

180
		this.blending = source.blending;
181

182 183 184 185 186 187
		this.blendSrc = source.blendSrc;
		this.blendDst = source.blendDst;
		this.blendEquation = source.blendEquation;
		this.blendSrcAlpha = source.blendSrcAlpha;
		this.blendDstAlpha = source.blendDstAlpha;
		this.blendEquationAlpha = source.blendEquationAlpha;
188

189 190 191
		this.depthFunc = source.depthFunc;
		this.depthTest = source.depthTest;
		this.depthWrite = source.depthWrite;
M
Mr.doob 已提交
192

193 194 195
		this.polygonOffset = source.polygonOffset;
		this.polygonOffsetFactor = source.polygonOffsetFactor;
		this.polygonOffsetUnits = source.polygonOffsetUnits;
M
Mr.doob 已提交
196

197
		this.alphaTest = source.alphaTest;
M
Mr.doob 已提交
198

199
		this.overdraw = source.overdraw;
M
Mr.doob 已提交
200

201
		this.visible = source.visible;
202

203
		return this;
204

205
	},
206

207 208 209 210 211 212
	update: function () {

		this.dispatchEvent( { type: 'update' } );

	},

213
	dispose: function () {
L
libra guest 已提交
214

215
		this.dispatchEvent( { type: 'dispose' } );
M
Mr.doob 已提交
216

217
	}
M
Mr.doob 已提交
218 219

};
220

M
Mr.doob 已提交
221 222
THREE.EventDispatcher.prototype.apply( THREE.Material.prototype );

223
THREE.MaterialIdCount = 0;