Java Code Examples for processing.core.PShape#getVertex()

The following examples show how to use processing.core.PShape#getVertex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PShapeUtil.java    From haxademic with MIT License 6 votes vote down vote up
public static void addTextureUVSpherical(PShape shape, PImage img) {
	shape.setStroke(false);
	// shape.setFill(255);	// This seems to jack up vertex shaders
	shape.setTextureMode(P.NORMAL);
	
	for (int i = 0; i < shape.getVertexCount(); i++) {
		PVector p = shape.getVertex(i);
		// map spherical coordinate to uv coordinate :: https://stackoverflow.com/questions/19357290/convert-3d-point-on-sphere-to-uv-coordinate
		util.set(p.normalize()); 
		float u = P.atan2(util.x, util.z) / P.TWO_PI + 0.5f; 
		float v = P.asin(util.y) / P.PI + .5f;
		shape.setTextureUV(i, u, v);
	}
		
	for (int j = 0; j < shape.getChildCount(); j++) {
		PShape subShape = shape.getChild(j);
		addTextureUVToShape(subShape, img);
	}
	
	if(img != null) shape.setTexture(img);
}
 
Example 2
Source File: PShapeUtil.java    From haxademic with MIT License 6 votes vote down vote up
public static void exportMesh(PShape mesh) {
	StringBuilder verts = new StringBuilder();
	StringBuilder faces = new StringBuilder();
	final int vertsNum = mesh.getVertexCount();
	final PVector v = new PVector();
	for(int i=0; i < vertsNum; i+=3) {
		mesh.getVertex(i, v);
		verts.append("v " + v.x + " " + v.y + " " + v.z + "\n");
		mesh.getVertex(i+1, v);
		verts.append("v " + v.x + " " + v.y + " " + v.z + "\n");
		mesh.getVertex(i+2, v);
		verts.append("v " + v.x + " " + v.y + " " + v.z + "\n");
		faces.append("f " + (i+1) + " " + (i+2) + " " + (i+3) + "\n");
	}
	String outputStr = "o Sphere\n";
	outputStr += verts;
	outputStr += faces;
	FileUtil.writeTextToFile(FileUtil.haxademicOutputPath() + "text/model-"+SystemUtil.getTimestamp()+".obj", outputStr);
}
 
Example 3
Source File: PShapeUtil.java    From haxademic with MIT License 6 votes vote down vote up
public static void addVerticesToPointShape(PShape origShape, PShape newShape) {
	for (int i = 0; i < origShape.getVertexCount(); i++) {
		// check to see if vertex has already been added
		PVector v = origShape.getVertex(i);
		boolean foundDuplicateVertex = false;
		for (int j = 0; j < newShape.getVertexCount(); j++) {
			PVector addedVertex = newShape.getVertex(j);
			if(v.x == addedVertex.x && v.y == addedVertex.y && v.z == addedVertex.z) foundDuplicateVertex = true;
		}
		// if not already added, add it
		if(foundDuplicateVertex == false) newShape.vertex(v.x, v.y, v.z);
	}
	// recurse through children
	for (int j = 0; j < origShape.getChildCount(); j++) {
		addVerticesToPointShape(origShape.getChild(j), newShape);
	}
}
 
Example 4
Source File: PShapeUtil.java    From haxademic with MIT License 6 votes vote down vote up
public static void addTestStrokeToShape(PShape shape, float strokeWeight, float oscMult) {
	for (int i = 0; i < shape.getVertexCount(); i++) {
		PVector vertex = shape.getVertex(i);
		int strokeReplace = P.p.color(
				127 + 127f * P.sin(vertex.x * oscMult),
				127 + 127f * P.sin(vertex.y * oscMult),
				127 + 127f * P.sin(vertex.z * oscMult)
				);
		shape.noFill();
		shape.setStrokeWeight(i, 4);
		shape.setStroke(strokeReplace);
	}
	for (int j = 0; j < shape.getChildCount(); j++) {
		addTestStrokeToShape(shape.getChild(j), strokeWeight, oscMult);
	}
}
 
Example 5
Source File: PShapeUtil.java    From haxademic with MIT License 6 votes vote down vote up
public static void addTextureUVExactWidthHeight(PShape shape, PImage img, float width, float height) {
	shape.setStroke(false);
	// shape.setFill(255);	// This seems to jack up vertex shaders
	shape.setTextureMode(P.NORMAL);
	
	for (int i = 0; i < shape.getVertexCount(); i++) {
		PVector v = shape.getVertex(i);
		shape.setTextureUV(
				i, 
				P.map(v.x, -width/2f, width/2f, 0, 1f), 
				P.map(v.y, -height/2f, height/2f, 0, 1f)
				);
	}
	
	for (int j = 0; j < shape.getChildCount(); j++) {
		PShape subShape = shape.getChild(j);
		addTextureUVExactWidthHeight(subShape, img, width, height);
	}
	
	if(img != null) shape.setTexture(img);
}
 
Example 6
Source File: PShapeUtil.java    From haxademic with MIT License 6 votes vote down vote up
public static void addTextureUVToShape(PShape shape, PImage img, float outerExtent, boolean xyMapping) {
	shape.setStroke(false);
	// shape.setFill(255);	// This seems to jack up vertex shaders
	shape.setTextureMode(P.NORMAL);
	
	for (int i = 0; i < shape.getVertexCount(); i++) {
		PVector v = shape.getVertex(i);
		float uX = (xyMapping == true) ? v.x : v.z;
		shape.setTextureUV(
				i, 
				P.map(uX, -outerExtent, outerExtent, 0, 1f), 
				P.map(v.y, -outerExtent, outerExtent, 0, 1f)
				);
	}

	for (int j = 0; j < shape.getChildCount(); j++) {
		PShape subShape = shape.getChild(j);
		addTextureUVToShape(subShape, img, outerExtent, xyMapping);
	}

	if(img != null) shape.setTexture(img);
}
 
Example 7
Source File: HaxMapDrawingTool.java    From haxademic with MIT License 6 votes vote down vote up
public void exportVertices() {
	String export = "";
	for( int i=0; i < _shapeGroups.size(); i++ ) {
		export += "#group#\n";
		ArrayList<PShape> curGroup = _shapeGroups.get(i);
		for( int j=0; j < curGroup.size(); j++ ) {
			PShape curShape = curGroup.get(j);
			PVector vertex;
			export += "#poly#";
			for (int k = 0; k < curShape.getVertexCount(); k++) {
				vertex = curShape.getVertex(k);
				if( k > 0 ) export += ",";
				export += vertex.x+","+vertex.y;
			}
			export += "\n";
		}
	}
	FileUtil.writeTextToFile(FileUtil.haxademicDataPath() + "text/mapping/mapping-"+SystemUtil.getTimestamp()+".txt", export);
}
 
Example 8
Source File: Impeach.java    From haxademic with MIT License 6 votes vote down vote up
public void addFillToShape(PShape shape, float oscMult) {
	for (int i = 0; i < shape.getVertexCount(); i++) {
		PVector vertex = shape.getVertex(i);
		float zFade = P.map(vertex.z, 50, -50, 1, 0);
		int fillReplace = P.p.color(
			(127 + 127f * P.sin(vertex.x * oscMult)) * zFade,
			(127 + 127f * P.sin(vertex.y * oscMult)) * zFade,
			(127 + 127f * P.sin(vertex.z * oscMult)) * zFade
		);
		shape.setFill(i, fillReplace);
		shape.noStroke();
	}
	for (int j = 0; j < shape.getChildCount(); j++) {
		addFillToShape(shape.getChild(j), oscMult);
	}
}
 
Example 9
Source File: PShapeUtil.java    From haxademic with MIT License 5 votes vote down vote up
public static PShape clonePShape(PApplet p, PShape tesselation) {
	PShape newShape = p.createShape();
	newShape.beginShape(P.TRIANGLES);
	for (int i = 0; i < tesselation.getVertexCount(); i++) {
		PVector v = tesselation.getVertex(i);
		newShape.vertex(v.x, v.y);
	}
	newShape.endShape(P.CLOSE);
	newShape.setStroke(false);
	return newShape;
}
 
Example 10
Source File: PShapeUtil.java    From haxademic with MIT License 5 votes vote down vote up
public static ArrayList<PVector> getUniqueVertices(PShape shape, ArrayList<PVector> uniqueVertices) {
	if(uniqueVertices == null) uniqueVertices = new ArrayList<PVector>();
	for (int i = 0; i < shape.getVertexCount(); i++) {
		PVector point = shape.getVertex(i);
		if(hasVertex(uniqueVertices, point) == false) {
			uniqueVertices.add( point ); 
		}
	}
		
	for (int j = 0; j < shape.getChildCount(); j++) {
		PShape subShape = shape.getChild(j);
		getUniqueVertices(subShape, uniqueVertices);
	}
	return uniqueVertices;
}
 
Example 11
Source File: HaxMapDrawingTool.java    From haxademic with MIT License 5 votes vote down vote up
public void drawExistingShapes() {
	// draw already-drawn shapes
	p.noFill();
	p.stroke(255);

	for (int i=0; i < _shapes.size(); i++) {
		// get shape and set audio-reactive fill --------------
		PShape shape = _shapes.get(i);
		shape.setFill(p.color(255, AudioIn.audioFreq((i * 10 + 10)) * 2000));
		p.shape( shape );


		if( _debugging == true ) {
			// draw wireframe and handles -------------------------
			PVector v = null;
			PVector nextV = null;
			int numVertices = shape.getVertexCount();
			for (int j = 0; j < shape.getVertexCount(); j++) {
				v = shape.getVertex(j);
				p.ellipse( v.x, v.y, 6, 6 );
				if( j < numVertices - 1 ) {
					nextV = shape.getVertex(j+1);
					p.line( v.x, v.y, nextV.x, nextV.y );
				}
			}
			p.line( shape.getVertex(0).x, shape.getVertex(0).y, shape.getVertex(numVertices-1).x, shape.getVertex(numVertices-1).y );
		}
	}
}
 
Example 12
Source File: Demo_VectorFlyer.java    From haxademic with MIT License 5 votes vote down vote up
public void addPointsToAttractors(PShape shape) {
	for (int i = 0; i < shape.getVertexCount(); i++) {
		PVector point = shape.getVertex(i);
		if(attractorExists(point) == false) {
			attractors.add( point ); 
			attractorsCount++;
			DebugView.setValue("attractorsCount", attractorsCount);
		}
	}
		
	for (int j = 0; j < shape.getChildCount(); j++) {
		PShape subShape = shape.getChild(j);
		addPointsToAttractors(subShape);
	}
}
 
Example 13
Source File: PShapeUtil.java    From haxademic with MIT License 5 votes vote down vote up
public static void verticalTwistShape(PShape shape, float amp, float freq) {
	float height = PShapeUtil.getMaxAbsY(shape);
	for (int i = 0; i < shape.getVertexCount(); i++) {
		PVector v = shape.getVertex(i);
		float radius = MathUtil.getDistance(v.x, v.z, 0, 0);
		float twistAtY = ((v.y + radius) * freq) * 0.001f * amp;
		float newRads = MathUtil.getRadiansToTarget(0, 0, v.x, v.z) + twistAtY;
		shape.setVertex(i, radius * P.cos(newRads), v.y, radius * P.sin(newRads));
	}
	for (int j = 0; j < shape.getChildCount(); j++) {
		verticalTwistShape(shape.getChild(j), amp, freq);
	}
}
 
Example 14
Source File: PShapeUtil.java    From haxademic with MIT License 5 votes vote down vote up
public static void repairMissingSVGVertex(PShape shape) {
	PVector v1 = shape.getVertex(0);
	PVector v2 = shape.getVertex(0);
	PVector v3 = shape.getVertex(shape.getVertexCount() - 1);
	
	shape.beginShape();
	shape.fill(255, 255, 255);
	shape.noStroke();
	shape.vertex(v1.x, v1.y, v1.z);
	shape.vertex(v2.x, v2.y, v2.z);
	shape.vertex(v3.x, v3.y, v3.z);
	shape.endShape();
}
 
Example 15
Source File: PShapeUtil.java    From haxademic with MIT License 5 votes vote down vote up
public static void getDepth(PShape shape, float[] minMax) {
	for (int i = 0; i < shape.getVertexCount(); i++) {
		if(shape.getVertex(i).z < minMax[0]) minMax[0] = shape.getVertex(i).z;
		if(shape.getVertex(i).z > minMax[1]) minMax[1] = shape.getVertex(i).z;
	}
	for (int j = 0; j < shape.getChildCount(); j++) {
		getDepth(shape.getChild(j), minMax);
	}
}
 
Example 16
Source File: PShapeUtil.java    From haxademic with MIT License 5 votes vote down vote up
public static void scaleVertices(PShape s, float scale) {
	for (int i = 0; i < s.getVertexCount(); i++) {
		PVector curVertex = s.getVertex(i);
		s.setVertex(i, curVertex.x * scale, curVertex.y * scale, curVertex.z * scale);
	}
	
	for (int j = 0; j < s.getChildCount(); j++) {
		PShape subShape = s.getChild(j);
		scaleVertices(subShape, scale);
	}
}
 
Example 17
Source File: PShapeUtil.java    From haxademic with MIT License 5 votes vote down vote up
public static void scaleVertices(PShape s, float x, float y, float z) {
	for (int i = 0; i < s.getVertexCount(); i++) {
		PVector curVertex = s.getVertex(i);
		s.setVertex(i, curVertex.x * x, curVertex.y * y, curVertex.z * z);
	}
	
	for (int j = 0; j < s.getChildCount(); j++) {
		PShape subShape = s.getChild(j);
		scaleVertices(subShape, x, y, z);
	}
}
 
Example 18
Source File: IcosahedronQuadraticCurves.java    From haxademic with MIT License 5 votes vote down vote up
public void drawCurves(PShape shape) {
	for (int i = 0; i < shape.getVertexCount() - 2; i+=3) {
		PVector v1 = shape.getVertex(i);
		PVector v2 = shape.getVertex(i+1);
		PVector v3 = shape.getVertex(i+2);
		
		float eqAmp = 1f + AudioIn.audioFreq(i);
		p.stroke(255f * (-0.75f + eqAmp));
		
		// override for render
		// p.blendMode(PBlendModes.ADD);
		eqAmp = 1;
		p.stroke(185);

		p.beginShape();
		p.vertex(v1.x, v1.y, v1.z);
		p.quadraticVertex(v2.x * eqAmp, v2.y * eqAmp, v2.z * eqAmp, v3.x, v3.y, v3.z);
		p.endShape();

		p.beginShape();
		p.vertex(v2.x, v2.y, v2.z);
		p.quadraticVertex(v1.x * eqAmp, v1.y * eqAmp, v1.z * eqAmp, v3.x, v3.y, v3.z);
		p.endShape();
		
		p.beginShape();
		p.vertex(v1.x, v1.y, v1.z);
		p.quadraticVertex(v3.x * eqAmp, v3.y * eqAmp, v3.z * eqAmp, v2.x, v2.y, v2.z);
		p.endShape();
	}
	for (int j = 0; j < shape.getChildCount(); j++) {
		drawCurves(shape.getChild(j));
	}
}
 
Example 19
Source File: HeadSkullRender.java    From haxademic with MIT License 4 votes vote down vote up
protected void drawObj() {
		////////////////
		// set context
		////////////////
		p.pushMatrix();
		p.translate(p.width/2, p.height * 0.45f, -p.width);
		p.rotateZ(P.PI);
		p.rotateY(P.sin(P.TWO_PI * _progress) * 0.45f);
		
		////////////////
		// draw skull
		////////////////
		p.pushMatrix();
		p.translate(0, p.height * -0.03f, 0);
		skullObj.disableStyle();
		p.fill(120 + 30f * P.sin(_progress * P.TWO_PI * 50f), 0, 0);
//		p.scale(0.73f + 0.1f * P.sin(P.TWO_PI * _progress));
		p.scale(0.71f);
		p.shape(skullObj);
		p.popMatrix();
		
		////////////////
		// draw head
		////////////////
		p.pushMatrix();
		p.translate(0, p.height * -0.18f, 12);
		p.fill(255);
//		p.scale(0.9f + 0.2f * P.sin(P.PI + P.TWO_PI * _progress));
		
		// sweeping x progress for distance check
		float xProg = P.map(_progress, 0, 1, -maxObjExtent * 1.3f, maxObjExtent * 1.3f);
		
		// shrink/grow adjusted mesh
		for (int i = 0; i < obj.getChildren().length; i++ ) {
			PShape child = obj.getChild(i);
			PShape childOrig = objOrig.getChild(i);
			for(int vIndex = 0; vIndex < child.getVertexCount(); vIndex++) {
				// PVector vertex = child.getVertex(vIndex);
				PVector vertexOrig = childOrig.getVertex(vIndex);
//				float amp = 1.2f + 0.2f * P.sin((float)i/100f + _progress * P.TWO_PI);
//				float amp = 1.2f + 0.2f * P.sin((float)i/100f + P.abs(vertexOrig.x) + P.abs(vertexOrig.y) + P.abs(vertexOrig.z) + _progress * P.TWO_PI);
				// float amp = 1;
//				child.setVertex(vIndex, vertexOrig.x * amp, vertexOrig.y * amp, vertexOrig.z * amp);
				
				// float indexOffset = (float)i / 100f;
//				float easedProgress = Penner.easeInOutCubic(0.5f + 0.5f * P.sin(indexOffset + _progress * P.TWO_PI), 0, 1, 1);
				
				// get distance to sweeping x coord
				float dist = MathUtil.getDistance(xProg, 0, objFaceCenters[i].x, 0);
				float easedProgress = 0;
				float distanceMax = maxObjExtent * 0.65f;
				if(P.abs(dist) < distanceMax) {
//					easedProgress = P.map(dist, distanceMax, 0, 0, 1);
					easedProgress = Penner.easeInOutExpo(P.map(dist, distanceMax, 0, 0, 1), 0, 1, 1);
				}

				// set vertices of manipulated object
				child.setVertex(
					vIndex, 
					P.map(easedProgress, 0, 1, vertexOrig.x, objFaceCenters[i].x), 
					P.map(easedProgress, 0, 1, vertexOrig.y, objFaceCenters[i].y), 
					P.map(easedProgress, 0, 1, vertexOrig.z, objFaceCenters[i].z)
				);
			}
		}
		
		// draw debug box for x-distance check on shrinking triangles
		if(UI.active()) {
			p.pushMatrix();
			p.translate(xProg, 300);
			p.fill(255);
			p.box(20);
			p.popMatrix();
		}
		
//		obj.disableStyle();
		p.fill(255,185,40);
		p.shape(obj);
		p.popMatrix();

		// reset context
		p.popMatrix();
	}
 
Example 20
Source File: PShapeUtil.java    From haxademic with MIT License 4 votes vote down vote up
public static PShape createExtrudedShape(PShape shape, float depth, PShape newShape) {
	if(newShape == null) newShape = P.p.createShape();

	newShape.beginShape(P.TRIANGLES);
	// top
	for (int i = 0; i < shape.getVertexCount() - 3; i+=3) {
		// copy triangle vertices & UV coords
		PVector v1 = shape.getVertex(i);
		PVector v2 = shape.getVertex(i+1);
		PVector v3 = shape.getVertex(i+2);
		float texU1 = shape.getTextureU(i);
		float texV1 = shape.getTextureU(i);
		float texU2 = shape.getTextureU(i+1);
		float texV2 = shape.getTextureU(i+1);
		float texU3 = shape.getTextureU(i+2);
		float texV3 = shape.getTextureU(i+2);
		
		// half depth to keep new model centered on z-axis
		float halfDepth = depth / 2f;
		
		// top
		newShape.vertex(v1.x, v1.y, halfDepth, texU1, texV1);
		newShape.vertex(v2.x, v2.y, halfDepth, texU2, texV2);
		newShape.vertex(v3.x, v3.y, halfDepth, texU3, texV3);
		
		// bottom
		newShape.vertex(v1.x, v1.y, -halfDepth, texU1, texV1);
		newShape.vertex(v2.x, v2.y, -halfDepth, texU2, texV2);
		newShape.vertex(v3.x, v3.y, -halfDepth, texU3, texV3);
		
		// walls
		// wall 1
		newShape.vertex(v1.x, v1.y,  halfDepth, texU1, texV1);
		newShape.vertex(v1.x, v1.y, -halfDepth, texU1, texV1);
		newShape.vertex(v2.x, v2.y,  halfDepth, texU2, texV2);

		newShape.vertex(v1.x, v1.y, -halfDepth, texU1, texV1);
		newShape.vertex(v2.x, v2.y, -halfDepth, texU2, texV2);
		newShape.vertex(v2.x, v2.y,  halfDepth, texU2, texV2);

		// wall 2
		newShape.vertex(v2.x, v2.y,  halfDepth, texU2, texV2);
		newShape.vertex(v2.x, v2.y, -halfDepth, texU2, texV2);
		newShape.vertex(v3.x, v3.y,  halfDepth, texU3, texV3);
		
		newShape.vertex(v2.x, v2.y, -halfDepth, texU2, texV2);
		newShape.vertex(v3.x, v3.y, -halfDepth, texU3, texV3);
		newShape.vertex(v3.x, v3.y,  halfDepth, texU3, texV3);
		
		// wall 3
		newShape.vertex(v3.x, v3.y,  halfDepth, texU3, texV3);
		newShape.vertex(v3.x, v3.y, -halfDepth, texU3, texV3);
		newShape.vertex(v1.x, v1.y,  halfDepth, texU1, texV1);
		
		newShape.vertex(v3.x, v3.y, -halfDepth, texU3, texV3);
		newShape.vertex(v1.x, v1.y, -halfDepth, texU1, texV1);
		newShape.vertex(v1.x, v1.y,  halfDepth, texU1, texV1);
	}
	
	newShape.endShape();
	
	// recurse through original shape children if nested shapes
	for (int j = 0; j < shape.getChildCount(); j++) {
		PShape subShape = shape.getChild(j);
		createExtrudedShape(subShape, depth, newShape);
	}

	return newShape;
}