@renderlayer/textures 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @renderlayer/textures
@@ -0,0 +1,249 @@
1
+ import { ImageUtils, ClampToEdgeWrapping, LinearFilter, LinearMipmapLinearFilter, RGBAFormat, UnsignedByteType, NoColorSpace, UVMapping, MirroredRepeatWrapping, RepeatWrapping } from '@renderlayer/shared';
2
+ import { generateUUID, Vector2, Matrix3 } from '@renderlayer/math';
3
+ import { EventDispatcher } from '@renderlayer/core';
4
+
5
+ class Source {
6
+ constructor(data = null) {
7
+ this.isSource = true;
8
+ this.uuid = generateUUID();
9
+ this.data = data;
10
+ this.version = 0;
11
+ }
12
+ set needsUpdate(value) {
13
+ if (value === true)
14
+ this.version++;
15
+ }
16
+ toJSON(meta) {
17
+ const isRootObject = meta === void 0 || typeof meta === "string";
18
+ if (!isRootObject && meta.images[this.uuid] !== void 0) {
19
+ return meta.images[this.uuid];
20
+ }
21
+ const output = {
22
+ uuid: this.uuid,
23
+ url: ""
24
+ };
25
+ const data = this.data;
26
+ if (data !== null) {
27
+ let url;
28
+ if (Array.isArray(data)) {
29
+ url = [];
30
+ for (let i = 0, l = data.length; i < l; i++) {
31
+ if (data[i].isDataTexture) {
32
+ url.push(serializeImage(data[i].image));
33
+ } else {
34
+ url.push(serializeImage(data[i]));
35
+ }
36
+ }
37
+ } else {
38
+ url = serializeImage(data);
39
+ }
40
+ output.url = url;
41
+ }
42
+ if (!isRootObject) {
43
+ meta.images[this.uuid] = output;
44
+ }
45
+ return output;
46
+ }
47
+ }
48
+ function serializeImage(image) {
49
+ if (typeof HTMLImageElement !== "undefined" && image instanceof HTMLImageElement || typeof HTMLCanvasElement !== "undefined" && image instanceof HTMLCanvasElement || typeof ImageBitmap !== "undefined" && image instanceof ImageBitmap) {
50
+ return ImageUtils.getDataURL(image);
51
+ } else {
52
+ if (image.data) {
53
+ return {
54
+ data: Array.from(image.data),
55
+ width: image.width,
56
+ height: image.height,
57
+ type: image.data.constructor.name
58
+ };
59
+ } else {
60
+ console.warn("Texture: Unable to serialize Texture.");
61
+ return {};
62
+ }
63
+ }
64
+ }
65
+
66
+ let textureId = 0;
67
+ class Texture extends EventDispatcher {
68
+ constructor(image = Texture.DEFAULT_IMAGE, mapping = Texture.DEFAULT_MAPPING, wrapS = ClampToEdgeWrapping, wrapT = ClampToEdgeWrapping, magFilter = LinearFilter, minFilter = LinearMipmapLinearFilter, format = RGBAFormat, type = UnsignedByteType, anisotropy = Texture.DEFAULT_ANISOTROPY, colorSpace = NoColorSpace) {
69
+ super();
70
+ Object.defineProperty(this, "id", { value: textureId++ });
71
+ this.uuid = generateUUID();
72
+ this.isTexture = true;
73
+ this.name = "";
74
+ this.source = new Source(image);
75
+ this.mipmaps = [];
76
+ this.mapping = mapping;
77
+ this.channel = 0;
78
+ this.wrapS = wrapS;
79
+ this.wrapT = wrapT;
80
+ this.magFilter = magFilter;
81
+ this.minFilter = minFilter;
82
+ this.anisotropy = anisotropy;
83
+ this.format = format;
84
+ this.internalFormat = null;
85
+ this.type = type;
86
+ this.offset = new Vector2(0, 0);
87
+ this.repeat = new Vector2(1, 1);
88
+ this.center = new Vector2(0, 0);
89
+ this.rotation = 0;
90
+ this.matrixAutoUpdate = true;
91
+ this.matrix = new Matrix3();
92
+ this.generateMipmaps = true;
93
+ this.premultiplyAlpha = false;
94
+ this.flipY = true;
95
+ this.unpackAlignment = 4;
96
+ this.colorSpace = colorSpace;
97
+ this.userData = {};
98
+ this.version = 0;
99
+ this.onUpdate = null;
100
+ this.isRenderTargetTexture = false;
101
+ this.needsPMREMUpdate = false;
102
+ }
103
+ get image() {
104
+ return this.source.data;
105
+ }
106
+ set image(value = null) {
107
+ this.source.data = value;
108
+ }
109
+ updateMatrix() {
110
+ this.matrix.setUvTransform(
111
+ this.offset.x,
112
+ this.offset.y,
113
+ this.repeat.x,
114
+ this.repeat.y,
115
+ this.rotation,
116
+ this.center.x,
117
+ this.center.y
118
+ );
119
+ }
120
+ clone() {
121
+ return new this.constructor().copy(this);
122
+ }
123
+ copy(source) {
124
+ this.name = source.name;
125
+ this.source = source.source;
126
+ this.mipmaps = source.mipmaps.slice(0);
127
+ this.mapping = source.mapping;
128
+ this.channel = source.channel;
129
+ this.wrapS = source.wrapS;
130
+ this.wrapT = source.wrapT;
131
+ this.magFilter = source.magFilter;
132
+ this.minFilter = source.minFilter;
133
+ this.anisotropy = source.anisotropy;
134
+ this.format = source.format;
135
+ this.internalFormat = source.internalFormat;
136
+ this.type = source.type;
137
+ this.offset.copy(source.offset);
138
+ this.repeat.copy(source.repeat);
139
+ this.center.copy(source.center);
140
+ this.rotation = source.rotation;
141
+ this.matrixAutoUpdate = source.matrixAutoUpdate;
142
+ this.matrix.copy(source.matrix);
143
+ this.generateMipmaps = source.generateMipmaps;
144
+ this.premultiplyAlpha = source.premultiplyAlpha;
145
+ this.flipY = source.flipY;
146
+ this.unpackAlignment = source.unpackAlignment;
147
+ this.colorSpace = source.colorSpace;
148
+ this.userData = JSON.parse(JSON.stringify(source.userData));
149
+ this.needsUpdate = true;
150
+ return this;
151
+ }
152
+ toJSON(meta) {
153
+ const isRootObject = meta === void 0 || typeof meta === "string";
154
+ if (!isRootObject && meta.textures[this.uuid] !== void 0) {
155
+ return meta.textures[this.uuid];
156
+ }
157
+ const output = {
158
+ metadata: {
159
+ version: 4.5,
160
+ type: "Texture",
161
+ generator: "Texture.toJSON"
162
+ },
163
+ uuid: this.uuid,
164
+ name: this.name,
165
+ image: this.source.toJSON(meta).uuid,
166
+ mapping: this.mapping,
167
+ channel: this.channel,
168
+ repeat: [this.repeat.x, this.repeat.y],
169
+ offset: [this.offset.x, this.offset.y],
170
+ center: [this.center.x, this.center.y],
171
+ rotation: this.rotation,
172
+ wrap: [this.wrapS, this.wrapT],
173
+ format: this.format,
174
+ internalFormat: this.internalFormat,
175
+ type: this.type,
176
+ colorSpace: this.colorSpace,
177
+ minFilter: this.minFilter,
178
+ magFilter: this.magFilter,
179
+ anisotropy: this.anisotropy,
180
+ flipY: this.flipY,
181
+ generateMipmaps: this.generateMipmaps,
182
+ premultiplyAlpha: this.premultiplyAlpha,
183
+ unpackAlignment: this.unpackAlignment
184
+ };
185
+ if (Object.keys(this.userData).length > 0)
186
+ output.userData = this.userData;
187
+ if (!isRootObject) {
188
+ meta.textures[this.uuid] = output;
189
+ }
190
+ return output;
191
+ }
192
+ dispose() {
193
+ this.dispatchEvent({ type: "dispose" });
194
+ }
195
+ transformUv(uv) {
196
+ if (this.mapping !== UVMapping)
197
+ return uv;
198
+ uv.applyMatrix3(this.matrix);
199
+ if (uv.x < 0 || uv.x > 1) {
200
+ switch (this.wrapS) {
201
+ case RepeatWrapping:
202
+ uv.x = uv.x - Math.floor(uv.x);
203
+ break;
204
+ case ClampToEdgeWrapping:
205
+ uv.x = uv.x < 0 ? 0 : 1;
206
+ break;
207
+ case MirroredRepeatWrapping:
208
+ if (Math.abs(Math.floor(uv.x) % 2) === 1) {
209
+ uv.x = Math.ceil(uv.x) - uv.x;
210
+ } else {
211
+ uv.x = uv.x - Math.floor(uv.x);
212
+ }
213
+ break;
214
+ }
215
+ }
216
+ if (uv.y < 0 || uv.y > 1) {
217
+ switch (this.wrapT) {
218
+ case RepeatWrapping:
219
+ uv.y = uv.y - Math.floor(uv.y);
220
+ break;
221
+ case ClampToEdgeWrapping:
222
+ uv.y = uv.y < 0 ? 0 : 1;
223
+ break;
224
+ case MirroredRepeatWrapping:
225
+ if (Math.abs(Math.floor(uv.y) % 2) === 1) {
226
+ uv.y = Math.ceil(uv.y) - uv.y;
227
+ } else {
228
+ uv.y = uv.y - Math.floor(uv.y);
229
+ }
230
+ break;
231
+ }
232
+ }
233
+ if (this.flipY) {
234
+ uv.y = 1 - uv.y;
235
+ }
236
+ return uv;
237
+ }
238
+ set needsUpdate(value) {
239
+ if (value === true) {
240
+ this.version++;
241
+ this.source.needsUpdate = true;
242
+ }
243
+ }
244
+ }
245
+ Texture.DEFAULT_IMAGE = null;
246
+ Texture.DEFAULT_MAPPING = UVMapping;
247
+ Texture.DEFAULT_ANISOTROPY = 1;
248
+
249
+ export { Source, Texture };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@renderlayer/textures",
3
+ "version": "0.0.1",
4
+ "description": "@renderlayer/textures",
5
+ "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "sideEffects": false,
10
+ "buildOptions": {
11
+ "formats": [
12
+ "esm-bundler"
13
+ ]
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/epreston/renderlayer.git",
18
+ "directory": "packages/textures"
19
+ },
20
+ "keywords": [
21
+ "renderlayer"
22
+ ],
23
+ "license": "MIT",
24
+ "bugs": {
25
+ "url": "https://github.com/epreston/renderlayer/issues"
26
+ },
27
+ "homepage": "https://github.com/epreston/renderlayer/blob/main/packages/textures#readme",
28
+ "dependencies": {
29
+ "@renderlayer/math": "*",
30
+ "@renderlayer/core": "*",
31
+ "@renderlayer/shared": "*"
32
+ }
33
+ }