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. 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.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/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);
var renderer = new THREE.WebGLRenderer({
  antialias: true
});
renderer.setClearColor(0x606060);
var canvas = renderer.domElement;
document.body.appendChild(canvas);

var controls = new THREE.OrbitControls(camera, renderer.domElement);
//controls.target = new THREE.Vector3(0, 50, 0);
controls.update();

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

let xSize = 51;
let ySize = 101;
let zSize = 51;
let n = xSize * ySize * zSize;

let geometry = new THREE.BufferGeometry();
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, y: y, z: z };
}

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

let positionAttribute = new THREE.Float32BufferAttribute(positions, 3);
geometry.addAttribute("position", positionAttribute);
geometry.center();
//geometry.translate(0, ySize * 0.5, 0);

let points = new THREE.Points(
  geometry,
  new THREE.ShaderMaterial({ 
    uniforms:{
      solid: {
        value: {
            size: 10,
            position: new THREE.Vector3(0, 0, 0),
            rotation: new THREE.Vector3(),
            distance: new THREE.Vector3(25, 25, 25)
        }
      },
      size: {
        value: 1
      }
    },
    vertexShader:`
      uniform float size;
      varying vec3 vPos;
      void main(){
        vPos = position;
        vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
        gl_PointSize = size * ( 300.0 / length( mvPosition.xyz ) );
        gl_Position = projectionMatrix * mvPosition;
      }
    `,
    fragmentShader: `
      #define PI 3.1415926      

      struct solidParams {
        float size;
        vec3 position;
        vec3 rotation;
        vec3 distance;
      };

      uniform solidParams solid;

      varying vec3 vPos;
      
      // http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
      float udBox( vec3 p, vec3 b )
      {
        return length(max(abs(p)-b,0.0));
      }
      
      float sdSphere( vec3 p, float s )
      {
        return length(p)-s;
      }
      
      vec3 opRep( in vec3 p, in vec3 c, out vec3 idx)
      {
          idx = floor(p / c);
          vec3 res = mod(p,c)-0.5*c;
          return res;
      }
      
      // 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(){
        vec3 idx = vec3(0);
        vec3 p = opRep(((vPos - solid.position)  * rotate(solid.rotation)), solid.distance, idx);
        if ( sdSphere ( p, solid.size ) > 0.0 ) discard;
        
        gl_FragColor = vec4((idx + 2.) / 3., 1.0); // color each index

        // indices are in the range from [-2, -2, -2] to [1, 1, 1]
        // add scalar 2 to make the range from [0, 0, 0] to [3, 3, 3];
        // divide by 3 to set the color of the indexed clone
      }
    `,
    transparent: false
  })
);
scene.add(points);

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

// GUI section
var gui = new dat.GUI();
var solid = gui.addFolder("solid");
var size = solid.addFolder("size");
size.add(points.material.uniforms.solid.value, "size", 5, 15).name("radius");
size.open();
var position = solid.addFolder("position");
position.add(points.material.uniforms.solid.value.position, "x", -50, 50);
position.add(points.material.uniforms.solid.value.position, "y", -50, 50);
position.add(points.material.uniforms.solid.value.position, "z", -50, 50);
position.open();
var rotation = solid.addFolder("rotation")
rotation.add(points.material.uniforms.solid.value.rotation, "x", -Math.PI, Math.PI);
rotation.add(points.material.uniforms.solid.value.rotation, "y", -Math.PI, Math.PI);
rotation.add(points.material.uniforms.solid.value.rotation, "z", -Math.PI, Math.PI);
rotation.open();
var distance = solid.addFolder("distance");
distance.add(points.material.uniforms.solid.value.distance, "x", 25, 50);
distance.add(points.material.uniforms.solid.value.distance, "y", 25, 50);
distance.add(points.material.uniforms.solid.value.distance, "z", 25, 50);
distance.open();
var particles = gui.addFolder("particles");
particles.add(points.material.uniforms.size, "value", 0.5, 2).step(0.1).name("size");


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();
  }
  renderer.render(scene, camera);
  requestAnimationFrame(render);
}

              
            
!
999px

Console