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.149.0/build/three.module.js",
      "three/addons/": "https://unpkg.com/three@0.149.0/examples/jsm/"
    }
  }
</script>
              
            
!

CSS

              
                body {
  overflow: hidden;
  margin: 0;
}

              
            
!

JS

              
                // https://discourse.threejs.org/t/extrude-geometry-apply-texture-to-all-faces-of-the-mesh/48246

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


// general setup of environment

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

var camera = new THREE.PerspectiveCamera( 30, innerWidth/innerHeight );
    camera.position.set( 0, 20, 50 );
    camera.lookAt( scene.position );

var renderer = new THREE.WebGLRenderer( {antialias: true} );
    renderer.setSize( innerWidth, innerHeight );
    renderer.setAnimationLoop( animationLoop );
    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', 0.5 );
    light.position.set( 1, 1, 1 );
    scene.add( light );


// a cnavas to generate a texture image

var canvas = document.createElement( 'CANVAS' );
    canvas.width = 32;
    canvas.height = 32;


// procedurally defining of a black square

var context = canvas.getContext( '2d' );
    context.fillStyle = 'linen';
    context.fillRect( 0, 0, 32, 32 );
    context.strokeStyle = 'black';
    context.lineWidth = 1;
    context.strokeRect( 3, 3, 27, 27 );


// finally creating a canvas

var texture = new THREE.CanvasTexture( canvas );
		texture.wrapS = THREE.RepeatWrapping;
		texture.wrapT = THREE.RepeatWrapping;


// rectangular object

var obj1 = new THREE.Mesh(
			new THREE.BoxGeometry( 16, 15, 4 ),
			new THREE.MeshLambertMaterial( {color: 'tan', map: texture} )
		);
		obj1.position.z = -10;



// triangular object

var obj2 = new THREE.Mesh(
			new THREE.BoxGeometry( 8, 10, 6 ),
			new THREE.MeshLambertMaterial( {map: texture} )
		);
		obj2.position.x = 6;
		var pos = obj2.geometry.getAttribute( 'position' );
		for( var i=0; i<pos.count; i++ )
				if( pos.getY(i)>0.5 ) pos.setX(i,0);


// reversed triangular object

var obj3 = new THREE.Mesh(
			new THREE.BoxGeometry( 8, 10, 6 ),
			new THREE.MeshLambertMaterial( {map: texture} )
		);
		obj3.position.x = -6;
		var pos = obj3.geometry.getAttribute( 'position' );
		for( var i=0; i<pos.count; i++ )
				if( pos.getY(i)<0.5 ) pos.setX(i,0);


// dodecahedron object

var obj4 = new THREE.Mesh(
			new THREE.DodecahedronGeometry( 4 ),
			new THREE.MeshLambertMaterial( {color:'pink',map: texture} )
		);
		obj4.position.set( 0, -3, 8 );


scene.add( obj1, obj2, obj3, obj4 );



// a function that resets the UVs 
// trying to recreate a 3D texture

function resetUVs( object )
{
		var pos = object.geometry.getAttribute( 'position' ),
				nor = object.geometry.getAttribute( 'normal' ),
				uvs = object.geometry.getAttribute( 'uv' );
	
		for( var i=0; i<pos.count; i++ )
		{
				var x = 0,
						y = 0;
			
				var nx = Math.abs(nor.getX(i)),
						ny = Math.abs(nor.getY(i)),
						nz = Math.abs(nor.getZ(i));

				// if facing X
				if( nx>=ny && nx>=nz )
				{
						x = pos.getZ( i );
						y = pos.getY( i );
				}

				// if facing Y
				if( ny>=nx && ny>=nz )
				{
						x = pos.getX( i );
						y = pos.getZ( i );
				}

				// if facing Z
				if( nz>=nx && nz>=ny )
				{
						x = pos.getX( i );
						y = pos.getY( i );
				}

				uvs.setXY( i, x, y );
		}
}



// reset the UVs of our objects

resetUVs( obj1 );
resetUVs( obj2 );
resetUVs( obj3 );
resetUVs( obj4 )



// animation loop

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

              
            
!
999px

Console