MCPcopy Create free account
hub / github.com/processing/p5.js / saveObj

Method saveObj

src/webgl/p5.Geometry.js:359–408  ·  view source on GitHub ↗

* The `saveObj()` function exports `p5.Geometry` objects as * 3D models in the Wavefront .obj file format. * This way, you can use the 3D shapes you create in p5.js in other software * for rendering, animation, 3D printing, or more. * * The exported .obj file will include the faces an

(fileName = 'model.obj')

Source from the content-addressed store, hash-verified

357 * }
358 */
359 saveObj(fileName = 'model.obj') {
360 let objStr= '';
361
362
363 // Vertices
364 this.vertices.forEach(v => {
365 objStr += `v ${v.x} ${v.y} ${v.z}\n`;
366 });
367
368 // Texture Coordinates (UVs)
369 if (this.uvs && this.uvs.length > 0) {
370 for (let i = 0; i < this.uvs.length; i += 2) {
371 objStr += `vt ${this.uvs[i]} ${this.uvs[i + 1]}\n`;
372 }
373 }
374
375 // Vertex Normals
376 if (this.vertexNormals && this.vertexNormals.length > 0) {
377 this.vertexNormals.forEach(n => {
378 objStr += `vn ${n.x} ${n.y} ${n.z}\n`;
379 });
380
381 }
382 // Faces, obj vertex indices begin with 1 and not 0
383 // texture coordinate (uvs) and vertexNormal indices
384 // are indicated with trailing ints vertex/normal/uv
385 // ex 1/1/1 or 2//2 for vertices without uvs
386 this.faces.forEach(face => {
387 let faceStr = 'f';
388 face.forEach(index =>{
389 faceStr += ' ';
390 faceStr += index + 1;
391 if (this.vertexNormals.length > 0 || this.uvs.length > 0) {
392 faceStr += '/';
393 if (this.uvs.length > 0) {
394 faceStr += index + 1;
395 }
396 faceStr += '/';
397 if (this.vertexNormals.length > 0) {
398 faceStr += index + 1;
399 }
400 }
401 });
402 objStr += faceStr + '\n';
403 });
404
405 const blob = new Blob([objStr], { type: 'text/plain' });
406 downloadFile(blob, fileName , 'obj');
407
408 }
409
410 /**
411 * The `saveStl()` function exports `p5.Geometry` objects as

Callers

nothing calls this directly

Calls 1

downloadFileFunction · 0.90

Tested by

no test coverage detected