模型烘焙,就是把一个四边形,通过一步步方法,烘焙成供mc渲染管线使用来渲染一个四边形面的BakedQuad对象。
可以首先来看看BakedModel的构造方法
1 | public BakedQuad(int[] pVertices, int pTintIndex, Direction pDirection, TextureAtlasSprite pSprite, boolean pShade, boolean hasAmbientOcclusion) { |
其中vertices是最重要的顶点数据,是int[32]数组,它是由四个顶点构成,每个顶点数据由8个int数据构成,拼接为32长度的数组。
direction是这个面的朝向,用于mc进行单面剔除来优化渲染。
sprite是这个面的材质精灵图。
这时候我们来看一下烘焙模型的代码
1 | for (ModelQuadLayer pc : mpc) { |
getFaceUVs方法中将float[4] uvs进行变换,由from和to两个Vector3f对象和direction方向得出在材质平面上的长方形的两个顶点坐标。
得到float[4]{u1,v1,u2,v2}
得到了uv,就要开始烘焙顶点数据了,也就是makeVertices方法,这个方法从原版的模型烘焙类FaceBakery中抄来
1 | private int[] makeVertices(TextureAtlasSprite sprite, BlockFaceUV pBlockFaceUV,float[] pos, Direction direction, int[] to, int[] from) { |
在这里分别把四个顶点的数据通过fillVertex方法写进data[32],返回装好了顶点数据的浮点数数组
1 | private void fillVertex(int[] pVertexData, int pVertexIndex, Vector3f pVector, TextureAtlasSprite pSprite, BlockFaceUV pBlockFaceUV) { |
最后再用原版方法为面补上色彩和光照数据,结束模型烘焙
1 | applyingLightmap(data.blockLight(), data.skyLight()).processInPlace(quad); |