OpenGL - 水波(带噪音)

时间:2015-05-22 12:56:14

标签: c++ opengl vertex vertex-shader

我目前正在制作水波,所以基本上我从头开始。我创建了一个网格,它基本上是一个扁平的正方形,并在顶点着色器中设置了动画(下面是实现它的代码)

vtx.y = (sin(2.0 * vtx.x + a_time/1000.0 ) * cos(1.5 * vtx.y + a_time/1000.0) * 0.2);

基本上只是根据sin和cos函数移动y位置,可以观察到parse_known_args()的结果!

然后我尝试添加一些Perlin噪音(根据Ian McEwan的Perlin噪音函数,可在此处获取github.com/ashima/webgl-noise),如下所示

vtx.y = vtx.y + 0.1*cnoise((a_time/5000.0)*a_vertex.yz);

可以观察到here的结果!

正如您可以清楚地看到,我正在寻找没有真正的“随机”效应(模拟海洋的一些基本随机粗糙度)。

我想知道如何才能实现这一目标(关于如何改进任何改变y的功能的任何建议也将受到赞赏)。

1 个答案:

答案 0 :(得分:5)

最简单的解决方案是使用包含所需噪音的纹理。如果位移保留在纹理中,则可以在顶点着色器中应用位移,因此不需要修改顶点缓冲区。要使波浪移动,您可以添加一些动画偏移。 正如你所说,有很多方法可以假装"随机"影响。您可以从纹理中获取两个样本,使用不同更改的偏移,然后只需添加两个位移。

例如,请参阅以下顶点着色器:

uniform sampler2D u_heightMap;
uniform float u_time;
uniform mat4 modelViewMatrix 
uniform mat4 projectionMatrix 
attribute vec3 position;

void main()
{
    vec3 pos = position;
    vec2 offset1 = vec2(0.8, 0.4) * u_time * 0.1;
    vec2 offset2 = vec2(0.6, 1.1) * u_time * 0.1;
    float hight1 = texture2D(u_heightMap, uv + offset1).r * 0.02;
    float hight2 = texture2D(u_heightMap, uv + offset2).r * 0.02;
    pos.z += hight1 + hight2; 
    vec4 mvPosition = modelViewMatrix * vec4( pos, 1.0 );
    gl_Position = projectionMatrix * mvPosition;
} 

我使用threejs做了一个简单的例子:



var container;
   var camera, scene, renderer;
   var mesh;
   var uniforms;

   var clock = new THREE.Clock();

   init();
   animate();

   function init() {
     container = document.getElementById('container');

     camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.1, 100);
     camera.position.z = 0.6;
     camera.position.y = 0.2;
     camera.rotation.x = -0.45;

     scene = new THREE.Scene();

     var boxGeometry = new THREE.PlaneGeometry(0.75, 0.75, 100, 100);
     
     var heightMap = THREE.ImageUtils.loadTexture("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAA81BMVEVZWVlRUVFdXV1EREQ8PDxBQUFWVlY3NzdKSkoyMjJqamphYWFOTk5mZmZHR0dxcXEtLS0qKip0dHRubm4aGhpTU1M+Pj4vLy8nJyeCgoJ3d3chISE0NDSKiop6eno5OTkBAQEjIyMeHh5/f38SEhKOjo6FhYUWFhaRkZELCwsPDw+bm5t8fHyfn59jY2OVlZUICAiwsLCHh4elpaWioqL///+qqqqYmJitra20tLS3t7fIyMi5ubnR0dHh4eHDw8P8/PzKysq9vb2np6fw8PDd3d3Ozs67u7vFxcX09PTY2Ni/v7/p6enV1dXT09Pl5eXt7e2kvPjWAAAMM0lEQVRYwxVW1ZbjOBAtsSwzsx1jOOlAc/f0MO7M7v9/zWr0kNg+xyVX6RIQXOhlUyIJNQwOLc/vL1ZiHuzATLhtIppQ1DzA0vd7S9nz9oBiExE/JXFo7zACFOzscM5l6yDfk/bs2vmTKhc3i8U6xIbfIGSlkUgXC5FGnft2N0kvrSQRWV+qu0sBYLu7g3t9VMicmjAPTJ74sjbWm5ubcb1YLKq84JH+X9MQW2l1UNl4s8wQmCJbRik0ISCLGKL4+OHty5dfs6qWSxNxbErO07L0Or8KV0G6Xqw3ozzvsFe1ka4cwdDEwr/pKMGgcJxg+/L6/iolQknps3A4YAwmlY4jTZsaVNJoHMvEXU2IGMnYlWy3u+YAhjDDAMwmbIrTdapdSc0d8bO1P20ftnf7cw2x4RNa9pgZWVa26PxpBZaJwh0eF34xTG5gOxJY2Ewr7ASgJ+uB2d14op22p6Obh0XBx3W/ZIbXZ2VmUFoZguV3B7JZjFFassASkkCJcte0XFl56/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2B+KAi3TFJVaD7PJlYPqutSwQxAcZryYfq+B+KN6GVozttd0+htmj9vCgh1uCPIbudCPQ2ftkbUd8Gx0OyfJ2XvsHRAYhsxmI4h0G45trtaw2CyqaCwP+0swqg8X0+fv15Q/e+7LzPnfsaGFdEmzbTLhZyCidydaWtqYG7YivkSm5yM6z6zTDwo5CRseL19Bs8zHx+vu0AVu3prAltvRPEtsHcNWFlcNIqbYMeQa1KmiRnwFoamkdFGYEYAn59A9yeYYlXkcQhWduBaGi/MtoMQGMP1dXBNAGkqXrUeWjWA5rsBmEcTy4pN4fDr/qx44iciIweQQTCbgAgjDA7ofwxocDxlnyf5AAAAAElFTkSuQmCC");
     
     heightMap.wrapT = heightMap.wrapS = THREE.RepeatWrapping;

     uniforms = {u_time: {type: "f", value: 0.0 }, u_heightMap: {type: "t",value:heightMap} };

     var material = new THREE.ShaderMaterial({
       uniforms: uniforms,
       side: THREE.DoubleSide, 
       wireframe: true,
       vertexShader: document.getElementById('vertexShader').textContent,
       fragmentShader: document.getElementById('fragment_shader').textContent
     });

     mesh = new THREE.Mesh(boxGeometry, material);
     mesh.rotation.x = 3.14 / 2.0;
     scene.add(mesh);

     renderer = new THREE.WebGLRenderer();
     renderer.setClearColor( 0xffffff, 1 );
     container.appendChild(renderer.domElement);

     onWindowResize();

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

   }

   function onWindowResize(event) {
     camera.aspect = window.innerWidth / window.innerHeight;
     camera.updateProjectionMatrix();
     renderer.setSize(window.innerWidth, window.innerHeight);
   }

   function animate() {
     requestAnimationFrame(animate);
     render();
   }

   function render() {
     var delta = clock.getDelta();
     uniforms.u_time.value += delta;
     mesh.rotation.z += delta * 0.5;
     renderer.render(scene, camera);
   }

body { margin: 0px; overflow: hidden; }

<script src="http://threejs.org/build/three.min.js"></script>
<div id="container"></div>

<script id="fragment_shader" type="x-shader/x-fragment">
    void main( void ) 
    {
        gl_FragColor = vec4(vec3(0.0), 1.0);    
    }
</script>

<script id="vertexShader" type="x-shader/x-vertex">
	uniform lowp sampler2D u_heightMap;
	uniform float u_time;
                
    void main()
    {
        vec3 pos = position;
        vec2 offset1 = vec2(1.0, 0.5) * u_time * 0.1;
        vec2 offset2 = vec2(0.5, 1.0) * u_time * 0.1;
        float hight1 = texture2D(u_heightMap, uv + offset1).r * 0.02;
        float hight2 = texture2D(u_heightMap, uv + offset2).r * 0.02;
        pos.z += hight1 + hight2; 
		vec4 mvPosition = modelViewMatrix * vec4( pos, 1.0 );
		gl_Position = projectionMatrix * mvPosition;
    }
</script>
&#13;
&#13;
&#13;

使用更好的位移纹理,或者甚至使用两种不同的纹理来实现两个偏移,可以获得更好的效果。