Jump to content
ThoSzy

Spherical positions on multiple spheres

Recommended Posts

Hello, I try to place meshs on sphere depending on their rank in a json file.

I am looking for a way to define the position of my meshs in my loop in order to prevent them to be created on each other.

    function onMouseGauche(event) {

        event = event || window.event;
        if (event.button == 0) {
            mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
            mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;

            // update the picking ray with the camera and mouse position
            raycaster.setFromCamera(mouse, camera);
            // calculate objects intersecting the picking ray

            var intersects = 0;
            intersects = raycaster.intersectObjects(scene.children);

            if (intersects.length > 0) {


                if (intersects[0].object.userData.bool != true) {
                    //                    Scalar = Scalar + 400;
                    Scalar = Scalar / 1;
                    Scale = Scale / 1;
                    console.log(intersects[0].object.userData)

                    if (intersects[0].object.userData.children) {

                        var loop = 0;

                        intersects[0].object.userData.children.forEach(function(element) {

                            var sphereMaterial2 = new THREE.MeshBasicMaterial({
                                color: element.group
                            });

                            var mesh = new THREE.Mesh(sphereGeometry, sphereMaterial2);
                            var phi = ( 2 * Math.PI)
                            var theta = (2 * Math.PI)

                            var x = Math.random() * 2 - 1;
                            var y = Math.random() * 2 - 1;
                            var z = Math.random() * 2 - 1;

//                            if (loop == 0) {
//                                var x = Math.random() * 2 - 1;
//                                var y = Math.cos(theta) * Math.random();
//                                var z = Math.cos(phi) * Math.random()
//                            }
//                            if (loop == 1) {
//                                var x = Math.cos(phi) * Math.random();
//                                var y = Math.random() * 2 - 1;
//                                var z = Math.cos(theta) * Math.random()
//                            }
//                            if (loop == 2) {
//                                var x = Math.tan(phi) * Math.random();
//                                var y = Math.tan(theta) * Math.random();
//                                var z = Math.random() * 2 - 1
//                            }
                            mesh.position.x = x;
                            mesh.position.y = y;
                            mesh.position.z = z;

                            mesh.userData.children = element.children;
                            mesh.userData.name = element.label;
                            mesh.userData.rang = element.rang;

                            mesh.position.normalize();
                            mesh.position.multiplyScalar(Scalar / element.rang);
                            mesh.scale.set(Scale / element.rang, Scale / element.rang, Scale / element.rang)

                            mesh.position.x = mesh.position.x + intersects[0].object.position.x;
                            mesh.position.y = mesh.position.y + intersects[0].object.position.y;
                            mesh.position.z = mesh.position.z + intersects[0].object.position.z;

                            console.log(element)



                            scene.add(mesh);



                            // lines
                            var material = new THREE.LineBasicMaterial({
                                color: 0xffffff
                            });

                            var geometry = new THREE.Geometry();
                            geometry.vertices.push(
                                intersects[0].object.position,
                                mesh.position
                            );

                            var line = new THREE.Line(geometry, material);
                            scene.add(line);

                            intersects[0].object.userData.bool = true;
                            loop = loop + 1
                        });

                    }

                }

            }
        }
        // ajouter position de caméra actuelle
        camera.userData.push([Math.round(camera.position.x), Math.round(camera.position.y), Math.round(camera.position.z)])
    }

 Here is the function that draw the sphere meshs.

How can i define my coordinates more efficiently?

Thank you

Edited by ThoSzy

Share this post


Link to post
Share on other sites

If you put all of the coordinates in an array then you can use that loop variable to access each element in the array.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...