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

              
                
              
            
!

CSS

              
                body {
  overflow: hidden;
  margin: 0;
}
              
            
!

JS

              
                import * as THREE from "https://threejs.org/build/three.module.js";
import {OrbitControls} from "https://threejs.org/examples/jsm/controls/OrbitControls.js";
import {GUI} from "https://cdn.jsdelivr.net/npm/dat.gui@0.7.7/build/dat.gui.module.js";

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, innerWidth / innerHeight, 1, 100);
camera.position.set(0, 13, 21).setLength(7);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);

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

scene.add(new THREE.GridHelper(10, 10));

var pts = [
  new THREE.Vector2(0, 3),
  new THREE.Vector2(1, 1),
  new THREE.Vector2(2, 1),
  new THREE.Vector2(0.75, 0),
  new THREE.Vector2(1.5,-2),
  new THREE.Vector2(0,-3)
];
var g = new THREE.LatheBufferGeometry(pts, 5);
var m = new THREE.MeshBasicMaterial({color: "red", wireframe: true});
var o = new THREE.Mesh(g, m);
scene.add(o);

var mo = new THREE.Mesh(g, new THREE.MeshBasicMaterial({color: "pink", transparent: true, opacity: 0.25}));
o.add(mo);

var texIdx = new THREE.DataTexture(new Float32Array(g.index.array), g.index.count / 3, 1, THREE.RGBFormat, THREE.FloatType);
var texPos = new THREE.DataTexture(g.attributes.position.array, g.attributes.position.count, 1, THREE.RGBFormat, THREE.FloatType);

var pPts = [];
for(let z = 0; z < 50; z++){
  for(let y = 0; y < 50; y++){
    for (let x = 0; x < 50; x++){
      pPts.push(new THREE.Vector3(x, y, z));
    }
  }
}

var pG = new THREE.BufferGeometry().setFromPoints(pPts);
pG.center();
pG.scale(0.125, 0.125, 0.125);
var pM = new THREE.PointsMaterial({size: 0.05, color: 0x88ffff});
var pO = new THREE.Points(pG, pM);
scene.add(pO);

var pUniforms = {
  texIdx: {
    value: texIdx
  },
  texPos: {
    value: texPos
  },
  objMat: {
    value: o.matrixWorld
  }
}

pM.onBeforeCompile = shader => {
  
  shader.uniforms.texIdx = pUniforms.texIdx;
  shader.uniforms.texPos = pUniforms.texPos;
  shader.uniforms.objMat = pUniforms.objMat;
  
  shader.vertexShader = `
  uniform sampler2D texIdx;
  uniform sampler2D texPos;
  uniform mat4 objMat;
  
  bool intersectTriangle( vec3 orig, vec3 dir, vec3 A, vec3 B, vec3 C ) { 
     vec3 E1 = B-A;
     vec3 E2 = C-A;
     vec3 N = cross(E1,E2);
     float det = -dot(dir, N);
     float invdet = 1.0/det;
     vec3 AO  = orig - A;
     vec3 DAO = cross(AO, dir);
     float u =  dot(E2,DAO) * invdet;
     float v = -dot(E1,DAO) * invdet;
     float t =  dot(AO,N)  * invdet; 
     return (abs(det) > 1e-6 && t >= 0.0 && u >= 0.0 && v >= 0.0 && (u+v) <= 1.0);
  }

  ${shader.vertexShader}
`.replace(
  `#include <begin_vertex>`,
  `#include <begin_vertex>

  float idxStep = 1. / ${g.index.count / 3}.;
  float posStep = 1. / ${g.attributes.position.count}.;
  
  float counter = 0.;

  for (float i = 0.; i < ${g.index.count / 3}.; i++){
    vec3 indexData = vec3(texture2D(texIdx, vec2((i + 0.5) * idxStep, 0.5)));
    float a = indexData.r;
    float b = indexData.g;
    float c = indexData.b;

    vec3 vA = vec3(texture2D(texPos, vec2((a + 0.5) * posStep, 0.5)));
    vec3 vB = vec3(texture2D(texPos, vec2((b + 0.5) * posStep, 0.5)));
    vec3 vC = vec3(texture2D(texPos, vec2((c + 0.5) * posStep, 0.5)));

    vA = vec3(objMat * vec4(vA, 1.));
    vB = vec3(objMat * vec4(vB, 1.));
    vC = vec3(objMat * vec4(vC, 1.));

    if (intersectTriangle(transformed + vec3(1e-6), vec3(0, 1, 0), vA, vB, vC)){
      counter++;
    }
  }
  
  if (mod(counter, 2.) == 0.) transformed = vec3(10000);

`
  );
  //console.log(shader.vertexShader);
}

var gui = new GUI();
gui.add(o, "visible").name("base geometry");

var clock = new THREE.Clock();

renderer.setAnimationLoop(() => {
  let t = clock.getElapsedTime();
  o.rotation.x = t * 0.314;
  o.rotation.y = t * 0.27;
  renderer.render(scene, camera);
});

              
            
!
999px

Console