Creating a 3D visualization with three.js

My project requires me to create a 3D scatter graph using three.js. I have attempted to implement the code provided below. While the code is functional, the resulting graph appears more like a 2D representation. Adjusting the camera position can add depth to the graph, but I am struggling to pinpoint the precise camera position.

let scene,camera,renderer,axes;
let set = [];

let createGeometry = function()
{
    set.push(new THREE.Vector3(5,5,10) );
    set.push(new THREE.Vector3(10,15,17));
    set.push(new THREE.Vector3(15,20,12));
    set.push(new THREE.Vector3(20,16,10));
    set.push(new THREE.Vector3(17,9,19));
    set.push(new THREE.Vector3(23,2,13));

    let setgeometry = new THREE.BufferGeometry().setFromPoints(set);
    let setmaterial = new THREE.PointsMaterial({ color : 0xff0000 , size : 10 ,sizeAttenuation : false});
    let plot = new THREE.Points( setgeometry , setmaterial );

    scene.add(plot);

}    
let init = function(){
    scene = new THREE.Scene;
    scene.background = new THREE.Color(0x000000);

    camera = new THREE.PerspectiveCamera(25 , window.innerWidth/window.innerHeight , 1 , 1000);
    camera.position.set(20,10,90);

    let axes =new THREE.AxesHelper(25);
    scene.add(axes);

    createGeometry();

    renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth,window.innerHeight);
    document.body.appendChild(renderer.domElement);

};

let mainloop = function(){
    
    renderer.render(scene , camera);
    requestAnimationFrame(mainloop);
};

init();
mainloop();
html, body {margin: 0; padding: 0;overflow: hidden;}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d39253f28280d7d637c7c7a637c">[email protected]</a>/build/three.min.js"></script>

Answer №1

Your computer display functions as a two-dimensional plane with pixels restricted to the X and Y axes. Without movement, your chart may appear flat and lack depth. To enhance the third dimension, consider incorporating animation. In the example below, the chart is rotated around the center by updating the camera position on each frame using the following code:

camera.position.set(
    Math.sin(time) * 70,
    70,
    Math.cos(time) * 70);
camera.lookAt(0,0,0);

let scene,camera,renderer,axes;
let set = [];

let createGeometry = function()
{
    set.push(new THREE.Vector3(5,5,10) );
    set.push(new THREE.Vector3(10,15,17));
    set.push(new THREE.Vector3(15,20,12));
    set.push(new THREE.Vector3(20,16,10));
    set.push(new THREE.Vector3(17,9,19));
    set.push(new THREE.Vector3(23,2,13));

    let setgeometry = new THREE.BufferGeometry().setFromPoints(set);
    let setmaterial = new THREE.PointsMaterial({ color : 0xff0000 , size : 10 ,sizeAttenuation : false});
    let plot = new THREE.Points( setgeometry , setmaterial );

    scene.add(plot);

}    
let init = function(){
    scene = new THREE.Scene;
    scene.background = new THREE.Color(0x000000);

    camera = new THREE.PerspectiveCamera(25 , window.innerWidth/window.innerHeight , 1 , 1000);

    let axes =new THREE.AxesHelper(25);
    scene.add(axes);

    createGeometry();

    renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth,window.innerHeight);
    document.body.appendChild(renderer.domElement);

};

let mainloop = function(t){
    let time = t / 1000;
    camera.position.set(
        Math.sin(time) * 70,
        70,
        Math.cos(time) * 70);
    camera.lookAt(0,0,0);
    renderer.render(scene , camera);
    requestAnimationFrame(mainloop);
};

init();
mainloop(0);
html, body {margin: 0; padding: 0;overflow: hidden;}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="14607c66717154243a2525233a25">[email protected]</a>/build/three.min.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporating a JavaScript object into a DataTables JSON structure: A practical guide

I have integrated the datatables jQuery plugin into my project and I am looking to streamline the process by creating a shared function to easily call a datatable from multiple pages without duplicating code. To achieve this, I am attempting to define the ...

Is it possible to have Vue.js code within a v-for loop in such a way that it executes every time the v-for loop runs?

<div class="questions" v-for="(q,index) in questions " :key="index" {{this.idx+=1}} > <h3 > {{q.content}}</h3> <h3> A) {{q.a}} </h3> <h3> B) {q.b}} </h3> ...

Exploring node-validator's capabilities with asynchronous validation

Currently tackling my initial Node.js venture and facing a roadblock. Utilizing node-validator for input validation and working with Sequelize as an ORM. The Dilemma of Async Validation Striving to create custom validation to verify username availability ...

Guide to Displaying Items in Order, Concealing Them, and Looping in jQuery

I am trying to create a unique animation where three lines of text appear in succession, then hide, and then reappear in succession. I have successfully split the lines into span tags to make them appear one after the other. However, I am struggling to fin ...

"Having trouble getting the onChange function to work with Material-UI's Select

I have encountered an issue while trying to implement the material-ui SelectField component in my project. While the common select component works seamlessly, I face problems when using the SelectField component. The main problem arises with the invocati ...

The post processing effect in Three.js does not support FXAA when SSAO is activated

I am having trouble getting SSAO and FXAA effects to work together in this simple test scene. Enabling SSAO works fine, but when I also enable FXAA, the render turns black. In the provided fiddle, if you uncomment composer.addPass(FXAA_effect); you will s ...

Is it possible for Three.js to produce an accurate rendering without the use of specific WebGL

Recently, I noticed that in the latest Samsung OS update (testing on a Galaxy S6), Chrome disables WebGL. The explanation for this can be found here. To bypass it, I'm using the flag: --ignore-gpu-blacklist Here are the error messages I receive from ...

The MaskedInput text does not appear properly when it is passed through the props

Currently, I am facing an issue with a Material UI OutlinedInput along with the MaskedInput component from react-text-mask. Everything works fine when I input text initially and the element is not in focus. However, upon closing and reopening the Dialog wi ...

Discover siblings in React component siblings

Creating a parent element (Board) that generates a list of children and provides a method to access this list can be done like so: export default class Board extends React.Component { constructor(props) { super(props); this.getList = t ...

The discord.js argument for startsWith should not be a standard regular expression

https://i.sstatic.net/UG79z.png What could be the reason behind this not working as expected? I am trying to check if a string starts with a number, and furthermore, I want it to handle multiple numbers. For instance, if the string starts with 1259823 the ...

Determining the scrollWidth of a div with an absolutely positioned child div

Having some trouble determining the width of a div's content instead of the div itself. The typical solution would involve using Javascript's scrollWidth property. However, there is a complication in this case. Inside the div, another div is ab ...

The Fancybox iFrame is not appearing on the screen

I am facing an issue with the html and javascript code I have. The html looks like this: <ul> <a class="iframe" href="/posting/form?id=8"><li>Publish</li></a> </ul> and I am using the following javascript: <scr ...

typescript what type of functionality do dynamic class methods provide

I'm currently working on building a class that creates dynamic methods during the constructor stage. While everything is functioning properly, I've encountered an issue with VS Code's auto suggestion not recognizing these dynamic methods. Ho ...

The amazing magnific popup boasts a pair of close buttons

I recently started working on integrating a front-end HTML theme with a back-end Laravel app. Oddly enough, I noticed that the popup modal is displaying two close x buttons instead of just one. Despite my efforts, I have been unable to pinpoint what exactl ...

How can a dialog be displayed using a template in Onsen UI in a proper manner?

I am having trouble displaying a dialog in my Angular app with Onsen UI 1.3.6. Whenever I try to show the dialog, I encounter a 404 Not Found error. This is the JavaScript code I am using: ons.createDialog('iconselector.html').then(function(dl ...

Duplicate the array of objects and make alterations without affecting the original array

I have an array of objects that I need to deep copy and make modifications to each object without altering the original array or its contents. Here is my approach in JavaScript, but I am open to suggestions on a better method. const users = [ { ...

(Javascript - Arrays) Find the leftmost and rightmost connected characters

Looking for the leftmost and topmost connected '1' character in a 2D matrix? Find the most left & top connected '1' character Find the most right & bottom connected '1' character EDIT 2.0: To start, provide the coordina ...

Encountering a script error when upgrading to rc4 in Angular 2

After attempting to update my Angular 2 version to 2.0.0.rc.4, I encountered a script error following npm install and npm start. Please see my package.json file below "dependencies": { "@angular/common": "2.0.0-rc.4", "@angular/core": "2.0.0-rc.4", ...

Mastering the art of chaining promises in Mongoose

I need help figuring out how to properly chain promises for a "find or create" functionality using mongodb/mongoose. So far, I've attempted the following: userSchema.statics.findByFacebookIdOrCreate = function(facebookId, name, email) { var self = ...

Preventing data duplication when refreshing a webpage using Node.js

I am currently utilizing Mustache and Nodejs to populate a dropdown menu with a list of options on my website. However, every time the page is refreshed, I encounter duplicate entries in the dropdown. How can this issue be resolved? I trust that my inquiry ...