【小沐学Web3D】three.js 加载三维模型(Angular)

文章目录

  • 1、简介
    • 1.1 three.js
    • 1.2 angular.js
  • 2、three.js + Angular.js
  • 结语

1、简介

1.1 three.js

Three.js 是一款 webGL(3D绘图标准)引擎,可以运行于所有支持 webGL 的浏览器。Three.js 封装了 webGL 底层的 API ,为我们提供了高级的开发接口,可以使用简单的代码去实现 3D 渲染。
在这里插入图片描述

1.2 angular.js

https://angular.dev/
Angular 是一个用于构建移动和桌面 Web 应用的平台,拥有百万开发者的支持。
AngularJS 通过新的属性和表达式扩展了 HTML。
AngularJS 可以构建一个单一页面应用程序(SPAs:Single Page Applications)。
AngularJS 学习起来非常简单。
在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.net/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app=""><p>名字 : <input type="text" ng-model="name"></p><h1>Hello {{name}}</h1>
</div>
</body>
</html>

保存为index.html文件,用浏览器打开如下:
在这里插入图片描述

2、three.js + Angular.js

确保已经安装了 Angular CLI。如果没有安装,可以使用以下命令安装:

npm install -g @angular/cli

在这里插入图片描述
打印版本信息如下:
在这里插入图片描述

然后创建一个新的 Angular 项目:

ng new threejs-angular-app
cd threejs-angular-app

在这里插入图片描述
angular项目创建完毕:
在这里插入图片描述
生成的文件夹如下:
在这里插入图片描述
创建一个新的 Angular 组件来加载和渲染三维模型:
在这里插入图片描述
这会在 src/app 目录下生成一个新的组件文件夹 threejs-model。
生成文件夹如下:
在这里插入图片描述

npm install --save-dev @types/three

在这里插入图片描述

修改 threejs-model.component.ts:

import { Component, OnInit, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import * as THREE from 'three';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';@Component({selector: 'app-threejs-model',template: '<div #threeContainer class="three-container"></div>',styles: [`.three-container {width: 100%;height: 100%;}`]
})
export class ThreejsModelComponent implements AfterViewInit {@ViewChild('threeContainer') threeContainer!: ElementRef;private scene!: THREE.Scene;private camera!: THREE.PerspectiveCamera;private renderer!: THREE.WebGLRenderer;private model!: THREE.Group;private controls!: OrbitControls; // 添加 OrbitControlsngAfterViewInit() {this.initThree();this.loadModel();this.animate();}initThree() {// 创建场景this.scene = new THREE.Scene();this.scene.background = new THREE.Color(0xaaaaaa);// 创建相机this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 1000);this.camera.position.set(0, 0.15, 0.15);this.camera.lookAt(this.scene.position);// 创建渲染器this.renderer = new THREE.WebGLRenderer({ antialias: true });this.renderer.setSize(window.innerWidth, window.innerHeight);this.threeContainer.nativeElement.appendChild(this.renderer.domElement);// 添加环境光const ambientLight = new THREE.AmbientLight(0xf0f0f0, 0.9);this.scene.add(ambientLight);// 初始化 OrbitControlsthis.controls = new OrbitControls(this.camera, this.renderer.domElement);this.controls.enableDamping = true; // 添加阻尼效果this.controls.dampingFactor = 0.05; // 阻尼系数// 处理窗口大小变化window.addEventListener('resize', () => {this.camera.aspect = window.innerWidth / window.innerHeight;this.camera.updateProjectionMatrix();this.renderer.setSize(window.innerWidth, window.innerHeight);});}loadModel() {// 初始化 GLTF 加载器const loader = new GLTFLoader();// 加载 GLTF 模型loader.load('./assets/models/Avocado2.glb', // 模型路径(gltf) => {this.model = gltf.scene;this.scene.add(this.model);console.log('模型加载成功', gltf);},(xhr) => {console.log(`模型加载进度:${(xhr.loaded / xhr.total) * 100}%`);},(error) => {console.error('模型加载失败', error);});}animate() {requestAnimationFrame(() => this.animate());// 模型动画(例如旋转)if (this.model) {this.model.rotation.y += 0.01;}this.renderer.render(this.scene, this.camera);}
}

将你的 3D 模型文件(如 .glb 或 .gltf)放置在 public/assets/models 文件夹中。如果没有这个文件夹,可以手动创建:
public/assets/models/Avocado2.glb
在这里插入图片描述
在 app.routes.ts 中添加路由配置:

import { Routes } from '@angular/router';
import { ThreejsModelComponent } from './threejs-model/threejs-model.component';export const appRoutes: Routes = [{ path: '', component: ThreejsModelComponent },
];

在这里插入图片描述
修改 app.config.ts:

import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';export const appConfig: ApplicationConfig = {providers: [provideRouter(appRoutes)]
};

修改app.component.ts:

import { Component } from '@angular/core';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { ThreejsModelComponent } from './threejs-model/threejs-model.component';@Component({selector: 'app-root',standalone: true,imports: [ThreejsModelComponent],schemas: [CUSTOM_ELEMENTS_SCHEMA],templateUrl: './app.component.html',styleUrl: './app.component.css',
})
export class AppComponent {title = 'threejs-angular-app';
}

修改app.component.html:

<!-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -->
<!-- * * * * * * * * * * * The content below * * * * * * * * * * * -->
<!-- * * * * * * * * * * is only a placeholder * * * * * * * * * * -->
<!-- * * * * * * * * * * and can be replaced.  * * * * * * * * * * -->
<!-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -->
<!-- * * * * * * * * * Delete the template below * * * * * * * * * -->
<!-- * * * * * * * to get started with your project! * * * * * * * -->
<!-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * --><style></style><main class="main"><app-threejs-model></app-threejs-model>
</main><!-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -->
<!-- * * * * * * * * * * * The content above * * * * * * * * * * * * -->
<!-- * * * * * * * * * * is only a placeholder * * * * * * * * * * * -->
<!-- * * * * * * * * * * and can be replaced.  * * * * * * * * * * * -->
<!-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -->
<!-- * * * * * * * * * * End of Placeholder  * * * * * * * * * * * * -->
<!-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * --><router-outlet />

修改tsconfig.json:

/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
{"compileOnSave": false,"compilerOptions": {"outDir": "./dist/out-tsc","strict": true,"noImplicitOverride": true,"noPropertyAccessFromIndexSignature": true,"noImplicitReturns": true,"noFallthroughCasesInSwitch": true,"skipLibCheck": true,"isolatedModules": true,"esModuleInterop": true,"experimentalDecorators": true,"moduleResolution": "bundler","importHelpers": true,"target": "ES2022","module": "ES2022","typeRoots": ["node_modules/@types"]},"angularCompilerOptions": {"enableI18nLegacyMessageIdFormat": false,"strictInjectionParameters": true,"strictInputAccessModifiers": true,"strictTemplates": true}
}

执行项目:

ng serve

在这里插入图片描述
浏览器运行如下:
在这里插入图片描述
在这里插入图片描述

结语

如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;╮( ̄▽ ̄)╭
如果您感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进;o_O???
如果您需要相关功能的代码定制化开发,可以留言私信作者;(✿◡‿◡)
感谢各位童鞋们的支持!( ´ ▽´ )ノ ( ´ ▽´)っ!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/76868.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

简单程序语言理论与编译技术·22 实现一个从AST到RISCV的编译器

本文是记录专业课“程序语言理论与编译技术”的部分笔记。 LECTURE 22&#xff08;实现一个从AST到RISCV的编译器&#xff09; 一、问题分析 1、完整的编译器&#xff08;如LLVM&#xff09;需先完成AST到IR的转换&#xff0c;并进行代码优化&#xff0c;再到汇编&#xff0…

JavaWeb 课堂笔记 —— 02 JavaScript

本系列为笔者学习JavaWeb的课堂笔记&#xff0c;视频资源为B站黑马程序员出品的《黑马程序员JavaWeb开发教程&#xff0c;实现javaweb企业开发全流程&#xff08;涵盖SpringMyBatisSpringMVCSpringBoot等&#xff09;》&#xff0c;章节分布参考视频教程&#xff0c;为同样学习…

Python 如何高效实现 PDF 内容差异对比

Python 如何高效实现 PDF 内容差异对比 1. 安装 PyMuPDF 库2. 获取 PDF 内容通过文件路径获取通过 URL 获取 3. 提取 PDF 每页信息4. 内容对比metadata 差异文本对比可视化对比 5. 提升对比效率通过哈希值快速判断页面是否相同早停机制多进程机制 6. 其他 最近有接触到 PDF 内容…

OpenGL学习笔记(简介、三角形、着色器、纹理、坐标系统、摄像机)

目录 简介核心模式与立即渲染模式状态机对象GLFW和GLAD Hello OpenGLTriangle 三角形顶点缓冲对象 VBO顶点数组对象 VAO元素缓冲对象 EBO/ 索引缓冲对象 IEO 着色器GLSL数据类型输入输出Uniform 纹理纹理过滤Mipmap 多级渐远纹理实际使用方式纹理单元 坐标系统裁剪空间 摄像机自…

MIPI与DVP接口摄像头:深度解析与应用指南

1、MIPI 1.1 MIPI简介 MIPI是什么&#xff1f;MIPI&#xff1a;mobile industry processor interface移动行业处理器接口。它是一个由Intel、Motorola、Nokia、NXP、Samsung、ST&#xff08;意法半导体&#xff09;和TI&#xff08;德州仪器&#xff09;等公司发起的开放标准…

35信号和槽_信号槽小结

Qt 信号槽 1.信号槽是啥~~ 尤其是和 Linux 中的信号进行了对比&#xff08;三要素&#xff09; 1) 信号源 2) 信号的类型 3)信号的处理方式 2.信号槽 使用 connect 3.如何查阅文档. 一个控件&#xff0c;内置了哪些信号&#xff0c;信号都是何时触发 一…

6547网:蓝桥STEMA考试 Scratch 试卷(2025年3月)

『STEMA考试是蓝桥青少教育理念的一部分&#xff0c;旨在培养学生的知识广度和独立思考能力。考试内容主要考察学生的未来STEM素养、计算思维能力和创意编程实践能力。』 一、选择题 第一题 运行下列哪个程序后&#xff0c;飞机会向左移动&#xff1f; ( ) A. …

使用 Python 爬取并打印双色球近期 5 场开奖数据

使用 Python 爬取并打印双色球近期 5 场开奖数据 前期准备安装所需库 完整代码代码解析 1. 导入必要的库2. 定义函数 get_recent_five_ssq 3. 设置请求的 URL 和 Headers 4. 发送请求并处理响应5. 解析 HTML 内容6. 提取并打印数据7. 错误处理 首先看下运行的效果图&#xff1a…

前端快速入门学习3——CSS介绍与选择器

1.概述 CSS全名是cascading style sheets,中文名层叠样式表。 用于定义网页样式和布局的样式表语言。 通过 CSS&#xff0c;你可以指定页面中各个元素的颜色、字体、大小、间距、边框、背景等样式&#xff0c;从而实现更精确的页面设计。 HTML与CSS的关系&#xff1a;HTML相当…

JVM 内存区域详解

JVM 内存区域详解 Java 虚拟机&#xff08;JVM&#xff09;的内存区域划分为多个部分&#xff0c;每个部分有特定的用途和管理机制。以下是 JVM 内存区域的核心组成及其功能&#xff1a; 一、运行时数据区&#xff08;Runtime Data Areas&#xff09; 1. 线程共享区域 内存…

基于SpringBoot的水产养殖系统【附源码】

基于SpringBoot的水产养殖系统&#xff08;源码L文说明文档&#xff09; 目录 4 系统设计 4.1 总体功能 4.2 系统模块设计 4.3 数据库设计 4.3.1 数据库设计 4.3.2 数据库E-R 图 4.3.3 数据库表设计 5 系统实现 5.1 管理员功能模块的实…

从零构建大语言模型全栈开发指南:第五部分:行业应用与前沿探索-5.2.2超级对齐与AGI路径探讨

👉 点击关注不迷路 👉 点击关注不迷路 👉 点击关注不迷路 文章大纲 大语言模型全栈开发指南:伦理与未来趋势 - 第五部分:行业应用与前沿探索5.2.2 超级对齐与AGI路径探讨超级对齐:定义与核心挑战1. 技术挑战2. 伦理挑战AGI发展路径:从专用到通用智能阶段1:`专用智能…

基于大模型的重症肌无力的全周期手术管理技术方案

目录 技术方案文档1. 数据预处理模块2. 多任务预测模型架构3. 动态风险预测引擎4. 手术方案优化系统5. 技术验证模块6. 系统集成架构7. 核心算法清单8. 关键流程图详述实施路线图技术方案文档 1. 数据预处理模块 流程图 [输入原始数据] → [联邦学习节点数据对齐] → [多模态特…

盲盒小程序开发平台搭建:打造个性化、高互动性的娱乐消费新体验

在数字化浪潮席卷消费市场的今天&#xff0c;盲盒小程序以其独特的趣味性和互动性&#xff0c;迅速成为了年轻人追捧的娱乐消费新宠。盲盒小程序不仅为用户带来了拆盒的惊喜和刺激&#xff0c;更为商家提供了创新的营销手段。为了满足市场对盲盒小程序日益增长的需求&#xff0…

前端对接下载文件接口、对接dart app

嵌套在dart app里面的前端项目 1.前端调下载接口 ->后端返回 application/pdf格式的文件 ->前端将pdf处理为blob ->blob转base64 ->调用dart app的 sdk saveFile ->保存成功 async download() {try {// 调用封装的 downloadEContract 方法获取 Blob 数据const …

Spring常见问题复习

############Spring############# Bean的生命周期是什么&#xff1f; BeanFactory和FactoryBean的区别&#xff1f; ApplicationContext和BeanFactory的区别&#xff1f; BeanFactoryAware注解&#xff0c;还有什么其它的Aware注解 BeanFactoryAware方法和Bean注解的方法执行顺…

C++_类和对象(下)

【本节目标】 再谈构造函数Static成员友元内部类匿名对象拷贝对象时的一些编译器优化再次理解封装 1. 再谈构造函数 1.1 构造函数体赋值 在创建对象时&#xff0c;编译器通过调用构造函数&#xff0c;给对象中各个成员变量一个合适的初始值。 class Date { public:Date(in…

连续数据离散化与逆离散化策略

数学语言描述&#xff1a; 在区间[a,b]中有一组符合某分布的数据&#xff1a; 1.求相同区间中另一组符合同样分布的数据与这组数据的均方误差 2.求区间中点与数据的均方误差 3.求在区间中均匀分布的一组数据与这组数据的均方误差 一&#xff1a;同分布数据随机映射 假设在…

Redash:一个开源的数据查询与可视化工具

Redash 是一款免费开源的数据可视化与协作工具&#xff0c;可以帮助用户快速连接数据源、编写查询、生成图表并构建交互式仪表盘。它简化了数据探索和共享的过程&#xff0c;尤其适合需要团队协作的数据分析场景。 数据源 Redash 支持各种 SQL、NoSQL、大数据和 API 数据源&am…

FreeRTOS的空闲任务

在 FreeRTOS 中&#xff0c;空闲任务&#xff08;Idle Task&#xff09; 是操作系统自动创建的一个特殊任务&#xff0c;其作用和管理方式如下&#xff1a; 1. 空闲任务创建 FreeRTOS 内核自动创建&#xff1a;当调用 vTaskStartScheduler() 启动调度器时&#xff0c;内核会自…