Texture.js 5.6 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 51


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

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

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

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

62
Object.assign( Texture.prototype, EventDispatcher.prototype, {
M
Mr.doob 已提交
63

R
Rich Harris 已提交
64
	constructor: Texture,
65

66 67
	isTexture: true,

68
	set needsUpdate( value ) {
M
Mr.doob 已提交
69

M
Mr.doob 已提交
70
		if ( value === true ) this.version ++;
M
Mr.doob 已提交
71 72 73

	},

74
	clone: function () {
75

76
		return new this.constructor().copy( this );
77

78 79
	},

D
dubejf 已提交
80
	copy: function ( source ) {
81

82
		this.image = source.image;
83
		this.mipmaps = source.mipmaps.slice( 0 );
M
Mr.doob 已提交
84 85 86 87 88 89 90 91 92 93 94

		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 已提交
95
		this.format = source.format;
96
		this.type = source.type;
97

98 99
		this.offset.copy( source.offset );
		this.repeat.copy( source.repeat );
M
Mr.doob 已提交
100

101 102 103 104
		this.generateMipmaps = source.generateMipmaps;
		this.premultiplyAlpha = source.premultiplyAlpha;
		this.flipY = source.flipY;
		this.unpackAlignment = source.unpackAlignment;
105
		this.encoding = source.encoding;
M
Mr.doob 已提交
106

107
		return this;
108

109 110
	},

111 112 113 114 115 116 117
	toJSON: function ( meta ) {

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

			return meta.textures[ this.uuid ];

		}
E
elephantatwork 已提交
118

M
Mr.doob 已提交
119 120 121 122 123 124 125 126 127 128
		function getDataURL( image ) {

			var canvas;

			if ( image.toDataURL !== undefined ) {

				canvas = image;

			} else {

E
Eli Grey 已提交
129
				canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
M
Mr.doob 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
				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 已提交
149 150
		var output = {
			metadata: {
151
				version: 4.4,
E
elephantatwork 已提交
152
				type: 'Texture',
153
				generator: 'Texture.toJSON'
E
elephantatwork 已提交
154
			},
155

156
			uuid: this.uuid,
157 158 159 160 161 162 163 164 165 166
			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 已提交
167 168 169
			anisotropy: this.anisotropy,

			flipY: this.flipY
E
elephantatwork 已提交
170 171
		};

172
		if ( this.image !== undefined ) {
E
elephantatwork 已提交
173

174
			// TODO: Move to THREE.Image
E
elephantatwork 已提交
175

176
			var image = this.image;
E
elephantatwork 已提交
177

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

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

182
			}
E
elephantatwork 已提交
183

184 185 186 187 188 189
			if ( meta.images[ image.uuid ] === undefined ) {

				meta.images[ image.uuid ] = {
					uuid: image.uuid,
					url: getDataURL( image )
				};
190 191 192 193

			}

			output.image = image.uuid;
E
elephantatwork 已提交
194 195 196

		}

197 198
		meta.textures[ this.uuid ] = output;

E
elephantatwork 已提交
199
		return output;
200

E
elephantatwork 已提交
201 202
	},

203
	dispose: function () {
204

205
		this.dispatchEvent( { type: 'dispose' } );
206

207 208 209 210
	},

	transformUv: function ( uv ) {

211
		if ( this.mapping !== UVMapping ) return;
212 213 214 215 216

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

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

218
			switch ( this.wrapS ) {
R
rfm1201 已提交
219

R
Rich Harris 已提交
220
				case RepeatWrapping:
R
rfm1201 已提交
221

222 223
					uv.x = uv.x - Math.floor( uv.x );
					break;
R
rfm1201 已提交
224

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

227 228
					uv.x = uv.x < 0 ? 0 : 1;
					break;
R
rfm1201 已提交
229

R
Rich Harris 已提交
230
				case MirroredRepeatWrapping:
R
rfm1201 已提交
231 232 233

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

234
						uv.x = Math.ceil( uv.x ) - uv.x;
R
rfm1201 已提交
235

236
					} else {
R
rfm1201 已提交
237

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

240
					}
R
rfm1201 已提交
241 242
					break;

243
			}
R
rfm1201 已提交
244

245 246 247
		}

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

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

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

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

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

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

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

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

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

267
					} else {
R
rfm1201 已提交
268

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

271 272
					}
					break;
R
rfm1201 已提交
273

274
			}
R
rfm1201 已提交
275

276 277 278
		}

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

280
			uv.y = 1 - uv.y;
R
rfm1201 已提交
281

282
		}
R
rfm1201 已提交
283

284
	}
M
Mr.doob 已提交
285

286
} );
287

R
Rich Harris 已提交
288

289
export { Texture };