cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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, body, canvas
    margin 0
    padding 0
    width 100vw
    height 100vh
    overflow hidden
    background #1B2B34
    background radial-gradient(ellipse at center, #001B36 0%, #000 100%)
    position absolute
    top 0
    left 0

div
    z-index 100
    
div:last-child
    top 48px !important
        
canvas
    z-index 10
            
          
!
            
              const n = 64
    , m = 64

prepare(data => {

    let renderer
        , controls
        , scene
        , camera
        , w, h
        , statsFps
        , statsMs
    
    const color = d3.scaleSequential(d3.interpolateRdBu).domain([-1, 1])
        , thresholds = d3.range(-1.2, 1, 0.2)
        , contours = d3.contours().size([n, m])
        , tempCanvas = document.createElement('canvas')
        , material = new THREE.MeshPhongMaterial( {/* color: 0x00ff00 ,*/ side: 2} )
        , ctx = tempCanvas.getContext('2d')
        , path = d3.geoPath(null, ctx)

    resize()
    init()
    animate()

    function rand(min, max) {
        return Math.random() * (max - min) + min
    }

    function resize() {
        w = document.body.clientWidth
        h = document.body.clientHeight
        if (!camera) {
            return
        }

        camera.aspect = w / h
        camera.updateProjectionMatrix()
        renderer.setSize(w, h)
    }

    function init() {

        scene = new THREE.Scene()

        camera = new THREE.PerspectiveCamera(75, w / h, 1, 6000)
        camera.position.z = 120

        renderer = new THREE.WebGLRenderer({alpha: true, antialias: true})
        renderer.setSize(w, h)
        renderer.setClearColor("#0C7CA5", 0)
        renderer.setPixelRatio(window.devicePixelRatio)
        renderer.sortObjects = false
        renderer.autoClear = false
        renderer.generateMipmaps = false

        let light = new THREE.HemisphereLight('#fff', '#eee', 1)
        light.position.set(0, 0, 300)
        light.castShadow = true
        scene.add(light)

        light = new THREE.PointLight(0x222222, 3)
        light.position.x = 1000
        light.position.y = 500
        light.position.z = 1000
        light.castShadow = true
        scene.add(light)    

        camera.lookAt(scene.position)

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

        document.body.appendChild(renderer.domElement)

        statsFps = new Stats()
        document.body.appendChild(statsFps.dom)

        statsMs = new Stats()
        statsMs.showPanel(1)
        document.body.appendChild(statsMs.dom)

        window.addEventListener('resize', resize, false)
        create(scene)
    }

    function create(scene) {
        const geometry = new THREE.SphereBufferGeometry(30, 128, 128)
              //new THREE.PlaneBufferGeometry(87, 61, 128, 128)
        
        const tex = generateTexture(64, 64, data, 0)
        // const texMap = generateTexture(512, 512, data, 0, true)
        const texMap = tex
        
        material.map = texMap
        material.displacementMap = tex
        // material.normalMap = texMap

        // material.normalScale = new THREE.Vector2( -1, 1 )

        material.displacementScale = 20;
        material.displacementBias = -.5;

        const cylinder = new THREE.Mesh( geometry, material )
        
        cylinder.rotation.x = 1;
        cylinder.rotation.z = 0.5;
        cylinder.rotation.y = 0.2;
        
        scene.add(cylinder);
    }

    function animate() {

        requestAnimationFrame(animate)

        update()

        renderer.clear()
        renderer.render(scene, camera)

        controls.update()
        statsFps.update()
        statsMs.update()
        
    }

    let lastDate = 0
    // Date.now();
    function update() {
        
        // const inc = Date.now() - lastDate
        
        // const dv = (inc % 1000) / 1000 * 3
        
        // lastDate = Date.now()
        
        const tex = generateTexture(64, 64, data, lastDate)
        lastDate += .1
        // const texMap = generateTexture(512, 512, data, dv, true)
        const texMap = tex
        
        material.map = texMap
        // material.displacementMap = tex
        // material.normalMap = texMap

    }
    
    function fill(geometry) {
        ctx.beginPath();
        path(geometry);
        ctx.fillStyle = color(geometry.value);
        ctx.fill();
    }

    var ttt;
    function generateTexture(w, h, data, delta, map) {
        
        tempCanvas.width = n
        tempCanvas.height = m

        // contours
        //     .thresholds(thresholds.map(v => v + i))
        //     (data)
        //     .forEach(fill);
        
        let image = ctx.createImageData(n, m)

        for (let j = 0, k = 0, l = 0; j < m; ++j) {
            for (let i = 0; i < n; ++i, ++k, l += 4) {
                let c = d3.rgb(/* map ? ocolor(data[k]) :  */color(data[k] + Math.cos(delta)))
                image.data[l + 0] = c.r
                image.data[l + 1] = c.g
                image.data[l + 2] = c.b
                image.data[l + 3] = 255
            }
        }

        ctx.putImageData(image, 0, 0)
        
        
        const ttemp = document.createElement('canvas')
        const tctx = ttemp.getContext('2d');
        
        ttemp.width = w;
        ttemp.height = h;
        
        tctx.drawImage(tempCanvas, 0, 0, w, h);

        // const t = new Image()
        // t.src = ttemp.toDataURL()

        ttt = ttt || new THREE.Texture(ttemp)
        ttt.image = ttemp
        ttt.needsUpdate = true
        return ttt
    }
    
})

function prepare(cb) {
    const values = new Array(n * m)
    for (var j = 0.5, k = 0; j < m; ++j) {
      for (var i = 0.5; i < n; ++i, ++k) {
        values[k] = value(i / n * 19.2 - 9.6, 5 - j / m * 10)
      }
    }
    cb(values)
}

function value(x, y) {
  return Math.sin(x + y) * Math.sin(x - y);
}

            
          
!
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.
Loading ..................

Console