0.1.3 • Published 18 days ago

@playcanvas/web-components v0.1.3

Weekly downloads
-
License
MIT
Repository
-
Last release
18 days ago

PlayCanvas Web Components

Unleash the power of PlayCanvas in your HTML. PlayCanvas Web Components allows you to write PlayCanvas scenes using only HTML, enabling a clear and concise structure, and making it easier than ever to integrate with other web technologies. Just add tags for entities, components, and assets, and watch your 3D scene come to life!

Usage 🚧

If you are using a bundler (e.g. Rollup), you can install PlayCanvas Web Components and the PlayCanvas Engine using NPM:

npm install playcanvas @playcanvas/web-components --save-dev

Or you can include it directly in your HTML file from a CDN.

ES Modules:

<script type="module" src="https://cdn.jsdelivr.net/npm/@playcanvas/web-components@0.1.0/dist/pwc.mjs"></script>

UMD:

<script src="https://cdn.jsdelivr.net/npm/@playcanvas/web-components@0.1.0"></script>

Tag Reference 📖

Table of contents:

pc-app

The pc-app tag is the root element for your PlayCanvas application. It is used to initialize the PlayCanvas application and provide a container for your scene.

AttributeDescription
high-resolutionValueless attribute. If present, enables high-resolution mode.

pc-asset

The pc-asset tag is used to define an asset. It must be a direct child of pc-app.

AttributeDescription
idThe ID of the asset. This is used to reference the asset in scripts.
srcThe path to the asset.
preloadValueless attribute. If present, the asset is loaded immediately.
typeThe type of asset. If not specified, the type is inferred from the file extension. Can be: audio, binary, css, container, gsplat, html, json, script, shader, text, texture.

pc-camera

The pc-camera tag is used to define a camera component. It must be a direct child of an pc-entity.

AttributeDescription
clear-colorThe background color of the camera. Can be a comma-separated list of R, G, B, and A values or a hex color code. If unspecified, 0.75,0.75,0.75,1 is used.
clear-color-bufferBoolean attribute. Controls whether the camera clears the color buffer. If unspecified, the color buffer is cleared.
clear-depth-bufferBoolean attribute. Controls whether the camera clears the depth buffer. If unspecified, the depth buffer is cleared.
clear-stencil-bufferBoolean attribute. Controls whether the camera clears the stencil buffer. If unspecified, the stencil buffer is cleared.
cull-facesBoolean attribute. Controls whether the camera culls faces. If unspecified, faces are culled.
far-clipThe far clipping plane of the camera. If unspecified, 1000 is used.
flip-facesBoolean attribute. Controls whether the camera flips faces. If unspecified, faces are not flipped.
fovThe field of view of the camera. If unspecified, 45 is used.
frustum-cullingBoolean attribute. Controls whether the camera uses frustum culling. If unspecified, frustum culling is used.
near-clipThe near clipping plane of the camera. If unspecified, 0.1 is used.
orthographicValueless attribute. If present, the camera uses an orthographic projection. If unspecified, the camera uses a perspective projection.
ortho-heightThe height of the orthographic projection. If unspecified, 10 is used.
priorityThe priority of the camera. If unspecified, 0 is used.
rectThe viewport rectangle of the camera. Specified as a comma-separated list of X, Y, Width, and Height values. If unspecified, 0,0,1,1 is used.
scissor-rectThe scissor rectangle of the camera. Specified as a comma-separated list of X, Y, Width, and Height values. If not specified, 0,0,1,1 is used.

pc-collision

The pc-collision tag is used to define a collision component. It must be a direct child of a pc-entity.

AttributeDescription
axisThe axis of the collision component. If not specified, 1 is used (Y-axis).
convex-hullValueless attribute. If present, the collision component uses a convex hull.
enabledEnabled state of the collision component. If not specified, true is used.
half-extentsThe half-extents of the collision component. Specified as a comma-separated list of X, Y, and Z values. If not specified, 0.5,0.5,0.5 is used.
heightThe height of the collision component. If not specified, 2 is used.
radiusThe radius of the collision component. If not specified, 0.5 is used.
typeThe type of collision component. Can be box, capsule, cone, cylinder or sphere.

pc-element

The pc-element tag is used to define an element component. It must be a direct child of a pc-entity.

AttributeDescription
anchorThe anchor of the element component. Specified as a comma-separated list of X, Y, Z, and W values. If not specified, 0,0,0,1 is used.
assetA string that should match the id of a pc-asset tag that has a type of font.
auto-widthValueless attribute. If present, the element component automatically adjusts its width.
colorThe color of the element component. Can be a comma-separated list of R, G, B, and A values or a hex color code. If not specified, 1,1,1,1 is used.
font-sizeThe font size of the element component. If not specified, 16 is used.
line-heightThe line height of the element component. If not specified, 1.2 is used.
pivotThe pivot of the element component. Specified as a comma-separated list of X and Y values. If not specified, 0.5,0.5 is used.
textThe text of the element component.
typeThe type of element component. Can be group, image or text. If not specified, group is used.
widthThe width of the element component. If not specified, 0 is used.
wrap-linesValueless attribute. If present, the element component wraps lines.

pc-entity

The pc-entity tag is used to define an entity. It must be a direct child of pc-scene or another pc-entity.

AttributeDescription
enabledEnabled state of the entity. If not specified, true is used.
nameThe name of the entity.
positionThe position of the entity. Specified as a comma-separated list of X, Y, and Z values. If not specified, 0,0,0 is used.
rotationThe rotation of the entity. Specified as a comma-separated list of X, Y, and Z Euler angles in degrees. If not specified, 0,0,0 is used.
scaleThe scale of the entity. Specified as a comma-separated list of X, Y, and Z values. If not specified, 1,1,1 is used.
tagsA comma-separated list of tags for the entity.

pc-light

The pc-light tag is used to define a light component. It must be a direct child of a pc-entity.

AttributeDescription
colorThe color of the light. Can be a comma-separated list of R, G, B values or a hex color code. If not specified, 1,1,1 is used.
cast-shadowsValueless attribute. If present, the light casts shadows.
inner-cone-angleThe angle of the light's inner cone. If not specified, 40 is used.
intensityThe intensity of the light. If not specified, 1 is used.
normal-offset-biasThe bias of the light's normal offset. If not specified, 0.05 is used.
outer-cone-angleThe angle of the light's outer cone. If not specified, 45 is used.
rangeThe range of the light. If not specified, 10 is used.
shadow-biasThe bias of the light's shadows. If not specified, 0.2 is used.
shadow-distanceThe distance at which the light's shadows are no longer rendered. If not specified, 16 is used.
typeThe type of light. Can be directional, point or omni.

pc-listener

The pc-listener tag is used to define a listener component. It must be a direct child of a pc-entity. It has no attributes.

pc-module

The pc-module tag is used to define a WASM module. It must be a direct child of pc-app.

AttributeDescription
nameThe name of the module. This is used to reference the module in scripts.
glueThe path to the glue code for the module.
wasmThe path to the WASM file for the module.
fallbackThe path to the fallback (asm.js) code for the module.

pc-render

The pc-render tag is used to define a render component that render a 3D primitive. It must be a direct child of a pc-entity.

AttributeDescription
typeThe type of render component. Can be box, capsule, cone, cylinder, plane or sphere.
cast-shadowsValueless attribute. If present, the render component casts shadows.
receive-shadowsValueless attribute. If present, the render component receives shadows.

pc-rigidbody

The pc-rigidbody tag is used to define a rigidbody component. It must be a direct child of a pc-entity.

AttributeDescription
angular-dampingThe angular damping of the rigidbody. If not specified, 0 is used.
angular-factorThe angular factor of the rigidbody. Specified as a comma-separated list of X, Y, and Z values. If not specified, 1,1,1 is used.
frictionThe friction of the rigidbody. If not specified, 0.5 is used.
linear-dampingThe linear damping of the rigidbody. If not specified, 0 is used.
linear-factorThe linear factor of the rigidbody. Specified as a comma-separated list of X, Y, and Z values. If not specified, 1,1,1 is used.
massThe mass of the rigidbody. If not specified, 1 is used.
restitutionThe restitution of the rigidbody. If not specified, 0 is used.
rolling-frictionThe rolling friction of the rigidbody. If not specified, 0 is used.
typeThe type of rigidbody component. Can be static, kinematic or dynamic.

pc-scene

The pc-scene tag is used to define the scene. It must be a direct child of pc-app.

AttributeDescription
fogThe type of fog to use. Can be linear, exp, or exp2.
fog-colorThe color of the fog. Can be a CSS color string or a hex color code.
fog-startThe start distance of the fog.
fog-endThe end distance of the fog.
fog-densityThe density of the fog.

pc-screen

The pc-screen tag is used to define a screen component. It must be a direct child of a pc-entity.

AttributeDescription
blendValueless attribute. If present, the screen component blends.
priorityThe priority of the screen component. Must be an integer between 0 and 255. If not specified, 0 is used.
reference-resolutionThe reference resolution of the screen component. Specified as a comma-separated list of Width and Height values. If not specified, 640,320 is used.
resolutionThe resolution of the screen component. Specified as a comma-separated list of Width and Height values. If not specified, 640,320 is used.
scale-blendThe scale blend of the screen component. Must be a number between 0 and 1. If not specified, 0.5 is used.
screen-spaceValueless attribute. If present, the screen component is in screen space.

pc-script

The pc-script tag is used to define a script. It must be a direct child of a pc-scripts component.

AttributeDescription
attributesA JSON string of attributes for the script.
enabledEnabled state of the script. If not specified, true is used.
nameThe name of the script.

pc-scripts

The pc-scripts tag is used to define a scripts component. It must be a direct child of a pc-entity.

AttributeDescription
enabledEnabled state of the scripts component. If not specified, true is used.

pc-sky

The pc-sky tag is used to define a sky component. It must be a direct child of a pc-scene.

AttributeDescription
assetA string that should match the id of a pc-asset tag that has a type of texture.
intensityThe intensity of the sky. If not specified, 1 is used.
levelThe mipmap level used to render the sky. If not specified, 0 is used (base mip level).
rotationThe rotation of the sky. Specified as a comma-separated list of X, Y, and Z values. If not specified, 0,0,0 is used.
solid-colorValueless attribute. If present, the skybox itself is not rendered but is still used for environment lighting.

pc-sound

The pc-sound tag is used to define a sound. It must be a direct child of a pc-sounds.

AttributeDescription
assetA string that should match the id of a pc-asset tag that has a type of audio.
auto-playValueless attribute. If present, the sound slot plays automatically.
durationThe duration of the sound slot.
loopValueless attribute. If present, the sound slot loops.
nameThe name of the sound slot.
overlapValueless attribute. If present, the sound slot overlaps.
pitchThe pitch of the sound slot. If not specified, 1 is used.
start-timeThe start time of the sound slot. If not specified, 0 is used.
volumeThe volume of the sound slot. If not specified, 1 is used.

pc-sounds

The pc-sounds tag is used to define a sound component. It must be a direct child of a pc-entity.

AttributeDescription
pitchThe pitch of the sound. If not specified, 1 is used.
positionalValueless attribute. If present, the sound is positional.
volumeThe volume of the sound. If not specified, 1 is used.

pc-splat

The pc-splat tag is used to define a splat component. It must be a direct child of a pc-entity.

AttributeDescription
assetA string that should match the id of a pc-asset tag that has a type of gsplat.
enabledEnabled state of the splat component. If not specified, true is used.

Example

Below is a basic example of how to use PlayCanvas Web Components to create a simple 3D scene (a spinning cube):

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>PlayCanvas Web Components - Spinning Cube</title>
        <script type="importmap">
            {
                "imports": {
                    "playcanvas": "https://esm.run/playcanvas@2.1.0"
                }
            }
        </script>
        <script type="module" src="https://cdn.jsdelivr.net/npm/@playcanvas/web-components@0.1.0/dist/pwc.mjs"></script>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <pc-app>
            <script type="module">
                import { registerScript, Script } from 'playcanvas';

                class Rotate extends Script {
                    update(dt) {
                        this.entity.rotate(10 * dt, 20 * dt, 30 * dt);
                    }
                }

                registerScript(Rotate, 'rotate');
            </script>
            <pc-scene>
                <!-- Camera -->
                <pc-entity name="camera" position="0,0,3">
                    <pc-camera clear-color="#8099e6"></pc-camera>
                </pc-entity>
                <!-- Light -->
                <pc-entity name="light" rotation="45,0,0">
                    <pc-light></pc-light>
                </pc-entity>
                <!-- Cube -->
                <pc-entity name="cube">
                    <pc-render type="box"></pc-render>
                    <pc-scripts>
                        <pc-script name="rotate"></pc-script>
                    </pc-scripts>
                </pc-entity>
            </pc-scene>
        </pc-app>
    </body>
</html>