-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 107 use video texture #167
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ca7a73
feat: add use video texture, draft
JaimeTorrealba f9fb681
Merge branch 'main' into feature/107-use-video-texture
JaimeTorrealba dded3cf
chore: update types
JaimeTorrealba ce42a56
chore: fix types, clean the code
JaimeTorrealba db15e64
docs: add docs for useVideoTexture
JaimeTorrealba db192a5
chore: add error handling
JaimeTorrealba adcc8dc
chore: use complete words, less cognitive load
JaimeTorrealba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# useVideoProgress | ||
|
||
A composable to easily use videos as textures in your meshes. | ||
|
||
This composable is based on the Drei [useVideoTexture](https://github.com/pmndrs/drei/tree/master#usevideotexture) | ||
|
||
## Usage | ||
|
||
```ts | ||
import { useVideoTexture } from '@tresjs/cientos' | ||
import exampleVideo from '/myVideoPath' | ||
|
||
const texture = ref() | ||
texture.value = await useVideoTexture(exampleVideo, { loop: false }) | ||
``` | ||
|
||
Then you can use the texture in your material, for example: | ||
|
||
```vue{3} | ||
... | ||
<Sphere :position="[0, 2, 0]"> | ||
<TresMeshBasicMaterial :map="texture" /> | ||
</Sphere> | ||
... | ||
``` | ||
|
||
## Props | ||
|
||
| Prop | Description | Default | | ||
| :------------ | :----------------------------------------------------------------------- | ---------------- | | ||
| `src` | Path to the video. | `undefined` | | ||
| `unsuspend` | Path to the model file. | `loadedmetadata` | | ||
| `crossOrigin` | Whether to use CORS to fetch the related video. | `Anonymous` | | ||
| `muted` | Whether to set the audio silenced. | true | | ||
| `loop` | Automatically seek back to the start upon reaching the end of the video. | true | | ||
| `start` | To play to the video once loaded or not. | true | | ||
| `playsInline` | To be play the video inline or not. | true | | ||
|
||
- Any other attribute for a `<video>` tag is accepted and will automatically set |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
import { useVideoTexture, OrbitControls } from '@tresjs/cientos' | ||
import { Sphere } from '../../../src/core' | ||
import { TresCanvas } from '@tresjs/core' | ||
import { SRGBColorSpace, NoToneMapping } from 'three' | ||
|
||
const gl = { | ||
clearColor: '#333', | ||
alpha: true, | ||
outputColorSpace: SRGBColorSpace, | ||
toneMapping: NoToneMapping, | ||
useLegacyLights: false, | ||
} | ||
|
||
const exampleVideo = 'https://raw.githubusercontent.com/Tresjs/assets/main/textures/video-textures/useVideoTexture.mp4' | ||
|
||
const texture = ref() | ||
|
||
texture.value = await useVideoTexture(exampleVideo, { loop: false}) | ||
|
||
</script> | ||
<template> | ||
<TresCanvas v-bind="gl" ref="canvas"> | ||
<TresPerspectiveCamera :position="[0, 2, 5]" /> | ||
<OrbitControls /> | ||
<Sphere :position="[0, 2, 0]"> | ||
<TresMeshBasicMaterial :map="texture" /> | ||
</Sphere> | ||
<TresGridHelper :size="10" :divisions="10" /> | ||
<TresAmbientLight :intensity="1" /> | ||
</TresCanvas> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<script setup lang="ts"></script> | ||
<template> | ||
<Suspense> | ||
<OrbitControlsDemo /> | ||
</Suspense> | ||
<Suspense> | ||
<UseVideoTextureDemo /> | ||
</Suspense> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,73 @@ | ||||||
import { VideoTexture } from 'three' | ||||||
import { useLogger } from '../../composables/useLogger' | ||||||
|
||||||
interface VideoTextureProps extends HTMLVideoElement { | ||||||
unsuspend?: 'canplay' | 'canplaythrough' | 'loadstart' | 'loadedmetadata' | ||||||
start?: boolean | ||||||
} | ||||||
|
||||||
/** | ||||||
* Composable for loading video textures. | ||||||
* | ||||||
* ```ts | ||||||
* import { ref } from 'vue' | ||||||
* import { useVideoTexture } from '@tresjs/cientos' | ||||||
* import MyVideo from 'MyVideo.mp4' | ||||||
* | ||||||
* const texture = ref() | ||||||
* texture.value = await useVideoTexture(MyVideo) | ||||||
* ``` | ||||||
* Then you can use the texture in your material. | ||||||
* | ||||||
* ```vue | ||||||
* <TresMeshBasicMaterial :map="texture" /> | ||||||
* ``` | ||||||
* @see https://threejs.org/docs/index.html?q=video#api/en/textures/VideoTexture | ||||||
* @export | ||||||
* @param {HTMLVideoElement} src | ||||||
* @return {VideoTexture} {VideoTexture} | ||||||
*/ | ||||||
export async function useVideoTexture(src: string | MediaStream, options?: Partial<VideoTextureProps>) { | ||||||
/** | ||||||
* Load a video as a texture. | ||||||
* | ||||||
* @param {src} string | ||||||
* @return {VideoTexture} {VideoTexture} | ||||||
*/ | ||||||
const { logError } = useLogger() | ||||||
if (!src) return logError('Error no path provided') | ||||||
|
||||||
const { unsuspend, start, crossOrigin, muted, loop, ...rest } = { | ||||||
unsuspend: 'loadedmetadata', | ||||||
crossOrigin: 'Anonymous', | ||||||
muted: true, | ||||||
loop: true, | ||||||
start: true, | ||||||
playsInline: true, | ||||||
...options, | ||||||
} | ||||||
|
||||||
function loadTexture(): Promise<VideoTexture> { | ||||||
return new Promise((res, rej) => { | ||||||
const video = Object.assign(document.createElement('video'), { | ||||||
src: (typeof src === 'string' && src) || undefined, | ||||||
crossOrigin, | ||||||
loop, | ||||||
JaimeTorrealba marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
muted, | ||||||
autoplay: true, | ||||||
...rest, | ||||||
}) | ||||||
const texture = new VideoTexture(video) | ||||||
video.addEventListener(unsuspend, () => res(texture)) | ||||||
video.addEventListener('error', () => rej()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
return texture | ||||||
}) | ||||||
} | ||||||
try { | ||||||
const texture = await loadTexture() | ||||||
if (start && texture.image) texture.image.play() | ||||||
return texture | ||||||
} catch { | ||||||
logError('Error loading resource') | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a piece of general advice, try to avoid using acronyms or shorter versions of words when naming variables and methods, is always more important readability vs saving what, couple of letters?
Less cognitive load when reading the code ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JaimeTorrealba please use
resolve
andreject
. Avoid shorting variable and method names