鱼眼镜头
程序员文章站
2022-04-17 22:08:43
...
更多有趣示例 尽在小红砖社区
示例
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;
}
float sigmoid(float x) {
return x / ( 1. + abs(x));
}
vec2 sigmoidDistort(in vec2 uv, inout vec2 polar, inout float distortion) {
polar = vec2( atan(uv.y, uv.x), length(uv) );
float a = sin(polar.y * .8);
distortion = sigmoid(a * -.2);
polar.x += sigmoid(a * .2) * 1.;
polar.y += sigmoid(a * -.5) * 3.;
return vec2( cos(polar.x) * polar.y, sin(polar.x) * polar.y );
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
vec2 subuv;
uv *= 10.;
vec2 polar = vec2(0.);
float distortion = 0.;
uv = sigmoidDistort(uv, polar, distortion);
float t = u_time * .1;
uv *= mat2(
sin(t), -cos(t),
cos(t), sin(t)
);
uv.x += u_time;
uv += .5;
vec2 grid = floor(uv);
vec2 subgrid = fract(uv) - .5;
float c = mod(grid.x + grid.y, 2.);
// c = smoothstep(.44, .4, length(subgrid));
vec3 colour = vec3(c);
uv += distortion * .3;
grid = floor(uv);
subgrid = fract(uv) - .5;
colour.g = mod(grid.x + grid.y, 2.);
// colour.r = smoothstep(.44, .4, length(subgrid));
uv += distortion * .3;
grid = floor(uv);
subgrid = fract(uv) - .5;
colour.b = mod(grid.x + grid.y, 2.);
// colour.b = smoothstep(.44, .4, length(subgrid));
gl_FragColor = vec4(colour,1.0);
}
</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( window.devicePixelRatio );
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 / 1) / 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 );
}