在非使用unity作为3D渲染方案的前提下,对与目前web开发者比较友好的除了canvas场景需要的2D babylon.js
,fabric.js
, Three.js是目前针对于jsWeb用户最直接且比较友好的3D引擎方案了。
准备工作:
1.明确需要用的场景方案都有那些,模型需要的加载器是什么
2.模型的场景大小已经相关的交互业务
3.场景的工作环境(浏览器及硬件要求)
step1:
以.glb模型为例
import * as THREE from "three";
import {
GLTFLoader} from "three/examples/jsm/loaders/GLTFLoader";
import {
OrbitControls} from "three/examples/jsm/controls/OrbitControls";
以上就是一个场景绘制需要的基本3个要素
模型压缩
由于建模工程师因为场景规模的原因模型在建立时过多了使用了面,导致整个模型的体积很大,一个校区或者大园区为例,建筑加环境要素及周边地形都整体的模型体积已经达到了100M+,这个时候就需要我们在开发前就考虑模型的压缩问题了
DRACO压缩算法
npm install -g gltf-pipeline
--input, -i Path to the glTF or glb file.[string] [required]
--output, -o Output path of the glTF or glb file. Separate
resources will be saved to the same directory.
[string]
--binary, -b Convert the input glTF to glb. //将输入的glTF转换为glb
[boolean] [default: false]
--json, -j Convert the input glb to glTF. //将输入的glb转换为glTF
[boolean] [default: false]
--separate, -s Write separate buffers, shaders, and textures //编写单独的缓冲区、着色器和纹理而不是把它们嵌入到glTF中
instead of embedding them in the glTF.
[boolean] [default: false]
--separateTextures, -t Write out separate textures only. //只写出单独的纹理
[boolean] [default: false]
--stats Print statistics to console for output glTF //将统计信息打印到控制台以输出glTF文件
file. [boolean] [default: false]
--keepUnusedElements Keep unused materials, nodes and meshes. //保留未使用的材质、节点和网格
[boolean] [default: false]
--draco.compressMeshes, -d Compress the meshes using Draco. Adds the //使用Draco压缩网格。添加KHR_draco_mesh_压缩扩展
KHR_draco_mesh_compression extension.
[boolean] [default: false]
--draco.compressionLevel Draco compression level [0-10], most is 10, //Draco压缩级别[0-10],大多数是10,最小值为0。值为0将会连续应用 编码并保留face顺序。
least is 0. A value of 0 will apply sequential
encoding and preserve face order.
[number] [default: 7]
--draco.quantizePositionBits Quantization bits for position attribute when //位置坐标属性的量化位使用Draco压缩。
using Draco compression. [number] [default: 11]
--draco.quantizeNormalBits Quantization bits for normal attribute when //法线属性的量化位使用Draco压缩
using Draco compression. [number] [default: 8]
--draco.quantizeTexcoordBits Quantization bits for texture coordinate //纹理坐标的量化位属性。
attribute when using Draco compression.
[number] [default: 10]
--draco.quantizeColorBits Quantization bits for color attribute when using //使用时颜色属性的量化位德拉科压缩
Draco compression. [number] [default: 8]
--draco.quantizeGenericBits Quantization bits for skinning attribute (joint //蒙皮属性(关节的量化位索引和关节权重)ad自定义属性使用Draco压缩时。
indices and joint weights) ad custom attributes
when using Draco compression. [number] [default: 8]
--draco.uncompressedFallback Adds uncompressed fallback