JSC3D: translating object

2019-09-08 19:04发布

问题:

I am trying to translate object in space, and I have this function which should translate object, its stored in .js file

 JSC3D.Matrix3x4.prototype.translate = function(tx, ty, tz) {
this.m03 += tx;
this.m13 += ty;
this.m23 += tz;
 };

but in another js file I am trying to implement real numbers and to move object, how can I call this function and change its parameters ?

回答1:

Of course you can do it!

Expand the JSC3D.Mesh class by adding a method like this:

JSC3D.Mesh.prototype.translate = function(translX, translY, translZ) {
    var xformMat = new JSC3D.Matrix3x4;
    xformMat.translate(translX, translY, translZ);      
    JSC3D.Math3D.transformVectors(xformMat, this.vertexBuffer, this.vertexBuffer);
    this.calcAABB();
    this.compiled = null;
};

But you must know which mesh you will move, for example, you can pick a mesh by clicking an already loaded mesh inside the viewer canvas:

var currentMesh;

function onViewerMouseDown(x, y, button, depth, mesh) {
    if(button == 0/*left button down*/ && mesh != null) {
        currentMesh = mesh;
    }
}

// standard jsc3d code, set the canvas used for the 3d viewer    
var canvas = document.getElementById('cv');
var viewer = new JSC3D.Viewer(canvas);  
// viewer initialization parameters...
viewer.setParameter('SceneUrl', 'test.stl');
viewer.setParameter('RenderMode', 'texturesmooth');
viewer.init();
// at this time, you should have at least one mesh loaded ...
viewer.onmousedown = onViewerMouseDown;

Then, in your "another js file" you can call this new implementation:

currentMesh.translate(10, 20, 0);
viewer.update;

The values 10, 20, 0 depends how big your mesh is, and how much space it occupies in the viewer world, maybe you should increment this values to see an appreciable movement.



回答2:

JSC3D.Matrix3x4.prototype.translate = function(tx, ty, tz) {

var t=0;
var g=0;
var h=0;

 t = parseFloat(document.getElementById('translate_x').value);
 g = parseFloat(document.getElementById('translate_y').value);
 h = parseFloat(document.getElementById('translate_z').value);
console.log(t);

    if(t!=0 || g!=0 || h!=0)
    {

        console.log(this.m03);
        this.m03 += tx;
        this.m13 += ty;
        this.m23 += tz;
        tx=t;
        ty=g;
        tz=h;
        this.m03 += tx;
       this.m13 += ty;
       this.m23 += tz;
   }
   else
   {
     this.m03 += tx;
     this.m13 += ty;
     this.m23 += tz;
   }
};