Skip to content

Commit

Permalink
Merge pull request #477 from xBimTeam/fix/texture-indices-mapping
Browse files Browse the repository at this point in the history
Fixed vertex index to 2D texture coordinates in shader
  • Loading branch information
Ibrahim5aad authored Oct 31, 2024
2 parents a5d315d + 3556c5d commit 3a76bf1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
42 changes: 24 additions & 18 deletions src/shaders/vertex_shader_300.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,22 @@ vec4 getIdColor(float id) {
return vec4(R / 255.0, G / 255.0, B / 255.0, 1.0);
}

vec2 getTextureCoordinates(float index, float size)
vec2 getTextureCoordinates(int index, int size)
{
float x = floor(mod(index + 0.5, size)); // integral modulo
float y = floor((index + 0.5)/ size); // integral division
//ask for the middle of the pixel
return vec2((x + 0.5) / size, (y + 0.5) / size);
int x = index % size;
int y = index / size;
float texSize = float(size);
float halfTexel = 0.5 / texSize;
return (vec2(x, y) + halfTexel) / texSize;;
}


vec4 getColor() {
// overriding colour is not defined
float restyle = aState[1];
if (restyle > 224.5) {
vec2 coords = getTextureCoordinates(aStyleIndex, uStyleTextureSize);
int index = int(aStyleIndex);
int size = int(uStyleTextureSize);
vec2 coords = getTextureCoordinates(index, size);
vec4 col = texture(uStyleSampler, coords);

// gray scale colour mode
Expand All @@ -124,7 +126,7 @@ vec4 getColor() {

// return colour based on restyle
// restyling texture is fixed size 15x15 for up to 225 styling colours
vec2 coords = getTextureCoordinates(restyle, 15.0);
vec2 coords = getTextureCoordinates(int(restyle), 15);
vec4 col2 = texture(uStateStyleSampler, coords);

// gray scale colour mode
Expand All @@ -137,14 +139,16 @@ vec4 getColor() {
}

vec4 getVertexPosition(mat4 transform) {
vec2 coords = getTextureCoordinates(aVertexIndex, uVertexTextureSize);
vec3 point = vec3(texture(uVertexSampler, coords));
int index = int(aVertexIndex);
int size = int(uVertexTextureSize);
vec2 coords = getTextureCoordinates(index, size);
vec3 point = texture(uVertexSampler, coords).rgb;

if (aTransformationIndex < -0.5) {
return vec4 (point, 1.0);
}
if (aTransformationIndex < -0.5) {
return vec4(point, 1.0);
}

return transform * vec4(point, 1.0);
return transform * vec4(point, 1.0);
}

mat4 getTransform() {
Expand All @@ -155,10 +159,12 @@ mat4 getTransform() {
float tIndex = aTransformationIndex * 4.0;

// get transformation texture coordinates
vec2 c1 = getTextureCoordinates(tIndex, uMatrixTextureSize);
vec2 c2 = getTextureCoordinates(tIndex + 1.0, uMatrixTextureSize);
vec2 c3 = getTextureCoordinates(tIndex + 2.0, uMatrixTextureSize);
vec2 c4 = getTextureCoordinates(tIndex + 3.0, uMatrixTextureSize);
int index = int(tIndex);
int size = int(uMatrixTextureSize);
vec2 c1 = getTextureCoordinates(index, size);
vec2 c2 = getTextureCoordinates(index + 1, size);
vec2 c3 = getTextureCoordinates(index + 2, size);
vec2 c4 = getTextureCoordinates(index + 3, size);

// get transformation matrix components
vec4 v1 = texture(uMatrixSampler, c1);
Expand Down
2 changes: 1 addition & 1 deletion src/shaders/vertex_shader_300.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const vertex_shader_300 = "#version 300 es\r\nin highp float aVertexIndex;\r\nin highp float aTransformationIndex;\r\nin highp float aStyleIndex;\r\nin highp float aProduct;\r\nin highp vec2 aState;\r\nin highp vec2 aNormal;\r\n\r\n\r\nuniform mat4 uMVMatrix;\r\nuniform mat4 uPMatrix;\r\n\r\n\r\nuniform vec4 uHighlightColour;\r\n\r\n\r\nuniform vec4 uHoverPickColour;\r\n\r\n\r\nuniform vec4 uXRayColour;\r\n\r\n\r\nuniform float uMeter;\r\n\r\n\r\nuniform vec3 uWcs;\r\n\r\n\r\n\r\nuniform int uColorCoding;\r\n\r\n\r\n\r\n\r\n\r\n\r\nuniform int uRenderingMode;\r\n\r\n\r\nuniform highp sampler2D uVertexSampler;\r\nuniform highp float uVertexTextureSize;\r\n\r\n\r\nuniform highp sampler2D uMatrixSampler;\r\nuniform highp float uMatrixTextureSize;\r\n\r\n\r\nuniform highp sampler2D uStyleSampler;\r\nuniform highp float uStyleTextureSize;\r\n\r\n\r\nuniform highp sampler2D uStateStyleSampler;\r\n\r\n\r\nout vec4 vColor;\r\n\r\nout vec3 vPosition;\r\n\r\nout vec3 vNormal;\r\n\r\nout mediump float vDiscard;\r\n\r\nconst float PI = 3.1415926535897932384626433832795;\r\n\r\nvec3 getNormal(mat4 transform) {\r\n float U = aNormal[0];\r\n float V = aNormal[1];\r\n float lon = U / 252.0 * 2.0 * PI;\r\n float lat = V / 252.0 * PI;\r\n\r\n float x = sin(lon) * sin(lat);\r\n float z = cos(lon) * sin(lat);\r\n float y = cos(lat);\r\n\r\n vec3 normal = vec3(x, y, z);\r\n if (aTransformationIndex < -0.5) {\r\n return normalize(normal);\r\n }\r\n\r\n \r\n \r\n \r\n \r\n mat3 normTrans = mat3(transform);\r\n\r\n return normalize(vec3(normTrans * normal));\r\n}\r\n\r\nvec4 getIdColor(float id) {\r\n float B = floor(id / (256.0*256.0));\r\n float G = floor((id - B * 256.0*256.0) / 256.0);\r\n float R = mod(id, 256.0);\r\n \r\n \r\n\r\n return vec4(R / 255.0, G / 255.0, B / 255.0, 1.0);\r\n}\r\n\r\nvec2 getTextureCoordinates(float index, float size)\r\n{\r\n float x = floor(mod(index + 0.5, size)); \r\n float y = floor((index + 0.5)/ size); \r\n \r\n return vec2((x + 0.5) / size, (y + 0.5) / size);\r\n}\r\n\r\n\r\nvec4 getColor() { \r\n \r\n float restyle = aState[1];\r\n if (restyle > 224.5) {\r\n vec2 coords = getTextureCoordinates(aStyleIndex, uStyleTextureSize);\r\n vec4 col = texture(uStyleSampler, coords);\r\n \r\n \r\n if (uRenderingMode == 1) {\r\n float intensity = (col.r + col.g + col.b) / 3.0;\r\n return vec4(intensity, intensity, intensity, col.a);\r\n }\r\n\r\n \r\n return col;\r\n }\r\n\r\n \r\n \r\n vec2 coords = getTextureCoordinates(restyle, 15.0);\r\n vec4 col2 = texture(uStateStyleSampler, coords);\r\n\r\n \r\n if (uRenderingMode == 1) {\r\n float intensity = (col2.r + col2.g + col2.b) / 3.0;\r\n return vec4(intensity, intensity, intensity, col2.a);\r\n }\r\n\r\n return col2;\r\n}\r\n\r\nvec4 getVertexPosition(mat4 transform) {\r\n vec2 coords = getTextureCoordinates(aVertexIndex, uVertexTextureSize);\r\n vec3 point = vec3(texture(uVertexSampler, coords));\r\n\r\n if (aTransformationIndex < -0.5) {\r\n return vec4 (point, 1.0);\r\n }\r\n\r\n return transform * vec4(point, 1.0);\r\n}\r\n\r\nmat4 getTransform() {\r\n if (aTransformationIndex < -0.5) {\r\n return mat4(1.0);\r\n }\r\n\r\n float tIndex = aTransformationIndex * 4.0;\r\n\r\n \r\n vec2 c1 = getTextureCoordinates(tIndex, uMatrixTextureSize);\r\n vec2 c2 = getTextureCoordinates(tIndex + 1.0, uMatrixTextureSize);\r\n vec2 c3 = getTextureCoordinates(tIndex + 2.0, uMatrixTextureSize);\r\n vec2 c4 = getTextureCoordinates(tIndex + 3.0, uMatrixTextureSize);\r\n\r\n \r\n vec4 v1 = texture(uMatrixSampler, c1);\r\n vec4 v2 = texture(uMatrixSampler, c2);\r\n vec4 v3 = texture(uMatrixSampler, c3);\r\n vec4 v4 = texture(uMatrixSampler, c4);\r\n\r\n \r\n return mat4(v1, v2, v3, v4);\r\n}\r\n\r\nvoid main(void) {\r\n int state = int(floor(aState[0] + 0.5));\r\n vDiscard = 0.0;\r\n\r\n if (state == 254 || \r\n (uColorCoding == -1 && state == 251) || \r\n (uColorCoding == -1 && (\r\n (uRenderingMode == 2 && state != 253 && state != 252 && state != 250) || \r\n (uRenderingMode == 3 && (state == 253 || state == 252 || state == 250))) \r\n ))\r\n {\r\n vDiscard = 1.0;\r\n vColor = vec4(0.0, 0.0, 0.0, 0.0);\r\n vNormal = vec3(0.0, 0.0, 0.0);\r\n vPosition = vec3(0.0, 0.0, 0.0);\r\n gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\r\n return;\r\n }\r\n\r\n \r\n mat4 transform = getTransform();\r\n \r\n vPosition = vec3(getVertexPosition(transform)) + uWcs;\r\n vNormal = getNormal(transform);\r\n\r\n \r\n vec4 baseColor = getColor();\r\n\r\n \r\n if (baseColor.a < 0.98)\r\n {\r\n float correction = -0.002;\r\n if (uColorCoding == -2 || uColorCoding >= 0) {\r\n correction = -0.02;\r\n }\r\n vec3 trans = correction * uMeter * normalize(vNormal);\r\n vPosition = vPosition + trans;\r\n }\r\n \r\n \r\n if (uColorCoding == -2) {\r\n \r\n if ((uRenderingMode == 2 || uRenderingMode == 4) && state != 252 && state != 253)\r\n {\r\n vDiscard = 1.0;\r\n vColor = vec4(0.0, 0.0, 0.0, 0.0);\r\n vNormal = vec3(0.0, 0.0, 0.0); \r\n } \r\n else\r\n {\r\n float id = floor(aProduct + 0.5);\r\n vColor = getIdColor(id);\r\n vNormal = vec3(0.0, 0.0, 0.0);\r\n }\r\n }\r\n \r\n else if (uColorCoding >= 0) {\r\n \r\n if ((uRenderingMode == 2 || uRenderingMode == 4) && state != 252 && state != 253)\r\n {\r\n vDiscard = 1.0;\r\n vColor = vec4(0.0, 0.0, 0.0, 0.0);\r\n vNormal = vec3(0.0, 0.0, 0.0); \r\n } \r\n else\r\n {\r\n float id = float(uColorCoding);\r\n vColor = getIdColor(id);\r\n vNormal = vec3(0.0, 0.0, 0.0);\r\n }\r\n }\r\n \r\n else {\r\n \r\n \r\n \r\n if (state == 250) {\r\n baseColor = uHoverPickColour;\r\n }\r\n else if (state == 253) {\r\n baseColor = uHighlightColour;\r\n }\r\n \r\n else if (uRenderingMode == 2 || uRenderingMode == 3) {\r\n \r\n if (state == 252) { \r\n baseColor = getColor();\r\n }\r\n \r\n else {\r\n baseColor = uXRayColour; \r\n }\r\n }\r\n \r\n vColor = baseColor;\r\n }\r\n\r\n \r\n gl_Position = uPMatrix * uMVMatrix * vec4(vPosition, 1.0);\r\n}"
export const vertex_shader_300 = "#version 300 es\r\nin highp float aVertexIndex;\r\nin highp float aTransformationIndex;\r\nin highp float aStyleIndex;\r\nin highp float aProduct;\r\nin highp vec2 aState;\r\nin highp vec2 aNormal;\r\n\r\n\r\nuniform mat4 uMVMatrix;\r\nuniform mat4 uPMatrix;\r\n\r\n\r\nuniform vec4 uHighlightColour;\r\n\r\n\r\nuniform vec4 uHoverPickColour;\r\n\r\n\r\nuniform vec4 uXRayColour;\r\n\r\n\r\nuniform float uMeter;\r\n\r\n\r\nuniform vec3 uWcs;\r\n\r\n\r\n\r\nuniform int uColorCoding;\r\n\r\n\r\n\r\n\r\n\r\n\r\nuniform int uRenderingMode;\r\n\r\n\r\nuniform highp sampler2D uVertexSampler;\r\nuniform highp float uVertexTextureSize;\r\n\r\n\r\nuniform highp sampler2D uMatrixSampler;\r\nuniform highp float uMatrixTextureSize;\r\n\r\n\r\nuniform highp sampler2D uStyleSampler;\r\nuniform highp float uStyleTextureSize;\r\n\r\n\r\nuniform highp sampler2D uStateStyleSampler;\r\n\r\n\r\nout vec4 vColor;\r\n\r\nout vec3 vPosition;\r\n\r\nout vec3 vNormal;\r\n\r\nout mediump float vDiscard;\r\n\r\nconst float PI = 3.1415926535897932384626433832795;\r\n\r\nvec3 getNormal(mat4 transform) {\r\n float U = aNormal[0];\r\n float V = aNormal[1];\r\n float lon = U / 252.0 * 2.0 * PI;\r\n float lat = V / 252.0 * PI;\r\n\r\n float x = sin(lon) * sin(lat);\r\n float z = cos(lon) * sin(lat);\r\n float y = cos(lat);\r\n\r\n vec3 normal = vec3(x, y, z);\r\n if (aTransformationIndex < -0.5) {\r\n return normalize(normal);\r\n }\r\n\r\n \r\n \r\n \r\n \r\n mat3 normTrans = mat3(transform);\r\n\r\n return normalize(vec3(normTrans * normal));\r\n}\r\n\r\nvec4 getIdColor(float id) {\r\n float B = floor(id / (256.0*256.0));\r\n float G = floor((id - B * 256.0*256.0) / 256.0);\r\n float R = mod(id, 256.0);\r\n \r\n \r\n\r\n return vec4(R / 255.0, G / 255.0, B / 255.0, 1.0);\r\n}\r\n\r\nvec2 getTextureCoordinates(int index, int size)\r\n{\r\n int x = index % size;\r\n int y = index / size;\r\n float texSize = float(size);\r\n float halfTexel = 0.5 / texSize;\r\n return (vec2(x, y) + halfTexel) / texSize;;\r\n}\r\n\r\nvec4 getColor() { \r\n \r\n float restyle = aState[1];\r\n if (restyle > 224.5) {\r\n int index = int(aStyleIndex);\r\n int size = int(uStyleTextureSize);\r\n vec2 coords = getTextureCoordinates(index, size);\r\n vec4 col = texture(uStyleSampler, coords);\r\n \r\n \r\n if (uRenderingMode == 1) {\r\n float intensity = (col.r + col.g + col.b) / 3.0;\r\n return vec4(intensity, intensity, intensity, col.a);\r\n }\r\n\r\n \r\n return col;\r\n }\r\n\r\n \r\n \r\n vec2 coords = getTextureCoordinates(int(restyle), 15);\r\n vec4 col2 = texture(uStateStyleSampler, coords);\r\n\r\n \r\n if (uRenderingMode == 1) {\r\n float intensity = (col2.r + col2.g + col2.b) / 3.0;\r\n return vec4(intensity, intensity, intensity, col2.a);\r\n }\r\n\r\n return col2;\r\n}\r\n\r\nvec4 getVertexPosition(mat4 transform) {\r\n int index = int(aVertexIndex);\r\n int size = int(uVertexTextureSize);\r\n vec2 coords = getTextureCoordinates(index, size);\r\n vec3 point = texture(uVertexSampler, coords).rgb;\r\n\r\n if (aTransformationIndex < -0.5) {\r\n return vec4(point, 1.0);\r\n }\r\n\r\n return transform * vec4(point, 1.0);\r\n}\r\n\r\nmat4 getTransform() {\r\n if (aTransformationIndex < -0.5) {\r\n return mat4(1.0);\r\n }\r\n\r\n float tIndex = aTransformationIndex * 4.0;\r\n\r\n \r\n int index = int(tIndex);\r\n int size = int(uMatrixTextureSize);\r\n vec2 c1 = getTextureCoordinates(index, size);\r\n vec2 c2 = getTextureCoordinates(index + 1, size);\r\n vec2 c3 = getTextureCoordinates(index + 2, size);\r\n vec2 c4 = getTextureCoordinates(index + 3, size);\r\n\r\n \r\n vec4 v1 = texture(uMatrixSampler, c1);\r\n vec4 v2 = texture(uMatrixSampler, c2);\r\n vec4 v3 = texture(uMatrixSampler, c3);\r\n vec4 v4 = texture(uMatrixSampler, c4);\r\n\r\n \r\n return mat4(v1, v2, v3, v4);\r\n}\r\n\r\nvoid main(void) {\r\n int state = int(floor(aState[0] + 0.5));\r\n vDiscard = 0.0;\r\n\r\n if (state == 254 || \r\n (uColorCoding == -1 && state == 251) || \r\n (uColorCoding == -1 && (\r\n (uRenderingMode == 2 && state != 253 && state != 252 && state != 250) || \r\n (uRenderingMode == 3 && (state == 253 || state == 252 || state == 250))) \r\n ))\r\n {\r\n vDiscard = 1.0;\r\n vColor = vec4(0.0, 0.0, 0.0, 0.0);\r\n vNormal = vec3(0.0, 0.0, 0.0);\r\n vPosition = vec3(0.0, 0.0, 0.0);\r\n gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\r\n return;\r\n }\r\n\r\n \r\n mat4 transform = getTransform();\r\n \r\n vPosition = vec3(getVertexPosition(transform)) + uWcs;\r\n vNormal = getNormal(transform);\r\n\r\n \r\n vec4 baseColor = getColor();\r\n\r\n \r\n if (baseColor.a < 0.98)\r\n {\r\n float correction = -0.002;\r\n if (uColorCoding == -2 || uColorCoding >= 0) {\r\n correction = -0.02;\r\n }\r\n vec3 trans = correction * uMeter * normalize(vNormal);\r\n vPosition = vPosition + trans;\r\n }\r\n \r\n \r\n if (uColorCoding == -2) {\r\n \r\n if ((uRenderingMode == 2 || uRenderingMode == 4) && state != 252 && state != 253)\r\n {\r\n vDiscard = 1.0;\r\n vColor = vec4(0.0, 0.0, 0.0, 0.0);\r\n vNormal = vec3(0.0, 0.0, 0.0); \r\n } \r\n else\r\n {\r\n float id = floor(aProduct + 0.5);\r\n vColor = getIdColor(id);\r\n vNormal = vec3(0.0, 0.0, 0.0);\r\n }\r\n }\r\n \r\n else if (uColorCoding >= 0) {\r\n \r\n if ((uRenderingMode == 2 || uRenderingMode == 4) && state != 252 && state != 253)\r\n {\r\n vDiscard = 1.0;\r\n vColor = vec4(0.0, 0.0, 0.0, 0.0);\r\n vNormal = vec3(0.0, 0.0, 0.0); \r\n } \r\n else\r\n {\r\n float id = float(uColorCoding);\r\n vColor = getIdColor(id);\r\n vNormal = vec3(0.0, 0.0, 0.0);\r\n }\r\n }\r\n \r\n else {\r\n \r\n \r\n \r\n if (state == 250) {\r\n baseColor = uHoverPickColour;\r\n }\r\n else if (state == 253) {\r\n baseColor = uHighlightColour;\r\n }\r\n \r\n else if (uRenderingMode == 2 || uRenderingMode == 3) {\r\n \r\n if (state == 252) { \r\n baseColor = getColor();\r\n }\r\n \r\n else {\r\n baseColor = uXRayColour; \r\n }\r\n }\r\n \r\n vColor = baseColor;\r\n }\r\n\r\n \r\n gl_Position = uPMatrix * uMVMatrix * vec4(vPosition, 1.0);\r\n}"

0 comments on commit 3a76bf1

Please sign in to comment.