Skip to content

Instantly share code, notes, and snippets.

@artivilla
Created May 1, 2022 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save artivilla/cfcd39de6c6004ac1aaeda9d5bca49ea to your computer and use it in GitHub Desktop.
Save artivilla/cfcd39de6c6004ac1aaeda9d5bca49ea to your computer and use it in GitHub Desktop.
THREEJS
```
// generate random stars in the scene
function addStar() {
const geo = new THREE.SphereGeometry(0.25, 24, 24)
const mat = new THREE.MeshStandardMaterial({color: 0xffffff}) // standard responds to lights in the scene
const s = new THREE.Mesh(geo, mat)
const [x,y,z] = Array(3).fill().map(() => THREE.MathUtils.randFloatSpread(100)) // numbers between 0-100
s.position.set(x,y,z)
scene.add(s)
}
Array(200).fill().forEach(addStar)
---
// add background jgps with textureloader
// can also call cb and show loading until static assets are ready
const spaceTex = new THREE.TextureLoader().load('space.jpg')
scene.background = spaceTex
---
const moonTex = new THREE.TextureLoader().load('moon.jpg')
const bumpTex = new THREE.TextureLoader().load('normal.jpg')
const moon = new THREE.Mesh(
new THREE.SphereGeometry(3, 32, 32)
new THREE.MeshStandardMaterial({
map: moonTex
normalMap: bumpTex // bumpy surface
})
)
scene.add(moon)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment