Texture.js 6.3 KB
Newer Older
R
Rich Harris 已提交
1 2 3 4 5
import { EventDispatcher } from '../core/EventDispatcher';
import { UVMapping } from '../constants';
import { MirroredRepeatWrapping, ClampToEdgeWrapping, RepeatWrapping, LinearEncoding, UnsignedByteType, RGBAFormat, LinearMipMapLinearFilter, LinearFilter } from '../constants';
import { _Math } from '../math/Math';
import { Vector2 } from '../math/Vector2';
W
WestLangley 已提交
6
import { Matrix3 } from '../math/Matrix3';
R
Rich Harris 已提交
7

M
Mr.doob 已提交
8
/**
M
Mr.doob 已提交
9
 * @author mrdoob / http://mrdoob.com/
10
 * @author alteredq / http://alteredqualia.com/
11
 * @author szimek / https://github.com/szimek/
M
Mr.doob 已提交
12 13
 */

14 15
var textureId = 0;

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

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

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

22 23
	this.name = '';

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

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

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

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

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

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

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

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

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

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

58
	this.version = 0;
59
	this.onUpdate = null;
60

M
Mr.doob 已提交
61
}
M
Mr.doob 已提交
62

R
Rich Harris 已提交
63 64
Texture.DEFAULT_IMAGE = undefined;
Texture.DEFAULT_MAPPING = UVMapping;
65

T
Tristan VALCKE 已提交
66
Object.defineProperty( Texture.prototype, "needsUpdate", {
M
Mr.doob 已提交
67

M
Mr.doob 已提交
68 69 70 71
	set: function ( value ) {

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

T
Style  
Tristan VALCKE 已提交
72
	}
73

M
Mr.doob 已提交
74
} );
75

T
Tristan VALCKE 已提交
76
Object.assign( Texture.prototype, EventDispatcher.prototype, {
M
Mr.doob 已提交
77

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

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

82
	clone: function () {
83

84
		return new this.constructor().copy( this );
85

86 87
	},

D
dubejf 已提交
88
	copy: function ( source ) {
89

90
		this.name = source.name;
91

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

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

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

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

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

121
		return this;
122

123 124
	},

125 126 127 128 129 130 131
	toJSON: function ( meta ) {

		if ( meta.textures[ this.uuid ] !== undefined ) {

			return meta.textures[ this.uuid ];

		}
E
elephantatwork 已提交
132

M
Mr.doob 已提交
133 134 135 136 137 138 139 140 141 142
		function getDataURL( image ) {

			var canvas;

			if ( image.toDataURL !== undefined ) {

				canvas = image;

			} else {

E
Eli Grey 已提交
143
				canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
M
Mr.doob 已提交
144 145 146
				canvas.width = image.width;
				canvas.height = image.height;

147 148 149 150 151 152 153 154 155 156 157
				var context = canvas.getContext( '2d' );

				if ( image instanceof ImageData ) {

					context.putImageData( image, 0, 0 );

				} else {

					context.drawImage( image, 0, 0, image.width, image.height );

				}
M
Mr.doob 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

			}

			if ( canvas.width > 2048 || canvas.height > 2048 ) {

				return canvas.toDataURL( 'image/jpeg', 0.6 );

			} else {

				return canvas.toDataURL( 'image/png' );

			}

		}

E
elephantatwork 已提交
173 174
		var output = {
			metadata: {
175
				version: 4.5,
E
elephantatwork 已提交
176
				type: 'Texture',
177
				generator: 'Texture.toJSON'
E
elephantatwork 已提交
178
			},
179

180
			uuid: this.uuid,
181 182 183 184 185 186
			name: this.name,

			mapping: this.mapping,

			repeat: [ this.repeat.x, this.repeat.y ],
			offset: [ this.offset.x, this.offset.y ],
187
			rotation: this.rotation,
W
WestLangley 已提交
188 189 190 191

			matrixAutoUpdate: this.matrixAutoUpdate,
			matrix: this.matrix.toArray(),

192 193 194 195
			wrap: [ this.wrapS, this.wrapT ],

			minFilter: this.minFilter,
			magFilter: this.magFilter,
M
Mr.doob 已提交
196 197 198
			anisotropy: this.anisotropy,

			flipY: this.flipY
E
elephantatwork 已提交
199 200
		};

201
		if ( this.image !== undefined ) {
E
elephantatwork 已提交
202

203
			// TODO: Move to THREE.Image
E
elephantatwork 已提交
204

205
			var image = this.image;
E
elephantatwork 已提交
206

207
			if ( image.uuid === undefined ) {
E
elephantatwork 已提交
208

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

211
			}
E
elephantatwork 已提交
212

213 214 215 216 217 218
			if ( meta.images[ image.uuid ] === undefined ) {

				meta.images[ image.uuid ] = {
					uuid: image.uuid,
					url: getDataURL( image )
				};
219 220 221 222

			}

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

		}

226 227
		meta.textures[ this.uuid ] = output;

E
elephantatwork 已提交
228
		return output;
229

E
elephantatwork 已提交
230 231
	},

232
	dispose: function () {
233

234
		this.dispatchEvent( { type: 'dispose' } );
235

236 237 238 239
	},

	transformUv: function ( uv ) {

240
		if ( this.mapping !== UVMapping ) return;
241 242

		uv.multiply( this.repeat );
243
		uv.add( this.offset ); // todo: support rotation and matrixAutoUpdate flag
244 245

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

247
			switch ( this.wrapS ) {
R
rfm1201 已提交
248

R
Rich Harris 已提交
249
				case RepeatWrapping:
R
rfm1201 已提交
250

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

R
Rich Harris 已提交
254
				case ClampToEdgeWrapping:
R
rfm1201 已提交
255

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

R
Rich Harris 已提交
259
				case MirroredRepeatWrapping:
R
rfm1201 已提交
260 261 262

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

263
						uv.x = Math.ceil( uv.x ) - uv.x;
R
rfm1201 已提交
264

265
					} else {
R
rfm1201 已提交
266

267
						uv.x = uv.x - Math.floor( uv.x );
R
rfm1201 已提交
268

269
					}
R
rfm1201 已提交
270 271
					break;

272
			}
R
rfm1201 已提交
273

274 275 276
		}

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

278
			switch ( this.wrapT ) {
R
rfm1201 已提交
279

R
Rich Harris 已提交
280
				case RepeatWrapping:
R
rfm1201 已提交
281

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

R
Rich Harris 已提交
285
				case ClampToEdgeWrapping:
R
rfm1201 已提交
286

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

R
Rich Harris 已提交
290
				case MirroredRepeatWrapping:
R
rfm1201 已提交
291

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

294
						uv.y = Math.ceil( uv.y ) - uv.y;
R
rfm1201 已提交
295

296
					} else {
R
rfm1201 已提交
297

298
						uv.y = uv.y - Math.floor( uv.y );
R
rfm1201 已提交
299

300 301
					}
					break;
R
rfm1201 已提交
302

303
			}
R
rfm1201 已提交
304

305 306 307
		}

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

309
			uv.y = 1 - uv.y;
R
rfm1201 已提交
310

311
		}
R
rfm1201 已提交
312

313
	}
M
Mr.doob 已提交
314

315
} );
316

R
Rich Harris 已提交
317

318
export { Texture };