Texture.js 3.9 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
 */

7
THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
M
Mr.doob 已提交
8

M
Mr.doob 已提交
9 10
	Object.defineProperty( this, 'id', { value: THREE.TextureIdCount ++ } );

11
	this.uuid = THREE.Math.generateUUID();
M
Mr.doob 已提交
12

13
	this.name = '';
14
	this.sourceFile = '';
15

16
	this.image = image !== undefined ? image : THREE.Texture.DEFAULT_IMAGE;
17
	this.mipmaps = [];
18

19
	this.mapping = mapping !== undefined ? mapping : THREE.Texture.DEFAULT_MAPPING;
20

21 22
	this.wrapS = wrapS !== undefined ? wrapS : THREE.ClampToEdgeWrapping;
	this.wrapT = wrapT !== undefined ? wrapT : THREE.ClampToEdgeWrapping;
M
Mr.doob 已提交
23

24 25
	this.magFilter = magFilter !== undefined ? magFilter : THREE.LinearFilter;
	this.minFilter = minFilter !== undefined ? minFilter : THREE.LinearMipMapLinearFilter;
26

27 28
	this.anisotropy = anisotropy !== undefined ? anisotropy : 1;

29 30 31
	this.format = format !== undefined ? format : THREE.RGBAFormat;
	this.type = type !== undefined ? type : THREE.UnsignedByteType;

32
	this.offset = new THREE.Vector2( 0, 0 );
33 34
	this.repeat = new THREE.Vector2( 1, 1 );

35
	this.generateMipmaps = true;
36
	this.premultiplyAlpha = false;
37
	this.flipY = true;
38
	this.unpackAlignment = 4; // valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml)
39

40
	this.version = 0;
41
	this.onUpdate = null;
42

M
Mr.doob 已提交
43 44
};

M
Mr.doob 已提交
45
THREE.Texture.DEFAULT_IMAGE = undefined;
46
THREE.Texture.DEFAULT_MAPPING = THREE.UVMapping;
47

M
Mr.doob 已提交
48 49
THREE.Texture.prototype = {

50 51
	constructor: THREE.Texture,

M
Mr.doob 已提交
52 53
	set needsUpdate ( value ) {

M
Mr.doob 已提交
54
		if ( value === true ) this.version ++;
M
Mr.doob 已提交
55 56 57

	},

58
	clone: function () {
59

60
		return new this.constructor().copy( this );
61

62 63
	},

D
dubejf 已提交
64
	copy: function ( source ) {
65

66
		this.image = source.image;
67
		this.mipmaps = source.mipmaps.slice( 0 );
M
Mr.doob 已提交
68 69 70 71 72 73 74 75 76 77 78

		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 已提交
79
		this.format = source.format;
80
		this.type = source.type;
81

82 83
		this.offset.copy( source.offset );
		this.repeat.copy( source.repeat );
M
Mr.doob 已提交
84

85 86 87 88
		this.generateMipmaps = source.generateMipmaps;
		this.premultiplyAlpha = source.premultiplyAlpha;
		this.flipY = source.flipY;
		this.unpackAlignment = source.unpackAlignment;
M
Mr.doob 已提交
89

90
		return this;
91

92 93
	},

94 95 96 97 98 99 100
	toJSON: function ( meta ) {

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

			return meta.textures[ this.uuid ];

		}
E
elephantatwork 已提交
101

M
Mr.doob 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
		function getDataURL( image ) {

			var canvas;

			if ( image.toDataURL !== undefined ) {

				canvas = image;

			} else {

				canvas = document.createElement( 'canvas' );
				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 已提交
132 133
		var output = {
			metadata: {
134
				version: 4.4,
E
elephantatwork 已提交
135
				type: 'Texture',
136
				generator: 'Texture.toJSON'
E
elephantatwork 已提交
137
			},
138

139
			uuid: this.uuid,
140 141 142 143 144 145 146 147 148 149 150
			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,
			anisotropy: this.anisotropy
E
elephantatwork 已提交
151 152
		};

153
		if ( this.image !== undefined ) {
E
elephantatwork 已提交
154

155
			// TODO: Move to THREE.Image
E
elephantatwork 已提交
156

157
			var image = this.image;
E
elephantatwork 已提交
158

159
			if ( image.uuid === undefined ) {
E
elephantatwork 已提交
160

161
				image.uuid = THREE.Math.generateUUID(); // UGH
E
elephantatwork 已提交
162

163
			}
E
elephantatwork 已提交
164

165 166 167 168 169 170
			if ( meta.images[ image.uuid ] === undefined ) {

				meta.images[ image.uuid ] = {
					uuid: image.uuid,
					url: getDataURL( image )
				};
171 172 173 174

			}

			output.image = image.uuid;
E
elephantatwork 已提交
175 176 177

		}

178 179
		meta.textures[ this.uuid ] = output;

E
elephantatwork 已提交
180
		return output;
181

E
elephantatwork 已提交
182 183
	},

184
	dispose: function () {
185

186
		this.dispatchEvent( { type: 'dispose' } );
187

188
	}
M
Mr.doob 已提交
189 190

};
191

M
Mr.doob 已提交
192 193
THREE.EventDispatcher.prototype.apply( THREE.Texture.prototype );

194
THREE.TextureIdCount = 0;