WebGL - Textured terrain with heightmap

前端 未结 5 1598
滥情空心
滥情空心 2020-12-31 08:23

I\'m trying to create a 3D terrain using WebGL. I have a jpg with the texture for the terrain, and another jpg with the height values (-1 to 1).

I\'ve looked at vari

5条回答
  •  孤城傲影
    2020-12-31 08:40

    Check out this post over on GitHub:

    https://github.com/mrdoob/three.js/issues/1003

    The example linked there by florianf helped me to be able to do this.

    function getHeightData(img) {
        var canvas = document.createElement( 'canvas' );
        canvas.width = 128;
        canvas.height = 128;
        var context = canvas.getContext( '2d' );
    
        var size = 128 * 128, data = new Float32Array( size );
    
        context.drawImage(img,0,0);
    
        for ( var i = 0; i < size; i ++ ) {
            data[i] = 0
        }
    
        var imgd = context.getImageData(0, 0, 128, 128);
        var pix = imgd.data;
    
        var j=0;
        for (var i = 0, n = pix.length; i < n; i += (4)) {
            var all = pix[i]+pix[i+1]+pix[i+2];
            data[j++] = all/30;
        }
    
        return data;
    }
    

    Demo: http://oos.moxiecode.com/js_webgl/terrain/index.html

提交回复
热议问题