Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import * as macro from 'vtk.js/Sources/macros'; import DataAccessHelper from 'vtk.js/Sources/IO/Core/DataAccessHelper'; import vtkTexture from 'vtk.js/Sources/Rendering/Core/Texture'; // Enable data soure for DataAccessHelper import 'vtk.js/Sources/IO/Core/DataAccessHelper/LiteHttpDataAccessHelper'; // Just need HTTP // import 'vtk.js/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper'; // HTTP + gz // import 'vtk.js/Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper'; // html + base64 + zip // import 'vtk.js/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper'; // zip // ---------------------------------------------------------------------------- // vtkMTLReader methods // ---------------------------------------------------------------------------- function vtkMTLReader(publicAPI, model) { // Set our className model.classHierarchy.push('vtkMTLReader'); function imageReady() { model.requestCount--; if (model.requestCount === 0) { publicAPI.invokeBusy(false); } } function parseLine(line) { if (line[0] === '#' || line.length === 0) { return; } const tokens = line .split(/[ \t]+/) .map((s) => s.trim()) .filter((s) => s.length); if (tokens[0] === 'newmtl') { tokens.shift(); model.currentMaterial = tokens.join(' ').trim(); } else if (model.currentMaterial) { if (tokens.length < 2) { return; } if (!model.materials[model.currentMaterial]) { model.materials[model.currentMaterial] = {}; } model.materials[model.currentMaterial][tokens[0]] = tokens.slice(1); if (tokens[0] === 'map_Kd') { const image = new Image(); image.onload = () => setTimeout(imageReady, 0); image.src = [model.baseURL, tokens[1]].join('/'); model.materials[model.currentMaterial].image = image; model.requestCount++; } } } // Create default dataAccessHelper if not available if (!model.dataAccessHelper) { model.dataAccessHelper = DataAccessHelper.get('http'); } // Internal method to fetch Array function fetchData(url, options) { return model.dataAccessHelper.fetchText(publicAPI, url, options); } // Set DataSet url publicAPI.setUrl = (url, option = {}) => { if (url.indexOf('.mtl') === -1 && !option.fullpath) { model.baseURL = url; model.url = `${url}/index.mtl`; } else { model.url = url; // Remove the file in the URL const path = url.split('/'); path.pop(); model.baseURL = path.join('/'); } // Fetch metadata return publicAPI.loadData(option); }; // Fetch the actual data arrays publicAPI.loadData = (option) => new Promise((resolve, reject) => { fetchData(model.url, option).then( (content) => { publicAPI.parseAsText(content); resolve(); }, (err) => { reject(); } ); }); publicAPI.parseAsText = (content) => { publicAPI.modified(); model.materials = {}; content.split('\n').forEach(parseLine); }; // return Busy state publicAPI.isBusy = () => !!model.requestCount; publicAPI.getMaterialNames = () => Object.keys(model.materials); publicAPI.getMaterial = (name) => model.materials[name]; publicAPI.listImages = () => Object.keys(model.materials) .map((name) => model.materials[name].map_Kd) .filter((fileName) => !!fileName) .map((s) => s[0].trim()); publicAPI.setImageSrc = (imagePath, src) => new Promise((resolve, reject) => { const selectedName = Object.keys(model.materials).find( (name) => model.materials[name].map_Kd && model.materials[name].map_Kd[0].trim() === imagePath.trim() ); const material = model.materials[selectedName]; if (material && material.image) { material.image.src = src; material.image.onload = () => setTimeout(resolve, 0); } else { resolve(); } }); publicAPI.applyMaterialToActor = (name, actor) => { const material = model.materials[name]; if (material && actor) { const white = [1, 1, 1]; const actorProp = { ambientColor: material.Ka ? material.Ka.map((i) => Number(i)) : white, specularColor: material.Ks ? material.Ks.map((i) => Number(i)) : white, diffuseColor: material.Kd ? material.Kd.map((i) => Number(i)) : white, opacity: material.d ? Number(material.d) : 1, specularPower: material.Ns ? Number(material.Ns) : 1, }; const illum = Number(material.illum || 2); ['ambient', 'diffuse', 'specular'].forEach((k, idx) => { actorProp[k] = idx <= illum ? 1.0 : 0.0; }); if (material.image) { const texture = vtkTexture.newInstance({ interpolate: model.interpolateTextures, }); texture.setImage(material.image); actor.addTexture(texture); } actor.getProperty().set(actorProp); } }; } // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- const DEFAULT_VALUES = { numberOfOutputs: 1, requestCount: 0, materials: {}, interpolateTextures: true, // baseURL: null, // dataAccessHelper: null, // url: null, }; // ---------------------------------------------------------------------------- export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues); // Build VTK API macro.obj(publicAPI, model); macro.get(publicAPI, model, ['url', 'baseURL']); macro.setGet(publicAPI, model, [ 'dataAccessHelper', 'interpolateTextures', 'splitGroup', ]); macro.event(publicAPI, model, 'busy'); // Object methods vtkMTLReader(publicAPI, model); } // ---------------------------------------------------------------------------- export const newInstance = macro.newInstance(extend, 'vtkMTLReader'); // ---------------------------------------------------------------------------- export default { newInstance, extend }; |