|
1 | 1 | import { loadSkinToCanvas } from 'skinview-utils' |
2 | | -import * as THREE from 'three' |
3 | | -import stevePng from 'mc-assets/dist/other-textures/latest/entity/player/wide/steve.png' |
4 | | -import { getLoadedImage } from 'mc-assets/dist/utils' |
| 2 | +import { createCanvas, loadImageFromUrl } from '../utils' |
5 | 3 |
|
6 | | -const detectFullOffscreenCanvasSupport = () => { |
7 | | - if (typeof OffscreenCanvas === 'undefined') return false |
8 | | - try { |
9 | | - const canvas = new OffscreenCanvas(1, 1) |
10 | | - // Try to get a WebGL context - this will fail on iOS where only 2D is supported (iOS 16) |
11 | | - const gl = canvas.getContext('webgl2') || canvas.getContext('webgl') |
12 | | - return gl !== null |
13 | | - } catch (e) { |
14 | | - return false |
15 | | - } |
16 | | -} |
17 | | - |
18 | | -const hasFullOffscreenCanvasSupport = detectFullOffscreenCanvasSupport() |
19 | | - |
20 | | -export const loadThreeJsTextureFromUrlSync = (imageUrl: string) => { |
21 | | - const texture = new THREE.Texture() |
22 | | - const promise = getLoadedImage(imageUrl).then(image => { |
23 | | - texture.image = image |
24 | | - texture.needsUpdate = true |
25 | | - return texture |
26 | | - }) |
27 | | - return { |
28 | | - texture, |
29 | | - promise |
30 | | - } |
31 | | -} |
32 | | - |
33 | | -export const createCanvas = (width: number, height: number): OffscreenCanvas => { |
34 | | - if (hasFullOffscreenCanvasSupport) { |
35 | | - return new OffscreenCanvas(width, height) |
36 | | - } |
37 | | - const canvas = document.createElement('canvas') |
38 | | - canvas.width = width |
39 | | - canvas.height = height |
40 | | - return canvas as unknown as OffscreenCanvas // todo-low |
41 | | -} |
42 | | - |
43 | | -export const loadThreeJsTextureFromUrl = async (imageUrl: string) => { |
44 | | - const loaded = new THREE.TextureLoader().loadAsync(imageUrl) |
45 | | - return loaded |
46 | | -} |
47 | | -export const loadThreeJsTextureFromBitmap = (image: ImageBitmap) => { |
48 | | - const canvas = createCanvas(image.width, image.height) |
49 | | - const ctx = canvas.getContext('2d')! |
50 | | - ctx.drawImage(image, 0, 0) |
51 | | - const texture = new THREE.Texture(canvas) |
52 | | - texture.magFilter = THREE.NearestFilter |
53 | | - texture.minFilter = THREE.NearestFilter |
54 | | - return texture |
55 | | -} |
56 | | - |
57 | | -export const stevePngUrl = stevePng |
58 | | -export const steveTexture = loadThreeJsTextureFromUrl(stevePngUrl) |
59 | | - |
60 | | - |
61 | | -export async function loadImageFromUrl (imageUrl: string): Promise<ImageBitmap> { |
62 | | - const response = await fetch(imageUrl) |
63 | | - const blob = await response.blob() |
64 | | - return createImageBitmap(blob) |
65 | | -} |
| 4 | +export { default as stevePngUrl } from 'mc-assets/dist/other-textures/latest/entity/player/wide/steve.png' |
66 | 5 |
|
67 | 6 | const config = { |
68 | 7 | apiEnabled: true, |
|
0 commit comments