PIXI+THREE

使用 PIXI 和 THREE 将三维和二维渲染在同一个 canvas 下面

效果

思路

  1. 初始化 PIXI 的 Application, 作为 pixi 最重要的变量
const app = new PIXI.Application({
width: 800,
height: 600,
// 像素
resolution: window.devicePixelRatio,
// 透明
transparent: true,
// 抗锯齿
antialias: true,
});
  1. 创建 container 并加入到 app.stage
  2. 创建自己的 sprite, 也就是上图 pixi 中标识的 图片
  3. 将 pixi 创建的 canvas 加入到 body 中

  1. 创建 Three 相关变量, camera, scene, renderer, box, light
  2. 将相关变量设置好内容
  3. 新建 pixi 中的 sprite, 名为 sprite3D, 新建 PIXI.Texture , source 为 three 中 renderer.dom
  4. 在 animate 方法中 更新 texture

源代码

import * as THREE from "three";
import * as PIXI from "pixi.js";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import img1 from "./img/img1.jpg"; window.PIXI = PIXI; function init() {
const { container, app, sprite } = initPixi();
// 设置图片位置
sprite.scale.set(0.5, 0.5);
sprite.position.set(200, 200); const { scene, renderer, camera, orbitControls, box } = initThree(app); // 设置三维盒子变量
const sprite3DTexture = PIXI.Texture.from(renderer.domElement);
const sprite3D = new PIXI.Sprite(sprite3DTexture);
sprite3D.width = 300;
sprite3D.height = 200;
sprite3D.position.set(50, 100);
container.addChild(sprite3D); // 循环方法
function animate() {
requestAnimationFrame(animate); sprite.rotation += 0.01;
// 更新 three 渲染的内容
sprite3DTexture.update(); box.rotation.x += 0.01;
box.rotation.y += 0.01; orbitControls.update(); renderer.render(scene, camera);
}
animate();
} function initPixi() {
const app = new PIXI.Application({
width: 800,
height: 600,
// 像素
resolution: window.devicePixelRatio,
// 透明
transparent: true,
// 抗锯齿
antialias: true,
}); const container = new PIXI.Container();
// stage 全局内容
app.stage.addChild(container); const spriteTexture = createdPixiTexture(img1);
const sprite = new PIXI.Sprite(spriteTexture); container.addChild(sprite); document.body.appendChild(app.view); return { container, app, sprite };
} function createdPixiTexture(url) {
return PIXI.Texture.from(url);
} function initThree(app) {
const scene = new THREE.Scene();
// 环境光
scene.add(new THREE.AmbientLight(0xffffff, 0.8)); scene.add(new THREE.AxesHelper(100));
const pointLight = new THREE.PointLight(0xfd08aa, 1);
pointLight.position.set(100, 100, 100); const renderer = new THREE.WebGLRenderer({
// 设置透明
alpha: true,
antialias: true,
});
// 设置 pixi 限定的尺寸
renderer.setSize(300, 200);
renderer.pixelRatio = window.devicePixelRatio; const camera = new THREE.PerspectiveCamera(45, 300 / 200);
camera.position.set(200, 300, 200);
camera.lookAt(0, 0, 0); const orbitControls = new OrbitControls(camera, app.view);
orbitControls.update(); const box = new THREE.Mesh(
new THREE.BoxBufferGeometry(40, 50, 60),
new THREE.MeshStandardMaterial({
color: 0xfea67a,
roughness: 2,
})
);
scene.add(box); return { scene, camera, renderer, orbitControls, box };
} init();

three+pixi 将二维和三维结合的更多相关文章

  1. C++ 动态分配二维和三维数组

    目的:熟悉c++动态内存分配 描述:使用c++程序定义动态数组类,使用new和delete操作符实现动态二维数组和三维数组的定义 //main.cpp //主程序类 #include <iost ...

  2. VC、OpenGL、ArcGIS Engine开发的二维三维结合的GIS系统

    一.前言 众所周知,二维GIS技术发展了近四十年,伴随着计算机软硬件以及关系型数据库的飞速发展,二维GIS技术已日臻完善.在对地理信息的分析功能上有着无可比拟的优势.一些宏观的地理信息,一维的地理信息 ...

  3. [DeeplearningAI笔记]卷积神经网络4.11一维和三维卷积

    4.4特殊应用:人脸识别和神经网络风格转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 4.11一维和三维卷积 二维和一维卷积 对于2D卷积来说,假设原始图像为\(14*14*3\)的三通 ...

  4. 使用C语言实现二维,三维绘图算法(1)-透视投影

    使用C语言实现二维,三维绘图算法(1)-透视投影 ---- 引言---- 每次使用OpenGL或DirectX写三维程序的时候, 都有一种隔靴搔痒的感觉, 对于内部的三维算法的实现不甚了解. 其实想想 ...

  5. 使用C语言实现二维,三维绘图算法(3)-简单的二维分形

    使用C语言实现二维,三维绘图算法(3)-简单的二维分形 ---- 引言---- 每次使用OpenGL或DirectX写三维程序的时候, 都有一种隔靴搔痒的感觉, 对于内部的三维算法的实现不甚了解. 其 ...

  6. 使用C语言实现二维,三维绘图算法(2)-解析曲面的显示

    使用C语言实现二维,三维绘图算法(2)-解析曲面的显示 ---- 引言---- 每次使用OpenGL或DirectX写三维程序的时候, 都有一种隔靴搔痒的感觉, 对于内部的三维算法的实现不甚了解. 其 ...

  7. ARCGIS二维三维导航

    在使用代码前需要先安装arcgis10.0    或者10.1都可以    不过本人建议初学者安装10.0比较容易安装.. 安装方式和二维三维地图的加载网上都有,就不在此一一赘述了. 先从基本的功能开 ...

  8. ARCGIS二维三维互动

    当对三维模型进行操作时(如导航.平移)二维地图自动跟进. private void Synckron() { m_pGlobe = this._GlobeControl.Globe; m_pMap = ...

  9. ARCGIS二维三维放大缩小

    private void ULZoomPan() { ESRI.ArcGIS.SystemUI.ICommand com = new ControlsGlobeFixedZoomOutCommand( ...

随机推荐

  1. Gitea 1.4.0 目录穿越导致命令执行漏洞

    复现 POST /vulhub/repo.git/info/lfs/objects HTTP/1.1 Host: 192.168.49.2:3000 Accept-Encoding: gzip, de ...

  2. .NET5控制台程序使用EF连接MYSQL

    .NET5控制台程序使用EF连接MYSQL 1.使用nuget下载: Microsoft.Extensions.Configuration.Json Pomelo.EntityFrameworkCor ...

  3. Android达到什么水平才能顺利拿到 20k 无压力?

    程序员分很多种类和等级,如果要提高达到20k的概率,有两个条件如果满足的话,则很容易达到: 1.一线城市:北上广深杭 2.互联网行业 如果你非得抬杠,我要在三线城市,做外包要赚20k的话,很难,我自己 ...

  4. Java-Dubbo学习及整合SpringBoot

    Dubbo架构 Dubbo是Java的RPC框架,具有三大核心功能:面向接口的远程方法调用,智能容错和负载均衡,以及服务的自动注册和发现 Dubbo架构图: 节点角色说明: 节点 说明 Provide ...

  5. rancherUI添加configmap

    1.创建configmap 2.部署pod,挂载配置文件(通过卷的形式引用)

  6. CYPEESS USB3.0程序解读之---GPIO

    CPRESS 官方给出的SDK1.1中(目前最新的SDK),提供了大量的例程供我们开发软件的时候作参考,就像STM32的开发一样提供了库一样,但是又不是库,仅仅是参考例程. 首先看一个简单一点的GPI ...

  7. Create Shortcut to Get Jar File Meta Information

    You have to get meta information of cobertura.jar with command "unzip -q -c cobertura.jar META- ...

  8. 简单实现Linux服务器重启后自动启动Tomcat以及MongoDB

    1.场景描述 做一个网站差不多都会用到tomcat,用阿里云镜像市场提供的镜像里的tomcat是方便的,但是自己喜欢折腾,所以就自己在服务器里装了一个tomcat 但是有一次服务器被关了,然后自己要到 ...

  9. NOIP 模拟 $34\; \rm Merchant$

    题解 \(by\;zj\varphi\) 对于选的物品,总值一定有在前一段区间递减,后一段递增的性质,那么就可以二分. check()时只递归归并大的一段,用nth_element即可 Code #i ...

  10. CentOS7.6新增或修改SSH端口号的步骤

    1.修改SSH配置文件(注意是sshd_config而不是ssh_config,多了个d) vim /etc/ssh/sshd_config 找到"#Port 22",这一行直接键 ...