jqueryx3dom

X3dom : Rotate a cylinder


How to rotate a cylinder in x3dom ?

But, in API documentation : API, there is just how to create vertical cylinder. How to create horizontal cylinder ?.

This is the code :

myCanvas.addCylinder({ 
  id: item.coil_no,
  x:row,  y: tier , z:(Math.abs(column)),
  radius: 0.5, 
  height:1,
  color:warna,
  onclick:'ClickHandler(this)', 'data-description' : item.coil_no, 'data-lokasi' : item.lokasi_terakhir
}); 

Update :

This is my js :

$.fn.addCylinder = function( oArg) {
    this.find('scene').append( $( drawCylinder( oArg ) ) );
};

which is drawCylinder is :

function drawCylinder ( oArg) {
    var oAttr = {
        x:0, y:0, z:0, radius:.1, height:1, color:NODE_COLOR,
        label:'', labeloffset: 0.1,
        fontFamily:FONT_FAMILY, fontColor:FONT_COLOR, fontSize:FONT_SIZE
    };
    $.extend(true, oAttr, oArg);
    var label = (oAttr.label.length > 0) ? setLabel ( oAttr ) : '';
    var gObj = $('\
        <transform translation="' + oAttr.x + ' ' + oAttr.y + ' ' + oAttr.z + '">\
        <shape><appearance><material diffuseColor="'+hex2rgb(oAttr.color)+'"></material></appearance>\
        <Cylinder radius="'+oAttr.radius+' "height="'+oAttr.height+'"></Cylinder></shape>\
        </transform>'+label);
    if (oAttr.id) { $(gObj).find('shape').attr("id", oAttr.id); } 
    if (oAttr.onclick) { $(gObj).find('shape').attr("onclick", oAttr.onclick); } 
    $.each( oAttr, function( key, value ) {
        if (key.match("^data-")) { $(gObj).find('shape').attr(key, value); }
    });
    return gObj;
}

And in xml that generated looked like this :

<transform translation="1 1 1" render="true" bboxcenter="0,0,0" bboxsize="-1,-1,-1" center="0,0,0" rotation="0,0,0,0"
       scale="1,1,1" scaleorientation="0,0,0,0">
<shape id="DNA07X1A518081734A" onclick="ClickHandler(this)" data-description="DNA07X1A518081734A" data-lokasi="111"
       render="true" bboxcenter="0,0,0" bboxsize="-1,-1,-1" ispickable="true">
    <appearance sorttype="auto" alphaclipthreshold="0.1">
        <material diffusecolor="1,0,1" ambientintensity="0.2" emissivecolor="0,0,0" shininess="0.2"
                  specularcolor="0,0,0"></material>
    </appearance>
    <cylinder radius="0.5 " height="1" solid="true" ccw="true" usegeocache="true" lit="true" bottom="true"
              top="true" subdivision="32" side="true"></cylinder>
</shape>

Please advise.


Solution

  • You can use a transform node and rotate it 90 degrees. Since I don't know what your outside function addCylinder and your XML looks like, I add just the XML:

    <x3d id="x3dElement" style="width:100%; height:100%; border:0" width="1355px" height="826px">
        <scene>
            <transform rotation="1 0 0 -1.57">
                <transform rotation="0 0 1 1.57">
                    <shape>
                        <appearance>
                            <material diffusecolor="0 1 1" ambientintensity="0.2" emissivecolor="0,0,0" shininess="0.2" specularcolor="0,0,0"></material>
                        </appearance>
                        <cylinder radius="1" height="2"></cylinder>
                    </shape>
                </transform>
            </transform>
        </scene>
    </x3d>
    

    I have rotated the cylinder twice. First 90 degrees (PI/2) around the X-axis and then a second rotation of 90 degrees (PI/2) around the Z-axis.