SkeletonHelper is not rendered - three.js

I am trying to display a Three JS Skeleton using SkeletonHelper.
According to this answer, SkeletonHelper() takes the root bone. I have done just that. But it is not being displayed. Why is this the case?
var bones = []
var shoulder = new THREE.Bone();
var elbow = new THREE.Bone();
var hand = new THREE.Bone();
shoulder.add(elbow);
elbow.add(hand);
bones.push(shoulder);
bones.push(elbow);
bones.push(hand);
skeleton = new THREE.Skeleton(bones)
skeleton_helper = new THREE.SkeletonHelper(shoulder)
scene.add(skeleton_helper)
}
Thanks for the help!

THREE.SkeletonHelper assumes that the world matrices of the bones are up-to-date. So by adding a transformation to your bones and then adding the top-most bone to the scene, you should see your desired result. That works because the renderer ensures correct world matrices of the entire scene graph.
var bones = [];
var shoulder = new THREE.Bone();
var elbow = new THREE.Bone();
var hand = new THREE.Bone();
shoulder.add( elbow );
elbow.add( hand );
bones.push( shoulder );
bones.push( elbow );
bones.push( hand );
shoulder.position.y = 0;
elbow.position.y = - 1;
hand.position.y = - 0.5;
var armSkeleton = new THREE.Skeleton( bones );
var skeleton_helper = new THREE.SkeletonHelper( shoulder );
scene.add( skeleton_helper );
scene.add( shoulder );
https://jsfiddle.net/xvbLwyaq/3/
three.js R103

Related

THREE.js - vertex's coordinates of BufferGeometry (ObjLoader)

I'm new to Three.js, and have an issue with getting coordinates of 2 verticies. I need their x,y,z coordinates to calculate and draw sizes.
I used ObjLoader and I know exactly ID's (indexes) of verticies - 11889 and 11877.
I tried to use some properties of BufferGeometry, but, obviously, wrong. So here is the simple code I'm staying with.
var coord1 = [11889,0,0,0], coord2 = [11877,0,0,0];
var mtlLoader = new THREE.MTLLoader();
mtlLoader.load(
'bath1.mtl',
function( materials )
{
materials.preload();
oLoader.setMaterials( materials );
oLoader.load('bath1.obj', function ( bath )
{
bath.name="bath1";
bath.position.y -=1; bath.position.x -=0.25; bath.position.z -=1;
scene.add( bath );
},
function ( xhr ) {},
function ( error ) {console.log( 'An error happened' );}
);
}
);
I read a few similar questions, but I could not adapt the answers for my problem. What should I do to get coordinates? Thanks!
As an option, you can use .fromArray() method of THREE.Vector3(), this way:
var geo = new THREE.PlaneBufferGeometry(5, 5);
var bath = new THREE.Mesh(geo, new THREE.MeshBasicMaterial());
var vert1 = new THREE.Vector3().fromArray(bath.geometry.attributes.position.array, 1 * 3);
var vert2 = new THREE.Vector3().fromArray(bath.geometry.attributes.position.array, 2 * 3);
console.log(vert1, vert2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>
Another approach is to use .getXXX() methods of THREE.BufferAttribute():
var geom = new THREE.PlaneBufferGeometry(5, 5);
var bath = new THREE.Mesh(geom, new THREE.MeshBasicMaterial());
var position = bath.geometry.attributes.position;
var x = position.getX(1);
var y = position.getY(1);
var z = position.getZ(1);
console.log(x, y, z);
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>

Using multiple Textures with geometry.faceVertexUvs

I want to use multiple materials and multiple textures on meshes. I use 3 materials and a geometry.faceVertexUvs with two arrays of Uvs. One arrayof UVs for the first texture and one array of UVs for the second texture. However that doesn't quite work. Three.js always uses the uvs of the first array and geometry.faceVertexUvs[1] is never used. Do I have to put all the uvs in geometry.faceVertexUvs[0] for both textures or does it work differently? And if I have to put all uvs in geometry.faceVertexUvs[0] then I wonder why geometry.faceVertexUvs is an array at all. Explanation would be great. The Three.js documentation is not very detailed there.
here my sample code and tanks guys! Tom
var geometry = new THREE.BoxGeometry(60, 60, 60);
var texture1 = new THREE.TextureLoader().load( "../sons.png" );
var texture2 = new THREE.TextureLoader().load( "../Richard-branson.jpg" );
var material0 = new THREE.MeshBasicMaterial( {color:0xffff00});
var material1 = new THREE.MeshPhongMaterial({map:texture1, shininess:100});
var material2 = new THREE.MeshPhongMaterial({map:texture2, shininess:100});
for(var i=0; i< geometry.faces.length;i++){
geometry.faces[i].materialIndex = i%3;
geometry.faceVertexUvs[0][i][0] = new THREE.Vector2(0,0);
geometry.faceVertexUvs[0][i][1] = new THREE.Vector2(1,0);
geometry.faceVertexUvs[0][i][2] = new THREE.Vector2(0,1);
geometry.faceVertexUvs[1]= [];
geometry.faceVertexUvs[1][i] = [];
geometry.faceVertexUvs[1][i][0] = new THREE.Vector2(1,1);
geometry.faceVertexUvs[1][i][1] = new THREE.Vector2(1,0);
geometry.faceVertexUvs[1][i][2] = new THREE.Vector2(0,1);
}
var mesh = new THREE.Mesh(geometry, [material0, material1, material2]);
view.scene.add(mesh);
view.render();

three js directional light shadow

i am new to three js , i can able to generate the shadows using spot light but i receive unnecessary shadows too. what i need to do to remove the unwanted shadow .i need the shadows only for car and the wall and i need to remove the shadow like rectangle in the ground .
my code is as follows
var ambientLight = new THREE.AmbientLight( 0xffffff );
scene.add( ambientLight );
var light1 = new THREE.SpotLight(0xff00000);
light1.position.set(200, 1200, 0);
light1.target.position.set(0,0,0);
light1.shadowCameraVisible = true;
light1.castShadow = true;
light1.shadowDarkness = 0.8;
light1.shadowCameraNear = 400;
light1.shadowCameraFar = 1600;
//light1.shadowCameraFov = 30;
light1.shadowCameraLeft = -750;
light1.shadowCameraBottom = -500;
light1.shadowCameraRight = 1000;
light1.shadowCameraTop = 600;
var firstLight = new THREE.Object3D();
firstLight.add(light1);
scene.add(firstLight);
Thanks in advance
I see this you want selective shadow of object for the performance tweak.
The best you can do is for car model you have either .obj,.dae or which ever format loader you can load the object into scene via THREE.Object3D which has property "castShadow". Try this out:
var obj3D = new THREE.Object3D();
obj3D.add(/*content from loader*/);
obj3D.castShadow = true;
or
var mesh = new THREE.Mesh();
mesh.castShadow =true;
Enable or disable castShadow property with Mesh too.
I think this solve your problem

Three.js Collision detection of spheres with THREE.Raycaster

For my project I need collision tests in Three.js. In my CollisionDetection class I'm trying to get a Raycaster to work. And I found some weirdness that I can't explain and can't find a way around:
My CollisionDetector works fine for Cubes.. but when I use Spheres instead, it doesn't give me the same results – Am I wrong to expect the same results as for the cubes? Or do I miss something else?
Here is my Code:
var renderer, camera, scene;
init();
animate();
function init() {
var container = document.getElementById("scene");
var width = window.innerWidth;
var height = window.innerHeight;
renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);
camera = new THREE.OrthographicCamera( 0, width, 0, height, 1, 10000 );
camera.position.z = 300;
scene = new THREE.Scene();
scene.add(camera);
container.appendChild(renderer.domElement);
var geometry = new THREE.SphereGeometry(10,16, 16);
//var geometry = new THREE.CubeGeometry( 10, 10, 10 );
var material1 = new THREE.MeshBasicMaterial( { color: 0xFF3333} );
var material2 = new THREE.MeshBasicMaterial( { color: 0xFF3333} );
var material3 = new THREE.MeshBasicMaterial( { color: 0xFF3333} );
var material4 = new THREE.MeshBasicMaterial( { color: 0xFF3333} );
var material5 = new THREE.MeshBasicMaterial( { color: 0xFF3333} );
var element1 = new THREE.Mesh( geometry, material1 );
var element2 = new THREE.Mesh( geometry, material2 );
var element3 = new THREE.Mesh( geometry, material3 );
var element4 = new THREE.Mesh( geometry, material4 );
var element5 = new THREE.Mesh( geometry, material5 );
element1.position.set(200,200,0);
element2.position.set(200,100,0);
element3.position.set(200,300,0);
element4.position.set(100,200,0);
element5.position.set(300,200,0);
scene.add(element1);
scene.add(element2);
scene.add(element3);
scene.add(element4);
scene.add(element5);
var CollisionDetector = new CollisionDetection();
CollisionDetector.addRay(new THREE.Vector3(0, -1, 0));
CollisionDetector.addRay(new THREE.Vector3(0, 1, 0));
CollisionDetector.addRay(new THREE.Vector3(1, 0, 0));
CollisionDetector.addRay(new THREE.Vector3(-1, 0, 0));
CollisionDetector.addElement(element1);
CollisionDetector.addElement(element2);
CollisionDetector.addElement(element3);
CollisionDetector.addElement(element4);
CollisionDetector.addElement(element5);
document.onclick = function(){
CollisionDetector.testElement(element1);
};
}
function CollisionDetection(){
var caster = new THREE.Raycaster();
var rays = [];
var elements = [];
this.testElement = function(element){
for(var i=0; i<rays.length; i++) {
caster.set(element.position, rays[i]);
var hits = caster.intersectObjects(elements, true);
for(var k=0; k<hits.length; k++) {
console.log("hit", hits[k]);
hits[k].object.material.color.setHex(0x0000ff);
}
}
}
this.addRay = function(ray) {
rays.push(ray.normalize());
}
this.addElement = function(element){
elements.push(element);
}
}
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
}
Or best, see for yourself how it behaves: http://jsfiddle.net/mymL5/12/
On Click every element hit by a ray should turn blue and all hits are registered in the console.
Note the (imho) weird console output for spheres.
Also, why is the lower sphere not hit while the upper is?
You can switch between Cubes and Spheres by Commenting/Uncommenting lines 19/20
Can anyone help me? What am I not getting?
PS: I'm new to Three.js, so I'm probably being dumb.
Since this is homework-related, I am only going to provide some tips.
Your scene is rendering upside down because your args to orthographic camera are incorrect.
Your sphere is bigger than your cube.
Your rays are hitting the north and south poles of your spheres exactly. What is different about those points?
The material.side property tells Raycaster which side(s) of a face to consider the "front".
Your fidde example is running an old version (r.54) of three.js.
three.js r.58
Increased spheres size.
Rotated spheres by some non-trivial angle (so they don't get hit right in the N/S pole).
Now it works? :P
var geometry = new THREE.SphereGeometry(20,17, 17);
element1.position.set(0,0,0);
element2.position.set(0,100,0);
element3.position.set(100,0,0);
element4.position.set(0,-100,0);
element5.position.set(-100,0,0);
element1.rotation.set(0,0,10);
element2.rotation.set(0,0,10);
element3.rotation.set(0,0,10);
element4.rotation.set(0,0,10);
element5.rotation.set(0,0,10);
Still, ray test should be aware of the hitting exact vertex or edge of the triangle, so that might be considered as a place-to-improve for Three.js.
I filed an issue about this in the Three.js repository:
https://github.com/mrdoob/three.js/issues/3541

How to get the mouse clicking position on an obj file loaded from OBJLoader?

I loaded the model using OBJLoader, here is the code for loading the obj file:
var loader = new THREE.OBJLoader();
loader.load('obj/teeth/teeth4_5.obj', function(object) {
model = object;
scene.add( model );
objects.push( model );
});
And I'm trying to use raycaster to find the intersection. I implemented my code from the canvas_interactive_cubes example (http://mrdoob.github.io/three.js/examples/canvas_interactive_cubes.html) in three.js. Here is the code to find the intersection:
function onDocumentMouseDown( event ){
event.preventDefault();
var mouseX = (event.clientX / window.innerWidth)*2-1;
var mouseY = -(event.clientY /window.innerHeight)*2+1;
var vector = new THREE.Vector3( mouseX, mouseY, 0.5 );
projector.unprojectVector( vector, camera );
var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
var intersects = raycaster.intersectObjects( scene.children );
console.log( intersects[0].point);
}
Unfortunately I'm not able to get the x,y,z coordinates of the intersection, no matter where I clicked, it always showed "TypeError: intersects[0] is undefined".
I'm getting stuck here for several days. Can someone tell me a way to get the intersection on a loaded obj file? I appreciate your help.
Try adding the recursive flag like so:
var intersects = raycaster.intersectObjects( objects, true );
three.js r.58

Resources