carlmw/gitlactica

View on GitHub
adapters/webgl/planet.js

Summary

Maintainability
A
0 mins
Test Coverage
var THREE = require('three'),
    shaders = require('./shaders'),
    geometry = new THREE.SphereGeometry(1000, 128, 128),
    materials = {},
    atmosphereGeometry = geometry.clone(),
    atmosphereMaterial;

module.exports = Planet;

function Planet(name, colour, camera, texture) {
  if (!atmosphereMaterial) {
    atmosphereMaterial = generateAtmosphereMaterial(camera);
  }

  var pivot = new THREE.Object3D(),
      mesh = new THREE.Mesh(geometry, selectMaterial(colour, texture)),
      atmosphereMesh = new THREE.Mesh(atmosphereGeometry, atmosphereMaterial);

  atmosphereMesh.scale.multiplyScalar(1.05);

  pivot.add(mesh);
  pivot.add(atmosphereMesh);

  this.mesh = mesh;
  this.pivot = pivot;
}

function selectMaterial(colour, texture) {
  if (materials[colour]) {
    return materials[colour];
  }
  materials[colour] = generateMaterial(colour, texture);
  return materials[colour];
}

function generateMaterial(colour, texture) {
  var emissive = new THREE.Color(colour),
      matColour = new THREE.Color(),
      hsl = emissive.getHSL();
  matColour.setHSL(hsl.h, 1, 1);
  emissive.setHSL(hsl.h, 1, hsl.l);

  return new THREE.MeshPhongMaterial({
    color: matColour,
    emissive: emissive,
    map: texture
  });
}

function generateAtmosphereMaterial (camera) {
  return new THREE.ShaderMaterial({
    uniforms: {
      c: { type: "f", value: 0.5 },
      p: { type: "f", value: 1 },
      alpha: { type: "f", value: 0.4 },
      glowColor: { type: "c", value: new THREE.Color(0x72A7E5) },
      viewVector: { type: "v3", value: camera.position }
    },
    vertexShader: shaders.glowVert,
    fragmentShader: shaders.glowFrag,
    side: THREE.FrontSide,
    blending: THREE.AdditiveBlending,
    transparent: true
  });
}