Load positions attribute from a JSON file in Three

2019-09-10 09:00发布

问题:

I am new in using Three JS Buffer Geometry my task is to draw Buffer Geometry Points, I have position attribute and color attribute value saved in a separate JSON file, how can I use them to populate on Buffer Geometry points? I tried FileReader function to read the JSON but its a asynchronous call. I also tried Loader of Three JS but I couldn't success. My buffer geometry code init() function loop that creates position and set color:

for ( var i = 0; i < 81; i += 3 ) {

                    // positions

                    //var x = i; //x,y,z need positions from json file
                    //var y = i+1;
                    //var z = i+3;

                    var x = Math.random() * n - n2;
                    var y = Math.random() * n - n2;
                    var z = Math.random() * n - n2;

                    positions[ i ]     = x;
                    positions[ i + 1 ] = y;
                    positions[ i + 2 ] = z;

                    // colors

                    var vx = ( x / n ) + 0.5;
                    var vy = ( y / n ) + 0.5;
                    var vz = ( z / n ) + 0.5;

                    color.setRGB( vx, vy, vz );

                    colors[ i ]     = color.r; //value from json file
                    colors[ i + 1 ] = color.g;
                    colors[ i + 2 ] = color.b;

                }