Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Last active December 14, 2015 03:29
Show Gist options
  • Select an option

  • Save zeroeth/5021317 to your computer and use it in GitHub Desktop.

Select an option

Save zeroeth/5021317 to your computer and use it in GitHub Desktop.
A CodePen by Kevin Alford. Three.js Shader Fun - Having more fun with Three.js <3
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/three.js/r54/three.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/sole/tween.js/master/src/Tween.js"></script>
</head>
<body>
<!-- Shaders -->
<script type="x-shader/x-vertex" id="vertex_shader">
// switch on high precision floats
#ifdef GL_ES
precision highp float;
#endif
varying vec3 vNormal;
void main()
{
vNormal = normal;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}
</script>
<script type="x-shader/x-fragment" id="fragment_shader">
#ifdef GL_ES
precision highp float;
#endif
uniform float time;
varying vec3 vNormal;
void main()
{
// calc the dot product and clamp
// 0 -> 1 rather than -1 -> 1
vec3 light = vec3(0.5,0.2,1.0);
// ensure it's normalized
light = normalize(light);
// calculate the dot product of
// the light to the vertex normal
float dProd = max(0.0, dot(vNormal, light));
float scaler = 3.2;
// interesting scales: 3.2, 1.0
gl_FragColor = vec4(
sin(gl_FragCoord.x*gl_FragCoord.x*scaler + time)*dProd,
sin(gl_FragCoord.y*gl_FragCoord.x*scaler + time)*dProd,
sin(gl_FragCoord.y*gl_FragCoord.x*scaler + time)*dProd,
1.0);
}
</script>
<!-- End Shaders -->
<div id='container'></div>
</body>
</html>
$(document).ready(function () {
// Dimension Settings
var width = document.width, height = document.width*0.5625;
var angle = 45, aspect = width/height, near = 0.1, far = 10000;
// Uniforms
var uniforms = {
time: {
type: 'f', // a float
value: 0
}
};
// World Objects
var renderer = new THREE.WebGLRenderer();
var camera = new THREE.PerspectiveCamera(angle, aspect, near, far);
var scene = new THREE.Scene();
var light = new THREE.SpotLight(0xFFFFFF, 1);
var vertex_shader = $('#vertex_shader');
var fragment_shader = $('#fragment_shader');
var material = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: vertex_shader.text(),
fragmentShader: fragment_shader.text()
});
var radius = 20, segments = 32, rings = 32;
var geometry = new THREE.SphereGeometry(radius, segments, rings);
var sphere = new THREE.Mesh(geometry, material);
// Positions
light.position.x = 0;
light.position.y = 100;
light.position.z = 0;
light.lookAt(sphere.position);
camera.position.z = 40;
camera.position.y = 40;
camera.lookAt(sphere.position);
// Build World
scene.add(camera);
scene.add(light);
scene.add(sphere);
// Shadow
renderer.shadowMapEnabled = true;
light.castShadow = true;
light.shadowCameraNear = 1.0;
light.shadowDarkness = 0.5;
sphere.castShadow = true;
sphere.receiveShadow = true;
// GUI
//var gui = new dat.GUI();
//gui.add(camera.position, 'x').min(-50).max(50).step(0.01);
//gui.add(camera.position, 'y').min(-50).max(50).step(0.01);
//gui.add(camera.position, 'z').min(-50).max(50).step(0.01);
// deal with resizes
// Render
$('#container').append(renderer.domElement);
renderer.setSize(width, height);
// Update Method
var update = function() {
uniforms.time.value += 0.1
};
// Browser Animation Loop
var animate = function() {
update();
renderer.render(scene, camera);
requestAnimationFrame( animate );
};
animate();
});
body
{
background-color: #555753;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment