Video texture incorporated into Three.js

I'm currently experimenting with using a specific section of a video as a texture on a Three.js mesh.

The video in question can be found at this link: . It features a fisheye lens, and I am only interested in incorporating the central circular portion into my mesh while ignoring the surrounding black areas.

My goal is to find a way to mask, crop, or reposition the video on the mesh so that only the desired content is displayed, excluding the extraneous parts.

Incorporated within the video code, I create a new 'video' element and apply settings such as loop, crossOrigin, and preload before linking it to the source file. Upon playback initiation, a VideoTexture based on this video is created with specified filter and format properties. This texture is then utilized in generating a MeshBasicMaterial for visualization on the mesh.

To enhance the immersive VR experience, the video is projected onto a 220-degree sphere shape via specified geometry parameters.

For reference and further exploration, a demo showcasing the implementation can be accessed through this CodePen link: http://codepen.io/bknill/pen/vXBWGv.

If anyone has suggestions or insights on optimizing this process, I would greatly appreciate any guidance provided.

Answer №1

Utilize the texture.repeat method to adjust the size of the texture

For instance, to double the scale on both axes, you can use: texture.repeat.set(0.5, 0.5);

Answer №2

Simply put, updating the UV-Map of the sphere is essential to ensure that the appropriate area of your texture is assigned to the corresponding vertices of the sphere.

Each vertex's UV-coordinates determine the specific coordinates within the texture that are linked to that vertex (within a range of [0..1], with coordinates (0, 0) representing the top left corner and (1,1) denoting the bottom right corner of your video). Check out this example for a better understanding.

These UV-coordinates can be found in your geometry as geometry.faceVertexUvs[0], where each vertex of every face possesses a THREE.Vector2 value for the UV-coordinate. It's structured as a two-dimensional array, with the first index referring to the face-index and the second one indicating the vertex-index for the face (as shown in the example).

There are at least two methods for generating the UV-map. The simpler approach would involve creating the UV-map using 3D editing tools like blender and then exporting the object using the three.js exporter-plugin.

The alternative method is manually computing the values. Initially, you could try utilizing an orthographic projection of the sphere. Essentially, if you have a unit-sphere centered at the origin, you can disregard the z-coordinate of the vertices and utilize u = x/2 + 0.5 and v = y/2 + 0.5 as UV-coordinates.

In JavaScript, this might look something like:

// create the geometry
const geometry = new THREE.SphereGeometry(1, 18, 18, Math.PI, Math.PI)
const uvs = geometry.faceVertexUvs[0];
const vertices = geometry.vertices;

// calculate UV from sphere vertices
for(let i = 0; i<geometry.faces.length; i++) {
  const face = geometry.faces[i];
  const faceVertices = [vertices[face.a], vertices[face.b], vertices[face.c]];

  for(let j = 0; j<3; j++) {
    const vertex = faceVertices[j];
    uvs[i][j].set(vertex.x/2 + 0.5, vertex.y/2 + 0.5);
  }
}
geometry.uvsNeedUpdate = true;

(Feel free to ask for more details in either direction, and I'll provide further explanations.)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it necessary to specify a data type for the response when making an AJAX POST request?

After carefully reviewing my code, I have ensured that all the variables in my JavaScript match and are properly set up. Additionally, I have confirmed that all the variables in my data array for my AJAX request correspond correctly to my $_POST['var& ...

Accessing a key from an AJAX JSON response and applying it within a .each() loop in jQuery

I'm struggling with a seemingly simple task that I just can't seem to get right. My goal is to convert multiple text inputs into select fields using AJAX and jQuery. Everything works smoothly, except when trying to make the $.each function dynam ...

Error: Authentication Error - Headers have already been sent to the client and cannot be modified

I am currently working on handling authentication errors for my website. However, when I submit incorrect data, I encounter the following error: node:internal/errors:478 ErrorCaptureStackTrace(err); ^ Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers aft ...

Exploring methods to trace the factory's property that is receiving updates from another factory within AngularJS

Hey there, I'm new to Angularjs and I have a bunch of factories in my application. The situation is, let's say we have obj1 in factoryA. Whenever I console.log(obj1), it displays numerous properties associated with it. This object is being update ...

Can the functionality of a button be disabled after being clicked a total of 5 times?

Once a user clicks the button five times, I plan for it to be disabled. Can this be achieved solely with HTML, or would JavaScript be necessary? ...

Accessing the react-bootstrap tab using the history feature in React

Currently, I am in the process of developing a multi-form page that includes login, registration, and lost password functionalities displayed using tabs with react-bootstrap. The technologies I am using for this project are react 17.0.2, react-bootstrap 2 ...

How can I eliminate the default background of Mui tooltip and personalize it in react?

Below is an example of how to customize a nested tooltip within a default background tooltip in MUI. The challenge here is to remove the grey border in the customized tooltip and have only a white background with black text. Check out the code snippet be ...

Ways to efficiently verify if a URL is valid by checking if it loads a page with content

INQUIRY: How can I verify if a URL is valid and loads a webpage successfully? Currently, my code only checks the status code, which means that a URL like will be considered valid even though it does not load any content. Is there a way to ensure that t ...

Utilizing the power of THREE.ShaderLib.phong while integrating subsurface scattering within ThreeJS

My mesh utilizes a ShaderMaterial with THREE.ShaderLib.phong uniforms. I have successfully linked the map, bump, and specular maps textures. The code snippet below demonstrates this: defines = {}; defines["USE_MAP"] = ""; defines["USE_BUMPMAP"] = ""; defi ...

Using a for loop in JavaScript to dynamically display TextBoxFor(model=> model[i].prop) in an MVC application

I need some help getting this code to function correctly: $('#ddl').change(function () { $('#cont').html(''); var count = getCount($('#ddl').val()) for (var i = 0; i < count ; ...

Begin your meteor project with a remote MongoDB server on a Windows operating system

Currently tackling a project that requires me to integrate my meteor project with a remote MongoDB server on Windows. I successfully set the environment variable (MONGO_URL="DB LINK") from OSX using terminal commands, but I'm encountering difficulties ...

Deciphering the Syntax of React Functional Components

Seeking guidance as a ReactJS novice working through the react docs. I've hit a snag trying to understand and implement an example provided in the documentation. Can anyone please lend a hand in helping me spot my mistake?https://i.sstatic.net/DwlXp.p ...

Tips on how to automatically rearrange a jQuery UI sortable list

Currently, I am working with 2 jQuery UI sortable lists which can be found at http://jqueryui.com/demos/sortable/#connect-lists. The process involves dragging items from list A (catalog) to list B (basket). I have a specific requirement where I need the ...

JavaScript causing Axios network error

Recently, I've started exploring the combination of axios and stripe in my project but unfortunately, I have encountered some challenges. Whenever I attempt to initiate a post request using axios, an error pops up which looks like this: https://i.sta ...

Ensure that a function completes before moving on in JavaScript

I am attempting to modify the save method so that it waits for this.collection.create() to complete before running, in order to prevent a potential crash. class UserRepository extends BaseRepository<User> { constructor() { super(); ...

Avoid reloading the header component along with its APIs when navigating routes in React JS

I'm currently working on a web application using react js/next js and within it, I have various pages that make use of globally shared components like the Header and Footer. However, I am facing an issue where I want to prevent unnecessary re-renders ...

Retrieve JSON data using AngularJS

Can someone guide me on how to make a GET request to my API endpoint and handle the JSON response in my code? Sample Controller.js Code: oknok.controller('listagemController', function ($scope, $http) { $scope.init = function () { ...

Using onDoubleClick with MUI TextField: A Quick Guide

Whenever the user double clicks the input field, I would like to automatically select the text. I have created a function for this specific action: export const selectText = ( event: React.MouseEvent<HTMLInputElement | HTMLTextAreaElement, MouseEvent& ...

AngularJS - real-time validation using the $valid property

I have implemented AngularJS to handle form validation and submission. The submit button is configured as follows: <button type="submit" ng-disabled="btn" ng-click="submit(settings.$valid)" class="btn btn-primary"> Submit </button> The butt ...

You can definitely invoke a function within a React hook

This code snippet showcases a component utilizing Hooks in React Native import React, { useEffect, useState } from 'react'; import { StyleSheet, Text, View, TouchableOpacity, Animated } from 'react-native'; import CAStyles fro ...