Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

              
                <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.3/dat.gui.min.js"></script>
              
            
!

CSS

              
                html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}
canvas {
  width: 100%;
  height: 100%;
  display; block;
}
              
            
!

JS

              
                var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, 1, 1, 1000);
camera.position.setScalar(100).setX(-100);
var renderer = new THREE.WebGLRenderer({
  antialias: true
});
renderer.setClearColor(0x202020);
var canvas = renderer.domElement;
document.body.appendChild(canvas);

var controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.update();

console.log(new THREE.Color(0xface8d));

var grid = new THREE.GridHelper(100, 10); 
grid.position.set(0, -50, 0);
scene.add(grid);

let xSize = 50;
let ySize = 50;
let zSize = 50;
let n = xSize * ySize * zSize;

let geometry = new THREE.InstancedBufferGeometry();
let positions = [];

function mapTo3D(i) {
  let z = Math.floor(i / (xSize * ySize));
  i -= z * xSize * ySize;
  let y = Math.floor(i / xSize);
  let x = i % xSize;
  return { x: x * 2, y: y * 2, z: z * 2};
}

for (let i = 0; i < n; i++) {
  let p = mapTo3D(i);
  positions.push(p.x * 1, p.y * 1, p.z * 1);
}

let boxGeometry = new THREE.BoxBufferGeometry(2, 2, 2).toNonIndexed();
let pointsGeometry = new THREE.BufferGeometry();
pointsGeometry.addAttribute("position", new THREE.BufferAttribute(new Float32Array(positions), 3));
pointsGeometry.center();
geometry.addAttribute("position", new THREE.BufferAttribute(boxGeometry.attributes.position.array, 3));
geometry.addAttribute("instancePosition", new THREE.InstancedBufferAttribute(pointsGeometry.attributes.position.array, 3));

let points = new THREE.Mesh(
  geometry,
  new THREE.ShaderMaterial({ 
    uniforms:{
      time:{
        value: 0
      },
      size: {
        value: 1
      }
    },
    vertexShader:`
      #define PI 3.1415926  

      uniform float time;
      uniform float size;
      attribute vec3 instancePosition;
      varying vec3 vC;
      varying vec3 vP;
      
      // http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
      float udBox( vec3 p, vec3 b, float r )
      {
        return length(max(abs(p)-b,0.0)) - r;
      }
      
      float sdSphere( vec3 p, float s )
      {
        return length(p)-s;
      }

      float sdRoundCone( in vec3 p, in float r1, float r2, float h )
      {
        vec2 q = vec2( length(p.xz), p.y );

        float b = (r1-r2)/h;
        float a = sqrt(1.0-b*b);
        float k = dot(q,vec2(-b,a));

        if( k < 0.0 ) return length(q) - r1;
        if( k > a*h ) return length(q-vec2(0.0,h)) - r2;

        return dot(q, vec2(a,b) ) - r1;
      }
      
      float sdTorus( vec3 p, vec2 t )
      {
        vec2 q = vec2(length(p.xz)-t.x,p.y);
        return length(q)-t.y;
      }

      float opUnion( float d1, float d2 ) { return min(d1,d2); }

      float opSubtraction( float d1, float d2 ) { return max(-d1,d2); }

      float opIntersection( float d1, float d2 ) { return max(d1,d2); }
      
      vec3 opSymX( in vec3 p )
      {
          p.x = abs(p.x);
          return p;
      }
      
      // honestly stolen from here https://www.shadertoy.com/view/MlGGDh
      mat3 rotationMatrix(vec3 m,float a) {
          m = normalize(m);
          float c = cos(a),s=sin(a);
          return mat3(
            c+(1.-c)*m.x*m.x,     (1.-c)*m.x*m.y-s*m.z, (1.-c)*m.x*m.z+s*m.y,
            (1.-c)*m.x*m.y+s*m.z, c+(1.-c)*m.y*m.y,     (1.-c)*m.y*m.z-s*m.x,
            (1.-c)*m.x*m.z-s*m.y, (1.-c)*m.y*m.z+s*m.x, c+(1.-c)*m.z*m.z);
      }
      
      mat3 rotate(vec3 val){
        mat3 matX = rotationMatrix(vec3(1, 0, 0), val.x);
        mat3 matY = rotationMatrix(vec3(0, 1, 0), val.y);
        mat3 matZ = rotationMatrix(vec3(0, 0, 1), val.z);
        return matX * matY * matZ;
      }

      void main(){
        vP = position;
        vec3 vPos = instancePosition;
        vPos.y += 15. - abs(sin(time * PI)) * 5.;
        vPos.z += 5.;
        vPos *= rotate(vec3(0., time * 0.5, 0.));
        
        // head
        vec3 headPos = vPos;
        if (headPos.y > 20. && headPos.z > 0.) headPos.z -= 5.; 
        if (headPos.y < 18. && headPos.y > -7. && abs(headPos.x) < 5. && headPos.z > 0.) headPos.z -= 15. - (headPos.y) * 0.5; 
        if (headPos.y < -12. && abs(headPos.x) < 9. && headPos.z > 0.) headPos.z -= 5.;
        float head = udBox(headPos, vec3(12,22,9), 9.);
        
        // eyes
        vec3 eyesPos = vPos;
        float eyes = sdSphere(opSymX(eyesPos) - vec3(9., 16., 16.), 3.);
        
        // eye-brows
        vec3 browsPos = vPos;
        float brows = udBox(opSymX(browsPos) - vec3(9, 20., 22.), vec3(6., 2., 2.), 0.);

        // lips
        vec3 lipsPos = vPos;
        float lips = udBox(lipsPos - vec3(0., -11.5, 20.), vec3(8., 2., 2.), 0.);
        
        // hair
        vec3 hairPos = vPos;
        hairPos.y += 40.;
        hairPos.z *= 1.35;
        hairPos.z +=22.;
        float hair = sdRoundCone(hairPos * rotate(vec3(-PI * 0.1, 0., 0.)), 23., 27., 60.);

        // ring
        vec3 ringPos = vPos;
        ringPos.z *= 1.2;
        float ring = sdTorus((ringPos - vec3(0., 31., -6.)) * rotate(vec3(PI * 0.1, 0, 0)), vec2(22., 2));

        // feather
        vec3 featherPos = vPos;
        featherPos.x *= 4.;
        float feather = sdRoundCone((featherPos - vec3(100., 30., 0.)) * rotate(vec3(PI *0.2, 0., 0.)), 7., 8., 30.);
        
        float res = opUnion(head, eyes);
        res = opUnion(res, brows);
        res = opUnion(res, lips);
        res = opUnion(res, hair);
        res = opUnion(res, ring);
        res = opUnion(res, feather);
        float vSize = size;
        if ( res > 0.0 ) vSize *= 0.0;
        
        vC = vec3(0);
        if (res == head) vC = vec3(0.9903921568627451, 0.707843137254902, 0.4529411764705883);
        if (res == eyes) vC = vec3(0.3, 0.2, 0.1);
        if (res == brows) vC = vec3(0.5);
        if (res == lips) vC = vec3(0.9903921568627451, 0.707843137254902, 0.4529411764705883) * 0.5;
        if (res == hair) vC = vec3(0.7);
        if (res == ring) vC = vec3(0.5, 0., 0.);
        if (res == feather) vC = vec3(1.);
        

        vec4 mvPosition = modelViewMatrix * vec4( position * vSize + instancePosition, 1.0 );
        gl_Position = projectionMatrix * mvPosition;
      }
    `,
    fragmentShader: `
      
      varying vec3 vC;
      varying vec3 vP;
      
      void main(){
        vec3 c = vC;
        if (min(vP.x, min(vP.y, vP.z)) < -0.9) c *= 0.75; // fake shadow
        gl_FragColor = vec4( c, 1.0);
      }
    `
  })
);
scene.add(points);

// Box3Helper
/*var box3 = new THREE.Box3().setFromObject(points);
var box3Helper = new THREE.Box3Helper(box3, 0x404040);
scene.add(box3Helper);*/

var clock = new THREE.Clock();
var time = 0;

render();

function resize(renderer) {
  const canvas = renderer.domElement;
  const width = canvas.clientWidth;
  const height = canvas.clientHeight;
  const needResize = canvas.width !== width || canvas.height !== height;
  if (needResize) {
    renderer.setSize(width, height, false);
  }
  return needResize;
}

function render() {
  if (resize(renderer)) {
    camera.aspect = canvas.clientWidth / canvas.clientHeight;
    camera.updateProjectionMatrix();
  }
  
  time += clock.getDelta();
  points.material.uniforms.time.value = time;
  
  renderer.render(scene, camera);
  requestAnimationFrame(render);
}

              
            
!
999px

Console