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

              
                <script src="https://threejs.org/build/three.min.js"></script><!--r83-->
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
              
            
!

CSS

              
                
              
            
!

JS

              
                var height = 400;
var cb = 100; //slices
var ca = 20; //segments
//*****  setup scene -- ignore this section --bend function is below******
var camera = new THREE.PerspectiveCamera(
  30,
  window.innerWidth / window.innerHeight,
  1,
  3000
);
camera.position.set(0, 1000, 1000);

var scene = new THREE.Scene();

var light = new THREE.PointLight();
light.position.set(0, 20, 50);
scene.add(light);

var renderer = new THREE.WebGLRenderer({
  antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

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

var geom = new THREE.CylinderGeometry(50, 50, height, ca,cb, true);
var mesh = new THREE.Mesh(
  geom,
  new THREE.MeshNormalMaterial({ wireframe: true })
);
scene.add(mesh);
var plane = new THREE.GridHelper(20, 40);
scene.add(plane);
//***** end setup scene *****

//add wave FX
warp(mesh.geometry);

//add bend
bend(mesh, 200, 300, .5, height, cb, ca);

//allow camera control
animate();



function bend(mesh, l0, l1, ang, height, cb, ca){
  var slices = [];  //hold arrays of slice vertices
  var mid1 = [];  //center points
  var dir1 = [];  //central axis normalized
  var len1 = [];  //arc length position

  
  //store vertices into slices
  var y = -1000;
  var count = -1;
  var temp_array = []  //holds individual slice
  var slice_length = height/cb;
  var total_arc_length = 0
  mesh.geometry.vertices.forEach(v => {
    if(v.y != y){
      y= v.y;
      var temp_array_copy = temp_array.slice(0);
      slices.push(temp_array_copy);
      
      mid1.push(new THREE.Vector3(0,total_arc_length,0));
      dir1.push(new THREE.Vector3(0,slice_length,0));
      len1.push(total_arc_length);
      
      total_arc_length += slice_length;
      //reset temp array
      temp_array= [];
    }
    temp_array.push(v);
  });
  slices.shift();
  
  
  //set the height start positions
  var j0 = 0;  //start index;
  var j1 = 0;  //end index;
  for(var i=0; i<slices.length; i++){
    if (len1[i]>l0){
      j0 = i;
      break;
    }
  }
  for(var i=j0; i<slices.length; i++){
    if (len1[i]>l1){
      j1 = i;
      break;
    }
  }
  
  //basis vectors
  var P0 = mid1[j0];
  var X0 = new THREE.Vector3(1.0,0.0,0.0);
  var Y0 = new THREE.Vector3(0.0,1.0,0.0);
  var Z0 = new THREE.Vector3(0.0,0.0,1.0);
  
  
  //get the center point of rotation
  var r = (l1-l0)/ang;
  var X_Copy = new THREE.Vector3().copy(X0);
  X_Copy.multiplyScalar(r);
  var PC = new THREE.Vector3().copy(P0).sub(X_Copy);
  var r = Math.abs(r);
  console.log(PC);
  
  //iterate slices in bend section
  var a = 0;
  var p = new THREE.Vector3();
  for(var j=j0; j<slices.length; j++){    
    if(j < j1){
        a = ang*(len1[j]-l0) / (l1-l0);
        p = new THREE.Vector3().copy(Y0);
        p.multiplyScalar(len1[j]-l0);
    }
    else{
        a=ang;
        p = new THREE.Vector3().copy(Y0);
        p.multiplyScalar(l1-l0);
    }
    //performing mid1[j]=rotatex(mid1[j]-p,PC,a);
    var mid1_temp = new THREE.Vector3().copy(mid1[j]);
    mid1_temp.sub(p);
    mid1[j] = rotatez(mid1_temp,PC,a);
    
    var dir1_temp = new THREE.Vector3().copy(dir1[j]);
    dir1[j] = rotatez(dir1_temp, new THREE.Vector3(0,0,0),a);

    
    for (var i=0; i<ca; i++){
      //j is the slice index, //i is segment in slice index
      slices[j][i] = rotatez(slices[j][i].sub(p),PC,a);
    }
  }
  
  mesh.geometry.verticesNeedUpdate = true;
  mesh.geometry.computeVertexNormals(); 
}

function rotatex(p,p0,a){
    var new_position = new THREE.Vector3(0,0,0);
    
  p.sub(p0);
    new_position.z += (p.z*Math.cos(a))+(p.y*Math.sin(a));
    new_position.y -= (p.z*Math.sin(a))+(p.y*Math.cos(a));
    new_position.x = p.x;
    return new_position.add(p0);
}

function rotatey(p,p0,a){
    var new_position = new THREE.Vector3();
    p.sub(p0);
    new_position.z=+(p.x*Math.cos(a))+(p.z*Math.sin(a));
    new_position.y=-(p.x*Math.sin(a))+(p.z*Math.cos(a));
    new_position.x=p.y;
    return new_position.add(p0);
    }
//---------------------------------------------------------------------------
function rotatez(p,p0,a)
    {
    var new_position = new THREE.Vector3();
    p.sub(p0);
    new_position.x=+(p.x*Math.cos(a))+(p.y*Math.sin(a));
    new_position.y=-(p.x*Math.sin(a))+(p.y*Math.cos(a));
    new_position.z=p.z;
    return new_position.add(p0);
    }


function setup_three_js(){

 
  
  components.mesh = mesh;
  components.scene = scene;
  components.camera = camera;
  components.renderer = renderer;
  
  
}

function warp(geom) {
  var vec3 = new THREE.Vector3(); // temp vector
  var vec3_2 = new THREE.Vector3(); // temp vector 2
  var spherical = new THREE.Spherical();
  geom.vertices.forEach(v => {
    vec3.copy(v); // copy current vertex to the temp vector
    vec3.setY(0); // leave x and z (thus the vector is parallel to XZ plane)
    vec3.normalize(); // normalize
    vec3.multiplyScalar(Math.sin(v.y * Math.PI * 2 * 0.01) * 8); // multiply with sin function

    v.add(vec3); // add the temp vectors to the current vertex
  });
  geom.verticesNeedUpdate = true;
  geom.computeVertexNormals();
}

function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
}

              
            
!
999px

Console