Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Commit

Permalink
Example of using Shadertoy code
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Apr 25, 2019
1 parent c91bbb5 commit c956e37
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ include 'animation-001',
'start-dialog-001',
'shape-boolean-001',
'shape-contour-offset-001',
'shader-shadertoy-001',
'shader-watcher-001',
'shade-styles-001',
'shade-styles-002',
Expand Down
2 changes: 2 additions & 0 deletions shader-shadertoy-001/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apply plugin: 'java'
apply plugin: 'kotlin'
43 changes: 43 additions & 0 deletions shader-shadertoy-001/src/main/glsl/image.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#version 330

// list uniforms which are used by this shader
uniform vec3 iResolution; // viewport resolution (in pixels)
uniform float iTime; // shader playback time (in seconds)
//uniform float iTimeDelta; // render time (in seconds)
//uniform int iFrame; // shader playback frame
//uniform float iChannelTimeN; // channel playback time (in seconds)
//uniform vec3 iChannelResolutionN; // channel resolution (in pixels)
//uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click
//uniform sampler2D iChannelN; // input channel. XX = 2D/Cube
//uniform vec4 iDate; // (year, month, day, time in seconds)
//uniform float iSampleRate; // sound sample rate (i.e., 44100)

// -- default output
out vec4 o_color;

void mainImage(out vec4 fragColor, in vec2 fragCoord);

void main() {
mainImage(o_color, gl_FragCoord.xy);
}


// ------------------------------
// SHADERTOY CODE BEGINS HERE -
// ------------------------------

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;

// Time varying pixel color
vec3 col = 0.5 + 0.5*cos(iTime+uv.xyx+vec3(0,2,4));

// Output to screen
fragColor = vec4(col,1.0);
}

// ----------------------------
// SHADERTOY CODE ENDS HERE -
// ----------------------------
69 changes: 69 additions & 0 deletions shader-shadertoy-001/src/main/kotlin/Example.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import org.openrndr.Application
import org.openrndr.Configuration
import org.openrndr.Program
import org.openrndr.draw.*
import org.openrndr.filter.filterShaderFromUrl
import org.openrndr.math.Vector3
import org.openrndr.math.Vector4

/**
* The Shadertoy buffer, abstraction over Shadertoy tab containing GLSL code.
*/
class ShadertoyBuffer : Filter(filterShaderFromUrl("file:shader-shadertoy-001/src/main/glsl/image.frag")) {
// for live shader code reloading use:
// Filter(null, filterWatcherFromUrl("file:shader-shadertoy-001/src/main/glsl/image.frag")) {

var iResolution: Vector3 by parameters // viewport resolution (in pixels)
var iTime: Double by parameters // shader playback time (in seconds)
var iTimeDelta: Double by parameters // render time (in seconds)
var iFrame: Int by parameters // shader playback frame
var iChannelTime0: Double by parameters // channel playback time (in seconds)
var iChannelTime1: Double by parameters
var iChannelTime2: Double by parameters
var iChannelTime3: Double by parameters
var iChannelResolution0: Double by parameters // channel resolution (in pixels)
var iChannelResolution1: Double by parameters
var iChannelResolution2: Double by parameters
var iChannelResolution3: Double by parameters
var iMouse: Vector4 by parameters // mouse pixel coords. xy: current (if MLB down), zw: click
var iChannel0: ColorBuffer by parameters // input channel. XX = 2D/Cube
var iChannel1: ColorBuffer by parameters
var iChannel2: ColorBuffer by parameters
var iChannel3: ColorBuffer by parameters
var iDate: Vector4 by parameters // (year, month, day, time in seconds)
var iSampleRate: Double by parameters // sound sample rate (i.e., 44100)
}

/**
* This is a basic example that shows how to use Shadertoy code.
*/
class Example : Program() {

private var frameCounter: Int = 0

lateinit var image: ShadertoyBuffer
lateinit var imageOut: ColorBuffer

override fun setup() {
image = ShadertoyBuffer()
imageOut = colorBuffer(width, height)
}

override fun draw() {
populate(image)
image.apply(arrayOf(), imageOut)
drawer.image(imageOut)
frameCounter++
}

private fun populate(buffer: ShadertoyBuffer) {
buffer.iTime = seconds
buffer.iResolution = Vector3(width.toDouble(), height.toDouble(), 0.0)
buffer.iFrame = frameCounter
}

}

fun main(args: Array<String>) {
Application.run(Example(), Configuration())
}

0 comments on commit c956e37

Please sign in to comment.