Multiple materials with JSONLoader? - three.js

Is it possible to assign two materials to one mesh which has been loaded with JSONLoader?
I've made a simple character in blender and exported it to three.js format, which contains morph targets and UVs.
I was trying to assign solid color material to the body and a picture to my character's head (http://touhou.ru/dev/webgl-test-stackoverflow/kourindouhime.jpg), but after loading mesh and materials I get a gray-colored mesh.
Here's production version of my project (use wasd to move and when you see a gray player mesh which you'd be controlling, that's exactly the thing I'm talking about): http://touhou.ru/dev/webgl-test-stackoverflow/
And here's the way I'm loading mesh and materials with JSONLoader:
var player_loader = new THREE.JSONLoader();
player_loader.load( "running_babe.js", function(geo, material) {
material[0].morphTargets = true;
material[1].morphTargets = true;
var materials = new THREE.MeshFaceMaterial(material);
player = new THREE.Mesh( geo, materials );
scene.add(player);
});
Am I doing something wrong?
UPDATE: the problem was in my export. Now the second material looks that way:
{
"DbgColor" : 15597568,
"DbgIndex" : 1,
"DbgName" : "Material.001",
"blending" : "NormalBlending",
"colorAmbient" : [0.6400000190734865, 0.6400000190734865, 0.6400000190734865],
"colorDiffuse" : [0.6400000190734865, 0.6400000190734865, 0.6400000190734865],
"colorSpecular" : [0.5, 0.5, 0.5],
"depthTest" : true,
"depthWrite" : true,
"mapDiffuse" : "kourindouhime.jpg",
"mapDiffuseWrap" : ["repeat", "repeat"],
"shading" : "Lambert",
"specularCoef" : 50,
"transparency" : 1.0,
"transparent" : false,
"vertexColors" : false
}
and it works very nice. Thank you guys.

If I looked your code correctly, running_babe.js is the mesh you are talking about. Looking at its source, the materials are as follows:
"materials" : [ {
"DbgColor" : 15658734,
"DbgIndex" : 0,
"DbgName" : "Material",
"blending" : "NormalBlending",
"colorAmbient" : [0.6400000190734865, 0.6400000190734865, 0.6400000190734865],
"colorDiffuse" : [0.6400000190734865, 0.6400000190734865, 0.6400000190734865],
"colorSpecular" : [0.5, 0.5, 0.5],
"depthTest" : true,
"depthWrite" : true,
"shading" : "Lambert",
"specularCoef" : 50,
"transparency" : 1.0,
"transparent" : false,
"vertexColors" : false
},
{
"DbgColor" : 15658734,
"DbgIndex" : 0,
"DbgName" : "default",
"vertexColors" : false
}],
It can be clearly seen that there are no textures, the second one doesn't have really anything and the first one has all colors as a shade of gray. Seems like the materials aren't exported correctly. That is not a big surprise as exporting materials is hard, as there might not be a clear mapping between 3d modeler concepts and three.js material params. I'd just fix it by manually specifying the material params into that file.

You can have one material per mesh, that's the way OpenGL works. Are you sure you have only one mesh?

Related

How can I extract an object in threejs format using blender

I want to convert my object to THIS particular format
{
"metadata" :
{
"formatVersion" : 3.1,
"generatedBy" : "Blender 2.7 Exporter",
"vertices" : 8,
"faces" : 6,
"normals" : 8,
"colors" : 0,
"uvs" : [4],
"materials" : 1,
"morphTargets" : 0,
"bones" : 0
},
"scale" : 1.000000,
"materials" : [ {
"DbgColor" : 15658734,
"DbgIndex" : 0,
"DbgName" : "bake_mat.013",
"blending" : "NormalBlending",
"colorAmbient" : [1.0, 1.0, 1.0],
"colorDiffuse" : [1.0, 1.0, 1.0],
"colorEmissive" : [0.0, 0.0, 0.0],
"colorSpecular" : [0.5, 0.5, 0.5],
"depthTest" : true,
"depthWrite" : true,
"mapDiffuse" : "b_cb-blue-block60x96.png",
"mapDiffuseWrap" : ["repeat", "repeat"],
"shading" : "Lambert",
"specularCoef" : 50,
"transparency" : 1.0,
"transparent" : false,
"vertexColors" : false
}],
"vertices" : [-76.2,-0.249995,121.92,-76.2,-0.250005,-121.92,76.2,-0.250005,-121.92,76.2,-0.249995,121.92,-76.2,0.250005,121.92,-76.2,0.249995,-121.92,76.2,0.249995,-121.92,76.2,0.250005,121.92],
"morphTargets" : [],
"normals" : [-0.577349,-0.577349,-0.577349,-0.577349,-0.577349,0.577349,-0.577349,0.577349,0.577349,-0.577349,0.577349,-0.577349,0.577349,0.577349,-0.577349,0.577349,-0.577349,-0.577349,0.577349,0.577349,0.577349,0.577349,-0.577349,0.577349],
"colors" : [],
"uvs" : [[-2e-06,0.999998,2e-06,-2e-06,1,2e-06,0.999998,1]],
"faces" : [43,1,0,4,5,0,0,1,2,3,0,1,2,3,43,5,6,2,1,0,2,3,0,1,3,4,5,0,43,6,7,3,2,0,2,3,0,1,4,6,7,5,43,0,3,7,4,0,0,1,2,3,1,7,6,2,43,0,1,2,3,0,2,3,0,1,1,0,5,7,43,7,6,5,4,0,2,3,0,1,6,4,3,2],
"bones" : [],
"skinIndices" : [],
"skinWeights" : [],
"animations" : []
}
I tried using blender, but I doesn't gives me these particular keys and nodes that I desire. Does anyone know how this can be achieved? When I try to export the models, I get js like cars.js but I want it to be like the other js.
Please see the attached screenshot

3D max export to three.js with tiling

After exporting a 3D model from 3D max to three.js not work tiling. That is in itself not recorded json file settings for this feature
"materials": [
{
"DbgIndex" : 0,
"DbgName" : "02 - Default",
"colorDiffuse" : [0.5882, 0.5882, 0.5882],
"colorAmbient" : [0.5882, 0.5882, 0.5882],
"colorSpecular" : [0.9000, 0.9000, 0.9000],
"transparency" : 1.0,
"specularCoef" : 10.0,
"mapDiffuse" : "rabica.png",
"vertexColors" : false
},
I have used the max exporter but had trouble with smoothing groups so do not use it. I may end up patching it at some stage in the future.
It seems to also not set the 'wrap' setting that you want.
After looking at the JSONLoader and Loader code it seems that it does handle wrap being set in the json file.
I think if you edit your json file manually so it looks as below, it may work.
"materials": [
{
"DbgIndex" : 0,
"DbgName" : "02 - Default",
"colorDiffuse" : [0.5882, 0.5882, 0.5882],
"colorAmbient" : [0.5882, 0.5882, 0.5882],
"colorSpecular" : [0.9000, 0.9000, 0.9000],
"transparency" : 1.0,
"specularCoef" : 10.0,
"mapDiffuse" : "rabica.png",
"vertexColors" : false,
"mapDiffuseWrap" : ["repeat", "repeat"]
},
Though this is untested

threejs find position of plane from JSONLoader

I have this JS generated by blender and I load It trough JSONLoader. It is just a plane with a texture on it. I just want to find position of it to add a pointlight in its normal vector (in front of it) What is property to use to add a point light 3 points away? How can I know position of that light?
Im new on threejs and I've looking in javascript inspector some properties to find this but I can not see it...
Thank you!!!
{
"metadata" :
{
"formatVersion" : 3.1,
"generatedBy" : "Blender 2.63 Exporter",
"vertices" : 4,
"faces" : 2,
"normals" : 1,
"colors" : 0,
"uvs" : [4],
"materials" : 1,
"morphTargets" : 0,
"bones" : 0
},
"scale" : 1.000000,
"materials" : [ {
"DbgColor" : 15658734,
"DbgIndex" : 0,
"DbgName" : "texturaLadrillos",
"blending" : "NormalBlending",
"colorAmbient" : [0.6900000198185445, 0.6900000198185445, 0.6900000198185445],
"colorDiffuse" : [0.6900000198185445, 0.6900000198185445, 0.6900000198185445],
"colorSpecular" : [0.5, 0.5, 0.5],
"depthTest" : true,
"depthWrite" : true,
"mapDiffuse" : "JP_Brick01_Bump.jpg",
"mapDiffuseWrap" : ["repeat", "repeat"],
"shading" : "Lambert",
"specularCoef" : 50,
"transparency" : 1.0,
"transparent" : false,
"vertexColors" : false
}],
"vertices" : [-53.8264,3.757e-06,85.9501,-8.9211,3.757e-06,85.9501,-53.8264,46.0109,85.9501,-8.9211,46.0109,85.9501],
"morphTargets" : [],
"normals" : [0,0,1],
"colors" : [],
"uvs" : [[0,0,1,0,1,1,0,1]],
"faces" : [42,0,1,3,0,0,1,2,0,0,0,42,2,0,3,0,3,0,2,0,0,0],
"bones" : [],
"skinIndices" : [],
"skinWeights" : [],
"animation" : {}
}
This jsonfile doesn't contain the position data becuase it only has geometry and materials.
You should use a mesh to define the position.

Blender exporter - "uncaught syntaxError unexpected token" THREE.JSONLoader()

Have you come across this erro: "uncaught syntaxError unexpected token"
When loading a model exported from Blender 2.63a with the three exporter addon?
From this code copied from three.js github example
var loader = new THREE.JSONLoader();
loader.load( {"buffalo.js", function(geo){createModel(geo)}});
Thanks
Regards
Chris
Check whether the model has a material. Search in buffalo.s for the string: "material" : ,. If you find that string, your model has no material assigned - bad syntax error appears! Go to blender, assign material, and export again to buffalo.js.
Not working:
...
"objects" :
{
"Monkey" : {
"geometry" : "geo_Monkey",
"groups" : [ ],
"material" : ,
"position" : [ -0.150241, 5.41906, 2.96394 ],
"rotation" : [ 0.874236, 0.0220203, -3.11527 ],
"quaternion": [ 0.00726118, 0.0155444, -0.423137, -0.905903 ],
"scale" : [ 2.9257, 2.86503, 2.68207 ],
"visible" : true,
"castShadow" : false,
"receiveShadow" : false,
"doubleSided" : false
},
...
},
...
Working:
...
"objects" :
{
"Monkey" : {
"geometry" : "geo_Monkey",
"groups" : [ ],
"material" : "Material",
"position" : [ -0.150241, 5.41906, 2.96394 ],
"rotation" : [ 0.874236, 0.0220203, -3.11527 ],
"quaternion": [ 0.00726118, 0.0155444, -0.423137, -0.905903 ],
"scale" : [ 2.9257, 2.86503, 2.68207 ],
"visible" : true,
"castShadow" : false,
"receiveShadow" : false,
"doubleSided" : false
},
...
},
...
I got this working by leaving out the JSON signature of the load function. Like this:
loader = new THREE.JSONLoader();
loader.load( 'buffalo.js',function( geo )
{
....
Hope this helps
Regards

How to change the way shading looks on cubes in Three.js?

I'm creating a little game using Three.js and everything is going well apart from shome shading issues with cubes. I'm basically building a game level by just dropping textured cubes down to form a maze. The problem is that when the cubes are next to one another, each one is shaded in such a way that it looks as if it's a separate entity and not part of a larger wall.
Here is an example, notice the illusion of a single wall is lost:
Is there a different shading technique i should use or is there a nice property to be set somewhere to change this shading behavior?
This is my cube model:
{
"metadata" :
{
"formatVersion" : 3,
"generatedBy" : "Blender 2.60 Exporter",
"vertices" : 8,
"faces" : 6,
"normals" : 8,
"colors" : 0,
"uvs" : 4,
"materials" : 1,
"morphTargets" : 0
},
"scale" : 1.000000,
"materials": [{
"DbgColor" : 15658734,
"DbgIndex" : 0,
"DbgName" : "WallCube",
"colorAmbient" : [1.0, 1.0, 1.0],
"colorDiffuse" : [1.0, 1.0, 1.0],
"colorSpecular" : [0.15, 0.15, 0.15],
"mapDiffuse" : "../../textures/walls/stone/stone.png",
"mapDiffuseWrap" : ["repeat", "repeat"],
"mapNormal" : "../../textures/walls/stone/stone_normal.png",
"mapNormalFactor" : 1.0,
"shading" : "Lambert",
"specularCoef" : 25,
"transparency" : 1.0,
"vertexColors" : false
}],
"vertices": [50.000000,-50.000000,-50.000000,50.000000,-50.000000,50.000000,-50.000000,-50.000000,50.000000,-50.000000,-50.000000,-50.000000,50.000000,50.000000,-50.000000,50.000000,50.000000,50.0000050,-50.000000,50.000000,50.000000,-50.000000,50.000000,-50.000000],
"morphTargets": [],
"normals": [1.000000,-1.000000,-1.000000,1.000000,-1.000000,1.000000,-1.000000,-1.000000,1.000000,-1.000000,-1.000000,-1.000000,1.000000,1.000000,-1.000000,-1.000000,1.000000,-1.000000,-1.000000,1.000000,1.000000,1.000000,1.000000,1.000000],
"colors": [],
"uvs": [[0.000000,1.000000,1.000000,1.000000,1.000000,0.000000,0.000000,0.000000]],
"faces": [43,0,1,2,3,0,0,1,2,3,0,1,2,3,43,4,7,6,5,0,0,1,2,3,4,5,6,7,43,0,4,5,1,0,1,2,3,0,0,4,7,1,43,1,5,6,2,0,1,2,3,0,1,7,6,2,43,2,6,7,3,0,1,2,3,0,2,6,5,3,43,4,0,3,7,0,3,0,1,2,4,0,3,5]
}
and this is how i load it:
JSONLoader = new THREE.JSONLoader();
Light = new THREE.PointLight(0xFFFFFF);
Light.position = {x:0, y:75, z:350};
Meshes = [];
JSONLoader.load("../assets/models/cube.js", function(Geometry)
{
for (var MeshIndex = 0; MeshIndex <= 5; MeshIndex++)
{
Meshes[MeshIndex] = new THREE.Mesh(Geometry, new THREE.MeshFaceMaterial());
Meshes[MeshIndex].position.x = MeshIndex * 100;
Scene.add(Meshes[MeshIndex]);
}
});
Scene.add(Light);
Any ideas how to make the cubes look like a continuous wall?
JSONLoader.load("../assets/models/cube.js", function(Geometry)
{
Geometry.materials[ 0 ].shading = THREE.FlatShading;
// ...
}
This was kindly answered by alteredq over at the three.js site.
https://github.com/mrdoob/three.js/issues/1258#issuecomment-3834489

Resources