Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
	</body>
</html>
              
            
!

CSS

              
                body {
  overflow: hidden;
  margin: 0;
}

canvas {
  width: 100%;
  height: 100%;
}

              
            
!

JS

              
                function createDemo() {
  const sphere = new THREE.Mesh(
    new THREE.SphereGeometry(2.5, 32, 32),
    new THREE.MeshLambertMaterial({
      color: 0x0000ff
    })
  );
  sphere.castShadow = true;
  sphere.receiveShadow = true;
  sphere.rotation.x = Math.PI / 2;

  const rings = createRings();

  const demo = new Demo();
  demo.add(sphere);
  demo.add(rings);
  demo.add(new THREE.AxesHelper(5));
  demo.createLight(-5, 5, 1);
  demo.start();
}

function createRings() {
  const texture = new THREE.TextureLoader().load(
    "https://i.postimg.cc/zz7Gr430/saturn-rings-top.png"
  );

  const uniforms = THREE.UniformsUtils.merge([
    THREE.UniformsLib.ambient,
    THREE.UniformsLib.lights,
    THREE.UniformsLib.shadowmap,
    {
      ringTexture: { value: null },
      innerRadius: { value: 0 },
      outerRadius: { value: 0 },
      lightPosition: { value: null }
    }
  ]);
  uniforms.ringTexture.value = texture;
  uniforms.innerRadius.value = 3;
  uniforms.outerRadius.value = 5;
  uniforms.lightPosition.value = new THREE.Vector3(-5, 5, 1);

  const material = new THREE.ShaderMaterial({
    uniforms,
    vertexShader: `
varying vec3 vPos;                                                     
varying vec3 vWorldPosition;                                           
varying vec3 vNormal;                                                 

${THREE.ShaderChunk["shadowmap_pars_vertex"]}                         

void main() {                                                         
  vPos = position;                                                     
  //vec4 worldPosition = (modelViewMatrix * vec4(position, 1.));         
  vec4 worldPosition = modelMatrix * vec4(position, 1.0);

  gl_Position = projectionMatrix * viewMatrix * vec4(worldPosition.xyz, 1.);       

  vNormal = normalMatrix * normal;                                     
  vWorldPosition = worldPosition.xyz;                                 

  ${THREE.ShaderChunk["shadowmap_vertex"]}
}   
    `,
    fragmentShader: `
uniform sampler2D ringTexture;
uniform float innerRadius;
uniform float outerRadius;
uniform vec3 lightPosition;

varying vec3 vNormal;
varying vec3 vPos;
varying vec3 vWorldPosition;

${THREE.ShaderChunk["common"]}
${THREE.ShaderChunk["packing"]}
${THREE.ShaderChunk["bsdfs"]}
${THREE.ShaderChunk["lights_pars_begin"]}
${THREE.ShaderChunk["shadowmap_pars_fragment"]}
${THREE.ShaderChunk["shadowmask_pars_fragment"]}

vec4 color() {
  vec2 uv = vec2(0);
  uv.x = (length(vPos) - innerRadius) / (outerRadius - innerRadius);
  if (uv.x < 0.0 || uv.x > 1.0) {
    discard;
  }                                                                           
  vec4 pixel = texture2D(ringTexture, uv);
  return pixel;
}

void main() {
  gl_FragColor = color() * vec4(vec3(getShadowMask()), 1.0);
}
    `,
    transparent: true,
    lights: true,
    side: THREE.DoubleSide,
  });

  const geometry = new THREE.RingGeometry(3, 5, 64);
  const mesh = new THREE.Mesh(geometry, material);
  mesh.castShadow = true;
  mesh.receiveShadow = true;
  return mesh;
}

class Demo {
  constructor() {
    this._scene = new THREE.Scene();
    this._scene.background = new THREE.Color(0x1a1a1a);

    const renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.shadowMap.enabled = true;
    renderer.setSize(window.innerWidth, window.innerHeight);
    this._renderer = renderer;

    const camera = new THREE.PerspectiveCamera(
      75,
      window.innerWidth / window.innerHeight,
      0.1,
      1000
    );
    camera.position.set(10, 10, 10);
    camera.lookAt(0, 0, 0);
    this._camera = camera;
    
    this._controls = new THREE.OrbitControls(camera, renderer.domElement);

    this._group = new THREE.Group();
    this._scene.add(this._group);

    document.body.appendChild(renderer.domElement);

    this._animate = this._animate.bind(this);
  }

  createLight(x, y, z) {
    const spotLight = new THREE.SpotLight(0xffffff);
    spotLight.castShadow = true;
    spotLight.position.set(x, y, z);
    spotLight.shadow.bias = -5 * 0.001;

    const lightPole = new THREE.Object3D();
    lightPole.add(spotLight);

    this._scene.add(lightPole);
    //this._scene.add(new THREE.SpotLightHelper(spotLight));
    this._scene.add(new THREE.CameraHelper(spotLight.shadow.camera));

    this._light = lightPole;
  }

  add(obj) {
    this._group.add(obj);
  }

  start() {
    this._animate();
  }

  _animate() {
    this._controls.update();
    this._renderer.render(this._scene, this._camera);
    requestAnimationFrame(this._animate);
  }
}

createDemo();

              
            
!
999px

Console