kovacsv / JSModeler

A JavaScript framework to create and visualize 3D models.
MIT License
648 stars 124 forks source link

svg to 3d #17

Closed xploshioOn closed 7 years ago

xploshioOn commented 7 years ago

Hello, I am trying to use your example on svg to 3d and is working but I need to add dynamic numbers on the 3d example, I am using svg.js library to create the svg but isn't working, the diference I saw here is that the svg that is generated don't have the "d" attribute

this is the one generated <svg id="tower" segmentlength="1" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="600"><defs id="SvgjsDefs1007"></defs><text id="SvgjsText1010" font-family="Helvetica, Arial, sans-serif" fill="#ff0099" font-size="180"><tspan id="SvgjsTspan1011" dy="234" x="0">300</tspan></text></svg>

and this is the one you are using on your example

  <svg id="svgid" xmlns="http://www.w3.org/2000/svg" version="1.1" width="180" height="180" viewBox="0 0 180 180" segmentlength="1">
        <path
          style="fill:#ff0000;fill-opacity:1;stroke:none"
          d="M 30 10 C 18.92 10 10 18.92 10 30 L 10 150 C 10 161.08 18.92 170 30 170 L 150 170 C 161.08 170 170 161.08 170 150 L 170 30 C 170 18.92 161.08 10 150 10 L 30 10 z M 87.25 43.6875 L 116.25 47 L 95.65625 56.0625 L 61.46875 48.65625 L 87.25 43.6875 z M 118.25 51.09375 L 114.1875 82.6875 L 96.5625 102.375 L 97.65625 60.15625 L 118.25 51.09375 z M 59.5625 52.75 L 93.65625 60.15625 L 92.46875 102.375 L 64.5625 86.5625 L 59.5625 52.75 z M 83.4375 114.875 C 85.95539 114.87503 88.42837 115.51662 90.84375 116.78125 L 88.40625 121.5 C 87.08462 120.47463 85.75064 119.96877 84.40625 119.96875 C 83.75682 119.96877 83.16893 120.13837 82.65625 120.46875 C 82.07518 120.84475 81.81249 121.34214 81.8125 121.96875 C 81.81249 122.584 82.17415 123.08693 82.9375 123.53125 C 83.27929 123.73635 84.32226 124.09246 86.03125 124.59375 C 88.17317 125.22039 89.68228 126.13055 90.59375 127.28125 C 91.37987 128.26108 91.78123 129.52442 91.78125 131.0625 C 91.78123 135.00456 90.05727 137.55371 86.59375 138.75 C 85.38606 139.17155 84.07746 139.375 82.6875 139.375 C 79.77082 139.375 77.05761 138.51596 74.5625 136.75 L 77.15625 131.84375 C 78.97916 133.48438 80.75683 134.3125 82.5 134.3125 C 83.28612 134.3125 83.95311 134.14584 84.5 133.78125 C 85.13801 133.3711 85.46874 132.78321 85.46875 132.03125 C 85.46874 131.34766 85.11262 130.76824 84.40625 130.3125 C 83.88215 129.97072 82.99186 129.63445 81.75 129.28125 C 80.24609 128.83693 79.32486 128.51466 78.9375 128.34375 C 78.32226 128.08172 77.77669 127.7907 77.34375 127.4375 C 76.10189 126.41212 75.5 124.85483 75.5 122.78125 C 75.5 120.61656 76.15007 118.83042 77.4375 117.40625 C 78.9414 115.73147 80.93098 114.87503 83.4375 114.875 z M 64.625 115.4375 L 70.71875 115.4375 L 70.71875 131.59375 C 70.718736 132.5394 70.64517 133.30241 70.53125 133.90625 C 70.291979 135.19369 69.60806 136.33822 68.46875 137.375 C 66.999013 138.7194 65.212883 139.40625 63.09375 139.40625 C 60.587236 139.40625 58.549477 138.27018 57 135.96875 L 60.96875 131.90625 C 61.151037 133.42155 61.798172 134.1875 62.9375 134.1875 C 64.099602 134.1875 64.679029 133.26889 64.65625 131.46875 L 64.625 130.1875 L 64.625 115.4375 z M 98.5625 115.4375 L 104.53125 115.4375 L 109.28125 127.875 L 114.28125 115.4375 L 120.3125 115.4375 L 123.84375 138.78125 L 117.78125 138.78125 L 116.03125 125.34375 L 110.40625 138.78125 L 108 138.78125 L 102.625 125.34375 L 100.625 138.78125 L 94.59375 138.78125 L 98.5625 115.4375 z "
        />
      </svg>

if this "dynamic" generated is incompatible with jsmodeler, how can I achieve this? My case, I am creating dynamic 3d models with data from backend, and need to add labels, and metrics to the 3d model, not outside, in the 3d model dinamically too. Do you have and idea for this? I am trying to create dynamic svg (the text) and add it to the model.

thanks for your help.

kovacsv commented 7 years ago

Hello,

JSModeler can convert only path elements to 3D model. In your SVG there is a text element. I think you can do two things:

Hope it helps, Viktor

xploshioOn commented 7 years ago

Hello Viktor, I worked with path and it works perfect. I made a lot of testing, and the thing of positioning is a little crazy here. it just takes the viewBox="1150 1200 500 500" parameters as positioning and size. So it don't use the same scale? and I have x-value and y-value, but now how I specify the z-value? I generate the model like this

function to_svg(string, minx, miny){
    var svgObject = document.getElementById('svgid');
    var modelAndMaterials, model, materials, meshes
    $.each(string.split(""), function(index, el) {
      minx = minx-70;
      svgObject.setAttribute("viewBox", ""+minx+" "+miny+" 500 500");
      $("#svgid").find("path").attr("d", dictionary[el]);
      modelAndMaterials = JSM.SvgToModel(svgObject, 0.3, 50);
      model = modelAndMaterials[0];
      materials = modelAndMaterials[1];
      meshes = JSM.ConvertModelToThreeMeshes(model,materials);
      viewer.AddMeshes(meshes);
      viewer.Draw();
    });
  }
  $(document).ready(function(){
    var settings = {
      cameraEyePosition: [-1.3, -1.5, 1.0],
      cameraCenterPosition: [0.5, 0.0, 0.5],
      cameraUpVector: [0, 0, 1]
    };
    plot_building("plot_div", <%= @p.to_json.html_safe %>, <%= @plot.to_json.html_safe %>, settings);

    to_svg("78", 1150, 1200);
    to_svg("test", -470, 600);
    to_svg(".hash()", 500, 200);

the plot_building function creates some elements dynamically and my function adds those texts to the model, but how can I specify the z-value? I tried adding a transformation but didn't work. thanks for your help, I'll attach an image of this, the text will be like legends for the model. 3dmodel

kovacsv commented 7 years ago

Hello,

It seems that the problem is with the view box. JSModeler does not care about the view box settings, it converts the model independently. I would try to transform the model back in x and y with the inverse of view box coordinates.

A working example would be useful for more investigation.

Viktor

xploshioOn commented 7 years ago

Hello Viktor, I uploaded an example so you can see https://xploshioon.github.io/3D-model-with-svg-characters/

the function that add the model to the model is in custom.js, I discovered this with a lot of testing. Can you please tell me how can I change that function so I can have positioned the text with z-values? thanks I tried with transformation, but no changes, thanks.

kovacsv commented 7 years ago

Hello,

You can transform the generated model with this code:

for (var i = 0; i < model.BodyCount (); i++) {
    model.GetBody (i).Transform (new JSM.TranslationTransformation (new JSM.Vector (0.0, -70.0, 0.0)));
}

By the way I think that this is not the best solution. JSModeler.js currently does not support 3D text natively. It seems you are using the three.js based viewer, so you can use THREE.TextGeometry, and append the native three.js meshes to the viewer.

Hope it helps, Viktor

xploshioOn commented 7 years ago

perfect, that worked. ok I see, I will try with your sugestion then.

thanks for the help and glad that you plan to add support for this. I will close the issue, thanks again.