stage3d编程-基础6 关于uv,采样算法
1、纹理坐标
之前谈到,顶点数据组成了三角形,很多个三角形组成了模型。但是这个也只是一个线框图。为了让它看起来跟真实的一样,我们需要一个纹理图,给他贴上去。将它包起来,那么它看起来就跟真实的一样了。但是一张2d的平面图,如何能够贴上去呢。那么这个时候,我们需要一个坐标来解决这个问题了。我们称纹理坐标为uv坐标。u是水平,v是垂直向下,坐标原点是纹理图的左上角(0,0)。但是大家想一下,纹理图可大可小,小到4,8,16,32.大到1024x1024,1024x2048。尺寸完全是乱的,不同的,所以这个时候,我们就不能通过纹理图的尺寸来设置坐标了。但是我们可以使用比率来做。一张图,最大为1,最小为0.中间为0.5。这个比率总不会变把!!!。因此呢,gpu就通过这种方式来根据uv值对纹理图进行采样。我们先来看最简单的,正方形。额外参考http://17de.com/library/d3d_6im/d3dim6_18.htm。

大家看这个正方形。其实看了一眼就明了。原来是这样的。一共四个顶点。
假设(右手坐标系):
左上角:-1,1,0
右上角: 1,1,0
左下角:-1,-1,0
右下角: 1,-1,0
虽然顶点只有四个,但是我们可以通过索引来组合三角形。大家在看demo的时候,也就可以发现索引数据是0,1,2,0,2,3。
如果我们把uv坐标也加上,那么就变成了:
左上角:-1,1,0 0,0
右上角: 1,1,0 1,0
左下角:-1,-1,0 0,1
右下角: 1,-1,0 1,1
那么上面就有了四段数据,我们就可以通过创建vertexbuffer的时候,设置一段长度为5,总共4段数据来创建vertexbuffer了。然后
context3D.setVertexBufferAt(0, vertexBuffer, 0,Context3DVertexBufferFormat.FLOAT_3); 设置va0为顶点数据。context3D.setVertexBufferAt(0, vertexBuffer, 1,Context3DVertexBufferFormat.FLOAT_2); 设置va1为uv数据。(我之前贴的那个demo我没有细看,它上传了三个进去,但是不影响,因为只会用到这个寄存器的前面两个值)




-
texture dimension: 2d, 3d, or cube. A 2D texture is the most commonly used format, a rectangular bitmap. A 3D texture has length, width, and depth, and takes up a lot of texture RAM but is great for 3D materials such as wood, grain, or marble. It is rarely used. A cube texture is a specially encoded group of six rectangular bitmaps and is usually used for reflections, as each of these six images maps to a particular direction like the sides on the inside of a box.
-
mip mapping: nomip, mipnone, mipnearest, or miplinear. If your texture has mip maps (successively smaller versions of itself, generated to avoid jaggies during rendering), then you can instruct Stage3D to smoothly interpolate the texture using them. Mip mapped textures are very useful to increase render quality and avoid the "moiree effect" (flickering) when viewed from far away.
-
texture filtering: nearest or linear. If you prefer a retro, then pixilated look you can use nearest here to tell Stage3D not to interpolate the values smoothly, resulting in blocky textures when viewed up close, for example. If you use linear, when zoomed into a texture it will be blurry and will have smoother edges.
-
texture repeat: repeat, wrap, or clamp. If you want the texture tile properly, then you use repeat here. If you are not tiling the texture and run into problems with edge pixels being the color of the opposite edge (which often happens in transparent billboard particles and when rendering bitmap fonts in 3d), then specify clamp to force Stage3D not to blur adjacent edge pixels.
浙公网安备 33010602011771号