01 glTF(gl传输格式)

如果有可能的话,我们推荐使用glTF(gl传输格式)。.GLB和.GLTF是这种格式的这两种不同版本, 都可以被很好地支持。由于glTF这种格式是专注于在程序运行时呈现三维物体的,所以它的传输效率非常高,且加载速度非常快。 功能方面则包括了网格、材质、纹理、皮肤、骨骼、变形目标、动画、灯光和摄像机。

代码

  1. import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
  2. const loader = new GLTFLoader();
  3. loader.load( 'path/to/model.glb', function ( gltf ) {
  4. scene.add( gltf.scene );
  5. }, undefined, function ( error ) {
  6. console.error( error );
  7. } );

实例属性

  1. animations: [] // 动画属性
  2. castShadow: false
  3. children: [Object3D] // 子集
  4. frustumCulled: true
  5. layers: Layers {mask: 1}
  6. matrix: Matrix4 {elements: Array(16)}
  7. matrixAutoUpdate: true
  8. matrixWorld: Matrix4 {elements: Array(16)}
  9. matrixWorldNeedsUpdate: false
  10. name: "OSG_Scene"
  11. parent: Scene {uuid: "0FE5FF04-91EA-4A20-B92C-D11842A89A19", name: "", type: "Scene", parent: null, children: Array(2), …}
  12. position: Vector3 {x: -12.94427190999916, y: -8.533076741628822, z: -14.547591760888809}
  13. quaternion: Quaternion {_x: 0.2116299842512363, _y: -0.30126929315466977, _z: -0.06876275019668644, _w: 0.9272115437985525, _onChangeCallback: ƒ}
  14. receiveShadow: false
  15. renderOrder: 0
  16. rotation: Euler {_x: 0.44879895051282753, _y: -0.6283185307179585, _z: -0, _order: "XYZ", _onChangeCallback: ƒ}
  17. scale: Vector3 {x: 1.4, y: 1.4, z: 1.4}
  18. type: "Group"
  19. up: Vector3 {x: 0, y: 1, z: 0}
  20. userData: {}
  21. uuid: "74CBCCF7-2898-463A-B707-B916E0CC775C"
  22. visible: true
  23. eulerOrder: (...)
  24. id: 5
  25. modelViewMatrix: Matrix4 {elements: Array(16)}
  26. normalMatrix: Matrix3 {elements: Array(9)}
  27. useQuaternion: (...)
  28. __proto__: Object3D

threejs原型

  1. add: ƒ ( object )
  2. applyMatrix: ƒ ( matrix )
  3. applyMatrix4: ƒ ( matrix )
  4. applyQuaternion: ƒ ( q )
  5. attach: ƒ ( object )
  6. clear: ƒ ()
  7. clone: ƒ ( recursive )
  8. constructor: ƒ Object3D()
  9. copy: ƒ ( source, recursive = true )
  10. getChildByName: ƒ ( name )
  11. getObjectById: ƒ ( id )
  12. getObjectByName: ƒ ( name )
  13. getObjectByProperty: ƒ ( name, value )
  14. getWorldDirection: ƒ ( target )
  15. getWorldPosition: ƒ ( target )
  16. getWorldQuaternion: ƒ ( target )
  17. getWorldRotation: ƒ ()
  18. getWorldScale: ƒ ( target )
  19. isObject3D: true
  20. localToWorld: ƒ ( vector )
  21. lookAt: ƒ ( x, y, z )
  22. onAfterRender: ƒ ()
  23. onBeforeRender: ƒ ()
  24. raycast: ƒ ()
  25. remove: ƒ ( object )
  26. renderDepth: ƒ ()
  27. rotateOnAxis: ƒ ( axis, angle ) // 改变物体的旋转角度
  28. rotateOnWorldAxis: ƒ ( axis, angle ) // 改变物体的旋转角度 X
  29. rotateX: ƒ ( angle ) // 改变物体的旋转角度 X
  30. rotateY: ƒ ( angle ) // 改变物体的旋转角度 Y
  31. rotateZ: ƒ ( angle ) // 改变物体的旋转角度 Z
  32. setRotationFromAxisAngle: ƒ ( axis, angle )
  33. setRotationFromEuler: ƒ ( euler )
  34. setRotationFromMatrix: ƒ ( m )
  35. setRotationFromQuaternion: ƒ ( q )
  36. toJSON: ƒ ( meta )
  37. translate: ƒ ( distance, axis ) // 改变位置外加是哪个方向
  38. translateOnAxis: ƒ ( axis, distance ) // 改变哪个方向的位置
  39. translateX: ƒ ( distance ) // 只改变x轴的位置
  40. translateY: ƒ ( distance ) // 只改变y轴的位置
  41. translateZ: ƒ ( distance ) // 只改变z轴的位置
  42. traverse: ƒ ( callback )
  43. traverseAncestors: ƒ ( callback )
  44. traverseVisible: ƒ ( callback )
  45. updateMatrix: ƒ ()
  46. updateMatrixWorld: ƒ ( force )
  47. updateWorldMatrix: ƒ ( updateParents, updateChildren )
  48. worldToLocal: ƒ ( vector )

02 基本用法

一 position [ Vector3 ] (x,y,z) 设置位置

表示导入模型的位置信息;有一组x,y,z,的坐标信息表示三维中的数据的位置,可以通过对象的属性去改变,也可以通过.set(x,y,z)或者.setX,.setY,setZ,同样也可以three上面的方法 translate, translateX,translateY,translateZ,translateOnAxis去改变;当然还有一种方法就是直接改变对应的相机的视角也可以做到

还有一些不常用

  1. applyProjection: ƒ ( m )
  2. distanceToManhattan: ƒ ( v )
  3. fromAttribute: ƒ ( attribute, index, offset )
  4. getColumnFromMatrix: ƒ ( index, matrix )
  5. getPositionFromMatrix: ƒ ( m )
  6. getScaleFromMatrix: ƒ ( m )
  7. isVector3: true
  8. lengthManhattan: ƒ ()
  9. setEulerFromQuaternion: ƒ ()
  10. setEulerFromRotationMatrix: ƒ ()
  11. add: ƒ add( v, w )
  12. addScalar: ƒ addScalar( s )
  13. addScaledVector: ƒ addScaledVector( v, s )
  14. addVectors: ƒ addVectors( a, b )
  15. angleTo: ƒ angleTo( v )
  16. applyAxisAngle: ƒ applyAxisAngle( axis, angle )
  17. applyEuler: ƒ applyEuler( euler )
  18. applyMatrix3: ƒ applyMatrix3( m )
  19. applyMatrix4: ƒ applyMatrix4( m )
  20. applyNormalMatrix: ƒ applyNormalMatrix( m )
  21. applyQuaternion: ƒ applyQuaternion( q )
  22. ceil: ƒ ceil()
  23. clamp: ƒ clamp( min, max )
  24. clampLength: ƒ clampLength( min, max )
  25. clampScalar: ƒ clampScalar( minVal, maxVal )
  26. clone: ƒ clone()
  27. constructor: class Vector3
  28. copy: ƒ copy( v )
  29. cross: ƒ cross( v, w )
  30. crossVectors: ƒ crossVectors( a, b )
  31. distanceTo: ƒ distanceTo( v )
  32. distanceToSquared: ƒ distanceToSquared( v )
  33. divide: ƒ divide( v )
  34. divideScalar: ƒ divideScalar( scalar )
  35. dot: ƒ dot( v )
  36. equals: ƒ equals( v )
  37. floor: ƒ floor()
  38. fromArray: ƒ fromArray( array, offset = 0 )
  39. fromBufferAttribute: ƒ fromBufferAttribute( attribute, index, offset )
  40. getComponent: ƒ getComponent( index )
  41. length: ƒ length()
  42. lengthSq: ƒ lengthSq()
  43. lerp: ƒ lerp( v, alpha )
  44. lerpVectors: ƒ lerpVectors( v1, v2, alpha )
  45. manhattanDistanceTo: ƒ manhattanDistanceTo( v )
  46. manhattanLength: ƒ manhattanLength()
  47. max: ƒ max( v )
  48. min: ƒ min( v )
  49. multiply: ƒ multiply( v, w )
  50. multiplyScalar: ƒ multiplyScalar( scalar )
  51. multiplyVectors: ƒ multiplyVectors( a, b )
  52. negate: ƒ negate()
  53. normalize: ƒ normalize()
  54. project: ƒ project( camera )
  55. projectOnPlane: ƒ projectOnPlane( planeNormal )
  56. projectOnVector: ƒ projectOnVector( v )
  57. random: ƒ random()
  58. reflect: ƒ reflect( normal )
  59. round: ƒ round()
  60. roundToZero: ƒ roundToZero()
  61. set: ƒ set( x, y, z ) // 设置三维中的x,y,z的位置
  62. setComponent: ƒ setComponent( index, value )
  63. setFromCylindrical: ƒ setFromCylindrical( c )
  64. setFromCylindricalCoords: ƒ setFromCylindricalCoords( radius, theta, y )
  65. setFromMatrix3Column: ƒ setFromMatrix3Column( m, index )
  66. setFromMatrixColumn: ƒ setFromMatrixColumn( m, index )
  67. setFromMatrixPosition: ƒ setFromMatrixPosition( m )
  68. setFromMatrixScale: ƒ setFromMatrixScale( m )
  69. setFromSpherical: ƒ setFromSpherical( s )
  70. setFromSphericalCoords: ƒ setFromSphericalCoords( radius, phi, theta )
  71. setLength: ƒ setLength( length )
  72. setScalar: ƒ setScalar( scalar )
  73. setX: ƒ setX( x ) // 设置x轴的位置
  74. setY: ƒ setY( y ) // 设置y轴的位置
  75. setZ: ƒ setZ( z ) // 设置z轴的位置
  76. sub: ƒ sub( v, w ) //
  77. subScalar: ƒ subScalar( s )
  78. subVectors: ƒ subVectors( a, b )
  79. toArray: ƒ toArray( array = [], offset = 0 )
  80. transformDirection: ƒ transformDirection( m )
  81. unproject: ƒ unproject( camera )

**

二 rotation (x,y,z) 改变物体所对应的旋转角度

可能通过对于一个属性上去理解旋转角度不好理解但是你也可以理解为物体的朝向的三维坐标的对应,也可以通过.set(x,y,z, order )同样也可以three上面的方法 rotateOnWorldAxis, rotateX,rotateY,rotateZ,rotateOnAxis去改变;当然还有一种方法就是直接改变对应的相机的视角也可以做到

  1. isEuler: true
  2. clone: ƒ clone()
  3. constructor: class Euler
  4. copy: ƒ copy( euler )
  5. equals: ƒ equals( euler )
  6. fromArray: ƒ fromArray( array )
  7. order: (...)
  8. reorder: ƒ reorder( newOrder )
  9. set: ƒ set( x, y, z, order )
  10. setFromQuaternion: ƒ setFromQuaternion( q, order, update )
  11. setFromRotationMatrix: ƒ setFromRotationMatrix( m, order, update )
  12. setFromVector3: ƒ setFromVector3( v, order )
  13. toArray: ƒ toArray( array = [], offset = 0 )
  14. toVector3: ƒ toVector3( optionalResult )
  15. x: (...)
  16. y: (...)
  17. z: (...)
  18. _onChange: ƒ _onChange( callback )
  19. _onChangeCallback: ƒ _onChangeCallback()
  20. get order: ƒ order()
  21. set order: ƒ order( value )
  22. get x: ƒ x()
  23. set x: ƒ x( value )
  24. get y: ƒ y()
  25. set y: ƒ y( value )
  26. get z: ƒ z()
  27. set z: ƒ z( value )

三 scale [ Vector3 ] (x,y,z) 改变物体缩放比例

属性上面表示的是物体的缩放比例,可以通过.set(x,y,z)或者.setX,.setY,setZ,three上面的没有对应方法去改变,当然还有一种方法就是直接改变对应的相机的视角也可以做到

  1. applyProjection: ƒ ( m )
  2. distanceToManhattan: ƒ ( v )
  3. fromAttribute: ƒ ( attribute, index, offset )
  4. getColumnFromMatrix: ƒ ( index, matrix )
  5. getPositionFromMatrix: ƒ ( m )
  6. getScaleFromMatrix: ƒ ( m )
  7. isVector3: true
  8. lengthManhattan: ƒ ()
  9. setEulerFromQuaternion: ƒ ()
  10. setEulerFromRotationMatrix: ƒ ()
  11. add: ƒ add( v, w )
  12. addScalar: ƒ addScalar( s )
  13. addScaledVector: ƒ addScaledVector( v, s )
  14. addVectors: ƒ addVectors( a, b )
  15. angleTo: ƒ angleTo( v )
  16. applyAxisAngle: ƒ applyAxisAngle( axis, angle )
  17. applyEuler: ƒ applyEuler( euler )
  18. applyMatrix3: ƒ applyMatrix3( m )
  19. applyMatrix4: ƒ applyMatrix4( m )
  20. applyNormalMatrix: ƒ applyNormalMatrix( m )
  21. applyQuaternion: ƒ applyQuaternion( q )
  22. ceil: ƒ ceil()
  23. clamp: ƒ clamp( min, max )
  24. clampLength: ƒ clampLength( min, max )
  25. clampScalar: ƒ clampScalar( minVal, maxVal )
  26. clone: ƒ clone()
  27. constructor: class Vector3
  28. copy: ƒ copy( v )
  29. cross: ƒ cross( v, w )
  30. crossVectors: ƒ crossVectors( a, b )
  31. distanceTo: ƒ distanceTo( v )
  32. distanceToSquared: ƒ distanceToSquared( v )
  33. divide: ƒ divide( v )
  34. divideScalar: ƒ divideScalar( scalar )
  35. dot: ƒ dot( v )
  36. equals: ƒ equals( v )
  37. floor: ƒ floor()
  38. fromArray: ƒ fromArray( array, offset = 0 )
  39. fromBufferAttribute: ƒ fromBufferAttribute( attribute, index, offset )
  40. getComponent: ƒ getComponent( index )
  41. length: ƒ length()
  42. lengthSq: ƒ lengthSq()
  43. lerp: ƒ lerp( v, alpha )
  44. lerpVectors: ƒ lerpVectors( v1, v2, alpha )
  45. manhattanDistanceTo: ƒ manhattanDistanceTo( v )
  46. manhattanLength: ƒ manhattanLength()
  47. max: ƒ max( v )
  48. min: ƒ min( v )
  49. multiply: ƒ multiply( v, w )
  50. multiplyScalar: ƒ multiplyScalar( scalar )
  51. multiplyVectors: ƒ multiplyVectors( a, b )
  52. negate: ƒ negate()
  53. normalize: ƒ normalize()
  54. project: ƒ project( camera )
  55. projectOnPlane: ƒ projectOnPlane( planeNormal )
  56. projectOnVector: ƒ projectOnVector( v )
  57. random: ƒ random()
  58. reflect: ƒ reflect( normal )
  59. round: ƒ round()
  60. roundToZero: ƒ roundToZero()
  61. set: ƒ set( x, y, z ) // 改变缩放的比例自不过对应的三个数值
  62. setComponent: ƒ setComponent( index, value )
  63. setFromCylindrical: ƒ setFromCylindrical( c )
  64. setFromCylindricalCoords: ƒ setFromCylindricalCoords( radius, theta, y )
  65. setFromMatrix3Column: ƒ setFromMatrix3Column( m, index )
  66. setFromMatrixColumn: ƒ setFromMatrixColumn( m, index )
  67. setFromMatrixPosition: ƒ setFromMatrixPosition( m )
  68. setFromMatrixScale: ƒ setFromMatrixScale( m )
  69. setFromSpherical: ƒ setFromSpherical( s )
  70. setFromSphericalCoords: ƒ setFromSphericalCoords( radius, phi, theta )
  71. setLength: ƒ setLength( length )
  72. setScalar: ƒ setScalar( scalar )
  73. setX: ƒ setX( x ) // 改变x轴的缩放比例
  74. setY: ƒ setY( y ) // 改变y轴的缩放比例
  75. setZ: ƒ setZ( z ) // 改变z轴的缩放比例
  76. sub: ƒ sub( v, w )
  77. subScalar: ƒ subScalar( s )
  78. subVectors: ƒ subVectors( a, b )
  79. toArray: ƒ toArray( array = [], offset = 0 )
  80. transformDirection: ƒ transformDirection( m )
  81. unproject: ƒ unproject( camera )

目前来看导入到threejs中的模型,如果需要在gpu中渲染的话,第一使用的是内置的threejs中的shader的话,那么首先应该是一个BufferGeometry原型,究竟其根本来说也就是所有的数据都存在于
geometry.setAttribute( ‘position’, new THREE.BufferAttribute( vertices, 3 ) );
一般的自定义是属性都可以通过这种方式来添加点的位置,贴图材质,颜色的等信息去改变模型的基本信息,然后shader里面可以获取到Attribute的从缓存中拿到的属性,用于渲染。

所以导入进来的模型;我们有两种方式去用于粒子的渲染,第一找到模型的BufferGeometry,然后利用这个模型去渲染就可以,还有一种就是实例一个BufferGeometry然后获取其中的position重新赋值也可以;