123

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.

            
              <script src="https://threejs.org/build/three.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://threejs.org/examples/js/libs/dat.gui.min.js"></script>
            
          
!
            
              body{
  margin: 0px;
  background: #000000;
}
            
          
!
            
              var renderer, scene, camera, cameraH,sceneH, mainMaterial,mainMaterialShader, holeMaterial, holeMaterialShader, holeMaterialCube, holeMaterialCubeShader ,holeSphere, mainSphere,startTime, HelperMesh, curveObject;
var cubeMap = new THREE.CubeTextureLoader()
	.setPath( 'https://threejs.org/examples/textures/cube/Bridge2/' )
	.load( [
		'posx.jpg',
		'negx.jpg',
		'posy.jpg',
		'negy.jpg',
		'posz.jpg',
		'negz.jpg'
	] );
var materialParams = {
  color:0xffffff,
  transparent:true,
  roughness:0.7,
  metalness: 0.2,
  opacity:1.0,
  envMap:cubeMap,
}

var hSize = 10;

var params = {
  Cube: function(){
    console.log("its a cube!");
    if(mainSphere){
      mainSphere.geometry = new THREE.BoxGeometry( 20, 20, 20 );
    }
    if(holeSphere){
      holeSphere.material = holeMaterialCube;
    }
    
  },
  Sphere: function(){
    console.log("its a Sphere!")
    if(mainSphere){
      mainSphere.geometry = new THREE.SphereGeometry( 10, 100, 100 );
    }
    if(holeSphere){
      holeSphere.material = holeMaterial;
    }
  },
  size: hSize,
  showHelpers: true
}

var gui = new dat.GUI();
gui.add(params, 'Cube');
gui.add(params, 'Sphere');
gui.add(params, 'size', 1, 20).onFinishChange(function(value){
  console.log(HelperMesh);
  if(holeSphere){
    for(var i =0;i< holeSphere.geometry.vertices.length;i++){
      holeSphere.geometry.vertices[i].normalize().multiplyScalar(value);
    }
    holeSphere.geometry.verticesNeedUpdate = true;
    
    HelperMesh.geometry = new THREE.SphereBufferGeometry( value, 12, 12 );

    hSize = value;
  }
  
});
gui.add(params, 'showHelpers').onChange(function(value){
  HelperMesh.visible = value;
  curveObject.visible = value;
});
gui.open();

SetMainMaterial();
SetHoleMaterialCube();
SetHoleMaterial();

init();
animate();

function SetMainMaterial(){
  mainMaterial = new THREE.MeshStandardMaterial(materialParams);
  mainMaterial.onBeforeCompile = function ( shader ) {
    
    shader.vertexShader = shader.vertexShader.replace(
      '#include <uv_pars_vertex>',
      [
        '#include <uv_pars_vertex>',
        'varying vec3 localPosition;'
      ].join( '\n' )
    ).replace(
      '#include <uv_vertex>',
      [
        'localPosition = vec3(position);',
        '#include <uv_vertex>',
      ].join( '\n' )
    );
    
    shader.fragmentShader = shader.fragmentShader.replace(
      '#include <uv_pars_fragment>',
      [
        '#include <uv_pars_fragment>',
        'varying vec3 localPosition;',
        'uniform vec4 posSize1;',
      ].join( '\n' )
    ).replace(
      'gl_FragColor = vec4( outgoingLight, diffuseColor.a );',
      [
        'if(length(posSize1.xyz - localPosition) < posSize1.w * 0.99){',
        'diffuseColor.a = 0.0;',
        '}',
        'gl_FragColor = vec4( outgoingLight, diffuseColor.a );',
      ].join( '\n' )
    );
    shader.uniforms.posSize1 = {value: new THREE.Vector4(0,10,0,hSize)};
    
    mainMaterialShader = shader;
  }
}
function SetHoleMaterial(){
  holeMaterial = new THREE.MeshStandardMaterial(materialParams);
  holeMaterial.side = THREE.BackSide;
  holeMaterial.onBeforeCompile = function ( shader ) {
    
    shader.vertexShader = shader.vertexShader.replace(
      '#include <uv_pars_vertex>',
      [
        '#include <uv_pars_vertex>',
        'varying vec3 localPosition;'
      ].join( '\n' )
    ).replace(
      '#include <uv_vertex>',
      [
        'localPosition = vec3(position);',
        '#include <uv_vertex>',
      ].join( '\n' )
    );
    
    shader.fragmentShader = shader.fragmentShader.replace(
      '#include <uv_pars_fragment>',
      [
        '#include <uv_pars_fragment>',
        'varying vec3 localPosition;',
        'uniform vec4 posSize1;',
        'uniform float mainSize;',
      ].join( '\n' )
    ).replace(
      'gl_FragColor = vec4( outgoingLight, diffuseColor.a );',
      [
        'vec3 hPos = vec3(0.0,10.0,0.0);',
        'if(length(posSize1.xyz + localPosition) > 10.0){',
        'diffuseColor.a = 0.0;',
        '}',
        'gl_FragColor = vec4( outgoingLight, diffuseColor.a );',
      ].join( '\n' )
    );
    
    shader.uniforms.mainSize = {value: 10}
    shader.uniforms.posSize1 = {value: new THREE.Vector4(0,10,0,hSize)}
    
    
    holeMaterialShader = shader;
  }
}
function SetHoleMaterialCube(){
  holeMaterialCube = new THREE.MeshStandardMaterial(materialParams);
  holeMaterialCube.side = THREE.BackSide;
  holeMaterialCube.onBeforeCompile = function ( shader ) {
    
    shader.vertexShader = shader.vertexShader.replace(
      '#include <uv_pars_vertex>',
      [
        '#include <uv_pars_vertex>',
        'varying vec3 localPosition;'
      ].join( '\n' )
    ).replace(
      '#include <uv_vertex>',
      [
        'localPosition = vec3(position);',
        '#include <uv_vertex>',
      ].join( '\n' )
    );
    
    shader.fragmentShader = shader.fragmentShader.replace(
      '#include <uv_pars_fragment>',
      [
        '#include <uv_pars_fragment>',
        'varying vec3 localPosition;',
        'uniform vec4 posSize1;',
        'uniform float mainSize;',
      ].join( '\n' )
    ).replace(
      'gl_FragColor = vec4( outgoingLight, diffuseColor.a );',
      [
        'vec3 hPos = posSize1.xyz + localPosition;',
        'if(hPos.x > 10.0 || hPos.x < -10.0 || hPos.y > 10.0 || hPos.y < -10.0 || hPos.z > 10.0 || hPos.z < -10.0){',
        'diffuseColor.a = 0.0;',
        '}',
        'gl_FragColor = vec4( outgoingLight, diffuseColor.a );',
      ].join( '\n' )
    );
    
    shader.uniforms.mainSize = {value: 10}
    shader.uniforms.posSize1 = {value: new THREE.Vector4(0,10,0,hSize)}
    
    
    holeMaterialCubeShader = shader;
  }
}
function init(){
  
  renderer = new THREE.WebGLRenderer({alpha:true, antialias:true});
  renderer.setSize( window.innerWidth, window.innerHeight );
  renderer.autoClear = false;
  document.body.appendChild( renderer.domElement );
  
  scene = new THREE.Scene();
  sceneH = new THREE.Scene();
  camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  camera.position.set(40, 20, -40);
  cameraH = camera.clone();
  var ambientLight = new THREE.AmbientLight( 0x404040 );
  var light = new THREE.PointLight( 0xffffff, 0.3, 1000 );
  camera.add(light);
  cameraH.add(light.clone());
  // controls
  controls = new THREE.OrbitControls( camera );
  var controlsH = new THREE.OrbitControls( cameraH );
  
  var path = new THREE.Path();
  curve = new THREE.CatmullRomCurve3( [
    new THREE.Vector3( -10,0,-12 ),
    new THREE.Vector3( 5,-4,0),
    new THREE.Vector3( -10,0,10),
    new THREE.Vector3( -8,3,0),

  ] );
  curve.closed = true;
  var points = curve.getPoints( 150 );
  var geometry = new THREE.BufferGeometry().setFromPoints( points );

  var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );

  curveObject = new THREE.Line( geometry, material );

  //var mainSphereGeo = new THREE.SphereGeometry( 10, 100, 100 );
  var mainSphereGeo = new THREE.BoxGeometry( 20, 20, 20 );
  var holeSphereGeo = new THREE.SphereGeometry( hSize, 30, 30 );
  
  var HelperGeo = new THREE.SphereGeometry(hSize, 12, 12);
  var HelperWireframe = new THREE.WireframeGeometry(HelperGeo);
  HelperMesh = new THREE.LineSegments( HelperWireframe );
  HelperMesh.material.depthTest = false;
  HelperMesh.material.opacity = 0.5;
  HelperMesh.material.transparent = true;
  mainSphere = new THREE.Mesh(mainSphereGeo, mainMaterial);
  holeSphere = new THREE.Mesh(holeSphereGeo, holeMaterialCube);
  holeSphere.position.set(0,10,0);
  
  holeSphere.add(HelperMesh);
  
  scene.add(camera,mainSphere,curveObject);//, ambientLight
  sceneH.add(cameraH,holeSphere);//,ambientLight.clone()
  
  startTime = new Date().getTime();
}

function animate(){
  requestAnimationFrame( animate );
  
  var time = (new Date().getTime() - startTime)/8000;

  var alpha = -(Math.floor(time) - time);
  curve.getPoint( alpha, holeSphere.position );
  var posSize1 = new THREE.Vector4(holeSphere.position.x, holeSphere.position.y, holeSphere.position.z, hSize);
  if(mainMaterialShader){
    mainMaterialShader.uniforms.posSize1.value.copy(posSize1);
  }
  if(holeMaterialShader){
    holeMaterialShader.uniforms.posSize1.value.copy(posSize1);
  }
  if(holeMaterialCubeShader){
    holeMaterialCubeShader.uniforms.posSize1.value.copy(posSize1);
  }
  renderer.render(sceneH, cameraH);
  renderer.clearDepth();
  renderer.render( scene, camera );
}
            
          
!
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