leodeslf / worley-noise

A Worley Noise library for JavaScript.
https://www.npmjs.com/package/@leodeslf/worley-noise
MIT License
0 stars 0 forks source link

Plot points on 3D sphere #1

Closed digimbyte closed 2 years ago

digimbyte commented 2 years ago

Having some trouble - I followed the general guide of a torus example but with a sphere but the values rendering out are returning "Infinity"

I suspect the values aren't handled correctly and would like some advice from the developer


import Worley from '@leodeslf/worley-noise';
import sphere from 'primitive-sphere';
import RVP from 'random-volume-points';

const radius = 10;
const SphereMesh = sphere(radius, { segments: radius * 2 });
const spots = RVP(SphereMesh.positions, SphereMesh.cells, 1000)
const worley = new Worley(spots, '3d', 'manhattan');

for (let lat = 0; lat < 360 * f; lat++) {
    for (let long = 0; long < 180 * f; long++) {

        let phi = ((long) / f) * (Math.PI / 180);
        let theta = ((lat) / f) * (Math.PI / 180);

        let x = (radius) * Math.cos(lat) * Math.cos(long);
        let y = (radius) * Math.cos(lat) * Math.sin(long);
        let z = (radius) * Math.sin(lat);

        let noise= worley.st([x, y, z]);
        output.push(parseInt(noise* 128));
}}
digimbyte commented 2 years ago

tried another solution by mapping the array of values to a Vec3 but still have an Infinity on the output

const radius = 10;
const SphereMesh = sphere(radius, { segments: radius * 2 });
const spots = RVP(SphereMesh.positions, SphereMesh.cells, 3);
const newSpots = spots.map(spot => new Vec3(spot[0], spot[1], spot[2]));
const worley = new Worley(newSpots, '3d', 'euclidean');
leodeslf commented 2 years ago

Well, you almost got it right on the first try.

The Worley.st(position) needs position to be a Vec3, which is an object that has x, y, and z, among other features (while you are passing an array).

However, we don't actually need it to be a Vec3 in this case because we only need the x, the y, and the z to get the distances.

This should work:

// let noise = worley.st([x, y, z]);
let noise = worley.st({ x, y, z });

This should work in case you want to use the Vec3:

// let noise = worley.st([x, y, z]);
let noise = worley.st(new Vec3(...[x, y, z]));

I don't know what the shape of the RVP looks like, but notice each spot should also have its own x, y, and z attributes.


PD: I think the second try actually generates an array with three vectors, which is not what we need.