<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@v0.173.0/build/three.webgpu.js",
"three/webgpu": "https://cdn.jsdelivr.net/npm/three@v0.173.0/build/three.webgpu.js",
"three/tsl": "https://cdn.jsdelivr.net/npm/three@v0.173.0/build/three.webgpu.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@v0.173.0/examples/jsm/"
}
}
</script>
<div id="courses"><a href="https://niklever.com/courses" target="_blank">niklever.com/courses</a></div>
<style>
#source {
position: absolute;
top: 0;
left: 0;
width: 50%;
height: 100%;
}
#result {
position: absolute;
top: 0;
right: 0;
width: 50%;
height: 100%;
}
#renderer {
position: absolute;
bottom: 15px;
right: calc( 50% + 15px );
width: 200px;
height: 200px;
z-index: 100;
pointer-events: none;
}
</style>
<div id="source"></div>
<div id="result"></div>
<div id="renderer"></div>
body{
padding: 0;
margin: 0;
}
#courses{
font: bold 30px "Arial";
position: fixed;
left: 20px;
top: 20px;
color: #ffffff;
text-decoration: none;
}
a:link {
color: white;
text-decoration: none;
}
a:hover{
color: #dddd33;
text-decoration: underline;
}
a:visited {
color: white;
text-decoration: none;
}
import Transpiler from 'three/addons/transpiler/Transpiler.js';
import GLSLDecoder from 'three/addons/transpiler/GLSLDecoder.js';
import TSLEncoder from 'three/addons/transpiler/TSLEncoder.js';
init();
function init() {
// editor
window.require.config( { paths: { 'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@0.48.0/min/vs' } } );
require( [ 'vs/editor/editor.main' ], () => {
let timeout = null;
const editorDOM = document.getElementById( 'source' );
const resultDOM = document.getElementById( 'result' );
const glslCode = `
varying vec2 vUv;
uniform float uTime;
uniform float uContrast;
uniform float uBrightness;
uniform float uFrequency;
uniform vec3 uPhase;
uniform int uCount;
float mandelbrot( vec2 c )
{
float c2 = dot(c, c);
// skip computation inside M1 - https://iquilezles.org/articles/mset1bulb
if( 256.0*c2*c2 - 96.0*c2 + 32.0*c.x - 3.0 < 0.0 ) return 0.0;
// skip computation inside M2 - https://iquilezles.org/articles/mset2bulb
if( 16.0*(c2+2.0*c.x+1.0) - 1.0 < 0.0 ) return 0.0;
float B = float(uCount);
float n = 0.0;
vec2 z = vec2(0.0);
for( int i=0; i<uCount; i++ )
{
z = vec2( z.x*z.x - z.y*z.y, 2.0*z.x*z.y ) + c;
if( dot(z,z)>(B*B) ) break;
n += 1.0;
}
if( n>float(uCount - 1) ) return 0.0;
// ------------------------------------------------------
// optimized smooth interation count
float sn = n - log2(log2(dot(z,z))) + 4.0;
return sn;
}
void main( )
{
vec3 col = vec3(0.0);
int AA = 2;
float AAsq = float(AA*AA);
for( int m=0; m<AA; m++ ){
for( int n=0; n<AA; n++ )
{
vec2 p = (-1.0 + 2.0*(vUv+vec2(float(m) * 0.001,float(n) * 0.001)/float(AA)));
float w = float(AA*m+n);
float time = uTime + 0.5*(1.0/24.0)*w/AAsq;
float zoo = 0.72 + 0.28*cos(.09*time);
float theta = 0.05*(1.0-zoo)*time;
float coa = cos( theta );
float sia = sin( theta );
zoo = pow( zoo, 8.0);
vec2 xy = vec2( p.x*coa-p.y*sia, p.x*sia+p.y*coa);
//vec2 c = vec2(-.745,.186) + xy*zoo;
vec2 c = vec2(-0.720,-0.26) + xy*zoo;
float l = mandelbrot(c);
//color(t) = a + b ⋅ cos[ 2π(c⋅t+d)]
//a: contrast
//b: brightness
//c: frequency
//d: phase
col += (l<0.5) ? vec3(0.0,0.0,0.0) :
uContrast + uBrightness*cos( 6.28*(uFrequency*(l/float(uCount)) + uPhase) );
}
}
col /= AAsq;
gl_FragColor = vec4( col, 1.0 );
}
`;
const editor = window.monaco.editor.create( editorDOM, {
value: glslCode,
language: 'glsl',
theme: 'vs-dark',
automaticLayout: true,
minimap: { enabled: false }
} );
const result = window.monaco.editor.create( resultDOM, {
value: '',
language: 'javascript',
theme: 'vs-dark',
automaticLayout: true,
readOnly: true,
minimap: { enabled: false }
} );
const showCode = ( code ) => {
result.setValue( code );
result.revealLine( 1 );
};
const build = () => {
try {
const glsl = editor.getValue();
const decoder = new GLSLDecoder();
const encoder = new TSLEncoder();
const transpiler = new Transpiler( decoder, encoder );
const tsl = transpiler.parse( glsl );
showCode( tsl );
} catch ( e ) {
result.setValue( 'Error: ' + e.message );
}
};
build();
editor.getModel().onDidChangeContent( () => {
if ( timeout ) clearTimeout( timeout );
timeout = setTimeout( build, 1000 );
} );
} );
}
This Pen doesn't use any external CSS resources.