Material.js 11.1 KB
Newer Older
B
bentok 已提交
1
import { EventDispatcher } from '../core/EventDispatcher.js';
2
import { NoColors, FrontSide, FlatShading, NormalBlending, LessEqualDepth, AddEquation, OneMinusSrcAlphaFactor, SrcAlphaFactor, AlwaysStencilFunc, KeepStencilOp } from '../constants.js';
B
bentok 已提交
3
import { _Math } from '../math/Math.js';
R
Rich Harris 已提交
4

M
Mr.doob 已提交
5
/**
M
Mr.doob 已提交
6
 * @author mrdoob / http://mrdoob.com/
7
 * @author alteredq / http://alteredqualia.com/
M
Mr.doob 已提交
8 9
 */

10 11
var materialId = 0;

M
Mr.doob 已提交
12
function Material() {
13

14
	Object.defineProperty( this, 'id', { value: materialId ++ } );
M
Mr.doob 已提交
15

R
Rich Harris 已提交
16
	this.uuid = _Math.generateUUID();
M
Mr.doob 已提交
17

18
	this.name = '';
19
	this.type = 'Material';
20

M
Mr.doob 已提交
21
	this.fog = true;
M
Mr.doob 已提交
22
	this.lights = true;
M
Mr.doob 已提交
23

R
Rich Harris 已提交
24 25
	this.blending = NormalBlending;
	this.side = FrontSide;
26
	this.flatShading = false;
27
	this.vertexTangents = false;
R
Rich Harris 已提交
28
	this.vertexColors = NoColors; // THREE.NoColors, THREE.VertexColors, THREE.FaceColors
29

M
Mr.doob 已提交
30 31
	this.opacity = 1;
	this.transparent = false;
32

R
Rich Harris 已提交
33 34 35
	this.blendSrc = SrcAlphaFactor;
	this.blendDst = OneMinusSrcAlphaFactor;
	this.blendEquation = AddEquation;
36 37 38
	this.blendSrcAlpha = null;
	this.blendDstAlpha = null;
	this.blendEquationAlpha = null;
39

R
Rich Harris 已提交
40
	this.depthFunc = LessEqualDepth;
M
Mr.doob 已提交
41 42
	this.depthTest = true;
	this.depthWrite = true;
M
Mr.doob 已提交
43

44 45 46 47 48 49
	this.stencilFunc = AlwaysStencilFunc;
	this.stencilRef = 0;
	this.stencilMask = 0xff;
	this.stencilFail = KeepStencilOp;
	this.stencilZFail = KeepStencilOp;
	this.stencilZPass = KeepStencilOp;
G
Garrett Johnson 已提交
50
	this.stencilWrite = false;
51

T
tschw 已提交
52
	this.clippingPlanes = null;
53
	this.clipIntersection = false;
T
tschw 已提交
54 55
	this.clipShadows = false;

W
WestLangley 已提交
56 57
	this.shadowSide = null;

M
Mr.doob 已提交
58 59
	this.colorWrite = true;

60 61
	this.precision = null; // override the renderer's default precision for this material

M
Mr.doob 已提交
62 63 64
	this.polygonOffset = false;
	this.polygonOffsetFactor = 0;
	this.polygonOffsetUnits = 0;
65

66
	this.dithering = false;
67

M
Mr.doob 已提交
68
	this.alphaTest = 0;
69
	this.premultipliedAlpha = false;
70

71 72
	this.visible = true;

W
Wittmann Tobias 已提交
73
	this.userData = {};
A
alteredq 已提交
74

75 76
	this.needsUpdate = true;

M
Mr.doob 已提交
77
}
M
Mr.doob 已提交
78

79
Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
M
Mugen87 已提交
80

81
	constructor: Material,
T
Tristan VALCKE 已提交
82 83

	isMaterial: true,
84

M
Mr.doob 已提交
85 86
	onBeforeCompile: function () {},

87
	setValues: function ( values ) {
M
Mr.doob 已提交
88

89
		if ( values === undefined ) return;
M
Mr.doob 已提交
90

91
		for ( var key in values ) {
92

93
			var newValue = values[ key ];
94

95 96
			if ( newValue === undefined ) {

97
				console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
98 99 100 101
				continue;

			}

102 103 104 105 106 107 108 109 110
			// for backward compatability if shading is set in the constructor
			if ( key === 'shading' ) {

				console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' );
				this.flatShading = ( newValue === FlatShading ) ? true : false;
				continue;

			}

111
			var currentValue = this[ key ];
112

113
			if ( currentValue === undefined ) {
A
alteredq 已提交
114

115 116 117 118
				console.warn( "THREE." + this.type + ": '" + key + "' is not a property of this material." );
				continue;

			}
A
alteredq 已提交
119

120
			if ( currentValue && currentValue.isColor ) {
A
alteredq 已提交
121

122
				currentValue.set( newValue );
A
alteredq 已提交
123

M
Mr.doob 已提交
124
			} else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) {
A
alteredq 已提交
125

126
				currentValue.copy( newValue );
127

128
			} else {
A
alteredq 已提交
129

130
				this[ key ] = newValue;
A
alteredq 已提交
131 132

			}
M
Mr.doob 已提交
133

134
		}
M
Mr.doob 已提交
135

136
	},
137

138
	toJSON: function ( meta ) {
139

140
		var isRoot = ( meta === undefined || typeof meta === 'string' );
141 142 143 144 145 146 147 148 149 150

		if ( isRoot ) {

			meta = {
				textures: {},
				images: {}
			};

		}

151 152
		var data = {
			metadata: {
153
				version: 4.5,
154 155 156
				type: 'Material',
				generator: 'Material.toJSON'
			}
B
brason 已提交
157
		};
158

159 160
		// standard Material serialization
		data.uuid = this.uuid;
M
Mr.doob 已提交
161
		data.type = this.type;
162

163
		if ( this.name !== '' ) data.name = this.name;
164

165
		if ( this.color && this.color.isColor ) data.color = this.color.getHex();
166

167 168
		if ( this.roughness !== undefined ) data.roughness = this.roughness;
		if ( this.metalness !== undefined ) data.metalness = this.metalness;
169

170
		if ( this.emissive && this.emissive.isColor ) data.emissive = this.emissive.getHex();
171
		if ( this.emissiveIntensity && this.emissiveIntensity !== 1 ) data.emissiveIntensity = this.emissiveIntensity;
T
Tentone 已提交
172

173
		if ( this.specular && this.specular.isColor ) data.specular = this.specular.getHex();
M
Mr.doob 已提交
174
		if ( this.shininess !== undefined ) data.shininess = this.shininess;
175 176
		if ( this.clearCoat !== undefined ) data.clearCoat = this.clearCoat;
		if ( this.clearCoatRoughness !== undefined ) data.clearCoatRoughness = this.clearCoatRoughness;
M
Mr.doob 已提交
177

178
		if ( this.map && this.map.isTexture ) data.map = this.map.toJSON( meta ).uuid;
T
Temdog007 已提交
179
		if ( this.matcap && this.matcap.isTexture ) data.matcap = this.matcap.toJSON( meta ).uuid;
180 181
		if ( this.alphaMap && this.alphaMap.isTexture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
		if ( this.lightMap && this.lightMap.isTexture ) data.lightMap = this.lightMap.toJSON( meta ).uuid;
182 183 184 185 186 187 188 189

		if ( this.aoMap && this.aoMap.isTexture ) {

			data.aoMap = this.aoMap.toJSON( meta ).uuid;
			data.aoMapIntensity = this.aoMapIntensity;

		}

190
		if ( this.bumpMap && this.bumpMap.isTexture ) {
G
gero3 已提交
191

M
Mr.doob 已提交
192 193
			data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
			data.bumpScale = this.bumpScale;
G
gero3 已提交
194

M
Mr.doob 已提交
195
		}
T
Takahiro 已提交
196

197
		if ( this.normalMap && this.normalMap.isTexture ) {
G
gero3 已提交
198

M
Mr.doob 已提交
199
			data.normalMap = this.normalMap.toJSON( meta ).uuid;
200
			data.normalMapType = this.normalMapType;
201
			data.normalScale = this.normalScale.toArray();
G
gero3 已提交
202

203
		}
T
Takahiro 已提交
204

205
		if ( this.displacementMap && this.displacementMap.isTexture ) {
206 207 208

			data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
			data.displacementScale = this.displacementScale;
209
			data.displacementBias = this.displacementBias;
210

M
Mr.doob 已提交
211
		}
T
Takahiro 已提交
212

213 214
		if ( this.roughnessMap && this.roughnessMap.isTexture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid;
		if ( this.metalnessMap && this.metalnessMap.isTexture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid;
215

216 217
		if ( this.emissiveMap && this.emissiveMap.isTexture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid;
		if ( this.specularMap && this.specularMap.isTexture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
218

219
		if ( this.envMap && this.envMap.isTexture ) {
G
gero3 已提交
220

M
Mr.doob 已提交
221 222
			data.envMap = this.envMap.toJSON( meta ).uuid;
			data.reflectivity = this.reflectivity; // Scale behind envMap
223
			data.refractionRatio = this.refractionRatio;
G
gero3 已提交
224

225 226 227
			if ( this.combine !== undefined ) data.combine = this.combine;
			if ( this.envMapIntensity !== undefined ) data.envMapIntensity = this.envMapIntensity;

M
Mr.doob 已提交
228 229
		}

T
Takahiro 已提交
230
		if ( this.gradientMap && this.gradientMap.isTexture ) {
T
Takahiro 已提交
231

T
Takahiro 已提交
232
			data.gradientMap = this.gradientMap.toJSON( meta ).uuid;
T
Takahiro 已提交
233 234 235

		}

M
Mr.doob 已提交
236 237 238
		if ( this.size !== undefined ) data.size = this.size;
		if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;

R
Rich Harris 已提交
239
		if ( this.blending !== NormalBlending ) data.blending = this.blending;
240
		if ( this.flatShading === true ) data.flatShading = this.flatShading;
R
Rich Harris 已提交
241 242
		if ( this.side !== FrontSide ) data.side = this.side;
		if ( this.vertexColors !== NoColors ) data.vertexColors = this.vertexColors;
M
Mr.doob 已提交
243

244
		if ( this.opacity < 1 ) data.opacity = this.opacity;
M
Mr.doob 已提交
245
		if ( this.transparent === true ) data.transparent = this.transparent;
M
Mr.doob 已提交
246 247 248 249 250

		data.depthFunc = this.depthFunc;
		data.depthTest = this.depthTest;
		data.depthWrite = this.depthWrite;

251 252 253 254 255 256 257 258
		data.stencilWrite = this.stencilWrite;
		data.stencilFunc = this.stencilFunc;
		data.stencilRef = this.stencilRef;
		data.stencilMask = this.stencilMask;
		data.stencilFail = this.stencilFail;
		data.stencilZFail = this.stencilZFail;
		data.stencilZPass = this.stencilZPass;

259
		// rotation (SpriteMaterial)
260
		if ( this.rotation && this.rotation !== 0 ) data.rotation = this.rotation;
T
Tentone 已提交
261

B
bGute 已提交
262 263 264
		if ( this.polygonOffset === true ) data.polygonOffset = true;
		if ( this.polygonOffsetFactor !== 0 ) data.polygonOffsetFactor = this.polygonOffsetFactor;
		if ( this.polygonOffsetUnits !== 0 ) data.polygonOffsetUnits = this.polygonOffsetUnits;
T
Tentone 已提交
265

266
		if ( this.linewidth && this.linewidth !== 1 ) data.linewidth = this.linewidth;
267 268 269 270
		if ( this.dashSize !== undefined ) data.dashSize = this.dashSize;
		if ( this.gapSize !== undefined ) data.gapSize = this.gapSize;
		if ( this.scale !== undefined ) data.scale = this.scale;

271 272
		if ( this.dithering === true ) data.dithering = true;

M
Mr.doob 已提交
273
		if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
274
		if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = this.premultipliedAlpha;
275

M
Mr.doob 已提交
276
		if ( this.wireframe === true ) data.wireframe = this.wireframe;
277
		if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
278 279 280
		if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap;
		if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin;

281
		if ( this.morphTargets === true ) data.morphTargets = true;
282
		if ( this.morphNormals === true ) data.morphNormals = true;
283
		if ( this.skinning === true ) data.skinning = true;
M
Mr.doob 已提交
284

285 286
		if ( this.visible === false ) data.visible = false;
		if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
287

288 289
		// TODO: Copied from Object3D.toJSON

M
Mr.doob 已提交
290
		function extractFromCache( cache ) {
M
Mr.doob 已提交
291

292
			var values = [];
M
Mr.doob 已提交
293

294
			for ( var key in cache ) {
M
Mr.doob 已提交
295

296 297 298
				var data = cache[ key ];
				delete data.metadata;
				values.push( data );
M
Mr.doob 已提交
299

300
			}
M
Mr.doob 已提交
301

302
			return values;
M
Mr.doob 已提交
303

304 305 306 307 308 309 310 311 312 313 314
		}

		if ( isRoot ) {

			var textures = extractFromCache( meta.textures );
			var images = extractFromCache( meta.images );

			if ( textures.length > 0 ) data.textures = textures;
			if ( images.length > 0 ) data.images = images;

		}
315

316
		return data;
317 318 319

	},

320
	clone: function () {
M
Mr.doob 已提交
321

322
		return new this.constructor().copy( this );
323 324 325

	},

D
dubejf 已提交
326
	copy: function ( source ) {
M
Mr.doob 已提交
327

328
		this.name = source.name;
329

M
Mr.doob 已提交
330
		this.fog = source.fog;
M
Mr.doob 已提交
331
		this.lights = source.lights;
M
Mr.doob 已提交
332 333

		this.blending = source.blending;
334
		this.side = source.side;
335
		this.flatShading = source.flatShading;
M
Mr.doob 已提交
336
		this.vertexColors = source.vertexColors;
337

338 339
		this.opacity = source.opacity;
		this.transparent = source.transparent;
340

341 342 343 344 345 346
		this.blendSrc = source.blendSrc;
		this.blendDst = source.blendDst;
		this.blendEquation = source.blendEquation;
		this.blendSrcAlpha = source.blendSrcAlpha;
		this.blendDstAlpha = source.blendDstAlpha;
		this.blendEquationAlpha = source.blendEquationAlpha;
347

348 349 350
		this.depthFunc = source.depthFunc;
		this.depthTest = source.depthTest;
		this.depthWrite = source.depthWrite;
M
Mr.doob 已提交
351

352 353 354 355 356 357 358 359
		this.stencilWrite = source.stencilWrite;
		this.stencilFunc = source.stencilFunc;
		this.stencilRef = source.stencilRef;
		this.stencilMask = source.stencilMask;
		this.stencilFail = source.stencilFail;
		this.stencilZFail = source.stencilZFail;
		this.stencilZPass = source.stencilZPass;

M
Mr.doob 已提交
360 361
		this.colorWrite = source.colorWrite;

362 363
		this.precision = source.precision;

364 365 366
		this.polygonOffset = source.polygonOffset;
		this.polygonOffsetFactor = source.polygonOffsetFactor;
		this.polygonOffsetUnits = source.polygonOffsetUnits;
M
Mr.doob 已提交
367

368 369
		this.dithering = source.dithering;

370
		this.alphaTest = source.alphaTest;
371 372
		this.premultipliedAlpha = source.premultipliedAlpha;

373
		this.visible = source.visible;
374 375
		this.userData = JSON.parse( JSON.stringify( source.userData ) );

T
tschw 已提交
376
		this.clipShadows = source.clipShadows;
377
		this.clipIntersection = source.clipIntersection;
T
tschw 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

		var srcPlanes = source.clippingPlanes,
			dstPlanes = null;

		if ( srcPlanes !== null ) {

			var n = srcPlanes.length;
			dstPlanes = new Array( n );

			for ( var i = 0; i !== n; ++ i )
				dstPlanes[ i ] = srcPlanes[ i ].clone();

		}

		this.clippingPlanes = dstPlanes;
393

W
WestLangley 已提交
394 395
		this.shadowSide = source.shadowSide;

396
		return this;
397

398
	},
399

400
	dispose: function () {
L
libra guest 已提交
401

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

404
	}
M
Mr.doob 已提交
405

406
} );
407

R
Rich Harris 已提交
408

409
export { Material };