0

Is it possible to add an external image or text on an already jsc3d created 3d object.For example if any canvas imagedata needs to be stored on the created 3d object,then is it possible?

Guest
  • 25
  • 4

1 Answers1

0

Yes, its possible. If you look at the jsc3d implementation of Texture, you will see that a texture has already an underlying canvas.

Let say you have a canvas called "myTexture" and a Mesh called "myMesh", and to make it simple, you just only need a texture with a fixed size of 128x128 px, this will paint your canvas onto your mesh:

    var canvas = document.getElementById('myTexture');
    var context = canvas.getContext('2d');
    var dim = 128;
    var imgData = context.getImageData(0,0,dim,dim);
    var data = imgData.data;
    var size = data.length / 4;
    var texture = new JSC3D.Texture;
    texture.data = new Array(size);
    var alpha;
    for(var i=0, j=0; i<size; i++, j+=4) {
        alpha = data[j + 3];
        texture.data[i] = alpha << 24 | data[j] << 16 | data[j+1] << 8 | data[j+2];
        if(alpha < 255)
            texture.hasTransparency = true;
    }
    texture.width = dim;
    texture.height = dim;       

    myMesh.setTexture(texture);
    viewer.update();

The .data loop is taken from JSC3D.Texture.prototype.createFromImage (credits humu2009, creator of jsc3d).

deblocker
  • 7,629
  • 2
  • 24
  • 59