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

The following examples show how to use processing.core.PShape#setVertex() . 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: HaxMapDrawingTool.java    From haxademic with MIT License 6 votes vote down vote up
public void mousePressed() {
	super.mousePressed();
	_isPressed = true;
	
	
	// find closest vertex to mouse
	float closestPoint = 10;
	PShape curShape = null;
	PVector curVertex = null;
	for (int i=0; i < _shapes.size(); i++) {
		curShape = _shapes.get(i);
		for (int j = 0; j < curShape.getVertexCount(); j++) {
			curVertex = curShape.getVertex(j);
			float mouseDistToVertex = MathUtil.getDistance(p.mouseX, p.mouseY, curVertex.x, curVertex.y);
			if( mouseDistToVertex < closestPoint ) {
				// set z to 1 so that we know that we're dragging (this is a silly hack)
				curShape.setVertex(j, curShape.getVertex(j).x, curShape.getVertex(j).y, 1);
				_draggingShapes.add( curShape );
			}
		}
	}
}
 
Example 2
Source File: HaxMapDrawingTool.java    From haxademic with MIT License 5 votes vote down vote up
public void mouseReleased() {
	super.mouseReleased();
	_isPressed = false;

	if( _draggingShapes.size() > 0 ) {
		while( _draggingShapes.size() > 0 ) {
			// remove and set z back to 0
			PShape curShape = _draggingShapes.remove(_draggingShapes.size()-1);
			for (int j = 0; j < curShape.getVertexCount(); j++) curShape.setVertex(j, curShape.getVertex(j).x, curShape.getVertex(j).y, 0);
		}
	} else {			
		if (_curShape == null) {
			_curShape = p.createShape();
			_curShape.setFill(color(255, 200));
			_curShape.beginShape();
			_curShape.noStroke();  
			_curShape.vertex( curMouseX, curMouseY );
		} else {
			_curShape.vertex( curMouseX, curMouseY );
		}
		// auto-close shape
		if( drawMode == MODE_TRIANGLES ) {
			if( _curShape.getVertexCount() == 3 ) {
				closeShape();
			}
		} else if( drawMode == MODE_RECTS ) {
			if( _curShape.getVertexCount() == 4 ) {
				closeShape();
			}
		}
	}
}
 
Example 3
Source File: PShapeUtil.java    From haxademic with MIT License 5 votes vote down vote up
public static void offsetShapeVertices(PShape s, float xOffset, float yOffset, float zOffset) {
	for (int i = 0; i < s.getVertexCount(); i++) {
		PVector vertex = s.getVertex(i);
		s.setVertex(i, vertex.x + xOffset, vertex.y + yOffset, vertex.z + zOffset);
	}
	for (int i = 0; i < s.getChildCount(); i++) {
		PShape subShape = s.getChild(i);
		offsetShapeVertices(subShape, xOffset, yOffset, zOffset);
	}
}
 
Example 4
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 5
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 6
Source File: PShapeUtil.java    From haxademic with MIT License 5 votes vote down vote up
public static void meshFlipOnAxis(PShape shape, int axis) {
	for (int i = 0; i < shape.getVertexCount(); i++) {
		PVector v = shape.getVertex(i);
		if(axis == P.X) {
			shape.setVertex(i, v.x * -1f, v.y, v.z);
		} else if(axis == P.Y) {
			shape.setVertex(i, v.x, v.y * -1f, v.z);
		} else if(axis == P.Z) {
			shape.setVertex(i, v.x, v.y, v.z * -1f);
		}
	}
	for (int j = 0; j < shape.getChildCount(); j++) {
		meshFlipOnAxis(shape.getChild(j), axis);
	}
}
 
Example 7
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 8
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();
	}