Text doesn't seem to work the same in 2.0

Discussion in 'ShapeJS' started by niquegeek, May 19, 2016.

  1. niquegeek
    niquegeek Active Member
    My script worked great in the old version,

    This is basically the signet ring, with an text engraving inside the band, any ideas what I'm doing wrong, I think something changed and I'm missing it.

    I think the issue is happening around my "engraving" variable, the ring generates but not the text engraving.
    Any help is appreciated, thanks!

    var params = [
    {
    name: "image",
    label: "Pattern Image",
    desc: "Image",
    type: "uri",
    defaultVal: " http:\/\/www.shapeways.com\/rrstatic\/img\/shapejs\/creator\ /sw_logo.png "
    }
    ];

    var voxelSize = 0.05*MM;

    function makeImage(path, width, height, thickness){

    var img = new Image3D(path, width, height, thickness);
    img.setBaseThickness(0.0);
    img.setBlurWidth(0.5*voxelSize);
    img.setUseGrayscale(false);
    return img;
    }


    function main(args){
    // see signet_ring.svg for drawing

    // user definable variables

    var imageThickness = 1*MM;

    var ringSize = 19.76*MM;
    var ringThickness = 1.5*MM;
    var ringWidth = 5*MM;

    var bezelAngle = Math.PI/8; // angle of bezel sides, good values are between PI/5 and PI/20
    var bezelHeight = 1.5*MM; // thickness of the ring bezel

    var imagePath = args.image;
    var imageScale = 0.95; // size fo image relative to bezel size


    var engraving = new Text("Example Text",
    "Roboto", ringSize*3.1,ringWidth-1.5*MM, 0.75*MM, voxelSize);

    var ct = new CompositeTransform();
    ct.add(new RingWrap(-ringSize/2));
    ct.add(new Rotation(1,0,0, -Math.PI/2));

    engraving.setTransform(ct);

    // end of user definable variables


    var inRad = ringSize/2;
    var outRad = inRad + ringThickness;


    //var cylY = 1.5*MM;

    var By = inRad + bezelHeight;

    var sinA = Math.sin(bezelAngle);
    var BC = (Ay - By)*Math.tan(bezelAngle);
    var Ay = outRad/sinA;
    var Fy = Ay*sinA*sinA;
    var bezelBase = Fy;
    var bezelTop = By;

    var margin = 0.2*MM;// empty space around object
    // size of grid to fit the ring
    var gridWidth = 2*outRad + 2*margin;
    var gridHeight = 2*bezelTop + 2*margin;


    var Cx = (Ay - By)*Math.tan(bezelAngle);
    var Dx = Cx*(1 + bezelHeight/(Ay - By));

    var cutR = inRad; // radius of cylindrical transition

    var Ex = ringWidth/2 + cutR;
    var dd = 1*MM; // width of small bezel undercut
    var Ey = inRad - Math.sqrt(cutR*cutR - (Ex - Dx + dd)*(Ex - Dx + dd));

    var imageWidth = imageScale*Cx*2;
    var imageHeight = imageWidth;

    var image = makeImage(imagePath, imageWidth, imageHeight,imageThickness);
    var imageTransform = new CompositeTransform();
    imageTransform.add(new Rotation(new Vector3d(1,0,0), -Math.PI/2));
    imageTransform.add(new Translation(0,By, 0));
    image.setTransform(imageTransform);

    var ringBase = new Sphere(outRad);

    // bezel is cone truncated by 2 parallel planes
    var bezel = new Intersection();
    bezel.add(new Cone(new Vector3d(0,Ay, 0), new Vector3d(0,-1,0), bezelAngle));
    bezel.add(new Plane(new Vector3d(0,1,0), bezelTop));
    bezel.add(new Plane(new Vector3d(0,-1,0), -bezelBase));
    bezel.setBlend(0.1*MM);

    var ring = new Union();
    ring.add(ringBase);
    ring.add(bezel);

    var cutoff = new Union();

    var rounding = 0.02*MM;
    var gw = gridWidth;
    var gw2 = gridWidth/2;
    cutoff.add(new Cylinder(new Vector3d(0, 0, -gw2),new Vector3d(0,0,gw2),inRad));
    cutoff.add(new Cylinder(new Vector3d(-gw2, Ey, Ex), new Vector3d(gw2, Ey, Ex), cutR));
    cutoff.add(new Cylinder(new Vector3d(-gw2, Ey, -Ex), new Vector3d(gw2, Ey, -Ex), cutR));
    cutoff.add(new Box(0,-inRad, Ex+rounding/2, gw, 2*(inRad+Ey), 2*cutR));
    cutoff.add(new Box(0,-inRad, -Ex-rounding/2, gw, 2*(inRad+Ey), 2*cutR));
    cutoff.setBlend(rounding); // TODO this blend rounds the inner edges but causes an artifact

    var fullRing = new Subtraction(ring, cutoff);
    fullRing.setBlend(0.1*MM);

    var ringWithImage = new Subtraction(fullRing, image);

    var ringWithEngraving = new Subtraction(ringWithImage, engraving);

    var bounds = new Bounds(-gridWidth/2, gridWidth/2, -gridHeight/2, gridHeight/2, -gridWidth/2, gridWidth/2);
    var scene = new Scene(ringWithEngraving,bounds,voxelSize);
    scene.setName("SignetRing");

    return scene;
    }
     
  2. AlanHudson
    AlanHudson Shapeways Employee Dev Team
    Here is an example with text inside. It's possible we borked the original Text node. We've been moving to this pattern of using a Text2D datasource feed into a Image3D(that's what the Text was doing under the covers anyway).

    var params = [
    {
    name: "size",
    label: "Ring Size",
    desc: "US Ring Size",
    type: "double",
    rangeMin: 0,
    rangeMax: 16,
    step: 0.25,
    defaultVal: 11
    },
    {
    name: "ringWidth",
    desc: "Ring Width",
    label: "Ring Width",
    type: "double",
    rangeMin: 1,
    rangeMax: 10,
    step: 0.1,
    defaultVal: 2,
    unit: "MM"
    },
    {
    name: "image",
    label: "Pattern Image",
    desc: "Image",
    type: "uri",
    defaultVal: " https://www.shapeways.com/rrstatic/img/shapejs/creator/sw_log o.png",
    group: "Image"
    },
    {
    name: "useGrayscale",
    label: "Use Grayscale",
    desc: "Use Grayscale",
    type: "boolean",
    defaultVal: false,
    group: "Image"

    },
    {
    name: "imageScale",
    desc: "Scale Image to fit ring",
    type: "double",
    rangeMin:0.1,
    rangeMax:1.25,
    step: 0.01,
    defaultVal: 0.95,
    group: "Image"
    },
    {
    name: "text",
    label: "Text",
    desc: "Text",
    type: "string",
    group: "Text"
    },
    {
    name: "textStyle",
    label: "Style",
    desc: "Font",
    type: "enum",
    values: ["HelveticaRounded LT Std Bd"],
    group: "Text"
    },
    {
    name: "textSize",
    desc: "Text Height(pt)",
    type: "double",
    rangeMin:6,
    rangeMax:20,
    step: 1,
    defaultVal: 7,
    group: "Text"
    },
    {
    name: "textDepth",
    desc: "Text Depth",
    type: "double",
    rangeMin: 0.2,
    rangeMax: 0.8,
    step: 0.1,
    defaultVal: 0.4,
    unit: "MM",
    group: "Text"
    }
    ];

    var vs = 0.04*MM;
    var POINT_SIZE = 25.4 * MM / 72;
    var TORAD = PI / 180;

    function ringSizeToDiameter(size) {
    var diameter = 11.63*MM + (0.8128 * MM * size);

    return diameter;
    }

    function makeImage(path, width, height, thickness,gray){

    var img = new Image3D(path, width, height, thickness);
    img.setBaseThickness(0.0);
    img.setBlurWidth(0.5*vs);
    if (gray == true) {
    img.setUseGrayscale(true);
    img.setBaseThreshold(0.01);
    } else {
    img.setUseGrayscale(false);
    img.setBaseThreshold(0.5);
    }
    img.setImagePlace(Image3D.IMAGE_PLACE_BOTH);
    img.setImageType(Image3D.IMAGE_TYPE_EMBOSSED);

    return img;
    }


    function makeText3D(args) {
    var text2d = new Text2D(args.text);
    text2d.setFontName("HelveticaRounded LT Std Bd");
    text2d.setFontStyle(Text2D.BOLD);

    var bandLength = ringSizeToDiameter(args.size) * Math.PI;

    var tbx = bandLength;
    var tby = (args.textSize * POINT_SIZE);
    var tbz = 2*args.textDepth;

    var tvs = 0.05 * MM; // text voxel size
    text2d.setInset(2 * tvs);
    text2d.setVoxelSize(tvs);
    text2d.setFit("vertical"); // vertical, horizontal, both
    text2d.setHorizAlign("center"); // left, right, center
    text2d.setWidth(tbx);
    text2d.setHeight(tby);

    var imgBox = new Image3D(text2d,tbx,tby, tbz, vs);
    imgBox.setUseGrayscale(false);
    imgBox.setBaseThickness(0);
    imgBox.setBaseThreshold(0.5);
    imgBox.setBlurWidth(0.1*MM);

    return imgBox;
    }

    function main(args){
    // see signet_ring.svg for drawing

    // user definable variables

    var imageThickness = 0.75*MM;

    var ringSize = ringSizeToDiameter(args.size);
    var ringThickness = 1.5*MM;
    var ringWidth = args.ringWidth;

    var bezelAngle = Math.PI/8; // angle of bezel sides, good values are between PI/5 and PI/20
    var bezelHeight = 1.5*MM; // thickness of the ring bezel

    var imagePath = args.image;
    var imageScale = args.imageScale; // size of image relative to bezel size

    // end of user definable variables


    var inRad = ringSize/2;
    var outRad = inRad + ringThickness;


    var By = inRad + bezelHeight;

    var sinA = Math.sin(bezelAngle);
    var BC = (Ay - By)*Math.tan(bezelAngle);
    var Ay = outRad/sinA;
    var Fy = Ay*sinA*sinA;
    var bezelBase = Fy;
    var bezelTop = By;

    var margin = 0.2*MM;// empty space around object
    // size of grid to fit the ring
    var gridWidth = 2*outRad + 2*margin;
    var gridHeight = 2*bezelTop + 2*margin;


    var Cx = (Ay - By)*Math.tan(bezelAngle);
    var Dx = Cx*(1 + bezelHeight/(Ay - By));

    var cutR = inRad; // radius of cylindrical transition

    var Ex = ringWidth/2 + cutR;
    var dd = 1*MM; // width of small bezel undercut
    var Ey = inRad - Math.sqrt(cutR*cutR - (Ex - Dx + dd)*(Ex - Dx + dd));

    var imageWidth = imageScale*Cx*2;
    var imageHeight = imageWidth;

    var image = makeImage(imagePath, imageWidth, imageHeight,2*imageThickness,args.useGrayscale);
    var imageTransform = new CompositeTransform();
    imageTransform.add(new Rotation(new Vector3d(1,0,0), -Math.PI/2));
    imageTransform.add(new Translation(0,By, 0));
    image.setTransform(imageTransform);

    var ringBase = new Sphere(outRad);

    // bezel is cone truncated by 2 parallel planes
    var bezel = new Intersection();
    bezel.add(new Cone(new Vector3d(0,Ay, 0), new Vector3d(0,-1,0), bezelAngle));
    bezel.add(new Plane(new Vector3d(0,1,0), bezelTop));
    bezel.add(new Plane(new Vector3d(0,-1,0), -bezelBase));
    bezel.setBlend(0.1*MM);

    var ring = new Union();
    ring.add(ringBase);
    ring.add(bezel);

    var cutoff = new Union();

    var gw = gridWidth;
    var gw2 = gridWidth/2;
    var thru = new Cylinder(new Vector3d(0, 0, -gw2),new Vector3d(0,0,gw2),inRad);

    cutoff.add(new Cylinder(new Vector3d(-gw2, Ey, Ex), new Vector3d(gw2, Ey, Ex), cutR));
    cutoff.add(new Cylinder(new Vector3d(-gw2, Ey, -Ex), new Vector3d(gw2, Ey, -Ex), cutR));
    cutoff.add(new Box(0,-inRad, Ex, gw, 2*(inRad+Ey), 2*cutR));
    cutoff.add(new Box(0,-inRad, -Ex, gw, 2*(inRad+Ey), 2*cutR));

    var partialRing = new Subtraction(ring,thru);
    partialRing.setBlend(1*MM);

    var fullRing = new Subtraction(partialRing, cutoff);
    fullRing.setBlend(0.1*MM);

    var ringWithImage = new Subtraction(fullRing, image);

    var result = ringWithImage;

    if (args.text !== undefined) {
    var text = makeText3D(args);

    var trans = new CompositeTransform();
    trans.add(new Rotation(0,0,1,180*TORAD));
    trans.add(new Rotation(0,1,0,180*TORAD));
    trans.add(new RingWrap(ringSizeToDiameter(args.size) / 2));
    trans.add(new Rotation(1,0,0,90*TORAD));
    trans.add(new Rotation(0,0,1,0*TORAD));
    text.setTransform(trans);
    result = new Subtraction(result,text);
    }
    var bounds = new Bounds(-gridWidth/2, gridWidth/2, -gridHeight/2, gridHeight/2, -gridWidth/2, gridWidth/2);
    var scene = new Scene(result,bounds,vs);
    scene.setName("SignetRing");

    return scene;
    }
     
  3. niquegeek
    niquegeek Active Member
    @AlanHudson

    Wow! That helps a ton, it's basically the script I was trying to make and more!
    Now I just need to figure out how it works. Thanks again! I'll have more questions this week as I transition all of my scripts, your help is much appreciated! Thanks so much for Shape-js!
     
  4. AlanHudson
    AlanHudson Shapeways Employee Dev Team
    Your welcome. If you haven't seen this blog posting it contains an even more advanced version with images on the sides as well and it contains some more details on how the script works. It's not as fast rendering as I'd like but it makes nice geometry.

    https://www.shapeways.com/blog/archives/25424-learn-shapejs-s ignet-ring.html


     
  5. niquegeek
    niquegeek Active Member
    Oh man! I didn't know that existed! That's some pretty awesome geometry magic, I've been debating trying to tackle a square or rectangular signet, but I haven't wanted to contemplate the geometry, that post and the drawing explains a lot, thanks!