Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.jme3.scene.plugins.blender.modifiers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.plugins.blender.BlenderContext;
import com.jme3.scene.plugins.blender.file.BlenderFileException;
import com.jme3.scene.plugins.blender.file.Structure;
import com.jme3.scene.plugins.blender.meshes.Edge;
import com.jme3.scene.plugins.blender.meshes.Face;
import com.jme3.scene.plugins.blender.meshes.TemporalMesh;
import com.jme3.scene.plugins.blender.textures.TexturePixel;
/**
* A modifier that subdivides the mesh using either simple or catmull-clark subdivision.
*
* @author Marcin Roguski (Kaelthas)
*/
public class SubdivisionSurfaceModifier extends Modifier {
private static final Logger LOGGER = Logger.getLogger(SubdivisionSurfaceModifier.class.getName());
private static final int TYPE_CATMULLCLARK = 0;
private static final int TYPE_SIMPLE = 1;
private static final int FLAG_SUBDIVIDE_UVS = 0x8;
/** The subdivision type. */
private int subdivType;
/** The amount of subdivision levels. */
private int levels;
/** Indicates if the UV's should also be subdivided. */
private boolean subdivideUVS;
/** Stores the vertices that are located on original edges of the mesh. */
private Set verticesOnOriginalEdges = new HashSet();
/**
* Constructor loads all necessary modifier data.
* @param modifierStructure
* the modifier structure
* @param blenderContext
* the blender context
*/
public SubdivisionSurfaceModifier(Structure modifierStructure, BlenderContext blenderContext) {
if (this.validate(modifierStructure, blenderContext)) {
subdivType = ((Number) modifierStructure.getFieldValue("subdivType")).intValue();
levels = ((Number) modifierStructure.getFieldValue("levels")).intValue();
int flag = ((Number) modifierStructure.getFieldValue("flags")).intValue();
subdivideUVS = (flag & FLAG_SUBDIVIDE_UVS) != 0 && subdivType == TYPE_CATMULLCLARK;
if (subdivType != TYPE_CATMULLCLARK && subdivType != TYPE_SIMPLE) {
LOGGER.log(Level.SEVERE, "Unknown subdivision type: {0}.", subdivType);
invalid = true;
}
if (levels < 0) {
LOGGER.severe("The amount of subdivision levels cannot be negative.");
invalid = true;
}
}
}
@Override
public void apply(Node node, BlenderContext blenderContext) {
if (invalid) {
LOGGER.log(Level.WARNING, "Subdivision surface modifier is invalid! Cannot be applied to: {0}", node.getName());
} else if (levels > 0) {// no need to do anything if the levels is set to zero
TemporalMesh temporalMesh = this.getTemporalMesh(node);
if (temporalMesh != null) {
LOGGER.log(Level.FINE, "Applying subdivision surface modifier to: {0}", temporalMesh);
verticesOnOriginalEdges.clear();//in case the instance of this class was used more than once
for (Edge edge : temporalMesh.getEdges()) {
verticesOnOriginalEdges.add(edge.getFirstIndex());
verticesOnOriginalEdges.add(edge.getSecondIndex());
}
if (subdivType == TYPE_CATMULLCLARK) {
for (int i = 0; i < levels; ++i) {
this.subdivideSimple(temporalMesh);// first do simple subdivision ...
this.subdivideCatmullClark(temporalMesh);// ... and then apply Catmull-Clark algorithm
if (subdivideUVS) {// UV's can be subdivided only for Catmull-Clark subdivision algorithm
this.subdivideUVs(temporalMesh);
}
}
} else {
for (int i = 0; i < levels; ++i) {
this.subdivideSimple(temporalMesh);
}
}
} else {
LOGGER.log(Level.WARNING, "Cannot find temporal mesh for node: {0}. The modifier will NOT be applied!", node);
}
}
}
/**
* Catmull-Clark subdivision. It assumes that the mesh was already simple-subdivided.
* @param temporalMesh
* the mesh whose vertices will be transformed to form Catmull-Clark subdivision
*/
private void subdivideCatmullClark(TemporalMesh temporalMesh) {
Set boundaryVertices = new HashSet();
for (Edge edge : temporalMesh.getEdges()) {
if (!edge.isInFace()) {
boundaryVertices.add(edge.getFirstIndex());
boundaryVertices.add(edge.getSecondIndex());
} else {
if (temporalMesh.isBoundary(edge.getFirstIndex())) {
boundaryVertices.add(edge.getFirstIndex());
}
if (temporalMesh.isBoundary(edge.getSecondIndex())) {
boundaryVertices.add(edge.getSecondIndex());
}
}
}
List creasePoints = new ArrayList(temporalMesh.getVertexCount());
for (int i = 0; i < temporalMesh.getVertexCount(); ++i) {
// finding adjacent edges that were created by dividing original edges
List adjacentOriginalEdges = new ArrayList();
Collection adjacentEdges = temporalMesh.getAdjacentEdges(i);
if(adjacentEdges != null) {// this can be null if a vertex with index 'i' is not connected to any face nor edge
for (Edge edge : temporalMesh.getAdjacentEdges(i)) {
if (verticesOnOriginalEdges.contains(edge.getFirstIndex()) || verticesOnOriginalEdges.contains(edge.getSecondIndex())) {
adjacentOriginalEdges.add(edge);
}
}
creasePoints.add(new CreasePoint(i, boundaryVertices.contains(i), adjacentOriginalEdges, temporalMesh));
} else {
creasePoints.add(null);//the count of crease points must be equal to vertex count; otherwise we'll get IndexOutofBoundsException later
}
}
Vector3f[] averageVert = new Vector3f[temporalMesh.getVertexCount()];
int[] averageCount = new int[temporalMesh.getVertexCount()];
for (Face face : temporalMesh.getFaces()) {
Vector3f centroid = face.computeCentroid();
for (Integer index : face.getIndexes()) {
if (boundaryVertices.contains(index)) {
Edge edge = this.findEdge(temporalMesh, index, face.getIndexes().getNextIndex(index));
if (temporalMesh.isBoundary(edge)) {
averageVert[index] = averageVert[index] == null ? edge.computeCentroid() : averageVert[index].addLocal(edge.computeCentroid());
averageCount[index] += 1;
}
edge = this.findEdge(temporalMesh, face.getIndexes().getPreviousIndex(index), index);
if (temporalMesh.isBoundary(edge)) {
averageVert[index] = averageVert[index] == null ? edge.computeCentroid() : averageVert[index].addLocal(edge.computeCentroid());
averageCount[index] += 1;
}
} else {
averageVert[index] = averageVert[index] == null ? centroid.clone() : averageVert[index].addLocal(centroid);
averageCount[index] += 1;
}
}
}
for (Edge edge : temporalMesh.getEdges()) {
if (!edge.isInFace()) {
Vector3f centroid = temporalMesh.getVertices().get(edge.getFirstIndex()).add(temporalMesh.getVertices().get(edge.getSecondIndex())).divideLocal(2);
averageVert[edge.getFirstIndex()] = averageVert[edge.getFirstIndex()] == null ? centroid.clone() : averageVert[edge.getFirstIndex()].addLocal(centroid);
averageVert[edge.getSecondIndex()] = averageVert[edge.getSecondIndex()] == null ? centroid.clone() : averageVert[edge.getSecondIndex()].addLocal(centroid);
averageCount[edge.getFirstIndex()] += 1;
averageCount[edge.getSecondIndex()] += 1;
}
}
for (int i = 0; i < averageVert.length; ++i) {
if(averageVert[i] != null && averageCount[i] > 0) {
Vector3f v = temporalMesh.getVertices().get(i);
averageVert[i].divideLocal(averageCount[i]);
// computing translation vector
Vector3f t = averageVert[i].subtract(v);
if (!boundaryVertices.contains(i)) {
t.multLocal(4 / (float) averageCount[i]);
}
// moving the vertex
v.addLocal(t);
// applying crease weight if necessary
CreasePoint creasePoint = creasePoints.get(i);
if (creasePoint.getTarget() != null && creasePoint.getWeight() != 0) {
t = creasePoint.getTarget().subtractLocal(v).multLocal(creasePoint.getWeight());
v.addLocal(t);
}
}
}
}
/**
* The method performs a simple subdivision of the mesh.
*
* @param temporalMesh
* the mesh to be subdivided
*/
private void subdivideSimple(TemporalMesh temporalMesh) {
Map edgePoints = new HashMap();
Map facePoints = new HashMap();
Set newFaces = new LinkedHashSet();
Set newEdges = new LinkedHashSet(temporalMesh.getEdges().size() * 4);
int originalFacesCount = temporalMesh.getFaces().size();
List