Skip to content

Commit

Permalink
add color interpolation for highlighted landmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
mi-sts committed May 1, 2024
1 parent b3cac8b commit 89c6f50
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class LinesLayerRenderer(
override fun createNewBatch(zIndex: Int): RenderBatch {
val shaderAttributesTypes = listOf(
ShaderAttributeType.FLOAT2,
ShaderAttributeType.FLOAT
ShaderAttributeType.FLOAT,
ShaderAttributeType.FLOAT3
)

return RenderBatch(
Expand All @@ -58,7 +59,6 @@ class LinesLayerRenderer(
override fun uploadUniforms(shaderProgram: ShaderProgram) {
shaderProgram.uploadMatrix4f(ProjectionUniformName, window.calculateProjectionMatrix())
shaderProgram.uploadInt(UseCommonColorUniformName, if (useCommonColor()) 1 else 0)
shaderProgram.uploadVector3f(CommonColorUniformName, getLinesColor())
}

override fun beforeRender() {
Expand Down Expand Up @@ -91,8 +91,26 @@ class LinesLayerRenderer(
val lineVector = lineFinishShaderPosition - lineStartShaderPosition
val normalVector = Vector2f(-lineVector.y, lineVector.x).normalize()
val linePoints = listOf(lineStartShaderPosition, lineFinishShaderPosition)
val selectionProgress = lineLandmark.layerState.getLandmarkHighlightingProgress(lineLandmark.uid)
val widthMultiplier = 1f + selectionProgress * (HighlightingWidthMultiplier - 1f)
val highlightingProgress = lineLandmark.layerState.getLandmarkHighlightingProgress(lineLandmark.uid)

var widthMultiplier = 1f
var lineColor = lineLandmark.layerSettings.getColor(lineLandmark)
if (highlightingProgress == 1f) {
widthMultiplier = HighlightingWidthMultiplier
lineColor = lineLandmark.layerSettings.getUniqueColor(lineLandmark)
} else if (highlightingProgress > 0f && highlightingProgress < 1f ) {
widthMultiplier += highlightingProgress * (HighlightingWidthMultiplier - 1f)
lineColor = lineColor.interpolate(
lineLandmark.layerSettings.getUniqueColor(lineLandmark),
highlightingProgress.toDouble()
)
}

val lineColorVector = Vector3f(
lineColor.red.toFloat(),
lineColor.green.toFloat(),
lineColor.blue.toFloat()
)

linePoints.forEachIndexed { sideIndex, linePoint ->
val pointToVertexVector = Vector2f(normalVector) * linesWidth * widthMultiplier /
Expand All @@ -104,23 +122,15 @@ class LinesLayerRenderer(
val secondVertexPosition = if (sideIndex == 0) bottomVertexPosition else upperVertexPosition
batch.pushVector2f(firstVertexPosition)
batch.pushFloat(lineLandmarkIndex.toFloat())
batch.pushVector3f(lineColorVector)
batch.pushVector2f(secondVertexPosition)
batch.pushFloat(lineLandmarkIndex.toFloat())
batch.pushVector3f(lineColorVector)
}
}
}
}

private fun getLinesColor(): Vector3f {
val pointsCommonColor = lineLayers.firstOrNull()?.settings?.commonColor ?: return Vector3f(1f, 0f, 0f)

return Vector3f(
pointsCommonColor.red.toFloat(),
pointsCommonColor.green.toFloat(),
pointsCommonColor.blue.toFloat()
)
}

private fun getLinesWidth(): Float {
return lineLayers.firstOrNull()?.settings?.selectedWidth?.toFloat() ?: return 1f
}
Expand All @@ -135,7 +145,6 @@ class LinesLayerRenderer(

companion object {
private const val UseCommonColorUniformName = "uUseCommonColor"
private const val CommonColorUniformName = "uCommonColor"

private const val DefaultLocalVerticesPositionsDivider = 800f
private const val HighlightingWidthMultiplier = 2.5f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class PointsLayerRenderer(
val shaderAttributesTypes = listOf(
ShaderAttributeType.FLOAT2,
ShaderAttributeType.FLOAT2,
ShaderAttributeType.FLOAT
ShaderAttributeType.FLOAT,
ShaderAttributeType.FLOAT3
)

return RenderBatch(
Expand All @@ -58,7 +59,6 @@ class PointsLayerRenderer(
override fun uploadUniforms(shaderProgram: ShaderProgram) {
shaderProgram.uploadMatrix4f(ProjectionUniformName, window.calculateProjectionMatrix())
shaderProgram.uploadInt(UseCommonColorUniformName, if (useCommonColor()) 1 else 0)
shaderProgram.uploadVector3f(CommonColorUniformName, getPointsColor())
}

override fun beforeRender() {
Expand All @@ -83,30 +83,40 @@ class PointsLayerRenderer(
pointLandmark.coordinate.y.toFloat()
)
val pointShaderPosition = getFramePixelShaderPosition(pointsLayerIndex, pointLandmarkPosition)
val selectionProgress = pointLandmark.layerState.getLandmarkHighlightingProgress(pointLandmark.uid)
val radiusMultiplier = 1f + selectionProgress * (HighlightingRadiusMultiplier - 1f)

val highlightingProgress = pointLandmark.layerState.getLandmarkHighlightingProgress(pointLandmark.uid)

var radiusMultiplier = 1f
var pointColor = pointLandmark.layerSettings.getColor(pointLandmark)
if (highlightingProgress == 1f) {
radiusMultiplier = HighlightingRadiusMultiplier
pointColor = pointLandmark.layerSettings.getUniqueColor(pointLandmark)
} else if (highlightingProgress > 0f && highlightingProgress < 1f ) {
radiusMultiplier += highlightingProgress * (HighlightingRadiusMultiplier - 1f)
pointColor = pointColor.interpolate(
pointLandmark.layerSettings.getUniqueColor(pointLandmark),
highlightingProgress.toDouble()
)
}

val pointColorVector = Vector3f(
pointColor.red.toFloat(),
pointColor.green.toFloat(),
pointColor.blue.toFloat()
)

circleBoundsVerticesLocalPositions.forEach { vertexLocalPosition ->
val vertexPosition = pointShaderPosition + Vector2f(vertexLocalPosition) *
pointsRadius * radiusMultiplier / window.camera.zoom / DefaultLocalVerticesPositionsDivider
batch.pushVector2f(vertexPosition)
batch.pushVector2f(vertexLocalPosition)
batch.pushFloat(pointLandmarkIndex.toFloat())
batch.pushVector3f(pointColorVector)
}
}
}
}

private fun getPointsColor(): Vector3f {
val pointsCommonColor = pointLayers.firstOrNull()?.settings?.commonColor ?: return Vector3f(1f, 0f, 0f)

return Vector3f(
pointsCommonColor.red.toFloat(),
pointsCommonColor.green.toFloat(),
pointsCommonColor.blue.toFloat()
)
}

private fun useCommonColor(): Boolean {
return pointLayers.firstOrNull()?.settings?.useCommonColor ?: false
}
Expand All @@ -117,7 +127,6 @@ class PointsLayerRenderer(

companion object {
private const val UseCommonColorUniformName = "uUseCommonColor"
private const val CommonColorUniformName = "uCommonColor"

private val circleBoundsVerticesLocalPositions = listOf(
Vector2f(1f, 1f),
Expand Down
15 changes: 2 additions & 13 deletions src/main/resources/engine/shaders/landmark/line/line.frag
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
#version 330 core

in float fIndex;
in vec3 fColor;

uniform int uUseCommonColor;
uniform vec3 uCommonColor;

out vec4 color;

float random(float index)
{
vec2 seedVector = vec2(index, 133);

return mod(fract(sin(dot(seedVector.xy, vec2(12.9898, 78.233))) * 43758.5453123), 1.0);
}

void main()
{
if (uUseCommonColor == 1) {
color = vec4(uCommonColor, 1);
} else {
color = vec4(random(fIndex), random(fIndex + 7), random(fIndex + 19), 1.0);
}
color = vec4(fColor, 1.0);
}
3 changes: 3 additions & 0 deletions src/main/resources/engine/shaders/landmark/line/line.vert
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

layout (location=0) in vec2 aPos;
layout (location=1) in float aIndex;
layout (location=2) in vec3 aColor;

uniform mat4 uProjection;

out float fIndex;
out vec3 fColor;

void main()
{
fIndex = aIndex;
fColor = aColor;
gl_Position = uProjection * vec4(aPos, 0.0, 1.0);
}
15 changes: 2 additions & 13 deletions src/main/resources/engine/shaders/landmark/point/point.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,18 @@

in vec2 fLocalPos;
in float fIndex;
in vec3 fColor;

uniform int uUseCommonColor;
uniform vec3 uCommonColor;

out vec4 color;

float random(float index)
{
vec2 seedVector = vec2(index, 133);

return mod(fract(sin(dot(seedVector.xy, vec2(12.9898, 78.233))) * 43758.5453123), 1.0);
}

void main()
{
float localRadius = dot(fLocalPos, fLocalPos);
if (length(localRadius) > 1) {
discard;
}

if (uUseCommonColor == 1) {
color = vec4(uCommonColor, 1);
} else {
color = vec4(random(fIndex), random(fIndex + 7), random(fIndex + 19), 1.0);
}
color = vec4(fColor, 1.0);
}
3 changes: 3 additions & 0 deletions src/main/resources/engine/shaders/landmark/point/point.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
layout (location=0) in vec2 aPos;
layout (location=1) in vec2 aLocalPos;
layout (location=2) in float aIndex;
layout (location=3) in vec3 aColor;

uniform mat4 uProjection;

out vec2 fLocalPos;
out float fIndex;
out vec3 fColor;

void main()
{
fIndex = aIndex;
fLocalPos = aLocalPos.xy;
fColor = aColor;
gl_Position = uProjection * vec4(aPos, 0.0, 1.0);
}

0 comments on commit 89c6f50

Please sign in to comment.