二哥加载三弟模型 angular加载3DMax模型

x33g5p2x  于2021-11-12 转载在 Angular  
字(3.8k)|赞(0)|评价(0)|浏览(449)

建模工具: 3dsMax、maya

  1. import {
  2. Scene,
  3. AmbientLight,
  4. PointLight,
  5. WebGLRenderer,
  6. PerspectiveCamera,
  7. GridHelper,
  8. Color
  9. } from 'three';
  10. import {
  11. OrbitControls,
  12. } from 'three-full';
  13. import * as Stats from 'stats.js';
  14. import * as TWEEN from '@tweenjs/tween.js';
  15. import {
  16. FXAAShader, UnrealBloomPass, ShaderPass, FilmPass, OutlinePass, GeometryUtils, CopyShader, ColorifyShader, SepiaShader,
  17. OrbitControls, GLTFLoader, EffectComposer, RenderPass, SMAAShader, SMAAPass, ClearMaskPass, MaskPass,
  18. } from 'three-full';
  19. // three-full和tween以下代码没调用,先展示下调用方式
  20. // 渲染器
  21. export const RENDERER = new WebGLRenderer(); // 渲染器(去据此){ antialias: true }
  22. export function initRenderer(doc) {
  23. RENDERER.setSize(
  24. doc.clientWidth,
  25. doc.clientHeight
  26. );
  27. RENDERER.shadowMap.enabled = true; // 辅助线
  28. doc.appendChild(RENDERER.domElement);
  29. }
  30. // 场景
  31. export const SCENE = new Scene();
  32. export function initScene() {
  33. SCENE.background = new Color(0xcccccc);
  34. }
  35. // 灯光
  36. export function initLight() {
  37. const ambientLight = new AmbientLight(0xffffff, 0.2); // 全局光
  38. ambientLight.position.set(10, 20, 55); // 灯光
  39. SCENE.add(ambientLight);
  40. // 点光源
  41. const pointLight = new PointLight(0xffffff);
  42. pointLight.distance = 0;
  43. CAMERA.add(pointLight);
  44. SCENE.add(CAMERA);
  45. }
  46. // 相机
  47. export let CAMERA;
  48. export let CONTROLS;
  49. export function initCamera(doc) {
  50. const d = {
  51. fov: 30, // 拍摄距离 视野角值越大,场景中的物体越小
  52. near: 1, // 最小范围
  53. far: 1000, // 最大范围
  54. };
  55. CAMERA = new PerspectiveCamera(
  56. d.fov,
  57. doc.clientWidth / doc.clientHeight,
  58. d.near,
  59. d.far)
  60. ;
  61. const p = {
  62. x: -20,
  63. y: 10,
  64. z: -10,
  65. };
  66. CAMERA.position.set(p.x, p.y, p.z);
  67. CAMERA.lookAt(0, 0, 0);
  68. CONTROLS = new OrbitControls(CAMERA, RENDERER.domElement); // 控制镜头
  69. }
  70. // 网格
  71. export function initGrid() {
  72. const gridHelper = new GridHelper(100, 50);
  73. SCENE.add(gridHelper);
  74. }
  75. // 性能检测
  76. export const STATS = new Stats();
  77. export function initStats(doc) {
  78. STATS.setMode(0);
  79. STATS.domElement.style.position = 'absolute';
  80. STATS.domElement.left = '0px';
  81. STATS.domElement.top = '0px';
  82. doc.appendChild(STATS.domElement);
  83. }
  84. // 动画混合器组(把模型的动画混合器都push到这里面,在canvas.ts里面更新动画 )
  85. export const MIXER = [];

html

  1. <div id="canvas" #canvasFrame></div>

scss

  1. #canvas {
  2. width: 100%;
  3. display: block;
  4. height: 100%;
  5. position: relative;
  6. }

ts

  1. import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
  2. import {
  3. initRenderer,
  4. initCamera,
  5. initScene, initLight,
  6. initGrid, initStats,
  7. RENDERER, CAMERA, SCENE, CONTROLS, STATS, MIXER
  8. } from '../../config/base';
  9. import { ChassisService, LineService } from '../../importModels';
  10. import {
  11. FXAAShader, UnrealBloomPass, ShaderPass, FilmPass, OutlinePass, GeometryUtils, CopyShader, ColorifyShader, SepiaShader,
  12. OrbitControls, GLTFLoader, EffectComposer, RenderPass, SMAAShader, SMAAPass, ClearMaskPass, MaskPass,
  13. } from 'three-full';
  14. import * as TWEEN from '@tweenjs/tween.js';
  15. import { Vector2, Group, Scene, SphereGeometry, ImageUtils, AnimationMixer } from 'three';
  16. import { fromEvent } from 'rxjs';
  17. import { PickupService } from '../../service/pickup.service';
  18. @Component({
  19. selector: 'app-canva',
  20. templateUrl: './canva.component.html',
  21. styleUrls: ['./canva.component.scss']
  22. })
  23. export class CanvaComponent implements OnInit {
  24. @ViewChild('canvasFrame', { static: true }) canvasContainer: ElementRef;
  25. thing;
  26. constructor( ) { }
  27. ngOnInit() {
  28. this.init();
  29. }
  30. init() {
  31. initRenderer(this.canvasContainer.nativeElement);
  32. initCamera(this.canvasContainer.nativeElement);
  33. initScene();
  34. initLight();
  35. initGrid();
  36. initStats(this.canvasContainer.nativeElement);
  37. // 加载模型-star
  38. this.importantModel();
  39. // 加载模型-end
  40. // 渲染场景
  41. const delta = new Clock();
  42. const rendererOut = () => {
  43. requestAnimationFrame(rendererOut);
  44. RENDERER.render(SCENE, CAMERA);
  45. CONTROLS.update();
  46. STATS.update();
  47. if (MIXER) {
  48. MIXER.map(r => {
  49. r.update(delta.getDelta());
  50. });
  51. }
  52. };
  53. rendererOut();
  54. }
  55. // 这个模型可以使用blender2.8(正处于beta版) 直接导出gltf
  56. importantModel() {
  57. const loader = new GLTFLoader();
  58. loader.load('assets/model/1.gltf', (gltf) => {
  59. console.log(gltf);
  60. gltf.scene.traverse((child) => { // 遍历判断Mesh
  61. if (child.isMesh) {
  62. console.log(child);
  63. child.material.color = { r: 1, g: 2, b: 3 }; // 颜色
  64. child.material.metalness = 0.8; // 金属度
  65. gltf.scene.background = 'rgba(0,0,0, 0.5)';
  66. gltf.scene.translateX(5);
  67. this.thing = gltf.scene;
  68. SCENE.add(this.thing);
  69. }
  70. });
  71. },
  72. undefined,
  73. (error) => {
  74. console.error(error);
  75. });
  76. }
  77. }

相关文章

最新文章

更多