当前位置: 首页 > article >正文

three.js 在 webGL 添加纹理

在我们生成了3D设计之后,我们可以添加纹理使其更加吸引人。在 webGL 和 p5.js中,可以使用 gl.texImage2D() 和 texture()  API来为形状应用纹理。

使用 webGL

在 webGL 中,gl.texImage2D() 函数用于从图像文件生成2D纹理。该函数接受许多参数,包括目标,细节级别,内部格式,图像的宽度和高度,以及图像数据的格式和类型。

为了方便,我将使用 vite 搭建一个原生 js 项目。

1.创建项目

npm create vite@latest p5-demo
选:Vanilla
选:JavaScript

2.初始化项目
 cd p5-demo
 cnpm install

3.安装 p5.js
 cnpm install p5 --save

cd p5-demo
curl -O https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js

编写 three_texture.html  如下

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <script src="three.min.js"></script>
</head>
<body>
   <script>
      // curl -O https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js
      // Set up the scene
      var scene = new THREE.Scene();
      var camera = new THREE.PerspectiveCamera(75,
            window.innerWidth / window.innerHeight,
            0.1, 1000
        );
      var renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);

      // Create a cube
      var geometry = new THREE.BoxGeometry(3, 3, 3);//
      // 随意选一个.jpg 文件作为纹理图像
      var texture = new THREE.TextureLoader().load("./海边.jpg");
      var material = new THREE.MeshBasicMaterial({ map: texture });
      var cube = new THREE.Mesh(geometry, material);
      scene.add(cube);

      // Position the camera
      camera.position.z = 5; 
      // Render the scene
      function render() {
         requestAnimationFrame(render);
         cube.rotation.x += 0.01;
         cube.rotation.y += 0.01;
         renderer.render(scene, camera);
      }
      render();
   </script>
</body>
</html>

5.运行 npm run dev 
访问 http://localhost:5173/three_texture.html


 在 p5.js 中使用 texture() 函数可以将纹理应用到一个对象上。texture() 函数接受一个参数:纹理图像文件。

编写 p5_texture.html  如下

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>p5.js Texture Example</title>
   <script src="p5.min.js"></script>
</head>
<body>
   <script>
      let img;
      function preload() { 
         img = loadImage("./海边.jpg");
      }
      function setup() {
         createCanvas(650, 400, WEBGL);
         noStroke();
      }
      function draw() {
         background(200);
         texture(img);
         rotateX(frameCount * 0.01);
         rotateY(frameCount * 0.01);
         box(100);
      }
   </script>
</body>
</html>

 访问 http://localhost:5173/p5_texture.html

 可见同样的程序 p5.js 的代码量比 three.js 和 python 都要少。


http://www.kler.cn/a/577866.html

相关文章:

  • Django系列教程(5)——Django模型详解
  • [FE] React 初窥门径(五):React 组件的加载过程(commit 阶段)
  • 【爬虫软件】抖音评论区采集工具
  • [含文档+PPT+源码等]精品基于Python实现的django电动车租赁系统
  • FPGA 实验报告:四位全加器与三八译码器仿真实现
  • Windows 图形显示驱动开发-WDDM 3.2-本机 GPU 围栏对象(八)
  • uniapp:小程序将base64图片字符串保存到手机相册
  • java环境部署
  • hbase集群archive目录过大问题处理
  • 深度学习-143-Text2SQL之基于langchain的少量样本提示词模板FewShotPromptTemplate的应用实战(二)
  • oneinstack 部署 lamp/lnmp
  • 动量法与带阻尼的二阶 ODE:从离散优化到连续动态的奇妙联系
  • 力扣热题 100:二叉树专题进阶题解析(后7道)
  • C++从入门到精通系列教程之第十篇:异常处理与调试技巧
  • 车载以太网测试-3【Wireshark介绍】
  • LINUX网络基础 [五] - HTTP协议
  • LeetCode 热题 100_字符串解码(71_394_中等_C++)(栈)
  • 腾讯云短信
  • 【Python机器学习】1.8. 逻辑回归实战(基础):建立一阶边界模型、画分类散点图、逻辑回归模型的代码实现、可视化决策边界
  • PHP之特性