Texture.js 6.3 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 8 9 10 11 12
import { EventDispatcher } from '../core/EventDispatcher.js';
import { UVMapping } from '../constants.js';
import { MirroredRepeatWrapping, ClampToEdgeWrapping, RepeatWrapping, LinearEncoding, UnsignedByteType, RGBAFormat, LinearMipMapLinearFilter, LinearFilter } from '../constants.js';
import { _Math } from '../math/Math.js';
import { Vector2 } from '../math/Vector2.js';
import { Matrix3 } from '../math/Matrix3.js';
13
import { ImageUtils } from '../extras/ImageUtils.js';
R
Rich Harris 已提交
14

15 16
var textureId = 0;

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

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

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

23 24
	this.name = '';

R
Rich Harris 已提交
25
	this.image = image !== undefined ? image : Texture.DEFAULT_IMAGE;
26
	this.mipmaps = [];
27

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

R
Rich Harris 已提交
30 31
	this.wrapS = wrapS !== undefined ? wrapS : ClampToEdgeWrapping;
	this.wrapT = wrapT !== undefined ? wrapT : ClampToEdgeWrapping;
M
Mr.doob 已提交
32

R
Rich Harris 已提交
33 34
	this.magFilter = magFilter !== undefined ? magFilter : LinearFilter;
	this.minFilter = minFilter !== undefined ? minFilter : LinearMipMapLinearFilter;
35

36 37
	this.anisotropy = anisotropy !== undefined ? anisotropy : 1;

R
Rich Harris 已提交
38 39
	this.format = format !== undefined ? format : RGBAFormat;
	this.type = type !== undefined ? type : UnsignedByteType;
40

R
Rich Harris 已提交
41 42
	this.offset = new Vector2( 0, 0 );
	this.repeat = new Vector2( 1, 1 );
T
Tentone 已提交
43
	this.center = new Vector2( 0, 0 );
44
	this.rotation = 0;
W
WestLangley 已提交
45 46 47

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

49
	this.generateMipmaps = true;
50
	this.premultiplyAlpha = false;
51
	this.flipY = true;
52
	this.unpackAlignment = 4;	// valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml)
53 54 55 56 57

	// 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.
58
	this.encoding = encoding !== undefined ? encoding : LinearEncoding;
59

60
	this.version = 0;
61
	this.onUpdate = null;
62

M
Mr.doob 已提交
63
}
M
Mr.doob 已提交
64

R
Rich Harris 已提交
65 66
Texture.DEFAULT_IMAGE = undefined;
Texture.DEFAULT_MAPPING = UVMapping;
67

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

T
Tristan VALCKE 已提交
70
	constructor: Texture,
M
Mr.doob 已提交
71

T
Tristan VALCKE 已提交
72
	isTexture: true,
M
Mr.doob 已提交
73

W
WestLangley 已提交
74 75 76 77 78 79
	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 );

	},

80
	clone: function () {
81

82
		return new this.constructor().copy( this );
83

84 85
	},

D
dubejf 已提交
86
	copy: function ( source ) {
87

88
		this.name = source.name;
89

90
		this.image = source.image;
91
		this.mipmaps = source.mipmaps.slice( 0 );
M
Mr.doob 已提交
92 93 94 95 96 97 98 99 100 101 102

		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 已提交
103
		this.format = source.format;
104
		this.type = source.type;
105

106 107
		this.offset.copy( source.offset );
		this.repeat.copy( source.repeat );
108
		this.center.copy( source.center );
109
		this.rotation = source.rotation;
W
WestLangley 已提交
110 111 112

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

114 115 116 117
		this.generateMipmaps = source.generateMipmaps;
		this.premultiplyAlpha = source.premultiplyAlpha;
		this.flipY = source.flipY;
		this.unpackAlignment = source.unpackAlignment;
118
		this.encoding = source.encoding;
M
Mr.doob 已提交
119

120
		return this;
121

122 123
	},

124 125
	toJSON: function ( meta ) {

126 127 128
		var isRootObject = ( meta === undefined || typeof meta === 'string' );

		if ( ! isRootObject && meta.textures[ this.uuid ] !== undefined ) {
129 130 131 132

			return meta.textures[ this.uuid ];

		}
E
elephantatwork 已提交
133 134

		var output = {
135

E
elephantatwork 已提交
136
			metadata: {
137
				version: 4.5,
E
elephantatwork 已提交
138
				type: 'Texture',
139
				generator: 'Texture.toJSON'
E
elephantatwork 已提交
140
			},
141

142
			uuid: this.uuid,
143 144 145 146 147 148
			name: this.name,

			mapping: this.mapping,

			repeat: [ this.repeat.x, this.repeat.y ],
			offset: [ this.offset.x, this.offset.y ],
149
			center: [ this.center.x, this.center.y ],
150
			rotation: this.rotation,
W
WestLangley 已提交
151

152 153
			wrap: [ this.wrapS, this.wrapT ],

154
			format: this.format,
155 156
			minFilter: this.minFilter,
			magFilter: this.magFilter,
M
Mr.doob 已提交
157 158 159
			anisotropy: this.anisotropy,

			flipY: this.flipY
160

E
elephantatwork 已提交
161 162
		};

163
		if ( this.image !== undefined ) {
E
elephantatwork 已提交
164

165
			// TODO: Move to THREE.Image
E
elephantatwork 已提交
166

167
			var image = this.image;
E
elephantatwork 已提交
168

169
			if ( image.uuid === undefined ) {
E
elephantatwork 已提交
170

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

173
			}
E
elephantatwork 已提交
174

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

177 178 179 180 181 182 183 184 185 186
				var url;

				if ( Array.isArray( image ) ) {

					// process array of images e.g. CubeTexture

					url = [];

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

187
						url.push( ImageUtils.getDataURL( image[ i ] ) );
188 189 190 191 192 193 194

					}

				} else {

					// process single image

195
					url = ImageUtils.getDataURL( image );
196 197 198

				}

199 200
				meta.images[ image.uuid ] = {
					uuid: image.uuid,
201
					url: url
202
				};
203 204 205 206

			}

			output.image = image.uuid;
E
elephantatwork 已提交
207 208 209

		}

210 211 212 213 214
		if ( ! isRootObject ) {

			meta.textures[ this.uuid ] = output;

		}
215

E
elephantatwork 已提交
216
		return output;
217

E
elephantatwork 已提交
218 219
	},

220
	dispose: function () {
221

222
		this.dispatchEvent( { type: 'dispose' } );
223

224 225 226 227
	},

	transformUv: function ( uv ) {

228
		if ( this.mapping !== UVMapping ) return uv;
229

230
		uv.applyMatrix3( this.matrix );
231 232

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

234
			switch ( this.wrapS ) {
R
rfm1201 已提交
235

R
Rich Harris 已提交
236
				case RepeatWrapping:
R
rfm1201 已提交
237

238 239
					uv.x = uv.x - Math.floor( uv.x );
					break;
R
rfm1201 已提交
240

R
Rich Harris 已提交
241
				case ClampToEdgeWrapping:
R
rfm1201 已提交
242

243 244
					uv.x = uv.x < 0 ? 0 : 1;
					break;
R
rfm1201 已提交
245

R
Rich Harris 已提交
246
				case MirroredRepeatWrapping:
R
rfm1201 已提交
247 248 249

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

250
						uv.x = Math.ceil( uv.x ) - uv.x;
R
rfm1201 已提交
251

252
					} else {
R
rfm1201 已提交
253

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

256
					}
R
rfm1201 已提交
257 258
					break;

259
			}
R
rfm1201 已提交
260

261 262 263
		}

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

265
			switch ( this.wrapT ) {
R
rfm1201 已提交
266

R
Rich Harris 已提交
267
				case RepeatWrapping:
R
rfm1201 已提交
268

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

R
Rich Harris 已提交
272
				case ClampToEdgeWrapping:
R
rfm1201 已提交
273

274 275
					uv.y = uv.y < 0 ? 0 : 1;
					break;
R
rfm1201 已提交
276

R
Rich Harris 已提交
277
				case MirroredRepeatWrapping:
R
rfm1201 已提交
278

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

281
						uv.y = Math.ceil( uv.y ) - uv.y;
R
rfm1201 已提交
282

283
					} else {
R
rfm1201 已提交
284

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

287 288
					}
					break;
R
rfm1201 已提交
289

290
			}
R
rfm1201 已提交
291

292 293 294
		}

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

296
			uv.y = 1 - uv.y;
R
rfm1201 已提交
297

298
		}
R
rfm1201 已提交
299

300 301
		return uv;

302
	}
M
Mr.doob 已提交
303

304
} );
305

T
Takahiro 已提交
306 307 308 309 310 311 312 313 314 315
Object.defineProperty( Texture.prototype, "needsUpdate", {

	set: function ( value ) {

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

	}

} );

R
Rich Harris 已提交
316

317
export { Texture };