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. 开发者如何快速搭建自己的电商App?

    面向电商购物场景,HMS Core提供了创新的电商解决方案,帮助应用快速获客.提升转化率,实现业务增长.为了帮助开发者了解如何在电商购物类应用中集成HMS Core的各项能力,HMS Core开发了电 ...

  2. Centos8 Tomcat 开机自启配置

    第一步:修改catalina.sh文件 # vim 编辑 Tomcat bin/catalina.sh 文件.增加如下内容 CATALINA_PID="$CATALINA_BASE/bin/ ...

  3. Mybatis学习笔记-注解开发

    面向接口编程 根本原因:[解耦],[可拓展],[更高规范性] 接口类型: abstract class interface 使用注解开发 简单语句可用注解开发(直接查询,列名与属性名相同) 本质:反射 ...

  4. 虚拟机中桥接模式和NAT模式以及仅主机模式的区别

    桥接模式和NAT模式 网络连接类型的选择,网络连接类型一共有桥接.NAT.仅主机和不联网四种. 桥接:选择桥接模式的话虚拟机和宿主机在网络上就是平级的关系,相当于连接在同一交换机上. NAT:NAT模 ...

  5. silky微服务简介

    代理主机 silky微服务定义了三种类型的代理主机,开发者可以根据需要选择合适的silky代理主机托管微服务应用.代理主机定义了一个Startup模块,该模块给出了使用该种类型主机所必须依赖的模块. ...

  6. Run Shell Commands in Python

    subprocess.call This is the recommended way to run shell commands in Python compared with old-fashio ...

  7. Java多线程间的数据共享

    下面的程序演示了一个对象被两个线程访问的方法,"monitor.gotMessage();"这一句虽然是monitor对象的方法,但却是运行在"MyObject" ...

  8. Longhorn 企业级云原生容器存储解决方案-部署篇

    内容来源于官方 Longhorn 1.1.2 英文技术手册. 系列 Longhorn 是什么? Longhorn 云原生分布式块存储解决方案设计架构和概念 安装 Longhorn 可以通过多种方式安装 ...

  9. murmur3 hash(hash算法)

    HashUtil.java package com.example.test.util; import com.google.common.base.Charsets; import com.goog ...

  10. Lab: Brute-forcing a stay-logged-in cookie:点击保持登录状态返回的Cookie里面破解账号密码靶场复盘

    靶场内容: 此实验室允许用户在关闭浏览器会话后仍保持登录状态.用于提供此功能的 cookie 容易受到暴力破解. 为了解决实验室问题,暴力破解 Carlos 的 cookie 以访问他的"我 ...