/ chmod777_includes / shaders / draw_unit_custom.vs.glsl
draw_unit_custom.vs.glsl
  1  #version 420
  2  #extension GL_ARB_uniform_buffer_object : require
  3  #extension GL_ARB_shader_storage_buffer_object : require
  4  #extension GL_ARB_shading_language_420pack: require
  5  
  6  // File: draw_unit_custom.vs.glsl
  7  // Author: chmod777
  8  // Originally based on a shader by Beherith and Ivand (gfx_drawunitshape_gl4.lua)
  9  
 10  /*
 11  Copyright (C) 2024 chmod777
 12  
 13  This program is free software: you can redistribute it and/or modify it under
 14  the terms of the GNU Affero General Public License version 3 as published by the
 15  Free Software Foundation.
 16  
 17  This program is distributed in the hope that it will be useful, but WITHOUT ANY
 18  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 19  PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
 20  
 21  You should have received a copy of the GNU Affero General Public License along
 22  with this program. If not, see <https://www.gnu.org/licenses/>. 
 23  */
 24  
 25  layout (location = 0) in vec3 aPosition;
 26  layout (location = 1) in vec3 aNormal;
 27  layout (location = 2) in vec3 aTangent;
 28  layout (location = 3) in vec3 aBitangent;
 29  layout (location = 4) in vec4 aUV;
 30  layout (location = 5) in uvec2 bonesInfo; //boneIDs, boneWeights
 31  
 32  #define pieceIndex (bonesInfo.x & 0x000000FFu)
 33  
 34  layout (location = 6) in vec4 iWorldPosRot;  // xyz = pos, w = rotation
 35  layout (location = 10) in uvec4 instData;
 36  
 37  uniform vec3 uCamEye;       // xyz, w = unused
 38  uniform vec3 uCamTarget;    // xyz, w = unused
 39  uniform vec4 uPerspParams;  // x = near, y = far, z = fovy, w = aspect(unused)
 40  
 41  //__ENGINEUNIFORMBUFFERDEFS__
 42  
 43  layout(std140, binding=0) buffer MatrixBuffer {
 44  	mat4 mat[];
 45  };
 46  
 47  out DataVS {
 48  	vec2 uv;
 49  	vec3 currentTeamColor;
 50  
 51  	vec3 camEye;
 52  	vec3 camTarget;
 53  
 54  	// vec4 modelPosition;
 55  	// vec3 modelNormal;
 56  	// vec3 modelTangent;
 57  	// vec3 modelBitangent;
 58  
 59  	vec4 worldPosition;
 60  	vec3 worldNormal;
 61  	// vec3 worldTangent;
 62  	// vec3 worldBitangent;
 63  
 64  	// vec4 viewPosition;
 65  	// vec3 viewNormal;
 66  	// vec3 viewTangent;
 67  	// vec3 viewBitangent;
 68  
 69  	// mat3 modelTBN;
 70  	mat3 worldTBN;
 71  	// mat3 viewTBN;
 72  } OUT;
 73  
 74  
 75  mat4 LookAtTarget(vec3 eye, vec3 target);
 76  mat4 perspective(float near, float far, float fovy);
 77  
 78  void main() {
 79  	uint baseIndex = instData.x;
 80  
 81  	mat4 baseMatrix = mat[baseIndex];
 82  	baseMatrix = mat4(1.0);
 83  	mat4 pieceMatrix = mat[baseIndex + pieceIndex + 1u];
 84  
 85  	float rotation = iWorldPosRot.w;
 86  	mat4 modelRotationMatrix = mat4(rotation3dY(rotation));
 87  
 88  	mat4 modelTranslation = mat4(1.0);
 89  	modelTranslation[3] = vec4(iWorldPosRot.xyz, 1.0);
 90  
 91  	mat4 modelMat = modelTranslation
 92  		* modelRotationMatrix
 93  		* baseMatrix
 94  		* pieceMatrix;
 95  	mat4 viewMat = LookAtTarget(uCamEye, uCamTarget);
 96  	mat4 projMat = perspective(uPerspParams.x, uPerspParams.y, uPerspParams.z);
 97  
 98  	mat3 modelMat3 = mat3(modelMat);
 99  	mat4 modelViewMat = viewMat * modelMat;
100  	// mat3 modelViewMat3 = mat3(modelViewMat);
101  
102  	gl_Position = projMat * modelViewMat * vec4(aPosition, 1.0);
103  
104  	OUT.uv = aUV.xy;
105  
106  	uint teamIndex = (instData.z & 0x000000FFu); // leftmost ubyte is teamIndex
107  	OUT.currentTeamColor = teamColor[teamIndex].rgb;
108  
109  	OUT.camEye = uCamEye;
110  	OUT.camTarget = uCamTarget;
111  
112  	// OUT.modelPosition  = vec4(aPosition, 1.0);
113  	// OUT.modelNormal    = aNormal;
114  	// OUT.modelTangent   = aTangent;
115  	// OUT.modelBitangent = aBitangent;
116  
117  	OUT.worldPosition  = modelMat  * vec4(aPosition, 1.0);
118  	OUT.worldNormal    = modelMat3 * aNormal;
119  	// OUT.worldTangent   = modelMat3 * aTangent;
120  	// OUT.worldBitangent = modelMat3 * aBitangent;
121  
122  	// OUT.viewPosition  = modelViewMat  * vec4(aPosition, 1.0);
123  	// OUT.viewNormal    = modelViewMat3 * aNormal;
124  	// OUT.viewTangent   = modelViewMat3 * aTangent;
125  	// OUT.viewBitangent = modelViewMat3 * aBitangent;
126  
127  	// OUT.modelTBN = mat3(OUT.modelTangent, OUT.modelBitangent, OUT.modelNormal);
128  	// OUT.worldTBN = mat3(OUT.worldTangent, OUT.worldBitangent, OUT.worldNormal);
129  	// OUT.viewTBN  = mat3(OUT.viewTangent,  OUT.viewBitangent,  OUT.viewNormal);
130  
131  	// https://learnopengl.com/PBR/Lighting ------------------------------------
132  		// vec3 T = normalize(OUT.worldTangent);
133  		// vec3 N = normalize(OUT.worldNormal);
134  		// // re-orthogonalize T with respect to N
135  		// T = normalize(T - dot(T, N) * N);
136  		// // then retrieve perpendicular vector B with the cross product of T and N
137  		// vec3 B = cross(N, T);
138  		// OUT.worldTBN = mat3(T, B, N);
139  	// https://learnopengl.com/PBR/Lighting ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
140  }
141  
142  
143  // ShieldSphereColor.frag
144  mat4 LookAtTarget(vec3 eye, vec3 target) {
145  	vec3 localUp = vec3(0.0, 1.0, 0.0);
146  
147  	vec3 zaxis = normalize(eye - target);
148  	vec3 xaxis = normalize(cross(localUp, zaxis));
149  	vec3 yaxis = cross(zaxis, xaxis);
150  
151  	mat4 lookAtMatrix;
152  
153  	lookAtMatrix[0] = vec4(xaxis.x, yaxis.x, zaxis.x, 0.0);
154  	lookAtMatrix[1] = vec4(xaxis.y, yaxis.y, zaxis.y, 0.0);
155  	lookAtMatrix[2] = vec4(xaxis.z, yaxis.z, zaxis.z, 0.0);
156  	lookAtMatrix[3] = vec4(dot(xaxis, -eye), dot(yaxis, -eye), dot(zaxis, -eye), 1.0);
157  
158  	return lookAtMatrix;
159  }
160  
161  mat4 perspective(float near, float far, float fovy) {
162  	float half_fov = fovy/2.0;
163  	float cot = cos(half_fov)/sin(half_fov);
164  	float f = tan(fovy / 2.0);
165  	
166  	float aspect = 1.0;
167  	float a = (far + near) / (near - far);
168  	float b = (2 * far * near) / (near - far);
169  
170  	return mat4(
171  		vec4(f / aspect, 0, 0, 0),
172  		vec4(0, f, 0, 0),
173  		vec4(0, 0, a, -1),
174  		vec4(0, 0, b, 0)
175  	);
176  }