1 line
22 KiB
Plaintext
1 line
22 KiB
Plaintext
|
|
{"version":3,"file":"TiltLoader.cjs","sources":["../../src/loaders/TiltLoader.js"],"sourcesContent":["import {\n BufferAttribute,\n BufferGeometry,\n DoubleSide,\n FileLoader,\n Group,\n Loader,\n Mesh,\n MeshBasicMaterial,\n RawShaderMaterial,\n TextureLoader,\n Quaternion,\n Vector3,\n} from 'three'\nimport { unzipSync, strFromU8 } from 'fflate'\n\nclass TiltLoader extends Loader {\n load(url, onLoad, onProgress, onError) {\n const scope = this\n\n const loader = new FileLoader(this.manager)\n loader.setPath(this.path)\n loader.setResponseType('arraybuffer')\n loader.setWithCredentials(this.withCredentials)\n\n loader.load(\n url,\n function (buffer) {\n try {\n onLoad(scope.parse(buffer))\n } catch (e) {\n if (onError) {\n onError(e)\n } else {\n console.error(e)\n }\n\n scope.manager.itemError(url)\n }\n },\n onProgress,\n onError,\n )\n }\n\n parse(buffer) {\n const group = new Group()\n // https://docs.google.com/document/d/11ZsHozYn9FnWG7y3s3WAyKIACfbfwb4PbaS8cZ_xjvo/edit#\n\n const zip = unzipSync(new Uint8Array(buffer.slice(16)))\n\n /*\n\t\tconst thumbnail = zip[ 'thumbnail.png' ].buffer;\n\t\tconst img = document.createElement( 'img' );\n\t\timg.src = URL.createObjectURL( new Blob( [ thumbnail ] ) );\n\t\tdocument.body.appendChild( img );\n\t\t*/\n\n const metadata = JSON.parse(strFromU8(zip['metadata.json']))\n\n /*\n\t\tconst blob = new Blob( [ zip[ 'data.sketch' ].buffer ], { type: 'application/octet-stream' } );\n\t\twindow.open( URL.createObjectURL( blob ) );\n\t\t*/\n\n const data = new DataView(zip['data.sketch'].buffer)\n\n const num_strokes = data.getInt32(16, true)\n\n const brushes = {}\n\n let offset = 20\n\n for (let i = 0; i < num_strokes; i++) {\n const brush_index = data.getInt32(offset, true)\n\n const brush_color = [\n data.getFloat32(offset + 4, true),\n data.getFloat32(offset + 8, true),\n data.getFloat32(offset + 12, true),\n data.getFloat32(offset + 16, true),\n ]\n const brush_size = data.getFloat32(offset + 20, true)\n const stroke_mask = data.getUint32(offset + 24, true)\n const controlpoint_mask = data.getUint32(offset + 28, true)\n\n let offset_stroke_mask = 0\n let offset_controlpoint_mask = 0\n\n for (let j = 0; j < 4; j++) {\n // TOFIX: I don't understand these masks yet\n\n const byte = 1 << j\n if ((stroke_mask & byte) > 0) offset_stroke_mask += 4\n if ((controlpoint_mask & byte) > 0) offset_controlpoint_mask += 4\n }\n\n // console.log( { brush_index, brush_color, brush_size, stroke_mask, controlpoint_mask } );\n // console.log( offset_stroke_mask, offset_controlpoint_mask );\n\n offset = offset + 28 + offset_stroke_mask + 4 // TOFIX: This is wrong\n\n const num_control_points = data.getInt32(offset, true)\n\n // console.log( { num_control_points } );\n\n const positions = new Float32Array(num_control_points * 3)\n const quaternions = new Float32Array(num_control_points * 4)\n\n offset = offset + 4\n\n for (let j = 0, k = 0; j < positions.length; j += 3, k += 4) {\n positions[j + 0] = data.getFloat32(offset + 0, true)\n positions[j + 1] = data.getFloat32(offset + 4, true)\n positions[j + 2] = data.getFloat32(offset + 8, true)\n\n quaternions[k + 0] = data.getFloat32(offset + 12, true)\n quaternions[k + 1] = data.getFloat32(offset + 16, true)\n quaternions[k + 2] = data.getFloat32(offset + 20, true)\n quaternions[k + 3] = data.getFloat32(offset + 24, true)\n\n offset = offset + 28 + offset_controlpoint_mask // TOFIX: This is wrong\n }\n\n if (brush_index in brushes === false) {\n brushes[brush_index] = []\n }\n\n brushes[brush_index].push([positions, quaternions, brush_size, brush_color])\n }\n\n for (const brush_index in brushes) {\n const geometry = new Str
|