@zcomponent/three-video-player
v1.1.7
Published
HLS & MP4 Player for Mattercraft
Downloads
28
Readme
@zcomponent/three-video-player
This package adds support for video playback in 3D environments to the Mattercraft 3D content creation platform for the web. It allows you to easily integrate and control video content within your 3D scenes, supporting various video formats and transparency options.
Features
- Supports both HLS (.m3u8) and MP4 video formats
- Offers various video transparency types, including chroma key and side-by-side transparency
- Provides control over video playback properties like volume, looping, and muting
- Seamlessly integrates with the Mattercraft 3D environment
Getting Started
To use the VideoPlayer component in your Mattercraft project:
- Right-click on a Group node in your scene's Hierarchy.
- Navigate to the "Media" submenu.
- Select "VideoPlayer" to add it to your scene.
Video Formats
The VideoPlayer supports two primary video formats:
- HLS (.m3u8): For adaptive streaming
- MP4: For standard video playback
Transparency Options
The VideoPlayer offers three transparency types:
- None: Standard video playback without transparency
- Chroma Key: Removes a specific color from the video, typically used for green screen effects
- Side-by-Side: Uses a side-by-side video format where one side contains the alpha channel
Key Properties
source
: The URL or file path of the video (supports .m3u8 and .mp4 files)transparent
: The type of transparency to apply (none, chromaKey, or sideBySide)autoplay
: Whether the video should start playing automaticallyvolume
: The playback volume (0 to 1)muted
: Whether the video is mutedloop
: Whether the video should loop when it reaches the end
Chroma Key Properties
When using chroma key transparency:
similarity
: How closely a color must match the key color to be made transparentsmoothness
: The smoothness of the chroma key edgesspill
: Controls color spill reductionkeyColor
: The color to be made transparent (default is green: [0, 1, 0])
Side-by-Side Properties
When using side-by-side transparency:
direction
: The direction of the side-by-side split (LeftRight or TopBottom)alphaFirst
: Whether the alpha channel is on the left/top (true) or right/bottom (false)
Playback Control
The VideoPlayer provides several methods for controlling playback:
play()
: Starts or resumes playbackpause()
: Pauses playbackstop()
: Stops playback and resets to the beginningseek(time)
: Jumps to a specific time in the video (in milliseconds)
Events
The VideoPlayer emits several events that you can listen to:
onEnded
: Fired when the video playback endsonPause
: Fired when the video is pausedonPlay
: Fired when video playback startsonPlaying
: Fired when the video starts playing after being paused or stopped for bufferingonWaiting
: Fired when the video stops because it needs to buffer the next frameonError
: Fired when an error occurs during video loading or playback
Example Usage
Here's a simple example of how to use the VideoPlayer in a custom behavior:
import { Behavior, ContextManager } from '@zcomponent/core';
import { VideoPlayer } from '@zcomponent/three-video-player';
/**
* @zbehavior
*/
export class MyVideoBehavior extends Behavior<VideoPlayer> {
constructor(contextManager: ContextManager, instance: VideoPlayer) {
super(contextManager, instance);
console.log(this.instance.length())
// Listen for the video to end
this.register(this.instance.onEnded, () => {
console.log('Video playback ended');
});
}
}
Here's an example of dynamically creating a VideoPlayer component in a custom Three.js component:
import { Component, ContextManager, registerLoadable } from '@zcomponent/core';
import { Group } from '@zcomponent/three/lib/components/Group';
import { VideoPlayer, VideoTransparencyType } from '@zcomponent/three-video-player';
/**
* @zcomponent
*/
export class CustomVideoComponent extends Group {
private videoPlayer: VideoPlayer;
constructor(contextManager: ContextManager, props: {}) {
super(contextManager, {});
// Create a new VideoPlayer instance
const source = new URL('./path/to/your/video.mp4', import.meta.url).href;
this.videoPlayer = new VideoPlayer(contextManager, {
source,
transparent: VideoTransparencyType.none,
autoplay: false
});
// Add the VideoPlayer to our Group
this.appendChild(this.videoPlayer);
}
// Example method to start playback
playVideo() {
this.videoPlayer.play();
}
// Don't forget to clean up when the component is disposed
dispose() {
this.videoPlayer.dispose();
return super.dispose();
}
}