Ледяная звезда
На three.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-shadaer/x-fragment">
uniform vec2 u_resolution;
uniform vec4 u_mouse;
uniform float u_time;
uniform sampler2D u_noise;
uniform sampler2D u_buffer;
uniform bool u_renderpass;
uniform int u_frame;
// vec2 Diffusion = vec2(0.082, 0.041);
// float k = 0.055;
// float f = 0.023;
// float timeStep = 1.3;
// vec2 Diffusion = vec2(0.0805, 0.04131);
// float k = 0.065;
// float f = 0.038;
// float timeStep = .9;
vec2 Diffusion = vec2(0.0805, 0.02031);
// vec2 Diffusion = vec2(.12, .06);
float k = 0.075;
float f = 0.080;
float timeStep = 0.4;
#define PI 3.141592653589793
#define TAU 6.283185307179586
float starSDF(vec2 st, int V, float s) {
// st = st*4.-2.;
float a = atan(st.y, st.x)/TAU;
float seg = a * float(V);
a = ((floor(seg) + 0.5)/float(V) +
mix(s,-s,step(.5,fract(seg))))
* TAU;
return abs(dot(vec2(cos(a),sin(a)),
st));
}
vec2 hash2(vec2 p)
{
vec2 o = texture2D( u_noise, (p+0.5)/256.0, -100.0 ).xy;
return o;
}
vec3 hsb2rgb( in vec3 c ){
vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),
6.0)-3.0)-1.0,
0.0,
1.0 );
rgb = rgb*rgb*(3.0-2.0*rgb);
return c.z * mix( vec3(1.0), rgb, c.y);
}
vec3 domain(vec2 z){
return vec3(hsb2rgb(vec3(atan(z.y,z.x)/TAU,1.,1.)));
}
vec3 colour(vec2 z) {
return domain(z);
}
float rand(vec2 co){
// implementation found at: lumina.sourceforge.net/Tutorials/Noise.html
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
// Five point stencil Laplacian
vec4 laplacian5(vec2 position, vec3 offset) {
return
+ texture2D( u_buffer, position - offset.zy)
+ texture2D( u_buffer, position - offset.xz)
- 4.0 * texture2D( u_buffer, position )
+ texture2D( u_buffer, position + offset.xz )
+ texture2D( u_buffer, position + offset.zy );
}
// nine point stencil
vec4 laplacian9(vec2 position, vec4 offset) {
return
0.5* texture2D( u_buffer, position - offset.xy ) // first row
+ texture2D( u_buffer, position - offset.zy )
+ 0.5* texture2D( u_buffer, position - offset.wy )
+ texture2D( u_buffer, position - offset.xz) // seond row
- 6.0* texture2D( u_buffer, position )
+ texture2D( u_buffer, position + offset.xz )
+ 0.5*texture2D( u_buffer, position +offset.wy) // third row
+ texture2D( u_buffer, position +offset.zy )
+ 0.5*texture2D( u_buffer, position + offset.xy );
}
void main() {
vec2 uv = (gl_FragCoord.xy - .5 * u_resolution) / min(u_resolution.y, u_resolution.x);
vec2 sample = gl_FragCoord.xy / u_resolution;
float rot = (-18.) * PI / 180.;
float field = starSDF(uv * mat2(cos(rot), -sin(rot), sin(rot), cos(rot)), 5, .09);
float star = smoothstep(.225, .04, field);
star += smoothstep(.43, .45, length(uv)) * .8;
star = clamp(star, 0., 1.);
// if(star > .99) {
// Diffusion = vec2(0.0805, 0.06131);
// k = 0.045;
// f = 0.012;
Diffusion = mix(Diffusion, vec2(0.085, 0.04531), star);
// vec2 Diffusion = vec2(.12, .06);
k = mix(k, 0.064, star);
f = mix(f, 0.086, star);
timeStep = mix(timeStep, 1.7, star);
float outerDiff = clamp(length(uv * 1.1) - .4, 0., 1.);
Diffusion = mix(Diffusion, vec2(0.105, 0.05531), outerDiff);
k = mix(k, 0.104, outerDiff);
f = mix(f, 0.070, outerDiff);
// }
vec3 offset = vec3(1. / u_resolution, 0.0);
vec2 mouse = u_mouse.xy - uv;
float shade = smoothstep(.1, .015, length(mouse));
vec4 fragcolour = vec4(shade);
// vec3 fragcolour = colour(uv);
vec4 v = texture2D(u_buffer, sample);
if(u_renderpass == true) {
if(u_frame > 3) {
// This zooms the sample out on a continual basis
// sample -= .5;
// sample *= .9997;
// sample += .5;
// v = texture2D(u_buffer, sample);
// time step for Gray-Scott system:
// vec2 lv = laplacian5(sample, offset).xy; // laplacian
vec2 lv = laplacian9(sample, vec4(offset, -offset.x)).xy; // laplacian
float xyy = v.x*v.y*v.y; // utility term
k -= lv.x * .09;
vec2 dV = vec2( Diffusion.x * lv.x - xyy + f*(1.-v.x), Diffusion.y * lv.y + xyy - (f+k)*v.y);
v.xy += timeStep*dV;
} else {
// v = vec4(texture2D(u_noise, uv * .05).xy, 0., 0.);
// v *= v*v*v*v*5.;
v = vec4(smoothstep(.05, .0, field)*10.);
}
gl_FragColor = vec4(v);
if(u_mouse.z == 1.) {
gl_FragColor.x -= shade * .919;
}
} else {
if(u_frame > 20) {
vec4 v = texture2D(u_buffer, sample);
float c = smoothstep(.5, .35, v.x);
c += (1. - v.x)*1.5;
c *= .5;
// c -= v.y;
gl_FragColor = mix(vec4(0.1,.2,.4, 1.), vec4(0.,.05,.05,1.), clamp(length(uv), 0., 1.));
gl_FragColor += vec4(c);
// gl_FragColor = vec4(smoothstep(.2, .05, v.x));
// gl_FragColor -= v.y*v.y*v.y*v.y*10.;
}
}
}
</script>
<div id="container" touch-action="none"></div>
SCSS
body {
margin: 0;
padding: 0;
}
#container {
position: fixed;
touch-action: none;
}
JS
https://s3-us-west-2.amazonaws.com/s.cdpn.io/982762/ccapture.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 divisor = 1 / 8;
let textureFraction = 1 / 1;
let w = 2048;
let h = 1024;
let newmouse = {
x: 0,
y: 0
};
let loader=new THREE.TextureLoader();
let texture, rtTexture, rtTexture2;
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 );
rtTexture = new THREE.WebGLRenderTarget(window.innerWidth * textureFraction, window.innerHeight * textureFraction);
rtTexture2 = new THREE.WebGLRenderTarget(window.innerWidth * textureFraction, window.innerHeight * textureFraction);
uniforms = {
u_time: { type: "f", value: 1.0 },
u_resolution: { type: "v2", value: new THREE.Vector2() },
u_noise: { type: "t", value: texture },
u_buffer: { type: "t", value: rtTexture.texture },
u_mouse: { type: "v3", value: new THREE.Vector3() },
u_frame: { type: "i", value: -1. },
u_renderpass: { type: 'b', value: false }
};
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;
if(window.innerHeight > window.innerWidth) {
newmouse.x = (e.pageX - window.innerWidth / 2) / window.innerWidth;
newmouse.y = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1 * ratio;
} else {
newmouse.x = (e.pageX - window.innerWidth / 2) / window.innerWidth / ratio;
newmouse.y = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1;
}
e.preventDefault();
});
document.addEventListener('pointerdown', ()=> {
uniforms.u_mouse.value.z = 1;
console.log();
});
document.addEventListener('pointerup', ()=> {
uniforms.u_mouse.value.z = 0;
});
}
function onWindowResize( event ) {
w = 2048;
h = 1024;
w = window.innerWidth;
h = window.innerHeight;
renderer.setSize( w, h );
uniforms.u_resolution.value.x = renderer.domElement.width;
uniforms.u_resolution.value.y = renderer.domElement.height;
uniforms.u_frame.value = 0;
rtTexture = new THREE.WebGLRenderTarget(w * textureFraction, h * textureFraction);
rtTexture2 = new THREE.WebGLRenderTarget(w * textureFraction, h * textureFraction);
}
function animate(delta) {
requestAnimationFrame( animate );
render(delta);
}
let capturer = new CCapture( {
verbose: true,
framerate: 60,
// motionblurFrames: 4,
quality: 90,
format: 'webm',
workersPath: 'js/'
} );
let capturing = false;
isCapturing = function(val) {
if(val === false && window.capturing === true) {
capturer.stop();
capturer.save();
} else if(val === true && window.capturing === false) {
capturer.start();
}
capturing = val;
}
toggleCapture = function() {
isCapturing(!capturing);
}
window.addEventListener('keyup', function(e) { if(e.keyCode == 68) toggleCapture(); });
let then = 0;
function renderTexture(delta) {
// let ov = uniforms.u_buff.value;
let odims = uniforms.u_resolution.value.clone();
uniforms.u_resolution.value.x = w * textureFraction;
uniforms.u_resolution.value.y = h * textureFraction;
uniforms.u_buffer.value = rtTexture2.texture;
uniforms.u_renderpass.value = true;
// rtTexture = rtTexture2;
// rtTexture2 = buffer;
window.rtTexture = rtTexture;
renderer.setRenderTarget(rtTexture);
renderer.render( scene, camera, rtTexture, true );
let buffer = rtTexture
rtTexture = rtTexture2;
rtTexture2 = buffer;
// uniforms.u_buff.value = ov;
uniforms.u_buffer.value = rtTexture.texture;
uniforms.u_resolution.value = odims;
uniforms.u_renderpass.value = false;
}
function render(delta) {
uniforms.u_frame.value++;
uniforms.u_mouse.value.x += ( newmouse.x - uniforms.u_mouse.value.x ) * divisor;
uniforms.u_mouse.value.y += ( newmouse.y - uniforms.u_mouse.value.y ) * divisor;
uniforms.u_time.value = delta * 0.0005;
renderer.render( scene, camera );
renderTexture();
renderTexture();
renderTexture();
renderTexture();
renderTexture();
renderTexture();
renderTexture();
renderTexture();
renderTexture();
// renderTexture();
// renderTexture();
// renderTexture();
// renderTexture();
// renderTexture();
// renderTexture();
// renderTexture();
// renderTexture();
// renderTexture();
if(capturing) {
capturer.capture( renderer.domElement );
}
}