当前位置:   article > 正文

three.js教程学习初始化配置_threejs初始化

threejs初始化

记录一下学习three.js历程,我用的全是class,这里配置一下基础的class文件,由于目前也在学习ts就用了ts了

文件目录大概如下:common中写基础的配置,objects中写具体实例, scene中写具体逻辑创建调用

 Base.ts

  1. import { Scene } from "three"
  2. export default class Base {
  3. instance:any
  4. constructor(){}
  5. // 修改位置
  6. setPosition (x:number,y:number,z:number) {
  7. this.instance.position.set(x,y,z)
  8. }
  9. // 修改旋转
  10. setRotation (x:number,y:number,z:number) {
  11. this.instance.rotation.x = x
  12. this.instance.rotation.y = y
  13. this.instance.rotation.z = z
  14. }
  15. // 修改缩放
  16. setScale (x:number,y:number,z:number) {
  17. this.instance.scale.set(x,y,z); //缩放
  18. }
  19. // 修改名称
  20. setName (name:string) {
  21. this.instance.name = name; // 命名
  22. }
  23. // 添加到scene
  24. addToScene (scene:Scene) {
  25. scene.add(this.instance)
  26. }
  27. }

 Template.ts

  1. import {Scene, PerspectiveCamera, WebGLRenderer, Vector3, Color,
  2. Vector2, AmbientLight, DirectionalLight, AxesHelper, Mesh, Raycaster,GridHelper
  3. } from 'three'
  4. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls"
  5. import { CSS3DRenderer } from "three/examples/jsm/renderers/CSS3DRenderer.js";
  6. import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js"
  7. import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js"
  8. import { OutlinePass } from "three/examples/jsm/postprocessing/OutlinePass.js"
  9. import { ShaderPass } from "three/examples/jsm/postprocessing/ShaderPass.js"
  10. import { FXAAShader } from "three/examples/jsm/shaders/FXAAShader.js"
  11. interface PCamera{
  12. fov:number
  13. aspect:number
  14. near:number
  15. far:number
  16. }
  17. export default class Template {
  18. scene: Scene = new Scene()
  19. camera?: PerspectiveCamera
  20. renderer?: WebGLRenderer
  21. sceneBox: Scene = new Scene()
  22. renderBox?: CSS3DRenderer
  23. axesHelper?:AxesHelper
  24. gridHelper?:GridHelper
  25. ele:any = null
  26. PCamera:PCamera = {
  27. fov: 45,
  28. aspect: 0,
  29. near: 1,
  30. far: 1000
  31. }
  32. cameraPostion:Vector3 = new Vector3(0, 0, 0)
  33. cameraLookAt:Vector3 = new Vector3(0,0,0)
  34. rendererColor:Color = new Color(0x000000)
  35. rendererWidth:number = 0
  36. rendererHeight:number = 0
  37. intersects:any = []
  38. meshList:any = []
  39. composer?:EffectComposer
  40. outlinePass?:OutlinePass
  41. renderPass?:RenderPass
  42. constructor () {
  43. }
  44. // 初始化scene
  45. init <T>(ele:T) {
  46. this.ele = ele
  47. this.PCamera.aspect = this.ele.offsetWidth / this.ele.offsetHeight
  48. this.rendererWidth = this.ele.offsetWidth
  49. this.rendererHeight = this.ele.offsetHeight
  50. this.initPerspectiveCamera()
  51. this.initRenderer()
  52. }
  53. // 初始化弹窗scene
  54. initBox () {
  55. this.sceneBox = new Scene();
  56. const renderBox = new CSS3DRenderer();
  57. renderBox.setSize(this.rendererWidth, this.rendererHeight);
  58. renderBox.domElement.style.position = "absolute";
  59. renderBox.domElement.style.top = '0';
  60. this.ele.appendChild(renderBox.domElement);
  61. this.renderBox = renderBox
  62. }
  63. // 初始化相机
  64. initPerspectiveCamera () {
  65. const camera = new PerspectiveCamera(
  66. this.PCamera.fov,
  67. this.PCamera.aspect,
  68. this.PCamera.near,
  69. this.PCamera.far
  70. )
  71. camera.position.copy(this.cameraPostion)
  72. camera.lookAt(this.cameraLookAt)
  73. this.camera = camera
  74. this.scene.add(camera)
  75. }
  76. // 初始化render
  77. initRenderer () {
  78. const renderer = new WebGLRenderer({
  79. antialias: true
  80. })
  81. renderer.setClearColor(this.rendererColor)
  82. renderer.setSize(this.rendererWidth, this.rendererHeight)
  83. this.ele.appendChild(renderer.domElement)
  84. this.renderer = renderer
  85. }
  86. // 添加光照
  87. addAmbientLight (color:any) {
  88. this.scene.add( new AmbientLight( color ) )
  89. }
  90. // 添加方向光照
  91. addDirectionalLight (color:any,pos:{x:number,y:number,z:number},power?:number,castShadow?:boolean) {
  92. const light = new DirectionalLight( color, power?power:1 )
  93. light.position.set( pos.x, pos.y, pos.z )
  94. light.castShadow = castShadow?castShadow:false
  95. this.scene.add( light )
  96. }
  97. // 添加鼠标控制
  98. addOrbitControls (camera:PerspectiveCamera, el:any) {
  99. const controls = new OrbitControls( camera, el );
  100. controls.maxPolarAngle = Math.PI * 0.45;
  101. controls.enablePan = false
  102. }
  103. // 辅助坐标轴
  104. addAxesHelper (num:number) {
  105. this.axesHelper = new AxesHelper( num )
  106. this.scene.add( this.axesHelper )
  107. }
  108. // 辅助网格
  109. addGridHelper (w:number,h:number) {
  110. this.gridHelper = new GridHelper( w,h )
  111. this.scene.add( this.gridHelper )
  112. }
  113. // 高亮效果
  114. outlineObj (selectedObjects:any) {
  115. if(this.renderer && this.camera){
  116. // 创建一个EffectComposer(效果组合器)对象,然后在该对象上添加后期处理通道。
  117. this.composer = new EffectComposer(this.renderer)
  118. // 新建一个场景通道 为了覆盖到原理来的场景上
  119. this.renderPass = new RenderPass(this.scene, this.camera)
  120. this.composer.addPass(this.renderPass);
  121. // 物体边缘发光通道
  122. this.outlinePass = new OutlinePass(new Vector2(this.ele.offsetWidth, this.ele.offsetHeight), this.scene, this.camera, selectedObjects)
  123. this.outlinePass.selectedObjects = selectedObjects
  124. this.outlinePass.edgeStrength = 10.0 // 边框的亮度
  125. this.outlinePass.edgeGlow = 1// 光晕[0,1]
  126. this.outlinePass.usePatternTexture = false // 是否使用父级的材质
  127. this.outlinePass.edgeThickness = 1.0 // 边框宽度
  128. this.outlinePass.downSampleRatio = 1 // 边框弯曲度
  129. this.outlinePass.pulsePeriod = 5 // 呼吸闪烁的速度
  130. this.outlinePass.visibleEdgeColor.set("#ffffff") // 呼吸显示的颜色
  131. this.outlinePass.hiddenEdgeColor = new Color(0, 0, 0) // 呼吸消失的颜色
  132. this.outlinePass.clear = true
  133. this.composer.addPass(this.outlinePass)
  134. // 自定义的着色器通道 作为参数
  135. var effectFXAA = new ShaderPass(FXAAShader)
  136. effectFXAA.uniforms.resolution.value.set(1 / this.ele.offsetWidth, 1 / this.ele.offsetHeight)
  137. effectFXAA.renderToScreen = true
  138. this.composer.addPass(effectFXAA)
  139. }
  140. }
  141. // 鼠标点击
  142. onMouseDown(callback:any){
  143. // 监听鼠标点击
  144. window.addEventListener('click', (event)=>{
  145. if(this.camera){
  146. let vector = new Vector3(
  147. ((event.clientX-this.ele.getBoundingClientRect().x) / this.ele.offsetWidth) * 2 - 1,
  148. -((event.clientY-this.ele.getBoundingClientRect().y) / this.ele.offsetHeight) * 2 + 1,
  149. 0.5
  150. )
  151. vector = vector.unproject(this.camera)
  152. const raycaster = new Raycaster(
  153. this.camera.position,
  154. vector.sub(this.camera.position).normalize()
  155. )
  156. this.intersects = raycaster.intersectObjects(this.meshList)
  157. callback && callback()
  158. }
  159. })
  160. }
  161. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/928595
推荐阅读
相关标签
  

闽ICP备14008679号