90 lines
		
	
	
	
		
			1.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			90 lines
		
	
	
	
		
			1.7 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
let charges = new Map();
 | 
						|
let counter = 0;
 | 
						|
 | 
						|
let sphere_mode = 'circles';
 | 
						|
let sphere_radius = 200;
 | 
						|
 | 
						|
function preload() {
 | 
						|
  earth = loadImage("atlas1.jpg");
 | 
						|
}
 | 
						|
 | 
						|
function setup() {
 | 
						|
  createCanvas(600, 600, WEBGL);
 | 
						|
}
 | 
						|
 | 
						|
function draw() {
 | 
						|
  orbitControl();
 | 
						|
  background(50);
 | 
						|
 | 
						|
  make_lights();
 | 
						|
  draw_sphere(sphere_radius, 25);
 | 
						|
}
 | 
						|
 | 
						|
function draw_sphere(radius, n_axis_circles) {
 | 
						|
  stroke(0x3f);
 | 
						|
  noFill();
 | 
						|
 | 
						|
  push();
 | 
						|
  rotateX(TAU / 4);
 | 
						|
  draw_circles(
 | 
						|
    radius,
 | 
						|
    sphere_mode === 'earth' ? 2 : n_axis_circles,
 | 
						|
    color(0x00, 0x9f, 0xff),
 | 
						|
    color(0xff, 0x9f, 0x00),
 | 
						|
  );
 | 
						|
  pop();
 | 
						|
  push();
 | 
						|
  rotateY(TAU / 4);
 | 
						|
  draw_circles(
 | 
						|
    radius,
 | 
						|
    sphere_mode === 'earth' ? 2 : n_axis_circles,
 | 
						|
    color(0xff, 0x00, 0xff),
 | 
						|
    color(0x00, 0xff, 0x00),
 | 
						|
  );
 | 
						|
  pop();
 | 
						|
 | 
						|
  if (sphere_mode === 'earth') {
 | 
						|
    noStroke();
 | 
						|
    noFill();
 | 
						|
    tint(0xff, 0x9f);
 | 
						|
    texture(earth);
 | 
						|
    push();
 | 
						|
    rotateY(TAU / 4);
 | 
						|
    sphere(radius);
 | 
						|
    pop();
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
function draw_circles(radius, n_circles, pole_1_color, pole_2_color) {
 | 
						|
  push();
 | 
						|
  stroke(pole_1_color);
 | 
						|
  translate(0, 0, -radius);
 | 
						|
  point(0, 0);
 | 
						|
  pop();
 | 
						|
  for (let i = 1; i < n_circles; i += 1) {
 | 
						|
    const angle = map(i, 0, n_circles - 1, -TAU / 4, TAU / 4);
 | 
						|
    const circle_radius = radius * cos(angle);
 | 
						|
    push();
 | 
						|
    translate(0, 0, radius * sin(angle));
 | 
						|
    circle(0, 0, circle_radius * 2);
 | 
						|
    pop();
 | 
						|
  }
 | 
						|
  push();
 | 
						|
  stroke(pole_2_color);
 | 
						|
  translate(0, 0, radius);
 | 
						|
  point(0, 0);
 | 
						|
  pop();
 | 
						|
}
 | 
						|
 | 
						|
function make_lights() {
 | 
						|
  let light = createVector(0, 1, -1);
 | 
						|
  light.normalize();
 | 
						|
  directionalLight(0x1f, 0x1f, 0x1f, light);
 | 
						|
  ambientLight(0xbf);
 | 
						|
}
 | 
						|
 | 
						|
function keyPressed() {
 | 
						|
  if (key == 'd') {
 | 
						|
    sphere_mode = sphere_mode === 'earth' ? 'circles' : 'earth';
 | 
						|
  }
 | 
						|
}
 |