Module @zcomponent/three-video-player - v1.1.7

@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.

  • 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

To use the VideoPlayer component in your Mattercraft project:

  1. Right-click on a Group node in your scene's Hierarchy.
  2. Navigate to the "Media" submenu.
  3. Select "VideoPlayer" to add it to your scene.

The VideoPlayer supports two primary video formats:

  • HLS (.m3u8): For adaptive streaming
  • MP4: For standard video playback

The VideoPlayer offers three transparency types:

  1. None: Standard video playback without transparency
  2. Chroma Key: Removes a specific color from the video, typically used for green screen effects
  3. Side-by-Side: Uses a side-by-side video format where one side contains the alpha channel
  • 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 automatically
  • volume: The playback volume (0 to 1)
  • muted: Whether the video is muted
  • loop: Whether the video should loop when it reaches the end

When using chroma key transparency:

  • similarity: How closely a color must match the key color to be made transparent
  • smoothness: The smoothness of the chroma key edges
  • spill: Controls color spill reduction
  • keyColor: 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 playback
  • pause(): Pauses playback
  • stop(): Stops playback and resets to the beginning
  • seek(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 ends
  • onPause: Fired when the video is paused
  • onPlay: Fired when video playback starts
  • onPlaying: Fired when the video starts playing after being paused or stopped for buffering
  • onWaiting: Fired when the video stops because it needs to buffer the next frame
  • onError: Fired when an error occurs during video loading or playback

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();
}
}