unity渲染带颜色的文字
从api inspector可以看到使用的是vertex array 111
glVertexAttribPointer
void glVertexAttribPointer( GLuint index, // 要修改的顶点属性的索引,也就是vertex shader里面的attribute location GLint size, // 顶点分量,是vec/vec2/vec3/vec4 GLenum type, // 数据类型,最常用的是GL_UNSIGNED_BYTE、GL_FLOAT GLboolean normalized, // 归一化 GLsizei stride, // 一个顶点数据的字节大小 const void * pointer// 如果缓冲对象绑定到GL_ARRAY_BUFFER,则pointer应该是一个偏移量,否则它可以是一个指针。 );
举例
V3F_C4B_T2F _verts[VBO_SIZE]; #define kQuadSize sizeof(_verts[0]) // vertices glVertexAttribPointer( GLProgram::VERTEX_ATTRIB_POSITION, // positon attribute location 3, // xyz是有3个float分量 GL_FLOAT, // float类型 GL_FALSE, kQuadSize, // 一个顶点的数据大小 (GLvoid*) offsetof(V3F_C4B_T2F, vertices)// 位置顶点数据在一个顶点数据中的偏移量 ); // colors glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors)); // tex coords glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords));
有了以上基础知识,看下unity的细节
glVertexAttribPointer(0, 3, GL_FLOAT, False, 32, 0) glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, True, 32, 12) glVertexAttribPointer(2, 4, GL_FLOAT, False, 32, 16)
反推下结构:
struct UnityVertexData{ Vec3F postion,//float 3 RGBA color;// unsigned char 4 Vec4F uv;// float 4 }
一个顶点的大小为32 = 3*4 + 4*1 + 4*4
推导出这个结构后,我们看下Buffer填充的数据,renderdoc是以字节为单位,16进制进行显示的,大致结构拆分如下,一共4个顶点数据
这里面应该有字节大端小端的问题,展示出来的十六进制数值有点庞大,我尝试换了个颜色绿色:0x00FF00FF,发现了应该是倒着读
对应的unity shader
从renderdoc里面拔出来的,不一定准确
#version 450 #extension GL_ARB_explicit_attrib_location : require #ifdef GL_ARB_shader_bit_encoding #extension GL_ARB_shader_bit_encoding : enable #endif #define HLSLCC_ENABLE_UNIFORM_BUFFERS 1 #if HLSLCC_ENABLE_UNIFORM_BUFFERS #define UNITY_UNIFORM #else #define UNITY_UNIFORM uniform #endif #define UNITY_SUPPORTS_UNIFORM_LOCATION 1 #if UNITY_SUPPORTS_UNIFORM_LOCATION #define UNITY_LOCATION(x) layout(location = x) #define UNITY_BINDING(x) layout(binding = x, std140) #else #define UNITY_LOCATION(x) #define UNITY_BINDING(x) layout(std140) #endif uniform vec4 _TextureSampleAdd; UNITY_LOCATION(0) uniform sampler2D _MainTex; in vec4 vs_COLOR0; in vec2 vs_TEXCOORD0; layout(location = 0) out vec4 SV_Target0; vec4 u_xlat0; void main() { u_xlat0 = texture(_MainTex, vs_TEXCOORD0.xy); u_xlat0 = u_xlat0 + _TextureSampleAdd; u_xlat0 = u_xlat0 * vs_COLOR0; SV_Target0.xyz = u_xlat0.www * u_xlat0.xyz; SV_Target0.w = u_xlat0.w; return; }
#version 450 #extension GL_ARB_explicit_attrib_location : require #ifdef GL_ARB_shader_bit_encoding #extension GL_ARB_shader_bit_encoding : enable #endif #define HLSLCC_ENABLE_UNIFORM_BUFFERS 1 #if HLSLCC_ENABLE_UNIFORM_BUFFERS #define UNITY_UNIFORM #else #define UNITY_UNIFORM uniform #endif #define UNITY_SUPPORTS_UNIFORM_LOCATION 1 #if UNITY_SUPPORTS_UNIFORM_LOCATION #define UNITY_LOCATION(x) layout(location = x) #define UNITY_BINDING(x) layout(binding = x, std140) #else #define UNITY_LOCATION(x) #define UNITY_BINDING(x) layout(std140) #endif uniform vec4 _ScreenParams; uniform vec4 hlslcc_mtx4x4unity_ObjectToWorld[4]; uniform vec4 hlslcc_mtx4x4glstate_matrix_projection[4]; uniform vec4 hlslcc_mtx4x4unity_MatrixVP[4]; uniform vec4 _Color; uniform vec4 _ClipRect; uniform vec4 _MainTex_ST; uniform float _UIMaskSoftnessX; uniform float _UIMaskSoftnessY; in vec4 in_POSITION0; in vec4 in_COLOR0; in vec2 in_TEXCOORD0; out vec4 vs_COLOR0; out vec2 vs_TEXCOORD0; out vec4 vs_TEXCOORD1; out vec4 vs_TEXCOORD2; vec4 u_xlat0; vec4 u_xlat1; void main() { u_xlat0 = in_POSITION0.yyyy * hlslcc_mtx4x4unity_ObjectToWorld[1]; u_xlat0 = hlslcc_mtx4x4unity_ObjectToWorld[0] * in_POSITION0.xxxx + u_xlat0; u_xlat0 = hlslcc_mtx4x4unity_ObjectToWorld[2] * in_POSITION0.zzzz + u_xlat0; u_xlat0 = u_xlat0 + hlslcc_mtx4x4unity_ObjectToWorld[3]; u_xlat1 = u_xlat0.yyyy * hlslcc_mtx4x4unity_MatrixVP[1]; u_xlat1 = hlslcc_mtx4x4unity_MatrixVP[0] * u_xlat0.xxxx + u_xlat1; u_xlat1 = hlslcc_mtx4x4unity_MatrixVP[2] * u_xlat0.zzzz + u_xlat1; u_xlat0 = hlslcc_mtx4x4unity_MatrixVP[3] * u_xlat0.wwww + u_xlat1; gl_Position = u_xlat0; vs_COLOR0 = in_COLOR0 * _Color; vs_TEXCOORD0.xy = in_TEXCOORD0.xy * _MainTex_ST.xy + _MainTex_ST.zw; vs_TEXCOORD1 = in_POSITION0; u_xlat0.xy = _ScreenParams.yy * hlslcc_mtx4x4glstate_matrix_projection[1].xy; u_xlat0.xy = hlslcc_mtx4x4glstate_matrix_projection[0].xy * _ScreenParams.xx + u_xlat0.xy; u_xlat0.xy = u_xlat0.ww / abs(u_xlat0.xy); u_xlat0.xy = vec2(_UIMaskSoftnessX, _UIMaskSoftnessY) * vec2(0.25, 0.25) + abs(u_xlat0.xy); vs_TEXCOORD2.zw = vec2(0.25, 0.25) / u_xlat0.xy; u_xlat0 = max(_ClipRect, vec4(-2e+10, -2e+10, -2e+10, -2e+10)); u_xlat0 = min(u_xlat0, vec4(2e+10, 2e+10, 2e+10, 2e+10)); u_xlat0.xy = in_POSITION0.xy * vec2(2.0, 2.0) + (-u_xlat0.xy); vs_TEXCOORD2.xy = (-u_xlat0.zw) + u_xlat0.xy; return; }