MCPcopy Index your code
hub / github.com/processing/p5.js / parseBinarySTL

Function parseBinarySTL

src/webgl/loading.js:737–817  ·  view source on GitHub ↗

* @private * This function parses the Binary STL files. * https://en.wikipedia.org/wiki/STL_%28file_format%29#Binary_STL * * Currently there is no support for the colors provided in STL files.

(model, buffer)

Source from the content-addressed store, hash-verified

735 * Currently there is no support for the colors provided in STL files.
736 */
737 function parseBinarySTL(model, buffer) {
738 const reader = new DataView(buffer);
739
740 // Number of faces is present following the header
741 const faces = reader.getUint32(80, true);
742 let r,
743 g,
744 b,
745 hasColors = false,
746 colors;
747 let defaultR, defaultG, defaultB;
748
749 // Binary files contain 80-byte header, which is generally ignored.
750 for (let index = 0; index < 80 - 10; index++) {
751 // Check for `COLOR=`
752 if (
753 reader.getUint32(index, false) === 0x434f4c4f /*COLO*/ &&
754 reader.getUint8(index + 4) === 0x52 /*'R'*/ &&
755 reader.getUint8(index + 5) === 0x3d /*'='*/
756 ) {
757 hasColors = true;
758 colors = [];
759
760 defaultR = reader.getUint8(index + 6) / 255;
761 defaultG = reader.getUint8(index + 7) / 255;
762 defaultB = reader.getUint8(index + 8) / 255;
763 // To be used when color support is added
764 // alpha = reader.getUint8(index + 9) / 255;
765 }
766 }
767 const dataOffset = 84;
768 const faceLength = 12 * 4 + 2;
769
770 // Iterate the faces
771 for (let face = 0; face < faces; face++) {
772 const start = dataOffset + face * faceLength;
773 const normalX = reader.getFloat32(start, true);
774 const normalY = reader.getFloat32(start + 4, true);
775 const normalZ = reader.getFloat32(start + 8, true);
776
777 if (hasColors) {
778 const packedColor = reader.getUint16(start + 48, true);
779
780 if ((packedColor & 0x8000) === 0) {
781 // facet has its own unique color
782 r = (packedColor & 0x1f) / 31;
783 g = ((packedColor >> 5) & 0x1f) / 31;
784 b = ((packedColor >> 10) & 0x1f) / 31;
785 } else {
786 r = defaultR;
787 g = defaultG;
788 b = defaultB;
789 }
790 }
791 const newNormal = new Vector(normalX, normalY, normalZ);
792
793 for (let i = 1; i <= 3; i++) {
794 const vertexstart = start + i * 12;

Callers 1

parseSTLFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected