Repository navigation
Expand file tree
/
Copy pathimage-texture.ts
More file actions
50 lines (46 loc) · 1.5 KB
/
Copy pathimage-texture.ts
File metadata and controls
50 lines (46 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {Component, Material, Property, Texture} from '@wonderlandengine/api';
import {setFirstMaterialTexture} from './utils/utils.js';
import {property} from '@wonderlandengine/api/decorators.js';
/**
* Downloads an image from URL and applies it as `diffuseTexture` or `flatTexture`
* to an attached mesh component.
*
* Materials from the following shaders are supported:
* - "Phong Opaque Textured"
* - "Flat Opaque Textured"
* - "Background"
* - "Physical Opaque Textured"
* - "Foliage"
*/
export class ImageTexture extends Component {
static TypeName = 'image-texture';
/** URL to download the image from */
@property.string()
url!: string;
/** Material to apply the video texture to */
@property.material({required: true})
material!: Material;
/** Name of the texture property to set */
@property.string('auto')
textureProperty!: string;
texture?: Texture;
start() {
this.engine.textures
.load(this.url, 'anonymous')
.then((texture) => {
this.texture = texture;
const mat = this.material;
if (!setFirstMaterialTexture(mat, texture, this.textureProperty)) {
console.error(
'Pipeline',
mat.pipeline,
'not supported by image-texture'
);
}
})
.catch(console.error);
}
onDestroy() {
this.texture?.destroy();
}
}