Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can textureResize become textureCompress on the web? #801

Closed
elalish opened this issue Jan 26, 2023 · 3 comments · Fixed by #1075
Closed

Can textureResize become textureCompress on the web? #801

elalish opened this issue Jan 26, 2023 · 3 comments · Fixed by #1075
Labels
feature New enhancement or request package:functions
Milestone

Comments

@elalish
Copy link

elalish commented Jan 26, 2023

Is your feature request related to a problem? Please describe.
I'd like to compress jpg/png textures into webP on the web. textureCompress is the function I want, but it doesn't (yet) work on the web. textureResize does work on the web, but only supports jpg/png and apparently not converting between them.

Describe the solution you'd like
Looking at your ndarray-pixels package, it seems like textureResize would be able to do everything textureCompress does if this check were removed (at least on web) and the options were added. Is there any reason not to?

Describe alternatives you've considered
Wait for sharp to add web support.

Additional context
I'd like to take a stab at making a very simple, very automatic glTF optimizer based on gltf-transform, probably living next to the model-viewer editor.

@elalish elalish added the feature New enhancement or request label Jan 26, 2023
@donmccurdy
Copy link
Owner

donmccurdy commented Jan 26, 2023

I'm not using very much of the Sharp API, I wonder if we could 'polyfill' Sharp with a small wrapper around ndarray-pixels, and use it as a fallback to the Sharp API? Would just need to match the API here:

const srcImage = texture.getImage()!;
const instance = encoder(srcImage);
// Convert if target and source formats differ.
if (srcMimeType !== dstMimeType) {
instance.toFormat(dstFormat);
}
// Resize.
if (resize) {
instance.resize(resize[0], resize[1], {
fit: 'inside',
kernel: resizeFilter,
withoutEnlargement: true,
});
}

An alternative here would be https://github.com/kleisauke/wasm-vips ... same underlying native library as Sharp, compiled to WASM, but note the "early development" disclaimers. I tend to assume the compression with libvips is better than what browsers provide, but I don't really know on that.

@donmccurdy donmccurdy added this to the Backlog milestone Jan 26, 2023
@elalish
Copy link
Author

elalish commented Jan 26, 2023

Yes, I guess I thought most of the code for that polyfill was already in textureResize. Perhaps those two functions could be combined, thus providing the fallback with a single API? I have no problem with browser-level compression, so long as we can go from lossless to lossy.

@donmccurdy
Copy link
Owner

donmccurdy commented Jan 27, 2023

Maybe just removing textureResize would be the thing to do, yeah. Can't resize without recompressing anyway. Smaller/simpler API surface to maintain. Then let textureCompress default to ndarray-pixels if no encoder is given, so it works on the web out of the box.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New enhancement or request package:functions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants