Published
Edited
Aug 18, 2020
Importers
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import
{
mutable elapsedTime,
shading_function,
kCombined,
potentialBuffer,
k1,
k2,
k3,
k4,
combine_k,
potential,
setupDefault,
wave_unit_components,
psi_n,
regl,
}
with
{
TDSESimulation as TDSESimulation,
playing as playing,
animationTimeLimit as animationTimeLimit,
potential_selection as potential_function,
dt as dt,
wave_position as wave_position
}
from "@flimsyhat/tdse-simulation"
Insert cell
Insert cell
function *TDSESimulation() {
potential() // set up potential texture
while (playing) {
// regl.poll()
++mutable elapsedTime
if (elapsedTime > animationTimeLimit) {
mutable elapsedTime = 0;
}
// step through approximation, rendering each step to a frame buffer
setupDefault({time: elapsedTime}, () => {
psi_n({wave_position: wave_position, wave_unit_components: wave_unit_components});
k1();
k2();
k3();
k4();
combine_k();
})
camera(function () {
setup3DDefaults(() => {
if(complex_components_active) {
drawProbabilityAmplitudeReal();
drawProbabilityAmplitudeComplex();
}
if(complex_phase_active) {
drawComplexPhase();
}
if(selected_visualization == "probability") {
drawProbability();
}
if(selected_visualization == "probability_amplitude") {
drawProbabilityAmplitude();
}
drawPotential();
})
});
yield regl.container
}
if (!playing) {
while (true) {
camera(function () {
setup3DDefaults(() => {
if(complex_components_active) {
drawProbabilityAmplitudeReal();
drawProbabilityAmplitudeComplex();
}
if(complex_phase_active) {
drawComplexPhase();
}
if(selected_visualization == "probability") {
drawProbability();
}
if(selected_visualization == "probability_amplitude") {
drawProbabilityAmplitude();
}
drawPotential();
});
});
yield regl.container
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
probability_amplitude = `
float intensity(vec2 rg) {
return clamp((tex.r + tex.g + 1.) / 2., 0., 1.);
}

`
Insert cell
Insert cell
Insert cell
drawProbability = regl({
uniforms: {
texture: kCombined,
},
vert: `
uniform float WORLD_SIZE;
uniform float WORLD_HEIGHT;
uniform sampler2D texture;
${probability}
float getHeight(vec2 xz) {
vec2 uv = vec2(0.5, 0.5) + xz.yx;
return WORLD_HEIGHT*(-1.0 + 2.0 * remap_probability(texture2D(texture, uv).rg, 0.7));
}
vec3 getPosition(vec2 xz) {
return vec3(WORLD_SIZE*xz.x, getHeight(xz), WORLD_SIZE*xz.y);
}
precision mediump float;
attribute vec2 xzPosition;
varying vec3 vPosition;
varying vec2 vUv;
varying vec3 vNormal;
varying vec2 pos;
uniform mat4 projection, view;
void main() {
pos = xzPosition;
vec3 xyzPosition = getPosition(xzPosition);
vec2 uv = vec2(0.5, 0.5) + xzPosition.yx;
vUv = uv;
float eps = 1.0/16.0;
// approximate the normal with central differences.
vec3 va = vec3(2.0*eps,
getHeight(xzPosition + vec2(eps,0.0)) - getHeight(xzPosition - vec2(eps,0.0)) , 0.0 );
vec3 vb = vec3(0.0,
getHeight(xzPosition + vec2(0.0, eps)) - getHeight(xzPosition - vec2(0.0, eps)), 2.0*eps );
vNormal = normalize(cross(normalize(vb), normalize(va) ));
vPosition = xyzPosition;
gl_Position = projection * view * vec4(xyzPosition, 1);
}`,
frag: `
precision mediump float;
varying vec2 vUv;
varying vec3 vNormal;
varying vec2 pos;
uniform sampler2D texture;
${probability}
${color_map}
void main () {
vec2 tex = texture2D(texture, vUv).rg;
gl_FragColor = vec4(color_map(intensity(tex)), 1.0);
}`,
})
Insert cell
Insert cell
drawProbabilityAmplitude = regl({
uniforms: {
texture: kCombined,
},
vert: `
uniform float WORLD_SIZE;
uniform float WORLD_HEIGHT;
uniform sampler2D texture;
float getHeight(vec2 xz) {
vec2 uv = vec2(0.5, 0.5) + xz.yx;
float h = texture2D(texture, uv).r * 1.5;
return WORLD_HEIGHT*(-1.0 + 2.0 * h) / 2. - .5;
}
vec3 getPosition(vec2 xz) {
return vec3(WORLD_SIZE*xz.x, getHeight(xz), WORLD_SIZE*xz.y);
}
precision mediump float;
attribute vec2 xzPosition;
varying vec3 vPosition;
varying vec2 vUv;
varying vec3 vNormal;
varying vec2 pos;
uniform mat4 projection, view;
void main() {
pos = xzPosition;
vec3 xyzPosition = getPosition(xzPosition);
vec2 uv = vec2(0.5, 0.5) + xzPosition.yx;
vUv = uv;
float eps = 1.0/16.0;
// approximate the normal with central differences.
vec3 va = vec3(2.0*eps,
getHeight(xzPosition + vec2(eps,0.0)) - getHeight(xzPosition - vec2(eps,0.0)) , 0.0 );
vec3 vb = vec3(0.0,
getHeight(xzPosition + vec2(0.0, eps)) - getHeight(xzPosition - vec2(0.0, eps)), 2.0*eps );
vNormal = normalize(cross(normalize(vb), normalize(va) ));
vPosition = xyzPosition;
gl_Position = projection * view * vec4(xyzPosition, 1);
}`,
frag: `
precision mediump float;
varying vec2 vUv;
varying vec3 vNormal;
varying vec2 pos;
uniform sampler2D texture;
${color_map}
float nudge_intensity(float x) {
return 1. - pow((1. - x), 2.);
}
void main () {
vec2 tex = texture2D(texture, vUv).rg;
float h = pow(abs(tex.r), 0.9); // (tex.r + 1.) / 2.;
gl_FragColor = vec4(color_map(nudge_intensity(clamp(h, 0., 1.))), 1.);
}`
})
Insert cell
Insert cell
Insert cell
Insert cell
drawPotential = regl({
uniforms: {
texture: potentialBuffer,
},
vert: `
uniform float WORLD_SIZE;
uniform float WORLD_HEIGHT;
uniform sampler2D texture;
float getHeight(vec2 xz) {
vec2 uv = vec2(0.5, 0.5) + xz.yx;
return clamp(WORLD_HEIGHT*(-1.0 + 2.0 * texture2D(texture, uv).r), -1.0, 5.0) - 0.01;
}
vec3 getPosition(vec2 xz) {
return vec3(WORLD_SIZE*xz.x, getHeight(xz), WORLD_SIZE*xz.y);
}
precision mediump float;
attribute vec2 xzPosition;
varying vec2 pos;
varying vec3 vPosition;
varying vec2 vUv;
varying vec3 vNormal;
uniform mat4 projection, view;
varying float height;
void main() {
pos = xzPosition;
height = getHeight(xzPosition);
vec3 xyzPosition = getPosition(xzPosition);
vec2 uv = vec2(0.5, 0.5) + xzPosition.yx;
vUv = uv;
float eps = 1.0/16.0;
// approximate the normal with central differences.
vec3 va = vec3(2.0*eps,
getHeight(xzPosition + vec2(eps,0.0)) - getHeight(xzPosition - vec2(eps,0.0)) , 0.0 );
vec3 vb = vec3(0.0,
getHeight(xzPosition + vec2(0.0, eps)) - getHeight(xzPosition - vec2(0.0, eps)), 2.0*eps );
vNormal = normalize(cross(normalize(vb), normalize(va) ));
vPosition = xyzPosition;
gl_Position = projection * view * vec4(xyzPosition, 1);
}`,
frag: `
precision mediump float;
varying vec2 vUv;
varying vec3 vNormal;
varying float height;
uniform sampler2D texture;

void main () {
float tex = texture2D(texture, vUv).r;
gl_FragColor = vec4(vec3(1., 1., 1.) , clamp(ceil(height + 0.99), 0.0, 1.0) * 0.5);
}`
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more