All Projects → dananas → kotlin-glsl

dananas / kotlin-glsl

Licence: MIT license
Write your GLSL shaders in Kotlin.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to kotlin-glsl

deffx
A collection of useful shader effects made ready to be used with the Defold game engine
Stars: ✭ 33 (+10%)
Mutual labels:  shaders, glsl, glsl-shaders
YALCT
Yet Another Live Coding Tool - Powered by Veldrid and elbow grease
Stars: ✭ 25 (-16.67%)
Mutual labels:  shaders, glsl, glsl-shaders
ShaderView
ShaderView is an Android View that makes it easy to use GLSL shaders for your app. It's the modern way to use shaders for Android instead of RenderScript.
Stars: ✭ 53 (+76.67%)
Mutual labels:  shaders, glsl, glsl-shaders
Webgl Fundamentals
WebGL lessons that start with the basics
Stars: ✭ 3,315 (+10950%)
Mutual labels:  shaders, glsl, glsl-shaders
React Regl
React Fiber Reconciler Renderer for Regl WebGL
Stars: ✭ 171 (+470%)
Mutual labels:  shaders, glsl, glsl-shaders
ModularMusicVisualizer
Project in Hiatus, unmaintained, being rewritten privately. Will Open Source when stuff is ready. Project will be Renamed.
Stars: ✭ 81 (+170%)
Mutual labels:  shaders, glsl, glsl-shaders
30-days-of-shade
30 days of shaders in GLSL using GLSLCanvas
Stars: ✭ 134 (+346.67%)
Mutual labels:  shaders, glsl, glsl-shaders
Thebookofshaders
Step-by-step guide through the abstract and complex universe of Fragment Shaders.
Stars: ✭ 4,070 (+13466.67%)
Mutual labels:  shaders, glsl, glsl-shaders
3d Game Shaders For Beginners
🎮 A step-by-step guide to implementing SSAO, depth of field, lighting, normal mapping, and more for your 3D game.
Stars: ✭ 11,698 (+38893.33%)
Mutual labels:  shaders, glsl, glsl-shaders
glNoise
A collection of GLSL noise functions for use with WebGL with an easy to use API.
Stars: ✭ 185 (+516.67%)
Mutual labels:  shaders, glsl, glsl-shaders
FNode
Tool based in nodes to build GLSL shaders without any programming knowledge written in C using OpenGL and GLFW.
Stars: ✭ 81 (+170%)
Mutual labels:  shaders, glsl, glsl-shaders
ShaderBoy
Simple text editor that lets you write Shadertoy shaders more comfortably, anytime, anywhere.
Stars: ✭ 133 (+343.33%)
Mutual labels:  glsl, glsl-shaders
Messier87
A realtime raytracing blackhole renderer
Stars: ✭ 53 (+76.67%)
Mutual labels:  shaders, glsl
gamedex
👾 The code for my game dev + computer graphics experiments on YouTube.
Stars: ✭ 165 (+450%)
Mutual labels:  glsl, glsl-shaders
glsl-cos-palette
glsl function for making cosine palettes
Stars: ✭ 26 (-13.33%)
Mutual labels:  shaders, glsl
sparksl-noise
minimum proof of concept about procedural noise generation in SparkAR's shader language (SparkSL).
Stars: ✭ 16 (-46.67%)
Mutual labels:  shaders, glsl
andromeda
GLSL-targetting embedded compiler, and OpenGL rendering engine.
Stars: ✭ 75 (+150%)
Mutual labels:  shaders, glsl-shaders
ShaderToy-Chrome-Plugin
Web extension for shadertoy.com
Stars: ✭ 159 (+430%)
Mutual labels:  shaders, glsl-shaders
cellular-automata-explorer
(WIP) An interactive web app for exploring cellular automata.
Stars: ✭ 18 (-40%)
Mutual labels:  shaders, glsl
shaderview
A GLSL shader development tool for the LÖVE game framework.
Stars: ✭ 22 (-26.67%)
Mutual labels:  shaders, glsl

GLSL shaders development in Kotlin

The sources provide a way of creating GLSL-like code in Kotlin.

Shader source in Kotlin:

class FragmentShader(useAlphaTest: Boolean) : ShaderBuilder() {
    private val alphaTestThreshold by uniform(::GLFloat)
    private val texture by uniform(::Sampler2D)
    private val uv by varying(::Vec2)
    init {
        var color by vec4()
        color = texture2D(texture, uv)
        // static branching
        if (useAlphaTest) {
            // dynamic branching
            If(color.w lt alphaTestThreshold) {
                discard()
            }
        }
        gl_FragColor = color
    }
}

You can get your GLSL source in two ways:

1. Runtime generation

FragmentShader(useAlphaTest = true).getSource()

Pros: easy to generate sources.

Cons: GLSL swizzling functionality may affect performance, as it creates lots of objects at runtime.

2. Generation using @ShaderProgram annotation

This functionality can be used as follows:

@ShaderProgram(VertexShader::class, FragmentShader::class)
class ShaderProgramName(alphaTest: Boolean)

Annotation processor generates new sources during gradle kaptKotlin task which look as follows:

class ShaderProgramNameSources {
    enum class Sources(vertex: String, fragment: String): ShaderProgramSources {
        Source0("<vertex code>", "<fragment code>")
        ...
    }
    fun get(alphaTest: Boolean) {
        if (alphaTest) return Source0
        else return Source1
    }
}

After generation you can get both vertex and fragment sources as follows:

val sources = ShaderProgramNameSources.get(replaceAlpha = true)
println(sources.vertex)
println(sources.fragment)

Pros: no need to generate sources at runtime, as you will have direct access to them.

Cons: shader source classes have to be accessible for AP to load them, thus can't be stored just anywhere in the project.

Generated GLSL

Both methods produce the same result:

uniform sampler2D texture;
uniform float alphaTestThreshold;
varying vec2 uv;
void main(void) {
    vec4 color;
    color = texture2D(texture, uv);
    if ((color.w < alphaTestThreshold)) {
        discard;
    }
    gl_FragColor = color;
}

How to use

You will need gradle to build sources.

You have two options here. If you do not need @ShaderProgram annotation functionality in the above example, then you can simply import the KotlinGlsl project (and may remove ProgramExample module).

If you do want to use @ShaderProgram, then you will have to store your Kotlin shader sources either directly in the KotlinGlsl or in a project accessible for it during annotations processing stage, as AP will try to load classes provided with @ShaderProgram.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].