cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              *
*:before
*:after
  margin: 0
  padding: 0
  outline: 0
  border: 0
  box-sizing: border-box

body
  background: #00204A
  
canvas
  display: block
            
          
!
            
              // const options = {}

// const gui = new dat.GUI()
// // gui.add(options, 'meshes', 0, 100).step(1)


const stats = new Stats()

stats.setMode(0)
stats.domElement.style.position = 'absolute'
stats.domElement.style.left = '15px'
stats.domElement.style.top = '0px'
document.body.appendChild(stats.domElement)


const randomNumberInRange = (min, max) => Math.random() * (max - min) + min


const canvas  = document.getElementById('playground')

canvas.width  = window.innerWidth
canvas.height = window.innerHeight



const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(70, canvas.width/canvas.height, .1, 5000)

const controls = new THREE.OrbitControls(camera)
camera.position.x = 260
camera.position.y = 100
camera.position.z = 200
controls.update()


const renderer = new THREE.WebGLRenderer({canvas: canvas, antialias: true})
renderer.shadowMap.enabled = true
renderer.shadowMap.type = THREE.PCFSoftShadowMap
renderer.setClearColor(0x00204A)



// L I G H T
const ambientLight = new THREE.AmbientLight(0xffffff, .2)
scene.add( ambientLight )

const pointLight = new THREE.PointLight(0xffffff,.75)
pointLight.position.set(300, 150, 0)
pointLight.castShadow = true
pointLight.shadow.mapSize.width = 1024
pointLight.shadow.mapSize.height = 1024
scene.add( pointLight )

const pointLightBot = new THREE.PointLight(0xffffff, .25)
pointLightBot.position.set(-300, -150, 0)
pointLightBot.castShadow = true
pointLightBot.shadow.mapSize.width  = 1024
pointLightBot.shadow.mapSize.height = 1024
scene.add(pointLightBot)



// P L A N E T
const geometryPlanet = new THREE.OctahedronGeometry(64, 2)
const materialPlanet = new THREE.MeshStandardMaterial({
  color: 0xff0051,
  wireframe: false,
  shading: THREE.FlatShading,
  metalness: 0,
  roughness: .8,
})
const planet = new THREE.Mesh(geometryPlanet, materialPlanet)
planet.receiveShadow = true
scene.add(planet)


// A S T E R O I D S
const geometryAsteroid = new THREE.SphereGeometry(6, 3, 3)
const materialAsteroid = new THREE.MeshStandardMaterial({
  color: 0xFDB44B,
  wireframe: false,
  shading: THREE.FlatShading,
  metalness: 0,
  roughness: .8,
})

const asteroids = []

for (let i = 100; i--;) {
  asteroids.push({
    x: randomNumberInRange(.7, 1.2),
    y: randomNumberInRange(-20, 20),
    z: randomNumberInRange(.7, 1.2),

    angleX: Math.random(),
    angleY: Math.random(),
    angleZ: Math.random(),

    rotationX: randomNumberInRange(-1, 1),
    rotationY: randomNumberInRange(-1, 1),
    rotationZ: randomNumberInRange(-1, 1),
  })
}

asteroids.forEach((item, i) => {
  const theta = i * 2 * Math.PI / 100 + Math.PI/180*2

  const asteroid = new THREE.Mesh(geometryAsteroid, materialAsteroid)
  asteroid.receiveShadow = true

  asteroid.position.x = 160 * Math.cos(theta) * item.x
  asteroid.position.y = item.y
  asteroid.position.z = 120 * Math.sin(theta) * item.z

  asteroid.rotation.x = item.angleX
  asteroid.rotation.y = item.angleY
  asteroid.rotation.z = item.angleZ

  scene.add(asteroid)
})


// S T A R S
const stars = new THREE.Group()
const geometry = new THREE.SphereGeometry(10, 3, 3)
const material = new THREE.MeshBasicMaterial({
  color: 0xffffff,
  wireframe: false,
})

const radius     = 2000
const separation = 10

for (let i = 0 + separation; i <= 180 - separation; i += separation) {
  const radian1 = i*Math.PI/180

  for (let j = 0; j < 360; j += separation) {
    const radian2 = j*Math.PI/180

    const cube = new THREE.Mesh(geometry, material)

    cube.position.x = randomNumberInRange(.7, 2.2) * radius* Math.sin(radian1) * Math.cos(radian2)
    cube.position.y = randomNumberInRange(.7, 2.2) * radius* Math.sin(radian1) * Math.sin(radian2)
    cube.position.z = randomNumberInRange(.7, 2.2) * radius * Math.cos(radian1)

    cube.rotation.x = Math.random()
    cube.rotation.y = Math.random()
    cube.rotation.z = Math.random()

    stars.add(cube)
  }

}

scene.add(stars)


let tick = 0

const loop = () => {
  requestAnimationFrame(loop)
  stats.begin()
  controls.update()
  ////////////// DRAW START //////////////

  planet.rotation.y += .01
  planet.rotation.x += .0001

  tick += Math.PI/180*2

  for (let i = 4; i < 104; i++) {
    const theta = i * 2 * Math.PI / 100 + Math.PI/180*2

    scene.children[i].position.x = 160 * Math.cos(theta - tick/10) * asteroids[i-4].x
    scene.children[i].position.z = 120 * Math.sin(theta - tick/10) * asteroids[i-4].z

    scene.children[i].rotation.x += asteroids[i-4].rotationX/100
    scene.children[i].rotation.y += asteroids[i-4].rotationY/100
    scene.children[i].rotation.z += asteroids[i-4].rotationZ/100
  }

  stars.rotation.y -= .001
  stars.rotation.z -= .001

  // camera.position.x = scene.children[2].position.x
  // camera.position.z = scene.children[2].position.z
  // camera.lookAt(planet.position)

  renderer.render(scene, camera)

  /////////////// DRAW END ///////////////
  stats.end()
}

loop()



            
          
!
999px
Loading ..................

Console