-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I need your ideas how to simplify interface! #18
Comments
Thank you for releasing this wonderful library! binding.shaderView.apply {
initialize {
fragmentShaderRawResId(R.raw.distort_texture)
updateContinuously(true)
}
distortTextureShaderParameters {
uPointer(Vec2f(followPointerX, followPointerY))
uVelo(0f)
resolution(Vec4f(0f, 0f, 0f, 0f))
uTexture(Texture2D(textureResourceId = R.drawable.bokeh))
}
onDrawFrame {
viewHeight = measuredHeight.toFloat()
viewWidth = measuredWidth.toFloat()
...
uPointer(Vec2f(followPointerX / 1000, followPointerY / 1000))
resolution(
Vec4f(viewWidth, viewHeight, resZ, resW)
)
uVelo(min(targetSpeed / 100, 0.5f))
}
...
} In the current ShaderView, when passing uniforms to shaders, the paramName and its value are passed separately. I will briefly explain how I implement the above interface. 1. Modeling unifromsFirst, define unifroms as classes that can be passed to shaders. (example) data class Vec3i(
val x: Int,
val y: Int,
val z: Int,
) {
fun toArray(): IntArray = intArrayOf(x, y, z)
} Then, uniforms that are actually passed to the shader are grouped into a class annotated
@ShaderParameters
data class DistortTextureShaderParameters(
val uPointer: Vec2f,
val uTexture: Texture2D,
val uVelo: Float,
val resolution: Vec4f,
) 2. Build and Generate Extension FunctionsAfter creating the uniform model and building the project, the extension functions are generated by KSP(or kapt) and KotlinPoet. The ${ShaderParameters}BuilderThe public class DistortTextureShaderParametersBuilder {
private val shaderParamsBuilder: ShaderParamsBuilder = ShaderParamsBuilder()
public fun uPointer(uniform: Vec2f?): Unit {
shaderParamsBuilder.addVec2f("uPointer", uniform?.toArray())
}
...
public fun uVelo(uniform: Float?): Unit {
shaderParamsBuilder.addFloat("uVelo", uniform)
}
public fun build(): ShaderParams = shaderParamsBuilder.build()
} ${ShaderParameters}UpdaterThe Again, it would be easier to understand if the name of the generated method is the same as uniform. public class DistortTextureShaderParametersUpdater(
private val shaderParams: ShaderParams
) {
public fun uPointer(uniform: Vec2f): Unit {
shaderParams.updateValue("uPointer", uniform.toArray())
}
public fun uTexture(uniform: Texture2D, needToRecycleWhenUploaded: Boolean = false): Unit {
val textureResourceId = uniform.textureResourceId
val bitmap = uniform.bitmap
if (textureResourceId != null) {
shaderParams.updateValue2D("uTexture", textureResourceId)
} else if (bitmap != null) {
shaderParams.updateValue2D("uTexture", bitmap, needToRecycleWhenUploaded)
}
}
public fun uVelo(uniform: Float): Unit {
shaderParams.updateValue("uVelo", uniform)
}
public fun resolution(uniform: Vec4f): Unit {
shaderParams.updateValue("resolution", uniform.toArray())
}
...
public fun newBuilder(): ShaderParamsBuilder = shaderParams.newBuilder()
} ShaderViewExtension.ktAfter generating the above Builder and Updater, the following extension functions are generated. public
fun ShaderView.distortTextureShaderParameters(initializer: DistortTextureShaderParametersBuilder.() -> Unit):
Unit {
val parameters =
com.appspell.shaderview.demo.distortion.DistortTextureShaderParametersBuilder().run {
initializer()
build()
}
shaderParams = parameters
}
public fun ShaderView.onDrawFrame(listener: DistortTextureShaderParametersUpdater.() -> Unit):
Unit {
onDrawFrameListener = { shaderParams ->
val updater =
com.appspell.shaderview.demo.distortion.DistortTextureShaderParametersUpdater(shaderParams)
updater.listener()
}
} This method allows library users to use the first proposed interface. How code generation is performedPlease refer to the sample project below for generating the code described here. branch: https://github.com/tkhskt/ShaderView/tree/feature/kotlin_dsl_interface/processor Sample ProjectI created an activity called branch: https://github.com/tkhskt/ShaderView/tree/feature/kotlin_dsl_interface If you like this idea, please message me and I will send you a PR anytime :) Sorry for ranting... |
I want to make the simpler way how to use this library.
Do you have any ideas on how to make the architecture or API library more friendly?
Look at the code below, seems like there is a better (simpler) way to set up this view. Maybe some DSL?
The text was updated successfully, but these errors were encountered: