resource/imageasset
이미지 애셋.
Image asset.
ImageAssetextends Asset
이미지 애셋.
- 읽을 때 파일 바이트를 받아 그림 요소와, 주 스레드 밖에서 푼 ImageBitmap 을 함께 짓는다. (createImageBitmap) 그래서 처음 그릴 때 texImage2D 가 주 스레드에서 그림을 다시 풀지 않는다. (풀린 그림은 처음 올린 뒤 닫는다) ImageBitmap 결과가 그림 요소를 올린 결과와 화소까지 같은 꼴만 미리 푼다. (DecodedImageStore)
- 읽은 그림은 씬을 불러오는 동안(로딩 화면) SceneManager 가 GPU 에 올린다. (Graphic.prepareLoadedImages)
- 바이트를 받지 못하면(file:// 주소, CORS 없는 다른 출처) 예전처럼 그림 요소에 주소를 바로 넣는다. 그때 getImage().src 는 그 주소이고, 바이트를 받았으면 blob: 주소다. (애셋 경로는 getAssetPath)
Image asset.
- When loading, receives the file bytes and builds both the image element and an ImageBitmap decoded off the main thread. (createImageBitmap) So texImage2D does not decode the image again on the main thread when it is first drawn. (The decoded image is closed after the first upload) Only formats whose ImageBitmap result is identical to uploading the image element, down to the pixel, are pre-decoded. (DecodedImageStore)
- SceneManager uploads the loaded images to the GPU while the scene loads (the loading screen). (Graphic.prepareLoadedImages)
- If the bytes cannot be received (a file:// address, another origin without CORS), the address is put into the image element directly as before. In that case getImage().src is that address, and it is a blob: address if the bytes were received. (The asset path is getAssetPath)
import { ImageAsset } from "./build/vanilla.js";
const imageAsset = new ImageAsset();
await imageAsset.load("examples/assets/spritesheet.png");
const imageSize = imageAsset.getImageSize();
console.log(imageSize.x, imageSize.y, imageAsset.isLoaded());이미지 요소. (생성할 때 빈 요소를 만들고, 읽을 때마다 새 요소로 바꾼다)
Image element. (an empty element is made at construction and replaced with a new one on every load)
반환ReturnsHTMLImageElement
비동기 애셋 로드.
Loads the asset asynchronously.
- assetPathstring
- 그림 파일 경로. (이미 읽혔으면 아무것도 하지 않는다)Image file path. (does nothing if already loaded)
반환ReturnsPromise<void> 그림이 다 읽히면 풀리는 프로미스. (읽지 못하면 거부된다)A promise resolved when the image is loaded. (rejects if it cannot be loaded)
짝Counterpart: vanilla-engine ImageAsset.load
이미 가진 바이트로 읽기. (png, jpeg, webp 처럼 브라우저가 푸는 그림, 이미 읽혔어도 새 바이트로 바꾼다)
- 풀지 못하면 예외를 낸다. (load 와 같다)
Loads from bytes already held. (Images the browser decodes such as png, jpeg, webp, replaces with the new bytes even if already loaded)
- Throws an exception if it cannot decode. (Same as load)
- assetPathstring
- 실패 알림과 getAssetPath 에 쓸 이름.Name used for failure notices and getAssetPath.
- bytesArrayBuffer | ArrayBufferView | Blob
- 그림 파일의 바이트. (Blob 이면 그대로 쓰고, 아니면 Blob 으로 감싼다)Bytes of the image file. (used as is if a Blob, otherwise wrapped in a Blob)
반환ReturnsPromise<void> 그림이 다 풀리면 풀리는 프로미스. (풀지 못하면 거부된다)A promise resolved when the image is decoded. (rejects if it cannot be decoded)
짝Counterpart: vanilla-engine ImageAsset.load_from_bytes
import { ImageAsset } from "./build/vanilla.js";
const response = await fetch("assets/icons/icon.png");
const bytes = await response.arrayBuffer();
const imageAsset = new ImageAsset();
await imageAsset.loadFromBytes("icon.png", bytes);
const imageSize = imageAsset.getImageSize();
console.log(imageAsset.getAssetPath(), imageSize.x, imageSize.y);애셋 언로드. (아직 올리지 않은 미리 푼 그림이 있으면 닫는다)
Unloads the asset. (Closes the pre-decoded image if it has not been uploaded yet)
짝Counterpart: vanilla-engine ImageAsset.unload
이미지 반환.
Returns the image.
반환ReturnsHTMLImageElement 이미지 요소.Image element.
짝Counterpart: vanilla-engine ImageAsset.get_image
이미지 크기 반환.
Returns the image size.
반환ReturnsVector2 이미지 크기. (픽셀, 읽기 전이면 0, 0)Image size. (pixels, 0, 0 before loading)
짝Counterpart: vanilla-engine ImageAsset.get_image_size