MCPcopy
hub / github.com/CesiumGS/obj2gltf / createDiffuseAlphaTexture

Function createDiffuseAlphaTexture

lib/loadMtl.js:511–596  ·  view source on GitHub ↗
(diffuseTexture, alphaTexture, options)

Source from the content-addressed store, hash-verified

509}
510
511function createDiffuseAlphaTexture(diffuseTexture, alphaTexture, options) {
512 const packDiffuse = defined(diffuseTexture);
513 const packAlpha = defined(alphaTexture);
514
515 if (!packDiffuse) {
516 return undefined;
517 }
518
519 if (!packAlpha) {
520 return diffuseTexture;
521 }
522
523 if (diffuseTexture === alphaTexture) {
524 return diffuseTexture;
525 }
526
527 if (!defined(diffuseTexture.pixels) || !defined(alphaTexture.pixels)) {
528 options.logger(
529 `Could not get decoded texture data for ${diffuseTexture.path} or ${alphaTexture.path}. The material will be created without an alpha texture.`,
530 );
531 return diffuseTexture;
532 }
533
534 const packedTextures = [diffuseTexture, alphaTexture];
535 const dimensions = getMinimumDimensions(packedTextures, options);
536 const width = dimensions[0];
537 const height = dimensions[1];
538 const pixelsLength = width * height;
539 const pixels = Buffer.alloc(pixelsLength * 4, 0xff); // Initialize with 4 channels
540 const scratchChannel = Buffer.alloc(pixelsLength);
541
542 // Write into the R, G, B channels
543 const redChannel = getTextureChannel(
544 diffuseTexture,
545 0,
546 width,
547 height,
548 scratchChannel,
549 );
550 writeChannel(pixels, redChannel, 0);
551 const greenChannel = getTextureChannel(
552 diffuseTexture,
553 1,
554 width,
555 height,
556 scratchChannel,
557 );
558 writeChannel(pixels, greenChannel, 1);
559 const blueChannel = getTextureChannel(
560 diffuseTexture,
561 2,
562 width,
563 height,
564 scratchChannel,
565 );
566 writeChannel(pixels, blueChannel, 2);
567
568 // First try reading the alpha component from the alpha channel, but if it is a single color read from the red channel instead.

Calls 4

getMinimumDimensionsFunction · 0.85
getTextureChannelFunction · 0.85
writeChannelFunction · 0.85
isChannelSingleColorFunction · 0.85

Tested by

no test coverage detected