Skip to content

Instantly share code, notes, and snippets.

View smpnjn's full-sized avatar
🥶

Johnny smpnjn

🥶
View GitHub Profile
let string = 'orange apple apple apple pen';
// You need regex to replace all instances of something with replace()
let newString1 = string.replace('apple', 'kiwi'); // returns 'orange kiwi apple apple pen'
// But this is not the case with replaceAll
let newString2 = string.replaceAll('apple', 'kiwi'); // returns 'orange kiwi kiwi kiwi pen'
import * as THREE from './js/src/Three.js';
// We also want to do some post-processing so we need these packages
import { EffectComposer } from './js/examples/jsm/postprocessing/EffectComposer.js';
import { UnrealBloomPass } from './js/examples/jsm/postprocessing/UnrealBloomPass.js';
import { RenderPass } from './js/examples/jsm/postprocessing/RenderPass.js';
@smpnjn
smpnjn / globe.js
Last active December 9, 2020 17:40
document.addEventListener("DOMContentLoaded", function(e) {
const renderer = new THREE.WebGLRenderer({
powerPreference: "high-performance",
antialias: true,
alpha: true
});
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement )
varying vec2 vUv;
uniform sampler2D getTexture1;
void main() {
vUv = uv;
vec4 bumpData = texture2D( getTexture1, uv );
float displacement = 20.0 * bumpData.r * bumpData.a;
vec3 newPosition = position + normal * displacement;
vec3 rgb(float r, float g, float b) {
return vec3(r / 255., g / 255., b / 255.);
}
vec3 rgb(float c) {
return vec3(c / 255., c / 255., c / 255.);
}
varying vec2 vUv;
// A tuple
const tuple = #["testing", "test"];
// A record
const myRecord = #{
someName: "hello",
anotherName: "goodbye",
myTuple: #["some", "array"] // Tuples can be put into records, since they are primitives
};
let myTuple = #["some", "array"];
let foreignTuple = #["some", "array"];
let myRecord = #{
user: {
name: "Mark",
age: 25
location: {
address: "Manhattan"
}
const currentUnix = Temporal.now.instant() // get current unix time
const timezoneUnix = Temporal.now.timeZone() // get current unix time for current time zone
// It is easy to add or subtract() from a Temporal object
const modifyUnix = currentUnix.add({ "day" : 1 }); // Add one day to 'currentUnix'
zdt = Temporal.ZonedDateTime.from('1995-12-07T03:24-08:00[America/Los_Angeles]');
['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN'][zdt.dayOfWeek - 1]; // => THU
// toLocaleString still works as well
// current approach
old = map.get(key);
if (!old) {
map.set(key, value);
} else {
map.set(key, updated);
}
// new approach
map.emplace(key, {
// Create a plane, and pass that through to our shaders
let geometry = new THREE.PlaneGeometry(600, 600, 100, 100);
let material = new THREE.ShaderMaterial({
// These uniform variables can be adjusted in JS and are passed into the shader
// After that they are passed into the GPU and rendered. You can alter these values
// and change them through the mesh i.e. mesh.material.uniforms.u_height.value = x
uniforms: {
u_lowColor: {type: 'v3', value: low },
u_highColor: {type: 'v3', value: high },
u_time: {type: 'f', value: 0},