Texture.js 5.7 KB
Newer Older
R
Rich Harris 已提交
1 2 3 4 5 6
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';

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

13 14
var textureId = 0;

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

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

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

21 22
	this.name = '';

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

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

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

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

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

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

R
Rich Harris 已提交
39 40
	this.offset = new Vector2( 0, 0 );
	this.repeat = new Vector2( 1, 1 );
41

42
	this.generateMipmaps = true;
43
	this.premultiplyAlpha = false;
44
	this.flipY = true;
45
	this.unpackAlignment = 4;	// valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml)
46 47 48 49 50

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

53
	this.version = 0;
54
	this.onUpdate = null;
55

M
Mr.doob 已提交
56
}
M
Mr.doob 已提交
57

R
Rich Harris 已提交
58 59
Texture.DEFAULT_IMAGE = undefined;
Texture.DEFAULT_MAPPING = UVMapping;
60

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

M
Mr.doob 已提交
63 64 65 66
	set: function ( value ) {

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

T
Style  
Tristan VALCKE 已提交
67
	}
68

M
Mr.doob 已提交
69
} );
70

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

T
Tristan VALCKE 已提交
73
	constructor: Texture,
M
Mr.doob 已提交
74

T
Tristan VALCKE 已提交
75
	isTexture: true,
M
Mr.doob 已提交
76

77
	clone: function () {
78

79
		return new this.constructor().copy( this );
80

81 82
	},

D
dubejf 已提交
83
	copy: function ( source ) {
84

85
		this.name = source.name;
86

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

		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 已提交
100
		this.format = source.format;
101
		this.type = source.type;
102

103 104
		this.offset.copy( source.offset );
		this.repeat.copy( source.repeat );
M
Mr.doob 已提交
105

106 107 108 109
		this.generateMipmaps = source.generateMipmaps;
		this.premultiplyAlpha = source.premultiplyAlpha;
		this.flipY = source.flipY;
		this.unpackAlignment = source.unpackAlignment;
110
		this.encoding = source.encoding;
M
Mr.doob 已提交
111

112
		return this;
113

114 115
	},

116 117 118 119 120 121 122
	toJSON: function ( meta ) {

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

			return meta.textures[ this.uuid ];

		}
E
elephantatwork 已提交
123

M
Mr.doob 已提交
124 125 126 127 128 129 130 131 132 133
		function getDataURL( image ) {

			var canvas;

			if ( image.toDataURL !== undefined ) {

				canvas = image;

			} else {

E
Eli Grey 已提交
134
				canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
M
Mr.doob 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
				canvas.width = image.width;
				canvas.height = image.height;

				canvas.getContext( '2d' ).drawImage( image, 0, 0, image.width, image.height );

			}

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

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

			} else {

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

			}

		}

E
elephantatwork 已提交
154 155
		var output = {
			metadata: {
156
				version: 4.4,
E
elephantatwork 已提交
157
				type: 'Texture',
158
				generator: 'Texture.toJSON'
E
elephantatwork 已提交
159
			},
160

161
			uuid: this.uuid,
162 163 164 165 166 167 168 169 170 171
			name: this.name,

			mapping: this.mapping,

			repeat: [ this.repeat.x, this.repeat.y ],
			offset: [ this.offset.x, this.offset.y ],
			wrap: [ this.wrapS, this.wrapT ],

			minFilter: this.minFilter,
			magFilter: this.magFilter,
M
Mr.doob 已提交
172 173 174
			anisotropy: this.anisotropy,

			flipY: this.flipY
E
elephantatwork 已提交
175 176
		};

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

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

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

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

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

187
			}
E
elephantatwork 已提交
188

189 190 191 192 193 194
			if ( meta.images[ image.uuid ] === undefined ) {

				meta.images[ image.uuid ] = {
					uuid: image.uuid,
					url: getDataURL( image )
				};
195 196 197 198

			}

			output.image = image.uuid;
E
elephantatwork 已提交
199 200 201

		}

202 203
		meta.textures[ this.uuid ] = output;

E
elephantatwork 已提交
204
		return output;
205

E
elephantatwork 已提交
206 207
	},

208
	dispose: function () {
209

210
		this.dispatchEvent( { type: 'dispose' } );
211

212 213 214 215
	},

	transformUv: function ( uv ) {

216
		if ( this.mapping !== UVMapping ) return;
217 218 219 220 221

		uv.multiply( this.repeat );
		uv.add( this.offset );

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

223
			switch ( this.wrapS ) {
R
rfm1201 已提交
224

R
Rich Harris 已提交
225
				case RepeatWrapping:
R
rfm1201 已提交
226

227 228
					uv.x = uv.x - Math.floor( uv.x );
					break;
R
rfm1201 已提交
229

R
Rich Harris 已提交
230
				case ClampToEdgeWrapping:
R
rfm1201 已提交
231

232 233
					uv.x = uv.x < 0 ? 0 : 1;
					break;
R
rfm1201 已提交
234

R
Rich Harris 已提交
235
				case MirroredRepeatWrapping:
R
rfm1201 已提交
236 237 238

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

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

241
					} else {
R
rfm1201 已提交
242

243
						uv.x = uv.x - Math.floor( uv.x );
R
rfm1201 已提交
244

245
					}
R
rfm1201 已提交
246 247
					break;

248
			}
R
rfm1201 已提交
249

250 251 252
		}

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

254
			switch ( this.wrapT ) {
R
rfm1201 已提交
255

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

258 259
					uv.y = uv.y - Math.floor( uv.y );
					break;
R
rfm1201 已提交
260

R
Rich Harris 已提交
261
				case ClampToEdgeWrapping:
R
rfm1201 已提交
262

263 264
					uv.y = uv.y < 0 ? 0 : 1;
					break;
R
rfm1201 已提交
265

R
Rich Harris 已提交
266
				case MirroredRepeatWrapping:
R
rfm1201 已提交
267

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

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

272
					} else {
R
rfm1201 已提交
273

274
						uv.y = uv.y - Math.floor( uv.y );
R
rfm1201 已提交
275

276 277
					}
					break;
R
rfm1201 已提交
278

279
			}
R
rfm1201 已提交
280

281 282 283
		}

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

285
			uv.y = 1 - uv.y;
R
rfm1201 已提交
286

287
		}
R
rfm1201 已提交
288

289
	}
M
Mr.doob 已提交
290

291
} );
292

R
Rich Harris 已提交
293

294
export { Texture };