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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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 src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.3/dat.gui.min.js"></script>
html, body {
height: 100%;
margin: 0;
overflow: hidden;
}
canvas {
width: 100%;
height: 100%;
display; block;
}
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, 1, 1, 1000);
camera.position.setScalar(100).setX(-100);
var renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setClearColor(0x202020);
var canvas = renderer.domElement;
document.body.appendChild(canvas);
var controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.update();
console.log(new THREE.Color(0xface8d));
var grid = new THREE.GridHelper(100, 10);
grid.position.set(0, -50, 0);
scene.add(grid);
let xSize = 50;
let ySize = 50;
let zSize = 50;
let n = xSize * ySize * zSize;
let geometry = new THREE.InstancedBufferGeometry();
let positions = [];
function mapTo3D(i) {
let z = Math.floor(i / (xSize * ySize));
i -= z * xSize * ySize;
let y = Math.floor(i / xSize);
let x = i % xSize;
return { x: x * 2, y: y * 2, z: z * 2};
}
for (let i = 0; i < n; i++) {
let p = mapTo3D(i);
positions.push(p.x * 1, p.y * 1, p.z * 1);
}
let boxGeometry = new THREE.BoxBufferGeometry(2, 2, 2).toNonIndexed();
let pointsGeometry = new THREE.BufferGeometry();
pointsGeometry.addAttribute("position", new THREE.BufferAttribute(new Float32Array(positions), 3));
pointsGeometry.center();
geometry.addAttribute("position", new THREE.BufferAttribute(boxGeometry.attributes.position.array, 3));
geometry.addAttribute("instancePosition", new THREE.InstancedBufferAttribute(pointsGeometry.attributes.position.array, 3));
let points = new THREE.Mesh(
geometry,
new THREE.ShaderMaterial({
uniforms:{
time:{
value: 0
},
size: {
value: 1
}
},
vertexShader:`
#define PI 3.1415926
uniform float time;
uniform float size;
attribute vec3 instancePosition;
varying vec3 vC;
varying vec3 vP;
// http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
float udBox( vec3 p, vec3 b, float r )
{
return length(max(abs(p)-b,0.0)) - r;
}
float sdSphere( vec3 p, float s )
{
return length(p)-s;
}
float sdRoundCone( in vec3 p, in float r1, float r2, float h )
{
vec2 q = vec2( length(p.xz), p.y );
float b = (r1-r2)/h;
float a = sqrt(1.0-b*b);
float k = dot(q,vec2(-b,a));
if( k < 0.0 ) return length(q) - r1;
if( k > a*h ) return length(q-vec2(0.0,h)) - r2;
return dot(q, vec2(a,b) ) - r1;
}
float sdTorus( vec3 p, vec2 t )
{
vec2 q = vec2(length(p.xz)-t.x,p.y);
return length(q)-t.y;
}
float opUnion( float d1, float d2 ) { return min(d1,d2); }
float opSubtraction( float d1, float d2 ) { return max(-d1,d2); }
float opIntersection( float d1, float d2 ) { return max(d1,d2); }
vec3 opSymX( in vec3 p )
{
p.x = abs(p.x);
return p;
}
// honestly stolen from here https://www.shadertoy.com/view/MlGGDh
mat3 rotationMatrix(vec3 m,float a) {
m = normalize(m);
float c = cos(a),s=sin(a);
return mat3(
c+(1.-c)*m.x*m.x, (1.-c)*m.x*m.y-s*m.z, (1.-c)*m.x*m.z+s*m.y,
(1.-c)*m.x*m.y+s*m.z, c+(1.-c)*m.y*m.y, (1.-c)*m.y*m.z-s*m.x,
(1.-c)*m.x*m.z-s*m.y, (1.-c)*m.y*m.z+s*m.x, c+(1.-c)*m.z*m.z);
}
mat3 rotate(vec3 val){
mat3 matX = rotationMatrix(vec3(1, 0, 0), val.x);
mat3 matY = rotationMatrix(vec3(0, 1, 0), val.y);
mat3 matZ = rotationMatrix(vec3(0, 0, 1), val.z);
return matX * matY * matZ;
}
void main(){
vP = position;
vec3 vPos = instancePosition;
vPos.y += 15. - abs(sin(time * PI)) * 5.;
vPos.z += 5.;
vPos *= rotate(vec3(0., time * 0.5, 0.));
// head
vec3 headPos = vPos;
if (headPos.y > 20. && headPos.z > 0.) headPos.z -= 5.;
if (headPos.y < 18. && headPos.y > -7. && abs(headPos.x) < 5. && headPos.z > 0.) headPos.z -= 15. - (headPos.y) * 0.5;
if (headPos.y < -12. && abs(headPos.x) < 9. && headPos.z > 0.) headPos.z -= 5.;
float head = udBox(headPos, vec3(12,22,9), 9.);
// eyes
vec3 eyesPos = vPos;
float eyes = sdSphere(opSymX(eyesPos) - vec3(9., 16., 16.), 3.);
// eye-brows
vec3 browsPos = vPos;
float brows = udBox(opSymX(browsPos) - vec3(9, 20., 22.), vec3(6., 2., 2.), 0.);
// lips
vec3 lipsPos = vPos;
float lips = udBox(lipsPos - vec3(0., -11.5, 20.), vec3(8., 2., 2.), 0.);
// hair
vec3 hairPos = vPos;
hairPos.y += 40.;
hairPos.z *= 1.35;
hairPos.z +=22.;
float hair = sdRoundCone(hairPos * rotate(vec3(-PI * 0.1, 0., 0.)), 23., 27., 60.);
// ring
vec3 ringPos = vPos;
ringPos.z *= 1.2;
float ring = sdTorus((ringPos - vec3(0., 31., -6.)) * rotate(vec3(PI * 0.1, 0, 0)), vec2(22., 2));
// feather
vec3 featherPos = vPos;
featherPos.x *= 4.;
float feather = sdRoundCone((featherPos - vec3(100., 30., 0.)) * rotate(vec3(PI *0.2, 0., 0.)), 7., 8., 30.);
float res = opUnion(head, eyes);
res = opUnion(res, brows);
res = opUnion(res, lips);
res = opUnion(res, hair);
res = opUnion(res, ring);
res = opUnion(res, feather);
float vSize = size;
if ( res > 0.0 ) vSize *= 0.0;
vC = vec3(0);
if (res == head) vC = vec3(0.9903921568627451, 0.707843137254902, 0.4529411764705883);
if (res == eyes) vC = vec3(0.3, 0.2, 0.1);
if (res == brows) vC = vec3(0.5);
if (res == lips) vC = vec3(0.9903921568627451, 0.707843137254902, 0.4529411764705883) * 0.5;
if (res == hair) vC = vec3(0.7);
if (res == ring) vC = vec3(0.5, 0., 0.);
if (res == feather) vC = vec3(1.);
vec4 mvPosition = modelViewMatrix * vec4( position * vSize + instancePosition, 1.0 );
gl_Position = projectionMatrix * mvPosition;
}
`,
fragmentShader: `
varying vec3 vC;
varying vec3 vP;
void main(){
vec3 c = vC;
if (min(vP.x, min(vP.y, vP.z)) < -0.9) c *= 0.75; // fake shadow
gl_FragColor = vec4( c, 1.0);
}
`
})
);
scene.add(points);
// Box3Helper
/*var box3 = new THREE.Box3().setFromObject(points);
var box3Helper = new THREE.Box3Helper(box3, 0x404040);
scene.add(box3Helper);*/
var clock = new THREE.Clock();
var time = 0;
render();
function resize(renderer) {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
function render() {
if (resize(renderer)) {
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
time += clock.getDelta();
points.material.uniforms.time.value = time;
renderer.render(scene, camera);
requestAnimationFrame(render);
}
Also see: Tab Triggers