aneeshtan / jsc3d

Automatically exported from code.google.com/p/jsc3d
0 stars 0 forks source link

Add / change the texture of a object #53

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. How can i change the texture of a object?
2.
3.

I want to change the pattern of my Google Sketchup Duck:

Code:

    viewer = new JSC3D.Viewer(document.getElementById('canvas'));
            viewer.setParameter('SceneUrl',         'duckie/duckie.obj');
            viewer.setParameter('ModelColor',       '#FFFFFF');
            viewer.setParameter('BackgroundColor1', '#FFFFFF');
            viewer.setParameter('BackgroundColor2', '#FFFFFF');
            viewer.setParameter('RenderMode',       'textureSmooth');
            viewer.setParameter('setDefinition', 'high');

            viewer.onmousedown = function(x, y, button, depth, mesh) {
                if (button === 0/*left button down*/ && mesh !== null) {

                    var newMat = new JSC3D.Texture('duckie/blackandwhite.png', true);
                    mesh.setTexture(newMat);
                    viewer.update();
                }
            };

            viewer.init();

Original issue reported on code.google.com by papa...@gmail.com on 16 Dec 2013 at 9:11

GoogleCodeExporter commented 9 years ago
This could be done as following:

  var newTex = new JSC3D.Texture('blackandwhite');
  newTex.onready = function() {
    mesh.setTexture(this);
    viewer.update();
  };
  newTex.createFromUrl('duckie/blackandwhite.png');

The createFromUrl() method is described here: 
http://jsc3d.googlecode.com/svn/trunk/jsc3d/docs/symbols/JSC3D.Texture.html#crea
teFromUrl.

Original comment by Humu2...@gmail.com on 17 Dec 2013 at 3:32

GoogleCodeExporter commented 9 years ago
Thanks for support!

Original comment by papa...@gmail.com on 17 Dec 2013 at 9:05

GoogleCodeExporter commented 9 years ago
Just another similiar question about changing textures:
What if I want to loop through all the meshes and apply different textures on 
them?
The following is my trial code:

for (var mesh_index in currentScene.children)
{
    var mesh = currentScene.children[mesh_index];

    var myTexture = new JSC3D.Texture();

    myTexture.onready = function() 
    {
        // set texture to the model
        console.log(mesh);
        mesh.setTexture(this);
        // notify viewer to deliver a new frame
        viewer.update();
        console.log("done");
    }

    myTexture.createFromUrl(myURL, false);
}

However, console.log(mesh) in the onready() call back shows that the "mesh" 
variable is always pointing to the last mesh element undercurrentScene.children 
so only the texture of last mesh element is being changed. What is the correct 
approach to change all of them with possibility of they are using different 
textures?

Original comment by passerby...@gmail.com on 18 Dec 2013 at 3:47

GoogleCodeExporter commented 9 years ago
I'm afraid you had a wrong practice of function closure. Try this instead:

  var textures = [];
  var textureOwners = {};
  var meshes = viewer.getScene().getChildren();
  for (var i=0; i<meshes.length; i++) {
    var newTex = new JSC3D.Texture;
    textures.push(newTex);
    textureOwners[i] = meshes[i];
    newTex.onready = function() {
      textureOwners[textures.indexOf(this)].setTexture(this);
      viewer.update();
    };
    newTex.createFromUrl(myURL, false);
  }

The new one will work much better :-)

Original comment by Humu2...@gmail.com on 18 Dec 2013 at 7:25

GoogleCodeExporter commented 9 years ago
Can i add texture on specific x and y coordinates ?

Original comment by papa...@gmail.com on 8 Jan 2014 at 2:00

GoogleCodeExporter commented 9 years ago
When I add a texture to my 3D-model my used image gets very tiny and they are 
all shaped like triangles. I've used "hasMipmap()" to see if there's a mipmap, 
but it returns false. How can I set an image as texture that uses it's true 
scale. Thanks.

Original comment by michaelt...@gmail.com on 9 Jan 2014 at 10:43

GoogleCodeExporter commented 9 years ago
I guess there must be something wrong with the texture coords of your model. 
Try to check and adjust them using a modeling tool such as Blender or meshlab.

Original comment by Humu2...@gmail.com on 11 Jan 2014 at 4:01