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

用PlayCanvas打造一个3D模型

Alt

本文由ScriptEcho平台提供技术支持

项目地址:传送门

基于 PlayCanvas 的 3D 物理场景开发

应用场景介绍

PlayCanvas 是一款功能强大的 3D 引擎,可用于创建各种类型的 3D 体验,包括游戏、模拟和交互式可视化。本技术博客将介绍如何使用 PlayCanvas 创建一个具有物理交互功能的 3D 场景。

代码基本功能介绍

本代码演示了如何使用 PlayCanvas 创建一个具有以下功能的 3D 场景:

  • 物理碰撞和刚体运动
  • 使用 3D 模型和动画
  • 相机控制和场景交互
  • 动态创建和销毁对象

功能实现步骤及关键代码分析说明

1. 初始化 PlayCanvas 和加载资产

首先,我们初始化 PlayCanvas 并加载所需的资产,包括 3D 模型、动画和纹理。

import * as pc from 'playcanvas'const canvas = document.getElementById('canvas')
if (!(canvas instanceof HTMLCanvasElement)) {throw new Error('No canvas found')
}
const assets = {model: new pc.Asset('model', 'container', {url: 'playcanvas/assets/models/bitmoji.glb',}),idleAnim: new pc.Asset('idleAnim', 'container', {url: 'playcanvas/assets/animations/bitmoji/idle.glb',}),helipad: new pc.Asset('helipad-env-atlas','texture',{ url: 'playcanvas/assets/cubemaps/helipad-env-atlas.png' },{ type: pc.TEXTURETYPE_RGBP, mipmaps: false },),
}
2. 创建物理场景

接下来,我们创建物理场景并设置重力。

// Set the gravity for our rigid bodies
app.systems.rigidbody.gravity.set(0, -9.81, 0)
3. 创建地面

我们创建了一个平面作为地面,并添加了刚体和碰撞组件,以使其具有物理特性。

const floor = new pc.Entity()
floor.addComponent('render', {type: 'box',material: gray,
})// Scale it and move it so that the top is at 0 on the y axis
floor.setLocalScale(10, 1, 10)
floor.translateLocal(0, -0.5, 0)// Add a rigidbody component so that other objects collide with it
floor.addComponent('rigidbody', {type: 'static',restitution: 0.5,
})// Add a collision component
floor.addComponent('collision', {type: 'box',halfExtents: new pc.Vec3(5, 0.5, 5),
})// Add the floor to the hierarchy
app.root.addChild(floor)
4. 创建 3D 模型

我们从加载的资产中实例化 3D 模型,并添加了动画、刚体和碰撞组件。

const modelEntity = assets.model.resource.instantiateRenderEntity({castShadows: true,
})// Add an anim component to the entity
modelEntity.addComponent('anim', {activate: true,
})// Add a rigid body and collision for the head with offset as the model's origin is
// at the feet on the floor
modelEntity.addComponent('rigidbody', {type: 'static',restitution: 0.5,
})modelEntity.addComponent('collision', {type: 'sphere',radius: 0.3,linearOffset: [0, 1.25, 0],
})
5. 创建相机

我们创建了一个相机实体,并设置了它的位置和视角。

const cameraEntity = new pc.Entity()
cameraEntity.addComponent('camera')
cameraEntity.translate(0, 2, 5)
const lookAtPosition = modelEntity.getPosition()
cameraEntity.lookAt(lookAtPosition.x,lookAtPosition.y + 0.75,lookAtPosition.z,
)app.root.addChild(cameraEntity)
6. 动态创建对象

我们在更新循环中创建了一个球体模板,并根据需要动态创建和销毁球体。

const ball = new pc.Entity()
ball.tags.add('shape')
ball.setLocalScale(0.4, 0.4, 0.4)
ball.translate(0, -1, 0)
ball.addComponent('render', {type: 'sphere',
})ball.addComponent('rigidbody', {type: 'dynamic',mass: 50,restitution: 0.5,
})ball.addComponent('collision', {type: 'sphere',radius: 0.2,
})ball.enabled = false
// create a falling box every 0.2 seconds
if (count > 0) {timer -= dtif (timer <= 0) {count--timer = 0.5// Create a new ball to dropconst clone = ball.clone()clone.rigidbody.teleport(pc.math.random(-0.25, 0.25),5,pc.math.random(-0.25, 0.25),)app.root.addChild(clone)clone.enabled = true}
}

总结与展望

通过本代码,我们成功创建了一个具有物理交互功能的 3D 场景。我们了解了如何使用 PlayCanvas 创建 3D 模型、设置物理特性、动态创建对象以及实现场景交互。

未来,我们可以对该场景进行拓展和优化,例如:

  • 添加更多交互元素,如按钮或杠杆

  • 实现更复杂的物理交互,如绳索或弹簧

  • 优化场景性能,提高帧率

    更多组件:

    在这里插入图片描述


    在这里插入图片描述

    获取更多Echos

    本文由ScriptEcho平台提供技术支持

    项目地址:传送门

    扫码加入AI生成前端微信讨论群:

扫码加入群聊

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 焦化行业排放平台简介
  • 【机器学习】Qwen2大模型原理、训练及推理部署实战
  • LVS ipvsadm命令的使用(二)
  • 人工智能:项目管理的新视角与未来影响
  • 汽车网络安全深入分析
  • Guava常用方法
  • 内网安全--隧道技术代理技术
  • 树莓派 5 AI 套件(Hailo-8L)使用教程
  • unity38——MemoryProfiler性能分析器,截帧分析当前性能占用率的具体文件
  • 【Python】中的X[:,0]、X[0,:]、X[:,:,0]、X[:,:,1]、X[:,m:n]、X[:,:,m:n]和X[: : -1]
  • AWS无服务器 应用程序开发—第九章 文件存储(Amazon S3)
  • 安卓兼容的编程语言有哪些:探索多样化的开发选择
  • Dorkish:一款针对OSINT和网络侦查任务的Chrome扩展
  • Java老人护理上门服务类型系统小程序APP源码
  • 使用Java获取图片MD5编码的方法详解
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • 4个实用的微服务测试策略
  • angular学习第一篇-----环境搭建
  • CentOS7简单部署NFS
  • git 常用命令
  • iOS动画编程-View动画[ 1 ] 基础View动画
  • Iterator 和 for...of 循环
  • JavaScript 基础知识 - 入门篇(一)
  • Python 反序列化安全问题(二)
  • 多线程 start 和 run 方法到底有什么区别?
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 猴子数据域名防封接口降低小说被封的风险
  • 使用iElevator.js模拟segmentfault的文章标题导航
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 思维导图—你不知道的JavaScript中卷
  • 问题之ssh中Host key verification failed的解决
  • 小程序 setData 学问多
  • 用 Swift 编写面向协议的视图
  • 正则学习笔记
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • 阿里云ACE认证之理解CDN技术
  • ​Base64转换成图片,android studio build乱码,找不到okio.ByteString接腾讯人脸识别
  • ​经​纬​恒​润​二​面​​三​七​互​娱​一​面​​元​象​二​面​
  • #Datawhale X 李宏毅苹果书 AI夏令营#3.13.2局部极小值与鞍点批量和动量
  • #Linux(Source Insight安装及工程建立)
  • #前后端分离# 头条发布系统
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (04)Hive的相关概念——order by 、sort by、distribute by 、cluster by
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (libusb) usb口自动刷新
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (切换多语言)vantUI+vue-i18n进行国际化配置及新增没有的语言包
  • (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)...
  • (十三)Flink SQL
  • (五)activiti-modeler 编辑器初步优化
  • (一) springboot详细介绍