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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="info">Building of geometry... Please wait...</div>
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://threejs.org/examples/js/utils/BufferGeometryUtils.js"></script>
<script src="https://threejs.org/examples/js/math/ConvexHull.js"></script>
<script src="https://threejs.org/examples/js/geometries/ConvexGeometry.js"></script>
<script src="https://threejs.org/examples/js/modifiers/SubdivisionModifier.js"></script>
<script src="https://threejs.org/examples/js/libs/dat.gui.min.js"></script>
            
          
!
            
              html, body {
  position: relative;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background-color: black;
}
canvas {
  width: 100%;
  height: 100%;
  display; block;
}
#info {
  position: absolute;
  top: 45%;
  font-family: Monospace;
  color: white;
  width: 100%;
  text-align: center;
}
            
          
!
            
              var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, 1, 0.1, 100);
camera.position.set(-10, 10, 13).setLength(40);
var renderer = new THREE.WebGLRenderer({
  antialias: true
});
var canvas = renderer.domElement;
document.body.appendChild(canvas);

var controls = new THREE.OrbitControls(camera, canvas);

scene.add(new THREE.GridHelper(40, 40, 0x444444, 0x444444));

// lights
var light = new THREE.HemisphereLight(0xffffff, 0x202020, 0.5);
scene.add(light);
var dirLight = new THREE.DirectionalLight(0xffffff, 1.25);
dirLight.position.set(10, 5, 10);
scene.add(dirLight);
scene.add(new THREE.AmbientLight(0xffffff, 0.25));

// spiral
var modifier = new THREE.SubdivisionModifier(2);
var box3 = new THREE.Box3();

var spiralPoints = [];

var sResolution = 513;
var sTurns = 15;
var alpha = THREE.Math.degToRad(87);

var maxRadius = 20;
var scaleFactor = maxRadius / Math.exp(Math.PI * 2 * sTurns / Math.tan(alpha));

var radiusVector = new THREE.Vector3(1, 0, 0);
var axis = new THREE.Vector3(0, 1, 0);
let geoms = [];
let t0 = performance.now();
for (let i = 0; i <= sResolution; i++) {
  let theta = map(i, 0, sResolution, 0, Math.PI * 2 * sTurns);
  theta += THREE.Math.randFloatSpread(THREE.Math.degToRad(3));
  let rad = Math.exp(theta / Math.tan(alpha)) * scaleFactor; // cos / sin = cotangent = 1 / tan
  let p = new THREE.Vector3()
    .copy(radiusVector)
    .multiplyScalar(rad + THREE.Math.randFloatSpread(rad * 0.025))
    .applyAxisAngle(axis, theta + Math.PI);
  spiralPoints.push(p);

  let rad2 = Math.exp((theta - Math.PI * 2) / Math.tan(alpha)) * scaleFactor;
  let midRad = (rad + rad2) * 0.5;
  let geomScaling = (rad - midRad) * 1.5;
  let heightScaling =
    geomScaling + THREE.Math.randFloatSpread(geomScaling * 0.025);

  let geom;
  let uVal = i / sResolution;
  if (uVal < 0.9475) {
    geom = getRockGeometry(1, 2, 0.55 + THREE.Math.randFloatSpread(0.1));
  } else if (uVal < 0.965) {
    geom = getRockGeometryType2(1, 2, 0.5, heightScaling);
  } else {
    //geom = new THREE.BoxBufferGeometry(1, 2, 0.6).toNonIndexed();
    geom = getRockGeometryType3(1, 2, 0.525, heightScaling);
  }
  geom.translate(-0.5, 1, 0);
  geom.scale(geomScaling, heightScaling, geomScaling);
  geom.rotateY(theta + Math.PI + THREE.Math.randFloatSpread(Math.PI * 0.05));
  geom.translate(p.x, p.y, p.z);
  geoms.push(geom);
}

let t1 = performance.now();
console.log("Geometry built in " + (t1 - t0) / 1000 + " seconds");

var spiralGeom = new THREE.BufferGeometry().setFromPoints(spiralPoints);
var spiralMat = new THREE.PointsMaterial({ color: "yellow", size: 0.05 });
var spiral = new THREE.Points(spiralGeom, spiralMat);
spiral.visible = false;
scene.add(spiral);

var mainGeom = THREE.BufferGeometryUtils.mergeBufferGeometries(geoms);
var mainMat = new THREE.MeshStandardMaterial({
  color: 0xface8d,
  roughness: 1,
  metalness: 0.125,
  map: new THREE.TextureLoader().load(
    "https://threejs.org/examples/textures/transition/transition1.png",
    t => {
      t.wrapS = THREE.RepeatWrapping;
      t.wrapT = THREE.RepeatWrapping;
      t.repeat.set(0.27, 0.314);
    }
  )
});
var mainMesh = new THREE.Mesh(mainGeom, mainMat);
scene.add(mainMesh);

var wireMat = new THREE.MeshBasicMaterial({ color: 0x888888, wireframe: true });
var wireframe = new THREE.Mesh(mainGeom, wireMat);
//mainMesh.add(wireframe);

var gui = new dat.GUI();
gui.add(spiral, "visible").name("Show points");
gui.add(spiral.material, "size", 0.05, 0.5).name("Size of points");

info.style.display = "none";

render();

function render() {
  if (resize(renderer)) {
    camera.aspect = canvas.clientWidth / canvas.clientHeight;
    camera.updateProjectionMatrix();
  }
  renderer.render(scene, camera);
  requestAnimationFrame(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 map(val, r1start, r1end, r2start, r2end) {
  return r2start + (r2end - r2start) * ((val - r1start) / (r1end - r1start));
}

function getRockGeometryType3(x, y, z, height) {
  let amount = Math.ceil(height / (z)) + 1;
  
  let geoms = [];
  
  let startAngle = THREE.Math.randFloatSpread(THREE.Math.degToRad(120));
  let axis = new THREE.Vector3(0, 1, 0);
  let v3 = new THREE.Vector3(1, 0, 0).setLength(z * 0.5);
  let v3_ = new THREE.Vector3();
  let scaleV3 = new THREE.Vector3(2, 1, 1);
  for (let i = 0; i < amount; i++) {
    let geom = getRockGeometry(y, z * 0.75, z * 2);
    geom.scale(1 - (i * 0.025), 1, 1);
    geom.translate(0, (i * z) + (z * 0.25), 0);
    geoms.push(geom);
    if (i < amount - 1){
      for (let j = 0; j < 3; j++){
        v3_.copy(v3).applyAxisAngle(axis, startAngle + (j * THREE.Math.degToRad(120)) + THREE.Math.randFloatSpread(THREE.Math.degToRad(20))).multiply(scaleV3);
        let addGeom = getRockGeometry(z * 0.5, z * 0.5, z * 0.5);
        addGeom.translate(v3_.x, v3_.y, v3_.z);
        addGeom.translate(0, (i * z) + (z * 0.75), 0);
        geoms.push(addGeom);
      }
    }
  }

  let geomFinal = THREE.BufferGeometryUtils.mergeBufferGeometries(geoms);
  geomFinal.center();
  geomFinal.computeBoundingBox();

  let boxSize = new THREE.Vector3();
  geomFinal.boundingBox.getSize(boxSize);
  let scale = new THREE.Vector3(x, y, z).divide(boxSize);

  geomFinal.scale(scale.x, scale.y, scale.z);

  return geomFinal;
}

function getRockGeometryType2(x, y, z, height) {
  let amount = Math.floor(height / (z * 0.5));

  let geoms = [];

  for (let i = 0; i < amount; i++) {
    let geom = getRockGeometry(y, z, z * 2);
    geom.scale(1 - (i * 0.025), 1, 1);
    geom.translate(0, (i * z) + (z * 0.25), 0);
    geoms.push(geom);
  }

  let geomFinal = THREE.BufferGeometryUtils.mergeBufferGeometries(geoms);
  geomFinal.center();
  geomFinal.computeBoundingBox();

  let boxSize = new THREE.Vector3();
  geomFinal.boundingBox.getSize(boxSize);
  let scale = new THREE.Vector3(x, y, z).divide(boxSize);

  geomFinal.scale(scale.x, scale.y, scale.z);

  return geomFinal;
}

function getRockGeometry(x, y, z) {
  let points = [];

  for (let i = 0; i < 50; i++) {
    points.push(
      new THREE.Vector3(
        THREE.Math.randFloatSpread(x),
        THREE.Math.randFloatSpread(y),
        THREE.Math.randFloatSpread(z)
      )
    );
  }

  let geom = new THREE.ConvexBufferGeometry(points);

  let geomModified = modifier.modify(geom);

  let geomFinal = new THREE.BufferGeometry().fromGeometry(geomModified);
  geomFinal.center();
  geomFinal.computeBoundingBox();

  let boxSize = new THREE.Vector3();
  geomFinal.boundingBox.getSize(boxSize);
  let scale = new THREE.Vector3(x, y, z).divide(boxSize);

  geomFinal.scale(scale.x, scale.y, scale.z);

  //uvs
  let pos = geomFinal.attributes.position;
  let spherical = new THREE.Spherical();
  let v3 = new THREE.Vector3();
  let uvs = [];
  //let baseLength = new THREE.Vector3(x, y, z).multiplyScalar(0.5).length();
  let randTheta = THREE.Math.randFloatSpread(Math.PI);
  for (let i = 0; i < pos.count; i++) {
    v3.fromBufferAttribute(pos, i);
    spherical.setFromVector3(v3);
    let theta =
      spherical.theta >= 0.0 ? spherical.theta : Math.PI * 2 + spherical.theta;
    uvs.push(
      ((theta + randTheta) % (Math.PI * 2)) / (Math.PI * 2),
      ((spherical.phi + randTheta) % Math.PI) / Math.PI
    );
  }
  geomFinal.addAttribute(
    "uv",
    new THREE.BufferAttribute(new Float32Array(uvs), 2)
  );

  //a();

  return geomFinal;
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console