kovacsv / JSModeler

A JavaScript framework to create and visualize 3D models.
MIT License
648 stars 124 forks source link

Model Appear Black initially when Texture Applied #5

Closed saadfaisal closed 9 years ago

saadfaisal commented 9 years ago

Hi , in continuation on my previous issue, now that the texture has been applied successfully, however on the first view , the model appear completely black , however as soon as you starts to move it, the texture suddenly appears with all the glory. Is there something that can be easily fixed or if somehow we can force the update for the texture. In case of single color fills, this issue doesnt exist and only happends when you apply a texture.

kovacsv commented 9 years ago

In WebGL all texture objects appear black until the texture file is loaded. You can set a callback function to conversion which is called when a texture is loaded. In the callback you should call Draw:

var meshes = JSM.ConvertModelToThreeMeshes (model, materials, {
    textureLoadedCallback : function () {
        viewer.Draw ();
    }
});
saadfaisal commented 9 years ago

Tried it , but not working.

kovacsv commented 9 years ago

And what happens? Are there any console error messages? Are your viewer visible in this scope with this name?

If you could not figure out what is the problem, you can force the viewer to redraw continuously by calling StartDrawLoop. In this case it will be redrawed every time, not just in case of navigation, so I think the first solution is better.

viewer.StartDrawLoop ();
saadfaisal commented 9 years ago

No specific error messages , there are couple of warnings from three.js but they are there since beginning

"THREE.WebGLRenderer" "68"                              three.min.js:520
"THREE.WebGLProgram: gl.getProgramInfoLog()" "(70,2-97): warning X3557: loop only executes for 1 iteration(s), forcing loop to unroll"                        three.min.js:589

My viewer however is working the same way, comes out with model as black and then as soon as you moves it around , the texture gets displayed. Second my primary working browser is Firefox however i tried using Internet Explorer and at few times , IE does display the model with texture in the first attempt say 1 out of 7 times it does, rest it remains black.

here is the code for the reference along with the svg.

SVG

<svg height="400" width="400" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" style="overflow: visible;" id="svgid">
<g id="layer1">
<path fill="#ffff00" d="M304 146C242.5142822265625 146 189.93736267089844 176.41582489013672 168.5625 219.4375L126 219.4375L126 485.4375L486 485.4375L486 219.4375L439.4375 219.4375C418.05596923828125 176.4159164428711 365.4692077636719 146 304 146z " id="rect2985"></path>
</g>
</svg>

Code

var viewerSettings = {
    cameraEyePosition : [-3, -2, 1.0],
    cameraCenterPosition : [0.0, 0.0, 0.0],
    cameraUpVector : [0.0, 0.0, 1.0],
    nearClippingPlane : 1.0,
    farClippingPlane : 100000.0
};

var viewer = new JSM.ThreeViewer ();
if (!viewer.Start ('svgcanvas', viewerSettings)) {
    viewer = null;
    return;
}

var svgObject = document.getElementById ('svgid');
var modelAndMaterials = JSM.SvgToModel (svgObject, 35, 5);
// ---------------
var model = modelAndMaterials[0];
var materials = modelAndMaterials[1];

var mat = materials.GetMaterial(0);

materials.AddMaterial (new JSM.Material ({
    texture : 'img/egyptian_marble_128.jpg',
    ambient : 0xffffff,
    diffuse : 0xffffff,
    textureWidth : 128.0,
    textureHeight : 128.0
}));

var newMaterialIndex = materials.Count () - 1;

model.GetBody (0).SetPolygonsMaterialIndex (newMaterialIndex);    

var meshes = JSM.ConvertModelToThreeMeshes (model, materials , {
    textureCallback: function() {
        viewer.Draw ();            
    }
});

viewer.AddMeshes (meshes);
viewer.FitInWindow ();
saadfaisal commented 9 years ago

FireFox : 33.1 IE : 11

kovacsv commented 9 years ago

The callback name is textureLoadedCallback, not textureCallback.

saadfaisal commented 9 years ago

Now i am seriously embarrassed.. deepest apologize, my BAD!!!!! Thanks for such a wonderful work.

kovacsv commented 9 years ago

You welcome :)