Texture.js 6.4 KB
Newer Older
M
Mr.doob 已提交
1
/**
M
Mr.doob 已提交
2
 * @author mrdoob / http://mrdoob.com/
3
 * @author alteredq / http://alteredqualia.com/
4
 * @author szimek / https://github.com/szimek/
M
Mr.doob 已提交
5 6
 */

B
bentok 已提交
7
import { EventDispatcher } from '../core/EventDispatcher.js';
M
Mr.doob 已提交
8 9 10 11 12 13 14 15 16 17 18
import {
	MirroredRepeatWrapping,
	ClampToEdgeWrapping,
	RepeatWrapping,
	LinearEncoding,
	UnsignedByteType,
	RGBAFormat,
	LinearMipMapLinearFilter,
	LinearFilter,
	UVMapping
} from '../constants.js';
B
bentok 已提交
19 20 21
import { _Math } from '../math/Math.js';
import { Vector2 } from '../math/Vector2.js';
import { Matrix3 } from '../math/Matrix3.js';
22
import { ImageUtils } from '../extras/ImageUtils.js';
R
Rich Harris 已提交
23

24 25
var textureId = 0;

M
Mr.doob 已提交
26
function Texture( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding ) {
M
Mr.doob 已提交
27

28
	Object.defineProperty( this, 'id', { value: textureId ++ } );
M
Mr.doob 已提交
29

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

32 33
	this.name = '';

R
Rich Harris 已提交
34
	this.image = image !== undefined ? image : Texture.DEFAULT_IMAGE;
35
	this.mipmaps = [];
36

R
Rich Harris 已提交
37
	this.mapping = mapping !== undefined ? mapping : Texture.DEFAULT_MAPPING;
38

R
Rich Harris 已提交
39 40
	this.wrapS = wrapS !== undefined ? wrapS : ClampToEdgeWrapping;
	this.wrapT = wrapT !== undefined ? wrapT : ClampToEdgeWrapping;
M
Mr.doob 已提交
41

R
Rich Harris 已提交
42 43
	this.magFilter = magFilter !== undefined ? magFilter : LinearFilter;
	this.minFilter = minFilter !== undefined ? minFilter : LinearMipMapLinearFilter;
44

45 46
	this.anisotropy = anisotropy !== undefined ? anisotropy : 1;

R
Rich Harris 已提交
47 48
	this.format = format !== undefined ? format : RGBAFormat;
	this.type = type !== undefined ? type : UnsignedByteType;
49

R
Rich Harris 已提交
50 51
	this.offset = new Vector2( 0, 0 );
	this.repeat = new Vector2( 1, 1 );
T
Tentone 已提交
52
	this.center = new Vector2( 0, 0 );
53
	this.rotation = 0;
W
WestLangley 已提交
54 55 56

	this.matrixAutoUpdate = true;
	this.matrix = new Matrix3();
57

58
	this.generateMipmaps = true;
59
	this.premultiplyAlpha = false;
60
	this.flipY = true;
61
	this.unpackAlignment = 4;	// valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml)
62 63 64 65 66

	// Values of encoding !== THREE.LinearEncoding only supported on map, envMap and emissiveMap.
	//
	// Also changing the encoding after already used by a Material will not automatically make the Material
	// update.  You need to explicitly call Material.needsUpdate to trigger it to recompile.
67
	this.encoding = encoding !== undefined ? encoding : LinearEncoding;
68

69
	this.version = 0;
70
	this.onUpdate = null;
71

M
Mr.doob 已提交
72
}
M
Mr.doob 已提交
73

R
Rich Harris 已提交
74 75
Texture.DEFAULT_IMAGE = undefined;
Texture.DEFAULT_MAPPING = UVMapping;
76

77
Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
M
Mr.doob 已提交
78

T
Tristan VALCKE 已提交
79
	constructor: Texture,
M
Mr.doob 已提交
80

T
Tristan VALCKE 已提交
81
	isTexture: true,
M
Mr.doob 已提交
82

W
WestLangley 已提交
83 84 85 86 87 88
	updateMatrix: function () {

		this.matrix.setUvTransform( this.offset.x, this.offset.y, this.repeat.x, this.repeat.y, this.rotation, this.center.x, this.center.y );

	},

89
	clone: function () {
90

91
		return new this.constructor().copy( this );
92

93 94
	},

D
dubejf 已提交
95
	copy: function ( source ) {
96

97
		this.name = source.name;
98

99
		this.image = source.image;
100
		this.mipmaps = source.mipmaps.slice( 0 );
M
Mr.doob 已提交
101 102 103 104 105 106 107 108 109 110 111

		this.mapping = source.mapping;

		this.wrapS = source.wrapS;
		this.wrapT = source.wrapT;

		this.magFilter = source.magFilter;
		this.minFilter = source.minFilter;

		this.anisotropy = source.anisotropy;

M
Mr.doob 已提交
112
		this.format = source.format;
113
		this.type = source.type;
114

115 116
		this.offset.copy( source.offset );
		this.repeat.copy( source.repeat );
117
		this.center.copy( source.center );
118
		this.rotation = source.rotation;
W
WestLangley 已提交
119 120 121

		this.matrixAutoUpdate = source.matrixAutoUpdate;
		this.matrix.copy( source.matrix );
M
Mr.doob 已提交
122

123 124 125 126
		this.generateMipmaps = source.generateMipmaps;
		this.premultiplyAlpha = source.premultiplyAlpha;
		this.flipY = source.flipY;
		this.unpackAlignment = source.unpackAlignment;
127
		this.encoding = source.encoding;
M
Mr.doob 已提交
128

129
		return this;
130

131 132
	},

133 134
	toJSON: function ( meta ) {

135 136 137
		var isRootObject = ( meta === undefined || typeof meta === 'string' );

		if ( ! isRootObject && meta.textures[ this.uuid ] !== undefined ) {
138 139 140 141

			return meta.textures[ this.uuid ];

		}
E
elephantatwork 已提交
142 143

		var output = {
144

E
elephantatwork 已提交
145
			metadata: {
146
				version: 4.5,
E
elephantatwork 已提交
147
				type: 'Texture',
148
				generator: 'Texture.toJSON'
E
elephantatwork 已提交
149
			},
150

151
			uuid: this.uuid,
152 153 154 155 156 157
			name: this.name,

			mapping: this.mapping,

			repeat: [ this.repeat.x, this.repeat.y ],
			offset: [ this.offset.x, this.offset.y ],
158
			center: [ this.center.x, this.center.y ],
159
			rotation: this.rotation,
W
WestLangley 已提交
160

161 162
			wrap: [ this.wrapS, this.wrapT ],

163
			format: this.format,
164 165 166
			type: this.type,
			encoding: this.encoding,

167 168
			minFilter: this.minFilter,
			magFilter: this.magFilter,
M
Mr.doob 已提交
169 170
			anisotropy: this.anisotropy,

171
			flipY: this.flipY,
M
Mr.doob 已提交
172

173 174
			premultiplyAlpha: this.premultiplyAlpha,
			unpackAlignment: this.unpackAlignment
175

E
elephantatwork 已提交
176 177
		};

178
		if ( this.image !== undefined ) {
E
elephantatwork 已提交
179

180
			// TODO: Move to THREE.Image
E
elephantatwork 已提交
181

182
			var image = this.image;
E
elephantatwork 已提交
183

184
			if ( image.uuid === undefined ) {
E
elephantatwork 已提交
185

R
Rich Harris 已提交
186
				image.uuid = _Math.generateUUID(); // UGH
E
elephantatwork 已提交
187

188
			}
E
elephantatwork 已提交
189

190
			if ( ! isRootObject && meta.images[ image.uuid ] === undefined ) {
191

192 193 194 195 196 197 198 199 200 201
				var url;

				if ( Array.isArray( image ) ) {

					// process array of images e.g. CubeTexture

					url = [];

					for ( var i = 0, l = image.length; i < l; i ++ ) {

202
						url.push( ImageUtils.getDataURL( image[ i ] ) );
203 204 205 206 207 208 209

					}

				} else {

					// process single image

210
					url = ImageUtils.getDataURL( image );
211 212 213

				}

214 215
				meta.images[ image.uuid ] = {
					uuid: image.uuid,
216
					url: url
217
				};
218 219 220 221

			}

			output.image = image.uuid;
E
elephantatwork 已提交
222 223 224

		}

225 226 227 228 229
		if ( ! isRootObject ) {

			meta.textures[ this.uuid ] = output;

		}
230

E
elephantatwork 已提交
231
		return output;
232

E
elephantatwork 已提交
233 234
	},

235
	dispose: function () {
236

237
		this.dispatchEvent( { type: 'dispose' } );
238

239 240 241 242
	},

	transformUv: function ( uv ) {

243
		if ( this.mapping !== UVMapping ) return uv;
244

245
		uv.applyMatrix3( this.matrix );
246 247

		if ( uv.x < 0 || uv.x > 1 ) {
R
rfm1201 已提交
248

249
			switch ( this.wrapS ) {
R
rfm1201 已提交
250

R
Rich Harris 已提交
251
				case RepeatWrapping:
R
rfm1201 已提交
252

253 254
					uv.x = uv.x - Math.floor( uv.x );
					break;
R
rfm1201 已提交
255

R
Rich Harris 已提交
256
				case ClampToEdgeWrapping:
R
rfm1201 已提交
257

258 259
					uv.x = uv.x < 0 ? 0 : 1;
					break;
R
rfm1201 已提交
260

R
Rich Harris 已提交
261
				case MirroredRepeatWrapping:
R
rfm1201 已提交
262 263 264

					if ( Math.abs( Math.floor( uv.x ) % 2 ) === 1 ) {

265
						uv.x = Math.ceil( uv.x ) - uv.x;
R
rfm1201 已提交
266

267
					} else {
R
rfm1201 已提交
268

269
						uv.x = uv.x - Math.floor( uv.x );
R
rfm1201 已提交
270

271
					}
R
rfm1201 已提交
272 273
					break;

274
			}
R
rfm1201 已提交
275

276 277 278
		}

		if ( uv.y < 0 || uv.y > 1 ) {
R
rfm1201 已提交
279

280
			switch ( this.wrapT ) {
R
rfm1201 已提交
281

R
Rich Harris 已提交
282
				case RepeatWrapping:
R
rfm1201 已提交
283

284 285
					uv.y = uv.y - Math.floor( uv.y );
					break;
R
rfm1201 已提交
286

R
Rich Harris 已提交
287
				case ClampToEdgeWrapping:
R
rfm1201 已提交
288

289 290
					uv.y = uv.y < 0 ? 0 : 1;
					break;
R
rfm1201 已提交
291

R
Rich Harris 已提交
292
				case MirroredRepeatWrapping:
R
rfm1201 已提交
293

294
					if ( Math.abs( Math.floor( uv.y ) % 2 ) === 1 ) {
R
rfm1201 已提交
295

296
						uv.y = Math.ceil( uv.y ) - uv.y;
R
rfm1201 已提交
297

298
					} else {
R
rfm1201 已提交
299

300
						uv.y = uv.y - Math.floor( uv.y );
R
rfm1201 已提交
301

302 303
					}
					break;
R
rfm1201 已提交
304

305
			}
R
rfm1201 已提交
306

307 308 309
		}

		if ( this.flipY ) {
R
rfm1201 已提交
310

311
			uv.y = 1 - uv.y;
R
rfm1201 已提交
312

313
		}
R
rfm1201 已提交
314

315 316
		return uv;

317
	}
M
Mr.doob 已提交
318

319
} );
320

T
Takahiro 已提交
321 322 323 324 325 326 327 328 329 330
Object.defineProperty( Texture.prototype, "needsUpdate", {

	set: function ( value ) {

		if ( value === true ) this.version ++;

	}

} );

R
Rich Harris 已提交
331

332
export { Texture };