沃罗诺汤

2023-11-05 20:59
文章标签 沃罗诺

本文主要是介绍沃罗诺汤,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

沃罗诺汤

  • 示例
  • HTML
  • CSS
  • JS

更多有趣示例 尽在 知屋安砖社区

示例

在这里插入图片描述

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/88/three.min.js"></script>
<script id="vertexShader" type="x-shader/x-vertex">void main() {gl_Position = vec4( position, 1.0 );}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">uniform vec2 u_resolution;uniform vec2 u_mouse;uniform float u_time;uniform sampler2D u_noise;vec2 hash2(vec2 p){vec2 o = texture2D( u_noise, (p+0.5)/256.0, -100.0 ).xy;return o;}void grid(vec2 uv, inout vec3 colour, vec3 gridcolour, vec2 size, float linewidth) {vec2 grid = (fract(uv/size+.5)-.5)*size;grid = abs(grid);float gridlines = smoothstep(linewidth, linewidth + 0.005, min(grid.x, grid.y));colour = mix(gridcolour, colour, gridlines);}float voronoi(vec2 uv) {float dist = 4.;float   res = 0.;vec2 grid_id = floor(uv);vec2 grid_uv = fract(uv);for(float j = -1.; j < 2.; j++) {for(float i = -1.; i < 2.; i++) {vec2 offset = vec2(i, j);vec2 grid_test_id = grid_id + offset;vec2 rand = hash2(grid_test_id);vec2 point_pos = offset + rand - grid_uv;// The following adds some random animation to the particlesrand = hash2(grid_test_id + 1000.);rand = 0.5 + 0.4*sin((u_time) + 6.2831*rand);point_pos = offset + rand - grid_uv;// float len = length(point_pos); // the length gives us a more linear (conic) lengthfloat len = dot(point_pos, point_pos); // The float gives us a more rounded distance// res += exp( -10.0*len );if(len < dist) {dist = len;}}}// return -(1.0/10.0)*log( res );return dist;}float pattern(vec2 uv, float time, inout vec2 q, inout vec2 r) {q = vec2( voronoi( uv + vec2(0.0,0.0) ),voronoi( uv + vec2(5.2,1.3) ) );r = vec2( voronoi( uv + 2.0*q + vec2(1.7 - time / 2.,9.2) ),voronoi( uv + 2.0*q + vec2(8.3 - time / 2.,2.8) ) );return voronoi( uv + 1.5*r );}vec4 colour(float pattern, vec2 distortion1, vec2 distortion2) {// All I'm doing here is mixing up the colours using the distortion vectors and the derrived patternvec3 col = vec3(pattern + distortion1.x, pattern + distortion2.y / 2. + distortion1.x, pattern + distortion1.x * 2.);// Try muxing the colours here. Uncomment the following lines, 1-by-1 for some examples// col = vec3(col.b, col.r, col.g);// col = vec3(col.b * col.r) / col;// col *= vec3(dot(distortion1, distortion1) * .5);// col = col * (1.5 + sin(gl_FragCoord.x / u_resolution.x * 10.) * .5 + cos(gl_FragCoord.y / u_resolution.y * 10.) * .5);// col = 1. - col;// col *= sin(col * 2.);// Ramping up the contrast a bitcol = col;return vec4(col, 1.);}void main() {vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);uv *= 5.;vec2 q = vec2(0.);vec2 r = vec2(0.);float pattern = pattern(uv, u_time, q, r);vec4 col = colour(pattern, q, r);gl_FragColor = col;}
</script><div id="container" touch-action="none"></div>

CSS

body {margin: 0;padding: 0;
}#container {position: fixed;touch-action: none;
}

JS

/*
Most of the stuff in here is just bootstrapping. Essentially it's just
setting ThreeJS up so that it renders a flat surface upon which to draw 
the shader. The only thing to see here really is the uniforms sent to 
the shader. Apart from that all of the magic happens in the HTML view
under the fragment shader.
*/let container;
let camera, scene, renderer;
let uniforms;let loader=new THREE.TextureLoader();
let texture;
loader.setCrossOrigin("anonymous");
loader.load('https://s3-us-west-2.amazonaws.com/s.cdpn.io/982762/noise.png',function do_something_with_texture(tex) {texture = tex;texture.wrapS = THREE.RepeatWrapping;texture.wrapT = THREE.RepeatWrapping;texture.minFilter = THREE.LinearFilter;init();animate();}
);function init() {container = document.getElementById( 'container' );camera = new THREE.Camera();camera.position.z = 1;scene = new THREE.Scene();var geometry = new THREE.PlaneBufferGeometry( 2, 2 );uniforms = {u_time: { type: "f", value: 1.0 },u_resolution: { type: "v2", value: new THREE.Vector2() },u_noise: { type: "t", value: texture },u_mouse: { type: "v2", value: new THREE.Vector2() }};var material = new THREE.ShaderMaterial( {uniforms: uniforms,vertexShader: document.getElementById( 'vertexShader' ).textContent,fragmentShader: document.getElementById( 'fragmentShader' ).textContent} );material.extensions.derivatives = true;var mesh = new THREE.Mesh( geometry, material );scene.add( mesh );renderer = new THREE.WebGLRenderer();renderer.setPixelRatio( 1 );container.appendChild( renderer.domElement );onWindowResize();window.addEventListener( 'resize', onWindowResize, false );document.addEventListener('pointermove', (e)=> {let ratio = window.innerHeight / window.innerWidth;uniforms.u_mouse.value.x = (e.pageX - window.innerWidth / 2) / window.innerWidth / ratio;uniforms.u_mouse.value.y = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1;e.preventDefault();});
}function onWindowResize( event ) {renderer.setSize( window.innerWidth, window.innerHeight );uniforms.u_resolution.value.x = renderer.domElement.width;uniforms.u_resolution.value.y = renderer.domElement.height;
}function animate() {requestAnimationFrame( animate );render();
}function render() {uniforms.u_time.value += 0.01;renderer.render( scene, camera );
}
更多有趣示例 尽在 小红砖社区https://xhz.bos.xyz

这篇关于沃罗诺汤的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/352476

相关文章

沃罗诺伊图(Voronoi Diagram,也称作Dirichlet tessellation,狄利克雷镶嵌)

沃罗诺伊图(Voronoi Diagram,也称作Dirichlet tessellation,狄利克雷镶嵌)是由俄国数学家格奥尔吉·沃罗诺伊建立的空间分割算法。灵感来源于笛卡尔用凸域分割空间的思想。在几何,晶体学建筑学,地理学,气象学,信息系统等许多领域有广泛的应用。 泰森多边形法,荷兰气候学家A·H·Thiessen提出了一种根据离散分布的气象站的降雨量,来计算平均降雨量的方法,即将所有相邻

沃罗诺伊图——Voronoi Partition 可汗学院

看到一个非常棒的解释 Voronoi Diagram 的视频,给大家搬运过来! 先说说两个圆的情况: 不停放大半径,直至相交,出现边。 再说说多个圆的情况: 放大相交得: 然后一起了解一下术语: 再说说性质: 1. 每一条边到最近的两个site的距离相等: 2. 每条边相交的点到最近的三个site距离相等:

【计算机视觉基础】最大核聚类,采用Canny等边缘滤波方法对图像边缘提取,提取图像的Harris角点特征并进行沃罗诺伊与德劳内网格划分,显示沃罗诺伊划分的最大核聚类,详细的最大核聚类计算算法

问题描述如下: 分别采用Sobel、Prewitt、Canny边缘滤波方法对一张彩色进行边缘特征提取,并分别显示对应的边缘图像,获取要求1中彩色图像上的Harris角点特征,基于生成的角点特征对要求1生成的Canny边缘图像进行德劳内网格划分与沃罗诺伊网格划分,要求两种划分分别单独显示在边缘检测结果图像上;编写代码实现要求2中沃罗诺伊网格划分的最大核聚类:挑选并显示该沃罗诺伊网格划分的最大核取

沃罗诺伊图(Voronoi):迷人的世界【1/2】

一、说明 Voronoi图(也称为狄利克雷镶嵌或泰森多边形)在自然界中无处不在。你已经遇到过他们数千次了,但也许没有这样称呼它。Voronoi图很简单,但它们具有令人难以置信的特性,在制图,生物学,计算机科学,统计学,考古学,一直到建筑和艺术等领域都有应用。 二、什么是沃罗诺伊图?         假设您有 n 个点分散在一个平面上,这些点的 Voron