├── .gitignore ├── .idea ├── .name ├── ant.xml ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── libraries │ ├── libs.xml │ ├── processing.xml │ └── processing_opengl.xml ├── misc.xml ├── modules.xml ├── uiDesigner.xml ├── vcs.xml └── workspace.xml ├── README.txt ├── fluids ├── fluids.iml └── src │ ├── Fluids.scala │ ├── Particle.scala │ └── ParticleSystem.scala ├── libs ├── MSAFluid.jar ├── TUIO.jar └── libTUIO.jar ├── plasma ├── plasma.iml └── src │ └── Plasma.scala └── processing.iml /.gitignore: -------------------------------------------------------------------------------- 1 | /out -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | processing -------------------------------------------------------------------------------- /.idea/ant.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/libraries/libs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/libraries/processing.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/libraries/processing_opengl.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 24 | 25 | http://www.w3.org/1999/xhtml 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 39 | 40 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 119 | 120 | 131 | 132 | 133 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 170 | 171 | 174 | 175 | 176 | 177 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 216 | 217 | 218 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 244 | 245 | 246 | 247 | 263 | 264 | 265 | 283 | 284 | 285 | 286 | 300 | 301 | 302 | 303 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 337 | 338 | 339 | 340 | 355 | 356 | 357 | 378 | 379 | 380 | 381 | 382 | 383 | localhost 384 | 5050 385 | 386 | 387 | 388 | 389 | 404 | 405 | 406 | 407 | 1298022342194 408 | 1298022342194 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 467 | 468 | 469 | 470 | 471 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 519 | 520 | 521 | 522 | 523 | 524 | Scala (plasma)|Scala 525 | 526 | 531 | 532 | 533 | 534 | 535 | 536 | scala-library-2.8.1 537 | 538 | 543 | 544 | 545 | 546 | 547 | 548 | 1.6 549 | 550 | 555 | 556 | 557 | 558 | 559 | 560 | Scala|plasma 561 | 562 | 567 | 568 | 569 | 570 | 571 | 572 | libs 573 | 574 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | SCALA-PROCESSING-EXAMPLES 2 | -------------------------------------------------------------- 3 | This is a repository with Processing sketches ported to Scala. 4 | To learn more about Processing read http://processing.org. 5 | To learn more about Scala read http://scala-lang.org. 6 | 7 | To run these you need to install latest version of Processing from 8 | http://processing.org/download/ and unpack native libraries of jogl and 9 | gluegen-rt from \libraries\opengl\library\ somewhere 10 | included in java.library.path (can be same folder). 11 | For example -Djava.library.path="c:\Program Files (x86)\processing-1.2.1\libraries\opengl\library" 12 | 13 | * fluids 14 | Scala port of MSA Visuals Fluids example 15 | http://memo.tv/msafluid_for_processing 16 | 17 | * plasma 18 | Scala port of Jorg Reuter's SinusPlasma sketch 19 | http://stachelig.de/ 20 | 21 | *NEW* Fluid Pong at https://github.com/InteractiveLab/fluid.pong 22 | Made using Processing, Scala, MSA Fluid and box2d. -------------------------------------------------------------------------------- /fluids/fluids.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /fluids/src/Fluids.scala: -------------------------------------------------------------------------------- 1 | import processing.core._ 2 | import processing.core.PConstants._ 3 | import scala.util.Random 4 | import msafluid._ 5 | import TUIO._ 6 | import processing.opengl._ 7 | import javax.media.opengl._ 8 | 9 | object Fluids extends PApplet { 10 | 11 | val FLUID_WIDTH = 120f 12 | val tuioCursorSpeedMult = 0.02f 13 | val tuioStationaryForce = 0.001f 14 | val tuioDoubleTapDistanceThreshold2 = 0.01f 15 | val tuioDoubleTapTimeThreshold = 0.2f 16 | 17 | private var fluids:Fluids = _ 18 | 19 | def main(args: Array[String]) = { 20 | fluids = new Fluids 21 | 22 | val frame = new javax.swing.JFrame("Fluids") 23 | frame.getContentPane().add(fluids) 24 | fluids.init 25 | 26 | frame.pack 27 | frame.setVisible(true) 28 | } 29 | 30 | implicit def Any2TuioCursor(value:Any) = value.asInstanceOf[TuioCursor] 31 | 32 | } 33 | 34 | class Fluids extends PApplet { 35 | 36 | import Fluids._ 37 | 38 | var invWidth, invHeight, aspectRatio, aspectRatio2: Float = _ 39 | var fluidSolver:MSAFluidSolver2D = _ 40 | var particleSystem:ParticleSystem = _ 41 | var imgFluid:PImage = _ 42 | var drawFluid:Boolean = true 43 | 44 | var tuioClient:TuioProcessing = _ 45 | var tuioLastTap:TuioPoint = _ 46 | var tuioDoubleTap:Boolean = false 47 | 48 | override def setup() = { 49 | size(800, 600, OPENGL ) 50 | hint( ENABLE_OPENGL_4X_SMOOTH ) 51 | 52 | invWidth = 1.0f/width 53 | invHeight = 1.0f/height 54 | aspectRatio = width * invHeight 55 | aspectRatio2 = aspectRatio * aspectRatio 56 | 57 | fluidSolver = new MSAFluidSolver2D( FLUID_WIDTH.toInt, (FLUID_WIDTH * height/width).toInt ) 58 | fluidSolver.enableRGB( true ).setFadeSpeed( 0.003f ).setDeltaT( 0.5f ).setVisc( 0.0001f ) 59 | 60 | imgFluid = createImage( fluidSolver.getWidth, fluidSolver.getHeight, RGB ) 61 | 62 | particleSystem = new ParticleSystem( fluidSolver ) 63 | 64 | initTUIO 65 | } 66 | 67 | override def draw() = { 68 | updateTUIO 69 | fluidSolver.update 70 | 71 | if( drawFluid ) { 72 | for( i <- 0 to fluidSolver.getNumCells-1 ) { 73 | val d = 2 74 | imgFluid.pixels(i) = color(fluidSolver.r(i) * d, fluidSolver.g(i) * d, fluidSolver.b(i) * d) 75 | } 76 | imgFluid.updatePixels 77 | image(imgFluid, 0, 0, width, height) 78 | } 79 | 80 | particleSystem.updateAndDraw(width, invWidth, height, invHeight, g.asInstanceOf[PGraphicsOpenGL], drawFluid) 81 | // println( frameRate.toString ) 82 | } 83 | 84 | override def mouseMoved = { 85 | val mouseNormX = mouseX * invWidth 86 | val mouseNormY = mouseY * invHeight 87 | val mouseVelX = (mouseX - pmouseX) * invWidth 88 | val mouseVelY = (mouseY - pmouseY) * invHeight 89 | 90 | addForce( mouseNormX, mouseNormY, mouseVelX, mouseVelY ) 91 | } 92 | 93 | override def mousePressed = { 94 | drawFluid ^= true 95 | } 96 | 97 | override def keyPressed = { 98 | key match { 99 | case 'r' => { 100 | ParticleSystem.renderUsingVA ^= true; 101 | println("renderUsingVA: " + ParticleSystem.renderUsingVA) 102 | } 103 | case _ => 104 | } 105 | } 106 | 107 | def addForce(x:Float, y:Float, dx:Float, dy:Float) = { 108 | val speed = dx * dx + dy * dy * aspectRatio2 109 | 110 | if( speed > 0 ) { 111 | var xx = x 112 | var yy = y 113 | 114 | if ( xx < 0 ) { 115 | xx = 0 116 | } else if ( xx > 1 ) { 117 | xx = 1 118 | } 119 | 120 | if ( yy < 0 ) { 121 | var yy = 0 122 | } else if ( yy > 1 ) { 123 | yy = 1 124 | } 125 | 126 | val colorMult = 5.0f 127 | val velocityMult = 30.0f 128 | 129 | val index = fluidSolver.getIndexForNormalizedPosition( xx, yy ) 130 | 131 | colorMode( HSB, 360, 1, 1 ) 132 | val hue = ((xx + yy) * 180 + frameCount) % 360 133 | val drawColor = color( hue, 1, 1 ) 134 | colorMode( RGB, 1 ) 135 | 136 | fluidSolver.rOld(index) += red(drawColor) * colorMult 137 | fluidSolver.gOld(index) += green(drawColor) * colorMult 138 | fluidSolver.bOld(index) += blue(drawColor) * colorMult 139 | 140 | particleSystem.addParticles(xx * width, yy * height, 10) 141 | fluidSolver.uOld(index) += dx * velocityMult 142 | fluidSolver.vOld(index) += dy * velocityMult 143 | } 144 | } 145 | 146 | def initTUIO = { 147 | tuioClient = new TuioProcessing( this, 3333 ) 148 | } 149 | 150 | def updateTUIO = { 151 | val tuioCursorList = tuioClient.getTuioCursors 152 | for( i <- 0 to tuioCursorList.size-1 ) { 153 | val tcur = tuioCursorList.elementAt(i) 154 | var vx = tcur.getXSpeed() * tuioCursorSpeedMult 155 | var vy = tcur.getYSpeed() * tuioCursorSpeedMult 156 | if ( vx == 0 && vy == 0 ) { 157 | vx = Random.nextFloat * 2 * tuioStationaryForce - tuioStationaryForce 158 | vy = Random.nextFloat * 2 * tuioStationaryForce - tuioStationaryForce 159 | } 160 | addForce( tcur.getX, tcur.getY, vx, vy ) 161 | } 162 | 163 | if ( tuioDoubleTap ) { 164 | drawFluid ^= true 165 | tuioDoubleTap = false 166 | } 167 | } 168 | 169 | def addTuioCursor(tcur:TuioCursor) = { 170 | if(tuioLastTap != null) { 171 | val timeMult = 0.000001f 172 | val nowTime = tcur.getTuioTime.getTotalMilliseconds * timeMult 173 | val lastTime = tuioLastTap.getTuioTime.getTotalMilliseconds * timeMult 174 | if( nowTime - lastTime < tuioDoubleTapTimeThreshold ) { 175 | val dx = tcur.getX - tuioLastTap.getX 176 | val dy = tcur.getY - tuioLastTap.getY 177 | val d2 = dx * dx + dy * dy 178 | if( d2 < tuioDoubleTapDistanceThreshold2 ) tuioDoubleTap = true 179 | } 180 | } 181 | tuioLastTap = new TuioPoint(tcur) 182 | } 183 | 184 | def addTuioObject(tobj:TuioObject) = {} 185 | def updateTuioObject(tobj:TuioObject) = {} 186 | def removeTuioObject(tobj:TuioObject) = {} 187 | def updateTuioCursor(tcur:TuioCursor) = {} 188 | def removeTuioCursor(tcur:TuioCursor) = {} 189 | def refresh(bundleTime:TuioTime) = {} 190 | 191 | } 192 | 193 | -------------------------------------------------------------------------------- /fluids/src/Particle.scala: -------------------------------------------------------------------------------- 1 | import scala.util.Random 2 | import java.nio.FloatBuffer 3 | import msafluid._ 4 | import javax.media.opengl._ 5 | 6 | object Particle { 7 | 8 | val MOMENTUM = 0.5f 9 | val FLUID_FORCE = 0.6f 10 | 11 | } 12 | 13 | class Particle(fluidSolver:MSAFluidSolver2D) { 14 | 15 | var x, y, vx, vy, radius, alpha, mass:Float = _ 16 | 17 | def init(x:Float, y:Float) = { 18 | this.x = x 19 | this.y = y 20 | radius = 5 21 | alpha = 0.3f + Random.nextFloat * 0.7f 22 | mass = 0.1f + Random.nextFloat * 0.9f 23 | } 24 | 25 | def update(width:Float, invWidth:Float, height:Float, invHeight:Float):Unit = { 26 | if ( alpha == 0 ) return 27 | 28 | val fluidIndex = fluidSolver.getIndexForNormalizedPosition( x * invWidth, y * invHeight ) 29 | vx = fluidSolver.u(fluidIndex) * width * mass * Particle.FLUID_FORCE + vx * Particle.MOMENTUM 30 | vy = fluidSolver.v(fluidIndex) * height * mass * Particle.FLUID_FORCE + vy * Particle.MOMENTUM 31 | 32 | x += vx 33 | y += vy 34 | 35 | if ( x < 0 ) { 36 | x = 0; 37 | vx *= -1 38 | } else if ( x > width ) { 39 | x = width 40 | vx *= -1 41 | } 42 | 43 | if ( y < 0 ) { 44 | y = 0 45 | vy *= -1 46 | } else if ( y > height ) { 47 | y = height 48 | vy *= -1 49 | } 50 | 51 | if ( vx*vx + vy*vy < 1) { 52 | vx = Random.nextFloat * 2f - 1f 53 | vy = Random.nextFloat * 2f - 1f 54 | } 55 | 56 | alpha *= 0.999f 57 | if ( alpha < 0.01 ) alpha = 0 58 | } 59 | 60 | def updateVertexArrays(i:Int, posBuffer:FloatBuffer, colBuffer:FloatBuffer) = { 61 | var vi = i * 4 62 | posBuffer.put( vi, x - vx ) 63 | vi += 1 64 | posBuffer.put( vi, y - vy ) 65 | vi += 1 66 | posBuffer.put( vi, x ) 67 | vi += 1 68 | posBuffer.put( vi, y ) 69 | 70 | var ci = i * 6 71 | colBuffer.put( ci, alpha ) 72 | ci += 1 73 | colBuffer.put( ci, alpha ) 74 | ci += 1 75 | colBuffer.put( ci, alpha ) 76 | ci += 1 77 | colBuffer.put( ci, alpha ) 78 | ci += 1 79 | colBuffer.put( ci, alpha ) 80 | ci += 1 81 | colBuffer.put( ci, alpha ) 82 | } 83 | 84 | def drawOldSchool(gl:GL) = { 85 | gl.glColor3f( alpha, alpha, alpha ) 86 | gl.glVertex2f( x - vx, y - vy ) 87 | gl.glVertex2f( x, y ) 88 | } 89 | 90 | } -------------------------------------------------------------------------------- /fluids/src/ParticleSystem.scala: -------------------------------------------------------------------------------- 1 | import scala.util.Random 2 | import java.nio.FloatBuffer 3 | import com.sun.opengl.util._ 4 | import processing.opengl._ 5 | import javax.media.opengl._ 6 | import msafluid._ 7 | 8 | object ParticleSystem { 9 | 10 | var renderUsingVA = true 11 | val maxParticles = 5000 12 | 13 | } 14 | 15 | class ParticleSystem(fluidSolver:MSAFluidSolver2D) { 16 | 17 | var posArray:FloatBuffer = BufferUtil.newFloatBuffer( ParticleSystem.maxParticles * 2 * 2 ) 18 | var colArray:FloatBuffer = BufferUtil.newFloatBuffer( ParticleSystem.maxParticles * 3 * 2 ) 19 | var curIndex:Int = 0 20 | val particles = new Array[Particle](ParticleSystem.maxParticles) 21 | 22 | for ( i <- 0 to ParticleSystem.maxParticles-1 ) particles(i) = new Particle(fluidSolver) 23 | 24 | def updateAndDraw(width:Float, invWidth:Float, height:Float, invHeight:Float, pgl:PGraphicsOpenGL, drawFluid:Boolean) = { 25 | val gl = pgl.beginGL 26 | gl.glEnable( GL.GL_BLEND ) 27 | if ( !drawFluid ) fadeToColor(width, height, gl, 0, 0, 0, 0.05f) 28 | 29 | gl.glBlendFunc( GL.GL_ONE, GL.GL_ONE ) 30 | gl.glEnable( GL.GL_LINE_SMOOTH ) 31 | gl.glLineWidth( 1 ) 32 | 33 | if ( ParticleSystem.renderUsingVA ) { 34 | for ( i <- 0 to ParticleSystem.maxParticles-1 ) { 35 | if ( particles(i).alpha > 0 ) { 36 | particles(i).update( width, invWidth, height, invHeight ) 37 | particles(i).updateVertexArrays( i, posArray, colArray ) 38 | } 39 | } 40 | gl.glEnableClientState( GL.GL_VERTEX_ARRAY ) 41 | gl.glVertexPointer( 2, GL.GL_FLOAT, 0, posArray ) 42 | gl.glEnableClientState( GL.GL_COLOR_ARRAY ) 43 | gl.glColorPointer( 3, GL.GL_FLOAT, 0, colArray ) 44 | 45 | gl.glDrawArrays( GL.GL_LINES, 0, ParticleSystem.maxParticles * 2 ) 46 | } else { 47 | gl.glBegin( GL.GL_LINES ); 48 | for( i <- 0 to ParticleSystem.maxParticles ) { 49 | if( particles(i).alpha > 0 ) { 50 | particles(i).update( width, invWidth, height, invHeight ) 51 | particles(i).drawOldSchool( gl ) 52 | } 53 | } 54 | gl.glEnd() 55 | } 56 | 57 | gl.glDisable( GL.GL_BLEND ) 58 | pgl.endGL() 59 | } 60 | 61 | def addParticles(x:Float, y:Float, count:Int) = { 62 | for ( i <- 0 to count-1 ) addParticle( x + Random.nextInt(30) - 15, y + Random.nextInt(30) - 15 ) 63 | } 64 | 65 | def addParticle(x:Float, y:Float) = { 66 | particles(curIndex).init( x, y ) 67 | curIndex += 1; 68 | if ( curIndex >= ParticleSystem.maxParticles ) curIndex = 0 69 | } 70 | 71 | def fadeToColor(width:Float, height:Float, gl:GL, r:Float, g:Float, b:Float, speed:Float) { 72 | gl.glBlendFunc( GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA ) 73 | gl.glColor4f( r, g, b, speed ) 74 | gl.glBegin( GL.GL_QUADS ) 75 | gl.glVertex2f( 0, 0 ) 76 | gl.glVertex2f( width, 0 ) 77 | gl.glVertex2f( width, height ) 78 | gl.glVertex2f( 0, height ) 79 | gl.glEnd() 80 | } 81 | 82 | } -------------------------------------------------------------------------------- /libs/MSAFluid.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valyard/scala-processing-examples/00d44071aa99393f7ae87f6bd4599fb57c8ca30c/libs/MSAFluid.jar -------------------------------------------------------------------------------- /libs/TUIO.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valyard/scala-processing-examples/00d44071aa99393f7ae87f6bd4599fb57c8ca30c/libs/TUIO.jar -------------------------------------------------------------------------------- /libs/libTUIO.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valyard/scala-processing-examples/00d44071aa99393f7ae87f6bd4599fb57c8ca30c/libs/libTUIO.jar -------------------------------------------------------------------------------- /plasma/plasma.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /plasma/src/Plasma.scala: -------------------------------------------------------------------------------- 1 | import processing.core._ 2 | import processing.core.PConstants._ 3 | import scala.util.Random 4 | 5 | object Plasma extends PApplet { 6 | 7 | val SCREENWIDTH = 640 8 | val SCREENHEIGHT = 360 9 | val GRADIENTLEN = 1500 10 | val SPEEDUP = 3 11 | val SWINGLEN = GRADIENTLEN * 3 12 | val SWINGMAX = GRADIENTLEN / 2 - 1 13 | 14 | private var plasma:Plasma = _ 15 | 16 | def main(args: Array[String]) = { 17 | plasma = new Plasma 18 | 19 | val frame = new javax.swing.JFrame("Plasma") 20 | frame.getContentPane().add(plasma) 21 | plasma.init 22 | 23 | frame.pack 24 | frame.setVisible(true) 25 | } 26 | 27 | } 28 | 29 | class Plasma extends PApplet { 30 | 31 | import Plasma._ 32 | 33 | var colorGrad:Array[Int] = _ 34 | var swingCurve:Array[Int] = _ 35 | 36 | override def setup() = { 37 | size( SCREENWIDTH, SCREENHEIGHT, P2D ) 38 | makeGradient( GRADIENTLEN ) 39 | makeSwingCurve( SWINGLEN, SWINGMAX ) 40 | } 41 | 42 | override def draw() = { 43 | loadPixels 44 | var i = 0 45 | val t = frameCount * SPEEDUP 46 | val swingT = swing(t) 47 | 48 | for ( y <- 0 to SCREENHEIGHT-1 ) { 49 | val swingY = swing(y) 50 | val swingYT = swing(y + t) 51 | for ( x <- 0 to SCREENWIDTH-1 ) { 52 | pixels(i) = gradient( 53 | swing(swing(x + swingT) + swingYT) + 54 | swing(swing(x + t ) + swingY )) 55 | i += 1 56 | } 57 | } 58 | updatePixels 59 | } 60 | 61 | override def mousePressed = { 62 | if ( mouseButton == LEFT ) { 63 | makeGradient( GRADIENTLEN ) 64 | } else if ( mouseButton == RIGHT ) { 65 | makeSwingCurve( SWINGLEN, SWINGMAX ) 66 | } 67 | } 68 | 69 | def makeSwingCurve(arrlen:Int, maxval:Int) = { 70 | var factor1 = 2 71 | var factor2 = 3 72 | var factor3 = 6 73 | 74 | if ( swingCurve == null ) { 75 | swingCurve = new Array[Int]( SWINGLEN ) 76 | } else { 77 | factor1 = Random.nextInt(6) + 1 78 | factor2 = Random.nextInt(6) + 1 79 | factor3 = Random.nextInt(6) + 1 80 | } 81 | 82 | val halfmax = (maxval/factor1).toInt 83 | 84 | for( i <- 0 to arrlen-1 ) { 85 | val ni = i * TWO_PI / arrlen 86 | swingCurve(i) = (math.cos( ni*factor1 ) * math.cos( ni*factor2 ) * math.cos( ni*factor3 ) * halfmax + halfmax ).toInt 87 | } 88 | } 89 | 90 | def makeGradient(arrlen:Int) = { 91 | var rf = 4 92 | var gf = 2 93 | var bf = 1 94 | var rd = 0 95 | var gd = arrlen / gf; 96 | var bd = arrlen / bf / 2 97 | 98 | if ( colorGrad == null ) { 99 | colorGrad = new Array[Int](GRADIENTLEN) 100 | } else { 101 | rf = Random.nextInt(6) + 1 102 | gf = Random.nextInt(6) + 1 103 | bf = Random.nextInt(6) + 1 104 | rd = Random.nextInt(arrlen) 105 | gd = Random.nextInt(arrlen) 106 | bd = Random.nextInt(arrlen) 107 | println("Gradient factors("+rf+","+gf+","+bf+"), displacement("+rd+","+gd+","+bd+")") 108 | } 109 | 110 | for ( i <- 0 to arrlen-1 ) { 111 | val r = cos256(arrlen / rf, i + rd) 112 | val g = cos256(arrlen / gf, i + gd) 113 | val b = cos256(arrlen / bf, i + bd) 114 | colorGrad(i) = color(r, g, b) 115 | } 116 | } 117 | 118 | def cos256(amplitude:Int, x:Int) = { 119 | (math.cos(x * TWO_PI / amplitude) * 127).toInt + 127 120 | } 121 | 122 | def swing(i:Int) = { 123 | swingCurve(i % SWINGLEN) 124 | } 125 | 126 | def gradient(i:Int) = { 127 | colorGrad(i % GRADIENTLEN) 128 | } 129 | 130 | } -------------------------------------------------------------------------------- /processing.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | --------------------------------------------------------------------------------