Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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 async src="https://ga.jspm.io/npm:es-module-shims@1.5.1/dist/es-module-shims.js" crossorigin="anonymous"></script>
<script type="importmap">
  {
    "imports": {
      "three": "https://unpkg.com/three@0.156.0/build/three.module.js",
      "three/addons/": "https://unpkg.com/three@0.156.0/examples/jsm/"
    }
  }
</script>
              
            
!

CSS

              
                body {
  overflow: hidden;
  margin: 0;
}

              
            
!

JS

              
                // https://discourse.threejs.org/t/vertex-shader-sphere-projection-and-cpu-distance-calculation/56270

import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";


// general setup, boring, skip to the next comment

console.clear( );

var scene = new THREE.Scene();
    scene.background = new THREE.Color( 'azure' );

var camera = new THREE.PerspectiveCamera( 30, innerWidth/innerHeight );
    camera.position.set( -10, 5, 10 );
    camera.lookAt( scene.position );

var renderer = new THREE.WebGLRenderer( {antialias: true} );
    renderer.setSize( innerWidth, innerHeight );
    document.body.appendChild( renderer.domElement );
			
window.addEventListener( "resize", (event) => {
    camera.aspect = innerWidth/innerHeight;
    camera.updateProjectionMatrix( );
    renderer.setSize( innerWidth, innerHeight );
});

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

var ambientLight = new THREE.AmbientLight( 'white', 0.5 );
    scene.add( ambientLight );

var light = new THREE.DirectionalLight( 'white', 3 );
    light.position.set( 0, 0, 1 );
    scene.add( light );


// next comment


// some random ball

const R = 1; // ball radius

var ball = new THREE.Mesh(
			new THREE.IcosahedronGeometry( R, 8 ),
    	new THREE.MeshPhysicalMaterial( {
          color: 'azure',
          roughness: 0.5,
          metalness: 0,
          transmission: 0.5,
          ior: 2,
					thickness: 0.1,
					polygonOffset: true,
					polygonOffsetUnits: 2,
					polygonOffsetFactor: 2,
      } )
    );	
		ball.position.x = -2*Math.random();
		scene.add( ball );


// some plane plane

var plane = new THREE.Mesh(
			new THREE.PlaneGeometry( 6, 6 ),
    	new THREE.MeshNormalMaterial( {
					side: THREE.DoubleSide,
					transparent: true,
					opacity: 0.3,
      } )
    );	
		scene.add( plane );



// function to project a point onthe the ball
// (the parameter is modified)
function project( v )
{
		v.sub( ball.position )
			.setLength( R )
			.add( ball.position );
}



// generate a patch (plane projected onto the sphere)
// this is only for the purpose of visualization

var patch = new THREE.Mesh(
			new THREE.PlaneGeometry( 6, 6, 30, 30 ),
    	new THREE.MeshBasicMaterial( {
					color: 'plum',
					side: THREE.DoubleSide,
			} )
    ),
		v = new THREE.Vector3( );
		scene.add( patch );


var m = new THREE.Mesh(
			new THREE.IcosahedronGeometry( 0.05, 8 ),
    	new THREE.MeshBasicMaterial( {color: 'black'} )
    );	
		scene.add( m );


function randomPlane( )
{
		// random plane
		plane.position.set( 2*Math.random()-1, 6*Math.random()-3, 3 );
		plane.rotation.set( Math.random()-0.5, Math.random()-0.5, Math.random()-0.5);
		plane.updateMatrix( );

	
		// update patch	
		patch.geometry.dispose();
		patch.geometry = new THREE.PlaneGeometry( 6, 6, 30, 30 );
		patch.geometry.applyMatrix4( plane.matrix );

		var pos = patch.geometry.getAttribute( 'position' );

		for( var i=0; i<pos.count; i++ )
		{
				v.set( pos.getX(i), pos.getY(i), pos.getZ(i) );
				project( v );
				pos.setXYZ( i, v.x, v.y, v.z );
		}
}



// center of projection is projection of center
function randomizeScene1( )
{
		randomPlane( );

		// A is the corner of the plane
		var A = plane.position.clone();

		// A is projected on the ball
		project( A );

		// the black point is moved to A
		m.position.copy( A );
}


// center of projection is center of corners
function randomizeScene4( )
{
		randomPlane( );

		// A, B, C, D are the corner points of the plane

		var A = plane.localToWorld( new THREE.Vector3( 3, 3, 0 ) ),
				B = plane.localToWorld( new THREE.Vector3(-3, 3, 0 ) ),
				C = plane.localToWorld( new THREE.Vector3( 3,-3, 0 ) ),
				D = plane.localToWorld( new THREE.Vector3(-3,-3, 0 ) );

		// A, B, C, D are projected on the ball

		project( A );
		project( B );
		project( C );
		project( D );

		// average the 4 points into some central point M

		var M = new THREE.Vector3();
	
		M.add(A).add(B).add(C).add(D).multiplyScalar(0.25);
		project( M );

		// the black point is moved to A
		m.position.copy( M );
}


setInterval( randomizeScene4, 1000 );


function animationLoop( t )
{
    controls.update( );
		light.position.copy( camera.position );
    renderer.render( scene, camera );
}

renderer.setAnimationLoop( animationLoop );

              
            
!
999px

Console