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.
<link href="https://fonts.googleapis.com/css2?family=Baloo+Tamma+2&display=swap" rel="stylesheet">
<script src="//cdn.rawgit.com/mrdoob/three.js/master/build/three.js"></script>
<script src="//cdn.rawgit.com/mrdoob/three.js/master/examples/js/controls/OrbitControls.js"></script>
* {
margin: 0;
}
import lilGui from '//cdn.skypack.dev/lil-gui';
console.clear();
let camera, scene, renderer, controls, mesh, ground;
const GLSLUtils = `
float lerp(float x, float y, float a) {
return (1.0 - a) * x + a * y;
}
vec3 lerp(vec3 x, vec3 y, float a) {
return (1.0 - a) * x + a * y;
}
vec4 lerp(vec4 x, vec4 y, float a) {
return (1.0 - a) * x + a * y;
}
`;
const GLSLToon2Lights = `
struct Toon2Material {
vec3 diffuseColor;
};
void RE_Direct(const in IncidentLight directLight, const in GeometricContext geometry, const in Toon2Material material, inout ReflectedLight reflectedLight) {
float dotLight = dot(geometry.normal, directLight.direction);
vec2 dotNormal = vec2(dotLight * 0.5 + 0.5, 0.0);
reflectedLight.directDiffuse += dotNormal.x * (RECIPROCAL_PI * material.diffuseColor);
}
void RE_IndirectDiffuse(const in vec3 irradiance, const in GeometricContext geometry, const in Toon2Material material, inout ReflectedLight reflectedLight) {
reflectedLight.indirectDiffuse += irradiance * (RECIPROCAL_PI * material.diffuseColor);
}
#define RE_Direct RE_Direct
#define RE_IndirectDiffuse RE_IndirectDiffuse
`;
const createMaterial = () => {
const material = new THREE.ShaderMaterial({
uniforms: {
...THREE.UniformsLib.fog,
...THREE.UniformsLib.lights,
fAlpha: {
value: Math.PI
},
fBeta: {
value: 0.0
},
vResolution: {
value: new THREE.Vector2(window.innerWidth, window.innerHeight)
},
fMaskAlpha: {
value: 10000.0
},
fMaskBeta: {
value: 100.0
},
fMaskThreshold: {
value: 50.0
},
fDebug: {
value: 1.0
},
fDiffuseTexture: {
value: new THREE.TextureLoader().load('//cdn.wtlstudio.com/sample.wtlstudio.com/49c8d4a8-1d2d-4013-a77e-f42cd50f1b10.png', texture => {
texture.wrapT = THREE.RepeatWrapping;
texture.wrapS = THREE.RepeatWrapping;
})
}
},
vertexShader: `
#include <common>
#include <shadowmap_pars_vertex>
${GLSLUtils}
uniform float fAlpha;
uniform float fDebug;
varying vec3 vPosition;
varying vec3 vNormal;
varying vec2 vUv;
varying vec3 vViewPosition;
varying float fDelta;
void main() {
float fAngle = position.x * fAlpha;
float fHalfAngle = fAngle * .5;
float fElevationRadius = sin(PI * position.y);
fDelta = fAlpha / PI;
vec4 vPlanet = vec4(
position.x,
position.y,
position.z,
1.0
);
vPlanet.x = lerp(position.x, sin(fHalfAngle) * fElevationRadius, fDelta);
vPlanet.z = lerp(position.z, cos(fHalfAngle) * fElevationRadius, fDelta);
vPlanet.y = lerp(position.y, cos(PI * position.y), fDelta);
vPosition = vPlanet.xyz;
vNormal = lerp(vec3(0.0, 1.0, 0.0), normalize(vPlanet.xyz), fDelta);
vec3 transformedNormal = vNormal;
vec3 transformed = vPosition;
vec4 mvPosition = modelViewMatrix * vPlanet;
vViewPosition = -mvPosition.xyz;
gl_Position = projectionMatrix * mvPosition;
#include <worldpos_vertex>
#include <shadowmap_vertex>
}
`,
fragmentShader: `
#include <common>
#include <packing>
#include <lights_pars_begin>
#include <shadowmap_pars_fragment>
uniform vec2 vResolution;
uniform float fDebug;
uniform sampler2D fDiffuseTexture;
varying vec3 vPosition;
varying vec3 vNormal;
varying vec3 vViewPosition;
varying float fDelta;
varying vec2 vUv;
${GLSLUtils}
${GLSLToon2Lights}
vec3 vFlatColor = vec3(0.49, 0.01, 0.05);
vec3 vShadowColor = vec3(0.19, 0.01, 0.05);
// vec3 vFlatColor = vec3(0.49, 0.56, 0.49);
// vec3 vShadowColor = vec3(0.50, 0.45, 0.44);
float fFresnelSharpness = .05;
float fFresnelGlowSharpness = .05;
void main() {
ReflectedLight reflectedLight = ReflectedLight(vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
vec2 viewCoord = vec2(gl_FragCoord.x / vResolution.x, gl_FragCoord.y / vResolution.y);
vec3 normal = vNormal;
vec3 vColor = vec3(0.0, 0.0, 0.0);
float fDistance = length(vPosition.xy);
vColor += vFlatColor;
Toon2Material material;
material.diffuseColor = vec3(1.0);
#include <lights_fragment_begin>
#include <lights_fragment_maps>
#include <lights_fragment_end>
vec3 vTotalIrradiance = saturate(reflectedLight.indirectDiffuse + reflectedLight.directDiffuse);
float fShadowMask = length(reflectedLight.directDiffuse) < 0.5 ? 0.0 : 1.0;
float fFresnelMask = dot(vNormal, normalize(cameraPosition - vPosition));
float fInvertedFresnelMark = 1.0 - fFresnelMask;
float fDirectMask = saturate(length(reflectedLight.directDiffuse / .01) - 75.);
vec3 triplanarNormal = abs(vNormal);
vec3 triplanarWeights = abs(vNormal) / (
triplanarNormal.x + triplanarNormal.y + triplanarNormal.z
);
vec4 vDiffuseTextureXZ = texture2D(fDiffuseTexture, vPosition.xz);
vec4 vDiffuseTextureXY = texture2D(fDiffuseTexture, vPosition.xy);
vec4 vDiffuseTextureYZ = texture2D(fDiffuseTexture, vPosition.yz);
vec4 vDiffuseTexture = (
vDiffuseTextureXZ * triplanarWeights.x +
vDiffuseTextureXY * triplanarWeights.y +
vDiffuseTextureYZ * triplanarWeights.z
);
gl_FragColor = vDiffuseTexture;
vec3 vMaskedIrradiance = vTotalIrradiance * vec3( saturate(fInvertedFresnelMark / fFresnelGlowSharpness - 12.));
vMaskedIrradiance.xyz += vec3(fDirectMask);
vMaskedIrradiance = saturate(vMaskedIrradiance);
gl_FragColor.xyz *= saturate(fShadowMask) * 0.2 + 0.8;
gl_FragColor.xyz += 1. * vTotalIrradiance * vMaskedIrradiance * RECIPROCAL_PI;
gl_FragColor.xyz *= vec3(saturate(fFresnelMask / fFresnelSharpness - 4.0));
gl_FragColor = lerp(vec4(vFlatColor, 1.0), gl_FragColor, fDelta);
}
`,
side: THREE.DoubleSide,
lights: true
});
const materialUi = new lilGui();
materialUi.add(material.uniforms.fAlpha, 'value', 0.0, Math.PI).name('Alpha');
materialUi.add(material.uniforms.fDebug, 'value', -2.0, 2.0).name('Debug');
return material;
};
const createWorld = () => {
mesh = new THREE.Mesh(
new THREE.PlaneBufferGeometry(2.0, 2.0, 100.0, 100.0),
createMaterial()
);
mesh.castShadow = true;
scene.add(mesh);
camera.lookAt(mesh.position);
ground = new THREE.Mesh(
new THREE.CylinderGeometry(15.0, 15.0, 0.1, 32),
new THREE.MeshPhongMaterial({ color: 0x333322 })
);
ground.position.y = -2.0;
ground.receiveShadow = true;
scene.add(ground);
};
const init = () => {
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000.0);
camera.position.set(-5, 5, 7);
scene = new THREE.Scene();
scene.background = new THREE.Color(0x151122);
scene.add(new THREE.HemisphereLight(0xffffcc, 0x000033, 1.0));
const light1 = new THREE.PointLight(0x0000ff, 1.0);
light1.position.set(2.0, 2.0, 2.0);
light1.castShadow = true;
scene.add(light1);
const light2 = new THREE.DirectionalLight(0xff0000, 0.5);
light2.position.set(-2.0, 2.0, -2.0);
light1.castShadow = true;
scene.add(light2);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.maxPolarAngle = Math.PI / 2.0;
createWorld();
}
const animate = () => {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
init();
animate();
Also see: Tab Triggers