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.
To use the VideoPlayer component in your Mattercraft project:
The VideoPlayer supports two primary video formats:
The VideoPlayer offers three transparency types:
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 endWhen 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])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)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)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 playbackHere'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();
}
}