SAMPLE SPHERE

USING THREE.JS

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.4.0/math.js"></script>

JavaScript

var camera, scene, material, mesh, geometry, renderer

const randomPointOnSphere = () => {
  let i = 0;
  let j = 0;
  let k = 0;

  let running = true;

  while (running) {
    const u = Math.random() * (Math.random() < 0.5 ? 1 : -1);
    const v = Math.random() * (Math.random() < 0.5 ? 1 : -1);

    if (u * u + v * v >= 1) continue;

    i = 2 * u * Math.sqrt(1 - u * u - v * v);
    j = 2 * v * Math.sqrt(1 - u * u - v * v);
    k = 1 - 2 * (u * u + v * v);

    running = false;
  }

  return [i, j, k];
};

// Matrices used for faster tensor conversion
const MI = [
  [0, 5, 4],
  [5, 1, 3],
  [4, 3, 2]
];

const MK = [
  [1, 0.5, 0.5],
  [0.5, 1, 0.5],
  [0.5, 0.5, 1]
];

const reorientAxes = (c, direction) => {
  const S = math.inv(c);

  // Normalize vector
  let [i, j, k] = direction;

  let length = math.sqrt(i * i + j * j + k * k);
  i /= length;
  j /= length;
  k /= length;

  length = math.sqrt(i * i + j * j);
  i = length === 0 ? 0 : i / length;
  j = length === 0 ? 0 : j / length;

  const s = [
    [
      [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
      ],
      [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
      ],
      [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
      ],
    ],
    [
      [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
      ],
      [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
      ],
      [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
      ],
    ],
    [
      [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
      ],
      [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
      ],
      [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
      ],
    ],
  ];

  const s2 = [
    [
      [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
      ],
      [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
      ],
      [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]
      ],
    ],
    [
      [
        [0,...