HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<script type="importmap">
{
"imports": {
"three": "https://threejs.org/build/three.module.js",
"three/": "https://threejs.org/"
}
}
</script>
body{
overflow: hidden;
margin: 0;
}
import {
Clock,
Color,
DirectionalLight,
LinearFilter,
LinearMipMapLinearFilter,
LoadingManager,
MathUtils,
Mesh,
MeshStandardMaterial,
Object3D,
PerspectiveCamera,
PlaneGeometry,
RepeatWrapping,
RGBAFormat,
Scene,
ShaderMaterial,
sRGBEncoding,
TextureLoader,
Vector2,
Vector3,
WebGLRenderer
} from "three";
import {OrbitControls} from "three/examples/jsm/controls/OrbitControls.js";
/* = Variables ===============================================================*/
// Common
let GrdSiz = 804.67; // Size of Grid in meters
GrdSiz = 1000;
let GrdRCs = 2;
let WtrCol = 0x1040f0; // Water (Tropical)
WtrCol = 0x081080; // Water (Navy)
// Animated
let segNum = 500; // Segments per Grid (fewer = sharper waves)
let GrdPtr = [0];
let WavMZV = [0];
let WavMXV = [0];
let geoWav, matWav;
// Textures
let NrmSrc = ["https://threejs.org/examples/textures/waternormals.jpg"];
let WtrNrm = 0; // Pointer to Water Normal Map
let MapSrc = ["https://threejs.org/examples/textures/water.jpg"]; // ### Address of Perlin or Displacement Map
let WtrMap; // ### Pointer to Perlin or Displacement Map
let WtrRep = 1; // Wrap Reps
let LodFlg = 0; // Load Flag
// Uniform
let gu = { // Uniform
time: {value: 0},
grid: {value: GrdSiz},
image: {value: WtrMap}, // ###
};
/* = Basic Values ============================================================*/
// Display
let scene = new Scene();
scene.background = new Color(0x1732c1);
let renderer = new WebGLRenderer({antialias: true});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputEncoding = sRGBEncoding;
document.body.appendChild(renderer.domElement);
window.addEventListener("resize", onWindowResize, false);
// Light
let dirLight = new DirectionalLight(0xffffff,1);
// dirLight.position.set(0,2000,-1000); // Default position
dirLight.position.set(0,2000,0); // High Noon
scene.add(dirLight);
// Camera
let camera = new PerspectiveCamera(45, window.innerWidth/window.innerHeight, 1, 15000);
let controls = new OrbitControls(camera, renderer.domElement);
camera.position.set(0,100,200);
controls.update();
// Clock
let clock = new Clock();
let etime;
// Loading Manager
// Create a loading manager to set RESOURCES_LOADED when appropriate.
// Pass loadingManager to all resource loaders.
let loadingManager = new LoadingManager();
let RESOURCES_LOADED = false;
loadingManager.onLoad = function(){
console.log("loaded all resources");
RESOURCES_LOADED = true;
initAll();
};
let txtrLoader = new TextureLoader(loadingManager);
/* = Main Program ============================================================*/
loadAll();
rendAll();
/* 0 Load All ================================================================*/
function loadAll() {
// Normal Map
txtrLoader.load(NrmSrc, function(texture) {
texture.format = RGBAFormat;
texture.magFilter = LinearFilter;
texture.minFilter = LinearMipMapLinearFilter;
texture.generateMipmaps = true;
texture.wrapS = texture.wrapT = RepeatWrapping;
texture.offset.set(0,0);
texture.repeat.set(WtrRep,WtrRep);
texture.needsUpdate = true
WtrNrm = texture;
});
// ### Perlin or Displacement Map
txtrLoader.load(MapSrc, function(texture) {
texture.format = RGBAFormat;
texture.magFilter = LinearFilter;
texture.minFilter = LinearMipMapLinearFilter;
texture.generateMipmaps = true;
texture.wrapS = texture.wrapT = RepeatWrapping;
texture.offset.set(0,0);
texture.repeat.set(WtrRep,WtrRep);
texture.needsUpdate = true
gu.image.value = texture; // assing to uniform's value
});
}
/* 1 Initialize ==============================================================*/
function initAll() {
let n, zx;
/* = Main Program ============================================================*/
// Planes with Extended Material -----------------------------------------
geoWav = new PlaneGeometry(GrdSiz,GrdSiz,segNum,segNum);
geoWav.rotateX(-Math.PI * 0.5);
matWav = new MeshStandardMaterial({
// normalMap: WtrNrm,
metalness: 0.5,
roughness: 0.6,
onBeforeCompile: shader => {
shader.uniforms.time = gu.time;
shader.uniforms.grid = gu.grid;
shader.uniforms.image = gu.image; // ###
shader.vertexShader = `
uniform float time;
uniform float grid;
uniform sampler2D image; // ###
varying float vHeight;
vec3 moveWave(vec3 p){
// Angle = distance offset + degree offset
vec3 retVal = p;
float ang;
float kzx = 360.0/grid;
// Wave1 (135 degrees)
ang = 50.0*time + -1.0*p.x*kzx + -2.0*p.z*kzx;
if (ang>360.0) ang = ang-360.0;
ang = ang*3.14159265/180.0;
retVal.y = 3.0*sin(ang);
// Wave2 (090)
ang = 25.0*time + -3.0*p.x*kzx;
if (ang>360.0) ang = ang-360.0;
ang = ang*3.14159265/180.0;
retVal.y = retVal.y + 2.0*sin(ang);
// Wave3 (180 degrees)
ang = 15.0*time - 3.0*p.z*kzx;
if (ang>360.0) ang = ang-360.0;
ang = ang*3.14159265/180.0;
retVal.y = retVal.y + 2.0*sin(ang);
// Wave4 (225 degrees)
ang = 50.0*time + 4.0*p.x*kzx + 8.0*p.z*kzx;
if (ang>360.0) ang = ang-360.0;
ang = ang*3.14159265/180.0;
retVal.y = retVal.y + 0.5*sin(ang);
// Wave5 (270 degrees)
ang = 50.0*time + 8.0*p.x*kzx;
if (ang>360.0) ang = ang-360.0;
ang = ang*3.14159265/180.0;
retVal.y = retVal.y + 0.5*sin(ang);
// Add Random Value from Perlin Image
//?? (for test, this replaces modified Y value)
vec2 texUV = fract(p.xz/1000.); // ###
retVal.y += texture(image, texUV).r * 5.; // ###
//
return retVal;
}
${shader.vertexShader}
`.replace(
`#include <beginnormal_vertex>`,
`#include <beginnormal_vertex>
vec3 p = position;
vec2 move = vec2(1, 0);
vec3 pos = moveWave(p);
vec3 pos2 = moveWave(p + move.xyy);
vec3 pos3 = moveWave(p + move.yyx);
vNormal = normalize(cross(normalize(pos2-pos), normalize(pos3-pos)));
`
).replace(
`#include <begin_vertex>`,
`#include <begin_vertex>
transformed.y = pos.y;
vHeight = pos.y;
`
);
shader.fragmentShader = `
varying float vHeight;
${shader.fragmentShader}
`.replace(
`#include <color_fragment>`,
`#include <color_fragment>
diffuseColor.rgb = mix(vec3(0.03125,0.0625,0.5), vec3(0.1,0.2,0.6), smoothstep(0.0, 6.0, vHeight));
if (vHeight>7.0) {
diffuseColor.rgb = vec3(0.2,0.3,0.7); // Adds "foam" highlight to highest waves
}
`
);
}
});
// Compute Starting Z and X Values
zx = -0.5*(GrdRCs)*GrdSiz+0.5*GrdSiz;
for (let i = 0; i < GrdRCs; i++) {
WavMZV[i] = zx;
WavMXV[i] = zx;
zx = zx + GrdSiz;
}
// 4 Adjacent Planes
n = 0;
for (let z = 0; z < GrdRCs; z++) { // Row X2
for (let x = 0; x < GrdRCs; x++) { // Column X2
GrdPtr[n] = new Mesh(geoWav,matWav);
scene.add(GrdPtr[n]);
GrdPtr[n].position.set(WavMXV[x],0,-WavMZV[z]);
n++;
}
}
//
LodFlg = 1;
}
/* 2 Render ==================================================================*/
function rendAll() {
requestAnimationFrame(rendAll);
if (LodFlg > 0) {
etime = clock.getElapsedTime();
gu.time.value = etime;
// WtrNrm.offset.x -= .0005;
// WtrNrm.offset.y += .00025;
}
controls.update();
renderer.render(scene, camera);
}
/* Window Resize Input ========================================================*/
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
Also see: Tab Triggers