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()函数。

相关推荐
蔡俊锋2 分钟前
把1500个业务的大迁移,做成了可复用流水线用 Skill+Agent+Rule,省下 60 人年的实战复盘
人工智能·skill+agent
ZGi.ai3 分钟前
AI中台和AI工具的区别:为什么说前者是基础设施而后者是应用
人工智能·chatgpt·ai工具·ai基础设施
飘落的数码折腾日记9 分钟前
OpenClaw 是什么?让 AI 真正 “动手“ 帮你干活的秘密武器
人工智能
fthux15 分钟前
用了 GitZip 这么多年,我动手做了一个「Pro」版
人工智能·开源·github
Zik----16 分钟前
DAEFR (ICLR 2024)— 盲脸超分模型解读
人工智能·python·高光谱图像·光谱恢复
TheRouter20 分钟前
Agent Harness系列(三):记忆层的3种持久化架构——从SQLite到向量库
人工智能·架构·sqlite·llm·ai-native
一切皆是因缘际会27 分钟前
从概率生成到内生心智:2026大模型瓶颈与下一代AI演进方向
人工智能·安全·ai·架构
X54先生(人文科技)31 分钟前
《元创力》纪实录·心田记釉下新声:当《纪·念》成为可聆听的星轨
人工智能·开源·ai写作·开源协议
CeshirenTester33 分钟前
字节面试官追问:“你的Agent调了三个工具就死循环了,异常处理在哪写的?”我:啊?还要写这个?
人工智能
小程故事多_8037 分钟前
[大模型面试系列] RAG系统检索失效全链路排查指南,从根源定位到落地优化方法
人工智能·智能体