Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas class="webgl"></canvas>
              
            
!

CSS

              
                * {
	box-sizing: border-box;
}

body {
	margin: 0;
	min-height: 100vh;
}
              
            
!

JS

              
                const canvas = document.querySelector('.webgl')
const scene = new THREE.Scene()

scene.background = new THREE.Color(0x1d1e21)

const sizes = {
  width: window.innerWidth,
  height: window.innerHeight
}

// Helpers
const degreesToRadians = (degrees) => {
	return degrees * (Math.PI / 180)
}

const center = (group) => {
	new THREE.Box3().setFromObject(group).getCenter( group.position ).multiplyScalar( - 1 )
	scene.add(group)
}

const random = (min, max, float = false) => {
  const val = Math.random() * (max - min) + min

  if (float) {
    return val
  }

  return Math.floor(val)
}

/**
 * Renderer
 */
const renderer = new THREE.WebGLRenderer({ canvas })

const render = () => {
	renderer.setSize(sizes.width, sizes.height)
	renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
	renderer.render(scene, camera)
}

// Camera
const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 1000)
camera.position.z = 30
scene.add(camera)

window.addEventListener('resize', () =>
{
    // Update sizes
    sizes.width = window.innerWidth
    sizes.height = window.innerHeight

    // Update camera
    camera.aspect = sizes.width / sizes.height
    camera.updateProjectionMatrix()

    // Update renderer
    render(renderer)
})

// Lighting
const lightAmbient = new THREE.AmbientLight(0x9eaeff, 0.5)
scene.add(lightAmbient)

const lightDirectional = new THREE.DirectionalLight(0xffffff, 1)
scene.add(lightDirectional)

// Move the light source towards us
lightDirectional.position.set(5, 5, 50)

const floorGroup = new THREE.Group()
scene.add(floorGroup)

// Figure
class Figure {
	constructor(params) {
		this.params = {
			x: 0,
			y: 0,
			z: 0,
			ry: 0,
			...params
		}
		
		this.dimensions = {
			head: random(4, 8),
			bodyWidth: random(3, 6),
			bodyHeight: random(2, 8),
			legHeight: random(1, 5)
		}
		
		// Create group and add to scene
		this.floorGroup = new THREE.Group()
		this.group = new THREE.Group()
		this.floorGroup.add(this.group)
		scene.add(this.floorGroup)
		
		// Position according to params
		this.group.position.x = this.params.x
		// this.group.position.y = this.params.y
		this.group.position.z = this.params.z
		this.group.rotation.y = this.params.ry
		
		// Material
		this.headHue = random(0, 260)
		this.bodyHue = random(0, 260)
		this.headMaterial = new THREE.MeshLambertMaterial({ color: `hsl(${this.headHue}, 30%, 50%)` })
		this.bodyMaterial = new THREE.MeshLambertMaterial({ color: `hsl(${this.bodyHue}, 85%, 50%)` })
	}
	
	createBody() {
		this.body = new THREE.Group()
		const geometry = new THREE.BoxGeometry(this.dimensions.bodyWidth, this.dimensions.bodyHeight, this.dimensions.bodyWidth)
		const bodyMain = new THREE.Mesh(geometry, this.bodyMaterial)
		
		this.body.add(bodyMain)
		this.group.add(this.body)
		
		this.createLegs()
	}
	
	createHead() {
		const headSize = this.dimensions.head
		// Create a new group for the head
		this.head = new THREE.Group()
		
		// Create the main cube of the head and add to the group
		const geometry = new THREE.BoxGeometry(headSize, headSize, headSize)
		const headMain = new THREE.Mesh(geometry, this.headMaterial)
		this.head.add(headMain)
		
		// Add the head group to the figure
		this.group.add(this.head)
		
		// Position the head group
		this.head.position.y = (this.dimensions.bodyHeight * 0.5) + (this.dimensions.head * 0.5) + 0.4
		
		// Add the eyes
		this.createEyes()
	}
	
	createArms() {
		const height = random(2, 4)
		const width = 0.6
		const xPos = this.dimensions.bodyWidth * 0.5 + width
		
		const rz = random(5, 85, true)
		
		for(let i = 0; i < 2; i++) {
			const armGroup = new THREE.Group()
			const geometry = new THREE.BoxGeometry(width, height, width)
			const arm = new THREE.Mesh(geometry, this.headMaterial)
			const m = i % 2 === 0 ? 1 : -1
			
			// Add arm to group
			armGroup.add(arm)
			
			// Add group to figure
			this.body.add(armGroup)
			
			// Translate the arm by half the height
			arm.position.y = height * -0.5
			
			// Position the arm relative to the figure
			armGroup.position.x = m * xPos
			armGroup.position.y = this.dimensions.bodyHeight * 0.5 - 0.5
			
			// Rotate the arm
			armGroup.rotation.z = degreesToRadians(rz * m)
		}
	}
	
	createEyes() {
		const eyes = new THREE.Group()
		const r = random(0.35, 0.6, true)
		const geometry = new THREE.SphereGeometry(r, 12, 8)
		const material = new THREE.MeshLambertMaterial({ color: 0x44445c })
		
		// Position eyes at random, but must be within the head width, and not too close together
		const eyeX = random(r + 0.2, this.dimensions.head * 0.5 - r, true)
		const eyeY = random(0, 1, true)
		
		for(let i = 0; i < 2; i++) {
			const eye = new THREE.Mesh(geometry, material)
			const m = i % 2 === 0 ? 1 : -1
			
			eyes.add(eye)
			eye.position.x = eyeX * m
		}
		
		this.head.add(eyes)
		
		eyes.position.y = eyeY * -1
		eyes.position.z = this.dimensions.head * 0.5
	}
	
	createLegs() {
		const width = 0.6
		const legs = new THREE.Group()
		const geometry = new THREE.BoxGeometry(width, this.dimensions.legHeight, width)
		
		for(let i = 0; i < 2; i++) {
			const leg = new THREE.Mesh(geometry, this.headMaterial)
			const m = i % 2 === 0 ? 1 : -1
			
			legs.add(leg)
			leg.position.x = m * 0.7
			leg.position.y = this.dimensions.legHeight * -0.5
		}
		
		this.group.add(legs)
		legs.position.y = this.dimensions.bodyHeight * -0.5
		
		this.body.add(legs)
	}
	
	positionVertically() {
		const boundingBox = new THREE.Box3()
		boundingBox.setFromObject(this.floorGroup)
		const size = boundingBox.getSize(new THREE.Vector3())

		this.group.position.y = size.y * 0.5
		this.floorGroup.position.y = -10
		
		// const helper = new THREE.Box3Helper(boundingBox, 0xffff00 );
		// scene.add(helper)
	}
	
	init() {
		this.createBody()
		this.createHead()
		this.createArms()
		this.positionVertically()
	}
}

const figure = new Figure({ ry: degreesToRadians(25) })
figure.init()

const figure2 = new Figure({ 
	x: 15,
	ry: degreesToRadians(-20)
})
figure2.init()

const figure3 = new Figure({ 
	x: -15,
	ry: degreesToRadians(-20)
})
figure3.init()

// center(figure.group)

render()
              
            
!
999px

Console