1.0.1 • Published 4 years ago

aframe-shadow-casting v1.0.1

Weekly downloads
4
License
ISC
Repository
github
Last release
4 years ago

aframe-shadow-casting

AFRAME components & primitives for real-time shadow casting on a transparent ground plane using custom lighting components and THREE.js ShadowMaterial

Inspiration

I was working on a project using AFRAME and 8th Wall to create a mobile webAR experience, and wanted to add real-time shadows to my scene. AFRAME documentation includes a section on real-time shadows, but that method only works if the receiving plane is opaque, which isn't ideal for AR applications. So I wrote this.

It includes two components - shadow-light and shadow-plane - and two primitives - <a-shadow-light> and <a-shadow-plane> - for use in your AFRAME and/or 8th Wall projects.

Installation

Working on it - for now, just copy and paste.

How to Use

Once installed in your project, you can either add the custom element tags directly or use the components on other AFRAME entities:

<a-shadow-light
    id="spot-light-primitive"
    type="spot"
    mapsize="2048 2048"
    far="20">
</a-shadow-light>
<a-entity
  id="directional-light-entity"
  shadow-light="type: directional; intensity: 0.8; helper: true">
</a-entity>
    
<a-shadow-plane
    id="shadow-plane-primitive
    dimensions="20 20">
</a-shadow-plane>
<a-entity
  id="shadow-plane-entity"
  shadow-plane="opacity: 0.4">
</a-entity>

The shadow-light component creates a directional or spot light in the scene that casts shadows with a configurable shadow camera. The shadow-plane component creates a ground plane that receives shadows. Make sure you add the shadow component to any objects in your scene that you want to cast shadows:

<a-entity gltf-model="#myModel" shadow="receive: false"></a-entity>

Configuration

shadow-light

Propertytypedefaultconstraintsdescription
typestringdirectional"directional" or "spot"Determines the type of THREE.js light that is added to the scene
intensityfloat0.5> 0Intensity of light
colorcolor'white'a valid THREE ColorColor of light
targetvec3{ x: 0, y: 0, z: 0}Light will rotate to face target point
nearfloat1> 0'near' property of shadow camera
farfloat5> near'far' property of shadow camera
tlrbvec4{ x: 5, y: -5, z: 5, w: -5 }'top', 'left', 'right', 'bottom' properties of shadow camera
mapSizevec2{ x: 1024, y: 1024 }powers of 2determines resolution of shadow (width, height) - higher resolution means smoother shadows at the cost of computing time
biasfloat-0.000222see THREE documentation
helperbooleanfalseadds/removes a visualizer to help debug / configure the shadow camera

You also have the ability to access the light directly through its element using the shadowLight property. This can be helpful for setting other attributes of the light that aren't included in the component - like the angle of a spotlight

var spotlight = document.querySelector('a-shadow-light');
spotlight.shadowLight.angle = Math.PI/2;

You can find all properties of Directional and Spot Lights in the THREEjs documentation.

shadow-plane

Propertytypedefaultconstraintsdescription
dimensionsvec2{ x: 10, y: 10 }> 0Defines dimensions (width, height) of ground plane
opacityfloat0.20-1Opacity of shadow

Example

In this repo, you'll find an example webpage (creatively named 'example.html') that you can reference. It creates a simple scene with a levitating ball casting a shadow on the ground.

example gif

It's not all that impressive considering you can't tell if the shadow-plane is actually transparent or not, so if you'd like to see an AR application, you can check out a video of an 8th Wall WebAR experience I created here

Enjoy