threejs纹理加载三(视频加载)

threejs中除了能把图片作为纹理进行几何体贴图以外,还可以把视频作为纹理进行贴图设置。纹理的类型有很多,我们可以用不同的加载器来加载,而对于视频作为纹理,我们需要用到今天的主角:VideoTexture。我们先看效果:

我们直接看代码:

复制代码
<template>
    <div>
    </div>
</template>
<script  setup>
import { ref } from "vue";

import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import * as Dat from "dat.gui";
const gui = new Dat.GUI();
const scene = new THREE.Scene();
const camara = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);
camara.position.set(0, 0, 10);

const Gemertry = new THREE.BoxGeometry(5, 5, 5);

//视频加载器
let video = document.createElement("video");
video.src = "/src/assets/819.mp4";
video.load();
video.crossOrigin = "anonymous";

document.addEventListener("click", () => {
  video
    .play()
    .then(() => {
      render();
    })
    .catch(err => {
      console.log("err:", err);
    });
});
// video.play();
let texture = new THREE.VideoTexture(video);
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const materials = [
  new THREE.MeshBasicMaterial({ color: "#f90" }),
  new THREE.MeshBasicMaterial({ map: texture }),
  new THREE.MeshBasicMaterial({ color: "#63a" }),
  new THREE.MeshBasicMaterial({ color: "#e2d" }),
  new THREE.MeshBasicMaterial({ color: "#c57" }),
  new THREE.MeshBasicMaterial({ color: "#f00" })
];

const cube = new THREE.Mesh(Gemertry, materials);
scene.add(cube);

// 将网格对象添加到场景中

const renderer = new THREE.WebGLRenderer();
document.body.appendChild(renderer.domElement);
renderer.setSize(window.innerWidth, window.innerHeight);

const control = new OrbitControls(camara, renderer.domElement);

const render = () => {
  renderer.render(scene, camara);
  requestAnimationFrame(render);
  if (video.readyState === video.HAVE_ENOUGH_DATA) {
    texture.needsUpdate = true;
  }
};
render();
</script>
<style scoped>
</style>

这里特别要注意:对于视频而言。很多浏览器禁止了默认播放,所以这里我们通过给文档对象添加点击事件来实现,然后在加载成功的回调中我们要重新调用我们的render()函数。

相关推荐
IT空门:门主10 分钟前
Spring AI 2.1.0 更新了什么:从 2.0 升上来的完整梳理
人工智能·python·spring
AI创界者31 分钟前
Python 进阶实战(七十章):遗留系统重构、渐进式绞杀与技术债治理实战
人工智能·python·aigc
ZhengEnCi1 小时前
电脑操作 Agent 一直点不中:截图没更新时,提示词该怎么改才能停止瞎试
人工智能·python·agent
甲维斯1 小时前
使用 GPT6.2+UE5 做3D游戏的第一天!
人工智能
字节跳动视频云技术团队2 小时前
不用自建后端,前端页面也能接入云端数据了:Pages Data SDK 正式上线
人工智能·敏捷开发
苏三的开发日记2 小时前
5 个 UI Skill 选型对照表:从需求梳理到方案汇报
人工智能
zhangfeng11333 小时前
2025–2026 国内 AI 赛事盘点:城市级黑客松 / AIGC 创作 / 硬件厂商赛事汇总
人工智能·aigc
麦豆GEO5 小时前
本地生活同城服务如何做好GEO优化
人工智能·生活
邂逅and回眸5 小时前
具身智能入门:机器人是怎么学会干活的
人工智能·机器人
阿里云大数据AI技术6 小时前
SENSER识季 × 阿里云OpenSearch:从时尚灵感到自我表达
人工智能