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

              
                
              
            
!

CSS

              
                body {
	background-color: #000;
	margin: 0px;
	overflow: hidden;
}
              
            
!

JS

              
                function getScene() {
    var scene = new THREE.Scene();
    scene.background = new THREE.Color(0xffffff);
    return scene;
  }

  function getCamera() {
    var aspectRatio = window.innerWidth / window.innerHeight;
    var camera = new THREE.PerspectiveCamera(75, aspectRatio, 0.1, 1000);
    camera.position.set(0, 1, 10);
    return camera;
  }

  function getRenderer() {
    // Create the canvas with a renderer
    var renderer = new THREE.WebGLRenderer({antialias: true});
    // Add support for retina displays
    renderer.setPixelRatio(window.devicePixelRatio);
    // Specify the size of the canvas
    renderer.setSize(window.innerWidth, window.innerHeight);
    // Add the canvas to the DOM
    document.body.appendChild(renderer.domElement);
    return renderer;
  }

  var uniforms = THREE.UniformsUtils.merge([
    THREE.UniformsLib['ambient'],
    THREE.UniformsLib['lights'],
    THREE.UniformsUtils.clone(THREE.ShaderLib.phong.uniforms),
	{
		"diffuse": {type: 'c',value: new THREE.Color(0xFF00FF)},
		"texture": {
          type: 't',
          value: new THREE.TextureLoader().load( 'https://s3.amazonaws.com/duhaime/blog/tsne-webgl/assets/cat.jpg')
        },
    }
  ]);

  var vertex = [
    "#define PHONG",
    "varying vec3 vViewPosition;",
    "varying vec2 vUv;",
    "varying vec3 vNormal;",
    THREE.ShaderChunk["common"],
    THREE.ShaderChunk["uv_pars_vertex"],
    THREE.ShaderChunk["uv2_pars_vertex"],
    THREE.ShaderChunk["displacementmap_pars_vertex"],
    THREE.ShaderChunk["envmap_pars_vertex"],
    THREE.ShaderChunk["color_pars_vertex"],
    THREE.ShaderChunk["fog_pars_vertex"],
    THREE.ShaderChunk["morphtarget_pars_vertex"],
    THREE.ShaderChunk["skinning_pars_vertex"],
    THREE.ShaderChunk["shadowmap_pars_vertex"],
    THREE.ShaderChunk["logdepthbuf_pars_vertex"],
    THREE.ShaderChunk["clipping_planes_pars_vertex"],
    "void main() {",
    THREE.ShaderChunk["uv_vertex"],
    THREE.ShaderChunk["uv2_vertex"],
    THREE.ShaderChunk["color_vertex"],
    THREE.ShaderChunk["beginnormal_vertex"],
    THREE.ShaderChunk["morphnormal_vertex"],
    THREE.ShaderChunk["skinbase_vertex"],
    THREE.ShaderChunk["skinnormal_vertex"],
    THREE.ShaderChunk["defaultnormal_vertex"],
    "vNormal = normalize( transformedNormal);",
    THREE.ShaderChunk["begin_vertex"],
    THREE.ShaderChunk["displacementmap_vertex"],
    THREE.ShaderChunk["morphtarget_vertex"],
    THREE.ShaderChunk["skinning_vertex"],
    THREE.ShaderChunk["project_vertex"],
    THREE.ShaderChunk["logdepthbuf_vertex"],
    THREE.ShaderChunk["clipping_planes_vertex"],
    "vViewPosition = - mvPosition.xyz;",
    THREE.ShaderChunk["worldpos_vertex"],
    THREE.ShaderChunk["envmap_vertex"],
    THREE.ShaderChunk["shadowmap_vertex"],
    THREE.ShaderChunk["fog_vertex"],
    "vUv = uv;",
    "}"
  ].join("\n");

  var fragment = [
	"#define PHONG",
	"uniform vec3 diffuse;",
	'uniform sampler2D texture;',
    "uniform float opacity;",
    "uniform vec3 ambient;",
    "uniform vec3 emissive;",
    "uniform vec3 specular;",
    "uniform float shininess;",
    "varying vec2 vUv;",
    THREE.ShaderChunk["common"],
    THREE.ShaderChunk["packing"],
    THREE.ShaderChunk["color_pars_fragment"],
    THREE.ShaderChunk["uv_pars_fragment"],
    THREE.ShaderChunk["uv2_pars_fragment"],
    THREE.ShaderChunk["map_pars_fragment"],
    THREE.ShaderChunk["alphamap_pars_fragment"],
    THREE.ShaderChunk["aomap_pars_fragment"],
    THREE.ShaderChunk["lightmap_pars_fragment"],
    THREE.ShaderChunk["emissivemap_pars_fragment"],
    THREE.ShaderChunk["envmap_pars_fragment"],
    THREE.ShaderChunk["gradientmap_pars_fragment"],
    THREE.ShaderChunk["fog_pars_fragment"],
    THREE.ShaderChunk["bsdfs"],
    THREE.ShaderChunk["lights_pars"],
    THREE.ShaderChunk["lights_phong_pars_fragment"],
    THREE.ShaderChunk["shadowmap_pars_fragment"],
    THREE.ShaderChunk["bumpmap_pars_fragment"],
    THREE.ShaderChunk["normalmap_pars_fragment"],
    THREE.ShaderChunk["specularmap_pars_fragment"],
    THREE.ShaderChunk["logdepthbuf_pars_fragment"],
    THREE.ShaderChunk["clipping_planes_pars_fragment"],
	"void main() {",
	"vec4 diffuseColor = vec4( diffuse, opacity );",
    "ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
    "vec3 totalEmissiveRadiance = emissive;",
    THREE.ShaderChunk["logdepthbuf_fragment"],
    THREE.ShaderChunk["map_fragment"],
    THREE.ShaderChunk["color_fragment"],
    THREE.ShaderChunk["alphamap_fragment"],
    THREE.ShaderChunk["alphatest_fragment"],
    THREE.ShaderChunk["specularmap_fragment"],
    THREE.ShaderChunk["normal_flip"],
    THREE.ShaderChunk["normal_fragment"],
    THREE.ShaderChunk["emissivemap_fragment"],
    THREE.ShaderChunk["lights_phong_fragment"],
    THREE.ShaderChunk["lights_template"],
    THREE.ShaderChunk["aomap_fragment"],
    "vec3 outgoingLight = reflectedLight.directDiffuse +reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;",
    THREE.ShaderChunk["envmap_fragment"],
    "gl_FragColor = texture2D(texture, vUv);",
    THREE.ShaderChunk["premultiplied_alpha_fragment"],
    THREE.ShaderChunk["tonemapping_fragment"],
    THREE.ShaderChunk["encodings_fragment"],
    THREE.ShaderChunk["fog_fragment"],
    "}"
  ].join("\n");

  function loadImage() {

    var geometry = new THREE.BufferGeometry();

    /*
    Now we need to push some vertices into that geometry to identify the coordinates the geometry should cover
    */

    // Identify the image size
    var imageSize = {width: 10, height: 7.5};

    // Identify the x, y, z coords where the image should be placed
    var coords = {x: -5, y: -3.75, z: 0};

    // Add one vertex for each corner of the image, using the 
    // following order: lower left, lower right, upper right, upper left
    var vertices = new Float32Array([
      coords.x, coords.y, coords.z, // bottom left
      coords.x+imageSize.width, coords.y, coords.z, // bottom right
      coords.x+imageSize.width, coords.y+imageSize.height, coords.z, // upper right
      coords.x, coords.y+imageSize.height, coords.z, // upper left
    ])

    // set the uvs for this box; these identify the following corners:
    // lower-left, lower-right, upper-right, upper-left
    var uvs = new Float32Array([
      0.0, 0.0,
      1.0, 0.0,
      1.0, 1.0,
      0.0, 1.0,
    ])

    // indices = sequence of index positions in `vertices` to use as vertices
    // we make two triangles but only use 4 distinct vertices in the object
    // the second argument to THREE.BufferAttribute is the number of elements
    // in the first argument per vertex
    geometry.setIndex([0,1,2, 2,3,0])
    geometry.addAttribute('position', new THREE.BufferAttribute( vertices, 3 ));
    geometry.addAttribute('uv', new THREE.BufferAttribute( uvs, 2) )


    // specify custom uniforms and attributes for shaders
	// Uniform types: https://github.com/mrdoob/three.js/wiki/Uniforms-types
	
	

    var material = new THREE.ShaderMaterial({  
      uniforms: uniforms,
      vertexShader: vertex,
      fragmentShader: fragment
    });

    // Combine our image geometry and material into a mesh
    var mesh = new THREE.Mesh(geometry, material);

    // Set the position of the image mesh in the x,y,z dimensions
    mesh.position.set(0,0,0)

    // Add the image to the scene
    scene.add(mesh);
  }

  /**
  * Render!
  **/

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

  var scene = getScene();
  var camera = getCamera();
  var renderer = getRenderer();
  loadImage();

  render();
              
            
!
999px

Console