⟨/⟩
SnipBox
← Voltar ao catálogo
Catálogo
›
Layouts
›
Harmonic Bauhaus Pattern Generator
Harmonic Bauhaus Pattern Generator
Layouts
HTML
CSS
JS
📅 07/04/2026
👁 10 visualizações
Pattern
Visualização ao Vivo
🔄 Recarregar
HTML
📋 Copiar
<!-- Import maps polyfill --> <script async src="https://unpkg.com/es-module-shims@1.8.0/dist/es-module-shims.js"></script> <!-- Import map for Three.js and addons --> <script type="importmap"> { "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js", "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/" } } </script>
CSS
📋 Copiar
body { margin: 0; padding: 0; overflow: hidden; background-color: #000; } canvas { display: block; width: 100vw; height: 100vh; }
JavaScript
📋 Copiar
import * as THREE from 'three'; import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; // --- SHADER CODE --- const vertexShader = ` varying vec2 vUv; void main() { vUv = uv; gl_Position = vec4(position, 1.0); } `; const fragmentShader = ` uniform vec2 u_resolution; uniform float u_columns; uniform float u_seed; uniform float u_density; uniform float u_radius; uniform float u_macro_prob; uniform float u_micro_prob; uniform vec3 u_colors[5]; varying vec2 vUv; // --- HASH FUNCTIONS --- vec3 hash32(vec2 p) { vec3 p3 = fract(vec3(p.xyx) * vec3(.1031, .1030, .0973)); p3 += dot(p3, p3.yxz + 33.33); return fract((p3.xxy + p3.yzz) * p3.zyx); } float hash12(vec2 p) { vec3 p3 = fract(vec3(p.xyx) * .1031); p3 += dot(p3, p3.yzx + 33.33); return fract((p3.x + p3.y) * p3.z); } // --- MATH PRIMITIVES --- // Advanced box with independent corner radii. Core to the Bauhaus aesthetic. float sdRoundBox(vec2 p, vec2 b, vec4 r) { r.xy = (p.x > 0.0) ? r.xy : r.zw; r.x = (p.y > 0.0) ? r.x : r.y; vec2 q = abs(p) - b + r.x; return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - r.x; } // --- LAYER 1: BASE STRUCTURAL GRID --- // Creates the foundational interlocking blocks, circles, and curves. void evalBaseLayer(vec2 p, float seed, float aa, inout vec3 finalCol) { vec2 id2 = floor(p * 0.5); // Control maximum shape size frequency via u_macro_prob float isMacro = step(1.0 - u_macro_prob, hash12(id2 + seed * 1.1)); vec2 id; vec2 uv; float scale; if (isMacro > 0.5 && u_macro_prob > 0.0) { id = id2 * 2.0; uv = fract(p * 0.5) - 0.5; // UV local to the 2x2 cell scale = 2.0; // Distance multiplier } else { id = floor(p); uv = fract(p) - 0.5; // UV local to 1x1 cell scale = 1.0; } vec3 h = hash32(id + seed); if (h.x > u_density) return; // Leave background color int cIdx = int(h.y * 4.0) + 1; // Pick shape color (1 to 4) vec3 shapeCol = u_colors[cIdx]; float rnd = h.z; float d = 100.0; float r = u_radius; // Controlled via GUI float b = 0.502; // Box half-size // Select Shape based on probability if (rnd < 0.25) { // 25% Solid Block (Always sharp to provide contrast to rounded shapes) d = sdRoundBox(uv, vec2(b), vec4(0.0)); } else if (rnd < 0.50) { // 25% Half Circle (Caps) vec4 rad = (hash12(id+1.0) < 0.25) ? vec4(r,r,0,0) : (hash12(id+1.0) < 0.50) ? vec4(0,r,r,0) : (hash12(id+1.0) < 0.75) ? vec4(0,0,r,r) : vec4(r,0,0,r); d = sdRoundBox(uv, vec2(b), rad); } else if (rnd < 0.75) { // 25% Quarter Circle (Corners) vec4 rad = (hash12(id+2.0) < 0.25) ? vec4(r,0,0,0) : (hash12(id+2.0) < 0.50) ? vec4(0,r,0,0) : (hash12(id+2.0) < 0.75) ? vec4(0,0,r,0) : vec4(0,0,0,r); d = sdRoundBox(uv, vec2(b), rad); } else { // 25% Perfect Full Circle (Radius controlled by u_radius) d = sdRoundBox(uv, vec2(r), vec4(r)); } d *= scale; // Adjust distance field back to global scale for accurate AA finalCol = mix(finalCol, shapeCol, smoothstep(aa, -aa, d)); } // --- LAYER 2: OFFSET BITES & ACCENTS --- void evalBiteLayer(vec2 p, float seed, float aa, inout vec3 finalCol) { vec2 sp = p + vec2(0.5); // Shift by half cell vec2 id2 = floor(sp * 0.5); float isMacro = step(1.0 - u_macro_prob, hash12(id2 + seed * 2.2)); vec2 id; vec2 uv; float scale; if (isMacro > 0.5 && u_macro_prob > 0.0) { id = id2 * 2.0; uv = fract(sp * 0.5) - 0.5; scale = 2.0; } else { id = floor(sp); uv = fract(sp) - 0.5; scale = 1.0; } vec3 h = hash32(id + seed); if (h.x > u_density * 0.55) return; float rnd = h.y; float d = length(uv) - 0.501; // Base circular cut d *= scale; if (rnd < 0.65) { // 65% Subtractive Cut (Background color) - creates S-Curves finalCol = mix(finalCol, u_colors[0], smoothstep(aa, -aa, d)); } else { // 35% Additive Color Dot int cIdx = int(h.z * 4.0) + 1; // 1 to 4 finalCol = mix(finalCol, u_colors[cIdx], smoothstep(aa, -aa, d)); if (hash12(id + 5.0) < 0.3) { float dInner = length(uv) - 0.25; dInner *= scale; finalCol = mix(finalCol, u_colors[0], smoothstep(aa, -aa, dInner)); } } } // --- LAYER 3: MICRO DETAILS (Scale x0.5) --- void evalMicroLayer(vec2 p, float seed, float aa, inout vec3 finalCol) { vec2 mp = p * 2.0; // Scale grid by 2 (makes shapes 0.5x size) vec2 id = floor(mp); vec2 uv = fract(mp) - 0.5; vec3 h = hash32(id + seed); // Sparse density controlled by micro probability if (h.x > u_density * u_micro_prob) return; float d = length(uv) - u_radius * 0.7; d *= 0.5; // Scale distance back int cIdx = int(h.y * 4.0) + 1; // 1 to 4 finalCol = mix(finalCol, u_colors[cIdx], smoothstep(aa, -aa, d)); } void main() { vec2 p = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / u_resolution.y; p *= u_columns; float aa = u_columns * 1.0 / u_resolution.y; vec3 col = u_colors[0]; // Base background evalBaseLayer(p, u_seed, aa, col); evalBiteLayer(p, u_seed + 13.37, aa, col); evalMicroLayer(p, u_seed + 42.00, aa, col); // Subtle print grain float noise = fract(sin(dot(gl_FragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453); col -= noise * 0.035; gl_FragColor = vec4(col, 1.0); } `; // --- APPLICATION SETUP --- const settings = { columns: 5.5, density: 0.55, seed: 142.5, // Default colors bgColor: '#ffffff', color1: '#1b1b1b', color2: '#3185d3', color3: '#ffac75', color4: '#ffec70', // New Shape Controls radius: 0.5, macroProbability: 0.5, microProbability: 0.05, randomizeLayout: () => { settings.seed = Math.random() * 10000.0; updateUniforms(); }, savePNG: () => { // Prepare 2x high resolution const currentWidth = window.innerWidth; const currentHeight = window.innerHeight; const exportWidth = currentWidth * 2; const exportHeight = currentHeight * 2; // Render at 2x resolution renderer.setSize(exportWidth, exportHeight); material.uniforms.u_resolution.value.set(exportWidth, exportHeight); renderer.render(scene, camera); // Get Image Data const dataURL = renderer.domElement.toDataURL('image/png'); // Revert to normal resolution renderer.setSize(currentWidth, currentHeight); material.uniforms.u_resolution.value.set(currentWidth, currentHeight); renderer.render(scene, camera); // Create download link const link = document.createElement('a'); link.download = `bauhaus-pattern-${Math.floor(Math.random()*10000)}.png`; link.href = dataURL; link.click(); } }; let scene, camera, renderer, material; function init() { scene = new THREE.Scene(); camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 10); camera.position.z = 1; // Preserve drawing buffer allows us to extract the image for download renderer = new THREE.WebGLRenderer({ antialias: false, preserveDrawingBuffer: true }); renderer.setSize(window.innerWidth, window.innerHeight); renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); document.body.appendChild(renderer.domElement); // Setup color uniforms array (1 Background + 4 Shapes) const colorUniforms = []; for (let i = 0; i < 5; i++) colorUniforms.push(new THREE.Color()); material = new THREE.ShaderMaterial({ vertexShader, fragmentShader, uniforms: { u_resolution: { value: new THREE.Vector2(window.innerWidth, window.innerHeight) }, u_columns: { value: settings.columns }, u_seed: { value: settings.seed }, u_density: { value: settings.density }, u_radius: { value: settings.radius }, u_macro_prob: { value: settings.macroProbability }, u_micro_prob: { value: settings.microProbability }, u_colors: { value: colorUniforms } } }); const geometry = new THREE.PlaneGeometry(2, 2); const mesh = new THREE.Mesh(geometry, material); scene.add(mesh); updateColors(); setupGUI(); window.addEventListener('resize', onWindowResize); renderer.render(scene, camera); } function updateColors() { material.uniforms.u_colors.value[0].set(settings.bgColor); material.uniforms.u_colors.value[1].set(settings.color1); material.uniforms.u_colors.value[2].set(settings.color2); material.uniforms.u_colors.value[3].set(settings.color3); material.uniforms.u_colors.value[4].set(settings.color4); if(renderer && scene && camera) renderer.render(scene, camera); } function updateUniforms() { material.uniforms.u_columns.value = settings.columns; material.uniforms.u_density.value = settings.density; material.uniforms.u_radius.value = settings.radius; material.uniforms.u_macro_prob.value = settings.macroProbability; material.uniforms.u_micro_prob.value = settings.microProbability; material.uniforms.u_seed.value = settings.seed; if(renderer && scene && camera) renderer.render(scene, camera); } function setupGUI() { const gui = new GUI({ title: 'Pattern Controls' }); const gridFolder = gui.addFolder('Grid & Density'); gridFolder.add(settings, 'columns', 3.0, 20.0).step(0.5).name('Grid Zoom').onChange(updateUniforms); gridFolder.add(settings, 'density', 0.2, 1.0).step(0.05).name('Shape Density').onChange(updateUniforms); const sizesFolder = gui.addFolder('Shape Sizes'); sizesFolder.add(settings, 'macroProbability', 0.0, 1.0).step(0.05).name('Max Size (Macro)').onChange(updateUniforms); sizesFolder.add(settings, 'microProbability', 0.0, 1.0).step(0.05).name('Min Size (Micro)').onChange(updateUniforms); const styleFolder = gui.addFolder('Style'); styleFolder.add(settings, 'radius', 0.0, 0.5).step(0.05).name('Rounding Radius').onChange(updateUniforms); const colorFolder = gui.addFolder('Colors'); colorFolder.addColor(settings, 'bgColor').name('Background').onChange(updateColors); colorFolder.addColor(settings, 'color1').name('Color 1').onChange(updateColors); colorFolder.addColor(settings, 'color2').name('Color 2').onChange(updateColors); colorFolder.addColor(settings, 'color3').name('Color 3').onChange(updateColors); colorFolder.addColor(settings, 'color4').name('Color 4').onChange(updateColors); gui.add(settings, 'randomizeLayout').name('SHUFFLE PATTERN'); gui.add(settings, 'savePNG').name('SAVE PNG (2x)'); } function onWindowResize() { renderer.setSize(window.innerWidth, window.innerHeight); material.uniforms.u_resolution.value.set(window.innerWidth, window.innerHeight); renderer.render(scene, camera); } init();