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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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
            
          
!
            
              if (!Detector.webgl)
    Detector.addGetWebGLMessage()

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

Console