From 89c6f500648b45da609cd5f24169b1fa5ecea877 Mon Sep 17 00:00:00 2001 From: Stanislav Mishchenko Date: Wed, 1 May 2024 16:33:10 +0300 Subject: [PATCH] add color interpolation for highlighted landmarks --- .../core/renderers/LinesLayerRenderer.kt | 39 ++++++++++++------- .../core/renderers/PointsLayerRenderer.kt | 39 ++++++++++++------- .../engine/shaders/landmark/line/line.frag | 15 +------ .../engine/shaders/landmark/line/line.vert | 3 ++ .../engine/shaders/landmark/point/point.frag | 15 +------ .../engine/shaders/landmark/point/point.vert | 3 ++ 6 files changed, 58 insertions(+), 56 deletions(-) diff --git a/src/main/kotlin/solve/rendering/engine/core/renderers/LinesLayerRenderer.kt b/src/main/kotlin/solve/rendering/engine/core/renderers/LinesLayerRenderer.kt index 940782d6..5176e2b5 100644 --- a/src/main/kotlin/solve/rendering/engine/core/renderers/LinesLayerRenderer.kt +++ b/src/main/kotlin/solve/rendering/engine/core/renderers/LinesLayerRenderer.kt @@ -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( @@ -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() { @@ -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 / @@ -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 } @@ -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 diff --git a/src/main/kotlin/solve/rendering/engine/core/renderers/PointsLayerRenderer.kt b/src/main/kotlin/solve/rendering/engine/core/renderers/PointsLayerRenderer.kt index 095136e0..b042a808 100644 --- a/src/main/kotlin/solve/rendering/engine/core/renderers/PointsLayerRenderer.kt +++ b/src/main/kotlin/solve/rendering/engine/core/renderers/PointsLayerRenderer.kt @@ -44,7 +44,8 @@ class PointsLayerRenderer( val shaderAttributesTypes = listOf( ShaderAttributeType.FLOAT2, ShaderAttributeType.FLOAT2, - ShaderAttributeType.FLOAT + ShaderAttributeType.FLOAT, + ShaderAttributeType.FLOAT3 ) return RenderBatch( @@ -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() { @@ -83,8 +83,27 @@ 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) * @@ -92,21 +111,12 @@ class PointsLayerRenderer( 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 } @@ -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), diff --git a/src/main/resources/engine/shaders/landmark/line/line.frag b/src/main/resources/engine/shaders/landmark/line/line.frag index 26f1303a..ccc5a553 100644 --- a/src/main/resources/engine/shaders/landmark/line/line.frag +++ b/src/main/resources/engine/shaders/landmark/line/line.frag @@ -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); } \ No newline at end of file diff --git a/src/main/resources/engine/shaders/landmark/line/line.vert b/src/main/resources/engine/shaders/landmark/line/line.vert index 267659a5..7a07ee69 100644 --- a/src/main/resources/engine/shaders/landmark/line/line.vert +++ b/src/main/resources/engine/shaders/landmark/line/line.vert @@ -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); } diff --git a/src/main/resources/engine/shaders/landmark/point/point.frag b/src/main/resources/engine/shaders/landmark/point/point.frag index f6ac919b..4886fda8 100644 --- a/src/main/resources/engine/shaders/landmark/point/point.frag +++ b/src/main/resources/engine/shaders/landmark/point/point.frag @@ -2,19 +2,12 @@ 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); @@ -22,9 +15,5 @@ void main() 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); } diff --git a/src/main/resources/engine/shaders/landmark/point/point.vert b/src/main/resources/engine/shaders/landmark/point/point.vert index 9cd1a4ce..edd29692 100644 --- a/src/main/resources/engine/shaders/landmark/point/point.vert +++ b/src/main/resources/engine/shaders/landmark/point/point.vert @@ -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); }