Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
	<head>
		<title>GLSL shaders</title>
	</head>
	<body>
		
	</body>
</html>
              
            
!

CSS

              
                body { margin: 0; }
canvas { width: 100%; height: 100% }
              
            
!

JS

              
                const vshader = `
#include <common>
#include <lights_pars_begin>

uniform float u_time;

varying vec3 vPosition;
varying mat4 vModelMatrix;
varying vec3 vWorldNormal;
varying vec3 vLightIntensity;


void main() {
  #include <simple_lambert_vertex>
  
  vPosition = position;
  float angleRotation = 3.1415*2.0 + u_time;
  float c = cos(angleRotation);
  float s = sin(angleRotation);
  mat4 rotation = mat4(c, 0.0, s, 0.0,
                       0, 1, 0.0, 0.0,
                       -s, 0.0, c, 0.0,
                       0.0, 0.0, 0.0, 1.0);
  vec3 rotatedPosition = (rotation * vec4(position, 1.0)).xyz;
  //vPosition = rotatedPosition.xyz;
  
  vLightIntensity = vLightFront + ambientLightColor;
  
  vWorldNormal = normalize((rotation * modelMatrix * vec4(normal, 0.0)).xyz);
  vModelMatrix = rotation * modelMatrix ;
  
  gl_Position = projectionMatrix * modelViewMatrix * vec4(rotatedPosition, 1.0);
}
`

const fshader = `
uniform float u_time;
uniform vec3 u_color;
uniform vec3 u_light_position;
uniform vec3 u_rim_color;
uniform float u_rim_strength;
uniform float u_rim_width;
uniform samplerCube u_envmap_cube;
uniform float u_envmap_strength;

varying vec3 vPosition;
varying vec3 vWorldNormal;
varying mat4 vModelMatrix;
varying vec3 vLightIntensity;

void main() {
  vec3 color = vec3(0.0);
  
  // use position x model matrix to get world coordinates
  vec3 worldPosition = (vModelMatrix * vec4(vPosition, 1.0)).xyz;
  
  // apply light in world coordinates
  vec3 lightVector = normalize(u_light_position - worldPosition);
  vec3 viewVector = normalize(cameraPosition - worldPosition);
  float rimndotv = max(0.0, u_rim_width - clamp(dot(vWorldNormal, viewVector), 0.0, 1.0));
  vec3 rimLight = rimndotv * u_rim_color * u_rim_strength;
  
  // env map
  vec3 reflection = reflect(-viewVector, vWorldNormal);
  vec3 envmapLight = textureCube(u_envmap_cube, reflection).rgb * u_envmap_strength;
  
  
  color = vLightIntensity * u_color + envmapLight + rimLight;
  
  
  gl_FragColor = vec4( color, 1.0 );
}

`


const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 60, window.innerWidth/window.innerHeight, 0.1, 1000 );

const light = new THREE.DirectionalLight(0xffda6f, 0.1);
light.position.set(0.0, 1.25, 1.25);
const ambientLight = new THREE.HemisphereLight(0x444444, 0x111111, 1);
scene.add(ambientLight);
scene.add(light);

const envCube = new THREE.CubeTextureLoader()
			.setPath( 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/2666677/' )
			.load( [
				'skybox2_px.jpg',
				'skybox2_nx.jpg',
				'skybox2_py.jpg',
				'skybox2_ny.jpg',
				'skybox2_pz.jpg',
				'skybox2_nz.jpg'
			] );

const renderer = new THREE.WebGLRenderer({antialias: true});
// for blend shape animation
let prevTime = Date.now();

renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

const clock = new THREE.Clock();
const {GLTFLoader} = THREE;

const uniforms = THREE.UniformsUtils.merge([
  THREE.UniformsLib[ "common" ],
  THREE.UniformsLib[ "lights" ]
]);

uniforms.u_color = { value: new THREE.Color(0xa6e4fa) };
uniforms.u_light_position = { value: light.position.clone() };
uniforms.u_rim_color = { value: new THREE.Color(0xffffff) };
uniforms.u_rim_strength = { value: 1.6 };
uniforms.u_rim_width = { value: 0.6 };
uniforms.u_time = { value: 0.0 };
uniforms.u_envmap_cube = { value: envCube };
uniforms.u_envmap_strength = { value: 0.7 };


//const geometry = new THREE.TorusGeometry( 1.5, 0.6, 16, 100 );
const geometry = new THREE.TorusKnotGeometry( 1, 0.1, 2000, 16, 0, 20 );
const material = new THREE.ShaderMaterial( {
  uniforms: uniforms,
  vertexShader: vshader,
  fragmentShader: fshader,
  wireframe: false,
  lights: true
} );
// const torus = new THREE.Mesh(geometry, material);
// scene.add(torus);

const loader = new THREE.GLTFLoader();
let mixer;
// for blend shape animation
let theta = 0;
loader.crossOrigin = true;
// loader.load('https://raw.githubusercontent.com/bb1100/3D-Models/main/fox.gltf', function(gltf) {
// Horse model from mrdoob three.js repo converted to gltf from glb
loader.load('https://raw.githubusercontent.com/bb1100/3D-Models/main/horse_running.gltf', function(gltf) {
   gltf.scene.traverse( function(child) {
     if(child instanceof THREE.Mesh) {
       child.material = material;
     };
   });

  const object = gltf.scene;
  object.position.set(0, -0.8, 0);
  object.scale.set(0.01, 0.01, 0.01);
  scene.add(object);
  
  mixer = new THREE.AnimationMixer( gltf.scene );
  // for baked animations:
  //  gltf.animations.forEach( ( clip ) => {   
  //     mixer.clipAction( clip ).play(); 
  //  });
  
  // supposedly for blend shape animation
  // mixer.clipAction(gltf.animations[0]).setDuration(1).play();
},
    function( xhr ){
        console.log( (xhr.loaded / xhr.total * 100) + "% loaded")
    },
    function( err ){
        console.error( "Error loading 'ship.obj'")
    }
);


//camera.position.set(0,0,5);
camera.position.z = 5;

const controls = new THREE.OrbitControls(camera, renderer.domElement);


function onWindowResize( event ) {
  camera.aspect = window.innerWidth/window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize( window.innerWidth, window.innerHeight );
}

function animate() {
  requestAnimationFrame( animate );
  render();
  let delta = clock.getDelta();
  uniforms.u_time.value += delta;
  // if(mixer) mixer.update(delta);
  
}

 function render() {
  theta += 0.1;
  if(mixer) {
    const time = Date.now();
    mixer.update((time - prevTime) * 0.001);
    prevTime = time;
 }
   renderer.render( scene, camera );
 }

onWindowResize();
if (!('ontouchstart' in window)) window.addEventListener( 'resize', onWindowResize, false );

animate();


              
            
!
999px

Console