Created
August 23, 2020 15:32
-
-
Save ylegall/850f03e7be7696ce18e6ee850a86e31c to your computer and use it in GitHub Desktop.
openRNDR code for orbiting inside colorful torus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.openrndr.application | |
import org.openrndr.color.ColorRGBa | |
import org.openrndr.draw.DrawPrimitive | |
import org.openrndr.draw.VertexElementType | |
import org.openrndr.draw.shadeStyle | |
import org.openrndr.draw.vertexBuffer | |
import org.openrndr.draw.vertexFormat | |
import org.openrndr.extra.compositor.blend | |
import org.openrndr.extra.compositor.compose | |
import org.openrndr.extra.compositor.draw | |
import org.openrndr.extra.compositor.layer | |
import org.openrndr.extra.compositor.post | |
import org.openrndr.extra.dnk3.post.SegmentContours | |
import org.openrndr.extra.fx.blend.Subtract | |
import org.openrndr.extra.fx.blur.FrameBlur | |
import org.openrndr.extras.camera.OrbitalCamera | |
import org.openrndr.extras.camera.OrbitalControls | |
import org.openrndr.extras.camera.applyTo | |
import org.openrndr.extras.meshgenerators.sphereMesh | |
import org.openrndr.ffmpeg.ScreenRecorder | |
import org.openrndr.math.Vector3 | |
import org.openrndr.math.transforms.transform | |
import org.ygl.openrndr.utils.colorMapBufferTextureFromStrings | |
import org.ygl.openrndr.utils.shaderColorMap | |
import kotlin.random.Random | |
private const val WIDTH = 920 | |
private const val HEIGHT = 920 | |
private const val TOTAL_FRAMES = 360 * 4 | |
private const val RECORDING = false | |
fun main() = application { | |
configure { | |
width = WIDTH | |
height = HEIGHT | |
} | |
program { | |
var time = 0.0 | |
val majorRadius = 300.0 | |
val minorRadius = 150.0 | |
val majorSegments = 60 | |
val minorSegments = 36 | |
val colors = listOf( | |
"ff7859","f2e122","37e6fa","5c7cff","9952fe","fa4a88" | |
) | |
val colorBufferTexture = colorMapBufferTextureFromStrings(colors) | |
val camera = OrbitalCamera( | |
eye = Vector3(0.0, 0.0, -majorRadius), | |
lookAt = Vector3(majorRadius - minorRadius/3, 0.0, 0.0) | |
) | |
val sphere = sphereMesh() | |
val points = MutableList(majorSegments * minorSegments) { Vector3.ZERO } | |
val sizes = List(points.size) { Random.nextDouble(15.0, 25.0) } | |
val colorValues = List(points.size) { Random.nextFloat() } | |
val instanceAttributes = vertexBuffer(vertexFormat { | |
attribute("transform", VertexElementType.MATRIX44_FLOAT32) | |
attribute("color", VertexElementType.FLOAT32) | |
}, points.size) | |
fun computePoint(majorAngle: Double, minorAngle: Double): Vector3 { | |
val transform = transform { | |
rotate(Vector3.UNIT_Y, 360 * majorAngle) | |
translate(Vector3(0.0, 0.0, majorRadius)) | |
rotate(Vector3.UNIT_X, 360 * minorAngle) | |
scale(minorRadius) | |
} | |
return (transform * Vector3.UNIT_Y.xyz1).xyz | |
} | |
fun computePoints() { | |
var idx = 0 | |
for (i in 0 until majorSegments) { | |
val pct1 = (i / (majorSegments - 0.0)) | |
for (j in 0 until minorSegments) { | |
val pct2 = (j / (minorSegments - 0.0) + pct1) | |
points[idx] = computePoint(pct1 + time, pct2 + time) | |
idx++ | |
} | |
} | |
instanceAttributes.put { | |
for (i in points.indices) { | |
write(transform { | |
translate(points[i]) | |
//scale(10.0) | |
scale(sizes[i]) | |
}) | |
write( | |
colorValues[i] | |
) | |
//write((i % 3) / 3f) | |
} | |
} | |
} | |
val composite = compose { | |
draw { | |
} | |
post(FrameBlur()) | |
layer { | |
draw { | |
drawer.clear(ColorRGBa.BLACK) | |
drawer.shadeStyle = shadeStyle { | |
vertexTransform = "x_viewMatrix = x_viewMatrix * i_transform;" | |
//vertexTransform = "x_modelMatrix = x_modelMatrix * i_transform;" | |
fragmentPreamble = shaderColorMap(colors.size) | |
fragmentTransform = """ | |
float dist = distance(v_viewPosition, va_position); | |
dist = 1.2 - (dist / 600); | |
x_fill.rgb = getColor(vi_color).rgb * dist; | |
//x_fill.rgb = getColor(vi_color).rgb; | |
""".trimIndent() | |
parameter("colorMap", colorBufferTexture) | |
} | |
drawer.vertexBufferInstances( | |
listOf(sphere), | |
listOf(instanceAttributes), | |
DrawPrimitive.TRIANGLES, | |
points.size | |
) | |
} | |
} | |
layer { | |
draw { | |
drawer.clear(ColorRGBa.BLACK) | |
drawer.shadeStyle = shadeStyle { | |
vertexTransform = "x_viewMatrix = x_viewMatrix * i_transform;" | |
fragmentPreamble = shaderColorMap(colors.size) | |
fragmentTransform = """ | |
float dist = distance(v_viewPosition, va_position); | |
dist = step(0.2, 1 - dist / 900); | |
x_fill.rgb = getColor(vi_color).rgb * dist; | |
""".trimIndent() | |
parameter("colorMap", colorBufferTexture) | |
} | |
drawer.vertexBufferInstances( | |
listOf(sphere), | |
listOf(instanceAttributes), | |
DrawPrimitive.TRIANGLES, | |
points.size | |
) | |
} | |
post(SegmentContours()) | |
blend(Subtract()) | |
} | |
} | |
if (RECORDING) { | |
extend(ScreenRecorder()) { | |
outputFile = "video/CandyTunnel.mp4" | |
frameRate = 60 | |
frameClock = true | |
} | |
} else { | |
extend(OrbitalControls(camera)) | |
} | |
extend { | |
time = ((frameCount - 1) % TOTAL_FRAMES) / TOTAL_FRAMES.toDouble() | |
camera.update(deltaTime) | |
camera.applyTo(drawer) | |
computePoints() | |
composite.draw(drawer) | |
if (RECORDING && frameCount == TOTAL_FRAMES) { | |
application.exit() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment