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.

            
              <div id='world'></div>

<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://threejs.org/examples/js/loaders/DDSLoader.js"></script>

<script id="vertexShader" type="x-shader/x-vertex">
    precision highp float;
    precision highp int;

    uniform mat4 modelMatrix;
    uniform mat4 modelViewMatrix;
    uniform mat4 projectionMatrix;
    uniform mat4 viewMatrix;
    uniform mat3 normalMatrix;
    uniform vec3 cameraPosition;
    attribute vec3 position;
    attribute vec3 normal;
    attribute vec2 uv;

    // varying vec2 vUv;
    varying vec3 vNormal;
    varying vec3 vViewPosition;

    void main() {

        ////////////
        // uv_vertex
        // vUv = uv;

        /////////////////////
        // beginnormal_vertex
        vec3 objectNormal = vec3( normal );

        ///////////////////////
        // defaultnormal_vertex
        vNormal = normalMatrix * objectNormal;

        ///////////////
        // begin_vertex
        vec3 transformed = vec3( position );
        vec4 mvPosition = modelViewMatrix * vec4( transformed, 1.0 );

        vViewPosition = - mvPosition.xyz;

        /////////////////
        // project_vertex
        gl_Position = projectionMatrix * mvPosition;

    }
</script>

<script id="fragmentShader" type="x-shader/x-vertex">
		#extension GL_EXT_shader_texture_lod : enable
    precision highp float;
    precision highp int;

    uniform mat4 viewMatrix;

    uniform samplerCube envMap;
    uniform float lod;

    // varying vec2 vUv;
    varying vec3 vNormal;
    varying vec3 vViewPosition;

    #define NORMAL

    // http://en.wikibooks.org/wiki/GLSL_Programming/Applying_Matrix_Transformations
    vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {
      return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );
    }

    void main() {

        vec3 reflectedDirection = normalize( reflect( vViewPosition, normalize( vNormal ) ) );
        // reflectedDirection.y = -reflectedDirection.y;

        reflectedDirection = inverseTransformDirection( -reflectedDirection, viewMatrix );

        vec4 color = textureCubeLodEXT( envMap, reflectedDirection, lod );

        gl_FragColor = vec4( color );

    }
</script>


            
          
!
            
              #world {
  background: linear-gradient(#652e37, #652e37);
  position:absolute;
  width:100%;
  height:100%;
  overflow:hidden;
}
            
          
!
            
              var container;
var camera, controls, scene, renderer;
var material = [];

init();
animate();

function init() {

    // renderer

    renderer = new THREE.WebGLRenderer({
      antialias: true,
      alpha:  false,
      precision:  "highp"
    });
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio( window.devicePixelRatio );
    renderer.autoClear = true;
    renderer.setClearColor(0x777777, 1);
  
    container = document.getElementById('world');
    container.appendChild(renderer.domElement);
  
    camera = new THREE.PerspectiveCamera(10, window.innerWidth / window.innerHeight, 1, 10000);
    camera.position.z = 1000;

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

    controls.rotateSpeed = 1.0;
    controls.zoomSpeed = 1.2;
    controls.panSpeed = 0.8;
    controls.minDistance = 100;
    controls.maxDistance = 4000;

    controls.enableDamping = true;
    controls.dampingFactor = 0.2;

    controls.keys = [65, 83, 68]; 

    // world

    scene = new THREE.Scene();

    var loader = new THREE.DDSLoader();
    var src = 'https://threejs.org/examples/textures/compressed/Mountains_argb_mip.dds'
    loader.load( src, function( texture ) {

      //texture.minFilter = THREE.LinearMipMapLinearFilter;
      //texture.magFilter = THREE.NearestFilter;
      var geo = new THREE.SphereGeometry( 30, 48, 48 );

      for ( var i = 0; i < 8; i++ ) {
        createSphere( i, ( -280 + ( i * 80 ) ) );
      }
      
      function createSphere( index, pos ) {

        var uniforms = {
          envMap: { type: 't', value: null },
          lod: { type: '1f', value: 0.0 }
        };
        
        uniforms.envMap.value = texture;
        uniforms.lod.value = index;
        
        material[ index ] = new THREE.RawShaderMaterial({
          uniforms: uniforms,
          vertexShader: document.getElementById( 'vertexShader' ).textContent,
          fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
          lights: false
        });
        
        material[ index ].extensions.shaderTextureLOD = true;

        var Mesh = new THREE.Mesh( geo, material[ index ] );
        Mesh.position.x = pos;
        scene.add(Mesh);

        material[ index ].needsUpdate = true;
        
      };

    });

    window.addEventListener('resize', onWindowResize, false);

}

function onWindowResize() {

    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();

    renderer.setSize(window.innerWidth, window.innerHeight);

}

function animate() {

    render();
    controls.update();
    requestAnimationFrame(animate);

}

function render() {

    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