Allow exporting Three.js either as bare mesh or mesh with viewer.
Most people just want a single self-contained .html file, but more advanced usage will involve embedding in a webpage, where the default viewer would be copied and customized, and fed with bare mesh export.
This commit is contained in:
parent
750842610c
commit
86315b2b1f
@ -575,7 +575,8 @@ void SolveSpaceUI::ExportMeshTo(const std::string &filename) {
|
||||
ExportMeshAsStlTo(f, m);
|
||||
} else if(FilenameHasExtension(filename, ".obj")) {
|
||||
ExportMeshAsObjTo(f, m);
|
||||
} else if(FilenameHasExtension(filename, ".js")) {
|
||||
} else if(FilenameHasExtension(filename, ".js") ||
|
||||
FilenameHasExtension(filename, ".html")) {
|
||||
SEdgeList *e = &(SK.GetGroup(SS.GW.activeGroup)->displayEdges);
|
||||
ExportMeshAsThreeJsTo(f, filename, m, e);
|
||||
} else {
|
||||
@ -658,15 +659,14 @@ void SolveSpaceUI::ExportMeshAsObjTo(FILE *f, SMesh *sm) {
|
||||
//-----------------------------------------------------------------------------
|
||||
// Export the mesh as a JavaScript script, which is compatible with Three.js.
|
||||
//-----------------------------------------------------------------------------
|
||||
void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, SMesh *sm,
|
||||
SEdgeList *sel)
|
||||
void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename,
|
||||
SMesh *sm, SEdgeList *sel)
|
||||
{
|
||||
SPointList spl = {};
|
||||
STriangle *tr;
|
||||
SEdge *e;
|
||||
Vector bndl, bndh;
|
||||
const char html[] =
|
||||
"/* Autogenerated Three.js viewer for Solvespace Model (copy into another document):\n"
|
||||
const char htmlbegin[] =
|
||||
"<!DOCTYPE html>\n"
|
||||
"<html lang=\"en\">\n"
|
||||
" <head>\n"
|
||||
@ -674,7 +674,9 @@ void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, S
|
||||
" <title>Three.js Solvespace Mesh</title>\n"
|
||||
" <script src=\"http://threejs.org/build/three.min.js\"></script>\n"
|
||||
" <script src=\"http://threejs.org/examples/js/controls/OrthographicTrackballControls.js\"></script>\n"
|
||||
" <script src=\"%s.js\"></script>\n"
|
||||
" <style type=\"text/css\">\n"
|
||||
" body { margin: 0; overflow: hidden; }\n"
|
||||
" </style>\n"
|
||||
" </head>\n"
|
||||
" <body>\n"
|
||||
" <script>\n"
|
||||
@ -848,11 +850,13 @@ void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, S
|
||||
" }\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" document.body.appendChild(solvespace(three_js_%s));\n"
|
||||
" </script>\n"
|
||||
" <script>\n";
|
||||
const char htmlend[] =
|
||||
" document.body.appendChild(solvespace(solvespace_model_%s));\n"
|
||||
" </script>\n"
|
||||
" </body>\n"
|
||||
"</html>\n"
|
||||
"*/\n\n";
|
||||
"</html>\n";
|
||||
|
||||
// A default three.js viewer with OrthographicTrackballControls is
|
||||
// generated as a comment preceding the data.
|
||||
@ -875,23 +879,27 @@ void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, S
|
||||
double largerBoundXY = max((bndh.x - bndl.x), (bndh.y - bndl.y));
|
||||
double largerBoundZ = max(largerBoundXY, (bndh.z - bndl.z + 1));
|
||||
|
||||
std::string baseFilename = filename;
|
||||
std::string extension = filename,
|
||||
noExtFilename = filename;
|
||||
size_t dot = noExtFilename.rfind('.');
|
||||
extension.erase(0, dot + 1);
|
||||
noExtFilename.erase(dot);
|
||||
|
||||
std::string baseFilename = noExtFilename;
|
||||
size_t lastSlash = baseFilename.rfind(PATH_SEP);
|
||||
if(lastSlash == std::string::npos) oops();
|
||||
baseFilename.erase(0, lastSlash + 1);
|
||||
|
||||
size_t dot = baseFilename.rfind('.');
|
||||
baseFilename.erase(dot);
|
||||
|
||||
for(int i = 0; i < baseFilename.length(); i++) {
|
||||
if(!isalpha(baseFilename[i]) &&
|
||||
/* also permit UTF-8 */ !((unsigned char)baseFilename[i] >= 0x80))
|
||||
baseFilename[i] = '_';
|
||||
}
|
||||
|
||||
fprintf(f, html, baseFilename.c_str(), baseFilename.c_str());
|
||||
fprintf(f, "var three_js_%s = {\n"
|
||||
if(extension == "html")
|
||||
fputs(htmlbegin, f);
|
||||
|
||||
fprintf(f, "var solvespace_model_%s = {\n"
|
||||
" bounds: {\n"
|
||||
" x: %f, y: %f, near: %f, far: %f, z: %f, edgeBias: %f\n"
|
||||
" },\n",
|
||||
@ -974,6 +982,10 @@ void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, S
|
||||
}
|
||||
|
||||
fputs(" ]\n};\n", f);
|
||||
|
||||
if(extension == "html")
|
||||
fprintf(f, htmlend, baseFilename.c_str());
|
||||
|
||||
spl.Clear();
|
||||
}
|
||||
|
||||
|
@ -188,7 +188,8 @@ int LoadAutosaveYesNo(void);
|
||||
#define MESH_PATTERN \
|
||||
PAT1("STL Mesh", "stl") \
|
||||
PAT1("Wavefront OBJ Mesh", "obj") \
|
||||
PAT1("Three.js-compatible JavaScript Mesh", "js") \
|
||||
PAT1("Three.js-compatible Mesh, with viewer", "html") \
|
||||
PAT1("Three.js-compatible Mesh, mesh only", "js") \
|
||||
ENDPAT
|
||||
// NURBS surfaces
|
||||
#define SRF_PATTERN PAT2("STEP File", "step", "stp") ENDPAT
|
||||
@ -856,7 +857,8 @@ public:
|
||||
void ExportMeshTo(const std::string &filename);
|
||||
void ExportMeshAsStlTo(FILE *f, SMesh *sm);
|
||||
void ExportMeshAsObjTo(FILE *f, SMesh *sm);
|
||||
void ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, SMesh *sm, SEdgeList *sel);
|
||||
void ExportMeshAsThreeJsTo(FILE *f, const std::string &filename,
|
||||
SMesh *sm, SEdgeList *sel);
|
||||
void ExportViewOrWireframeTo(const std::string &filename, bool wireframe);
|
||||
void ExportSectionTo(const std::string &filename);
|
||||
void ExportWireframeCurves(SEdgeList *sel, SBezierList *sbl,
|
||||
|
Loading…
Reference in New Issue
Block a user