// water test with vertex colouring and multitexturing // use mouse to control perlin noise octaves and position in space // author: info@toxi.co.uk float yoff,zoff,ns=0.125,zs=4.75; int ww=12; int dd=5; int imgScale=20; int imgW=ww*imgScale; int imgH=dd*imgScale; float du=(float)imgW/(2*ww+1); float dv=(float)imgH/(2*dd+1); float[] surfaceZ; float spray_threshold=0.78; BImage noiseImg; ParticleSystem spray; void setup() { size(512,256); // array to store noise values (z coordinate of vertices) surfaceZ=new float[(ww*2+1)*(dd*2+1)*2]; // create image object for texture noiseImg=new BImage(imgW,imgH); // set mode for UV mapping to use pixel space textureMode(IMAGE_SPACE); // setup light g.lightKind[2]=DIFFUSE; g.lightX[2] = width*0.5; g.lightY[2] = 10; g.lightZ[2] = height; g.lightR[2] = g.lightG[2] = g.lightB[2] = 1; // setup foam/spray particles spray=new ParticleSystem(20); } void loop() { float nx,ny=yoff,z1,z2,vz1,vz2,x0=mouseX*0.01; // ripple control noiseDetail(6,0.45+mouseY*(0.175/height)); // setup viewport background(20,40,50); noStroke(); translate(width*0.5,height*.6,0); rotateX(PI/3.25);//-noise(x0+ww*ns,yoff+2*dd,zoff)*0.25); rotateY((width*0.5-mouseX)*0.0005); scale(28); // no lights needed for 1st texture pass noLights(); // vertex counter int surfID=0; // start build mesh for(int y=-dd; y<=dd; y++) { nx=x0; beginShape(TRIANGLE_STRIP); for(int x=-ww; xspray_threshold) { spray.add(x,y,vz1); // use white as spray colour fill(255,150); } else fill(0,z1*160,z1*255,pow(10,0.85+z1*2)); vertex(x,y,vz1); if (z2>spray_threshold) { spray.add(x,y+1,vz2); fill(255,150); } else fill(0,z2*160,z2*255,pow(10,0.85+z2*2)); vertex(x,y+1,vz2); // store pre-mulitplied noise values for next pass surfaceZ[surfID++]=vz1; surfaceZ[surfID++]=vz2; nx+=ns; } endShape(); ny+=ns; } // 2nd pass, draw surface again with texture fill(255); lights(); // update texture image for this layer updateTexture(); // slightly offset transform matrix to avoid zBuffer fighting translate(0,0,0.01); float u,v=0; ny=yoff; surfID=0; for(int y=-dd; y<=dd; y++) { nx=x0; u=0; beginShape(TRIANGLE_STRIP); texture(noiseImg); for(int x=-ww; x