- Fixed the way handedness/parity is computed in the tangent binormal generator
git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10663 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
This commit is contained in:
parent
08b27f7673
commit
cf1ff27234
@ -35,6 +35,7 @@ import com.jme3.math.ColorRGBA;
|
|||||||
import com.jme3.math.FastMath;
|
import com.jme3.math.FastMath;
|
||||||
import com.jme3.math.Vector2f;
|
import com.jme3.math.Vector2f;
|
||||||
import com.jme3.math.Vector3f;
|
import com.jme3.math.Vector3f;
|
||||||
|
import com.jme3.math.Vector4f;
|
||||||
import com.jme3.scene.*;
|
import com.jme3.scene.*;
|
||||||
import com.jme3.scene.VertexBuffer.Format;
|
import com.jme3.scene.VertexBuffer.Format;
|
||||||
import com.jme3.scene.VertexBuffer.Type;
|
import com.jme3.scene.VertexBuffer.Type;
|
||||||
@ -442,14 +443,14 @@ public class TangentBinormalGenerator {
|
|||||||
{
|
{
|
||||||
ArrayList<VertexInfo> vertexMap = linkVertices(mesh);
|
ArrayList<VertexInfo> vertexMap = linkVertices(mesh);
|
||||||
|
|
||||||
FloatBuffer normalBuffer = (FloatBuffer) mesh.getBuffer(Type.Normal).getData();
|
// FloatBuffer normalBuffer = (FloatBuffer) mesh.getBuffer(Type.Normal).getData();
|
||||||
|
|
||||||
FloatBuffer tangents = BufferUtils.createFloatBuffer(vertices.length * 4);
|
FloatBuffer tangents = BufferUtils.createFloatBuffer(vertices.length * 4);
|
||||||
// FloatBuffer binormals = BufferUtils.createFloatBuffer(vertices.length * 3);
|
// FloatBuffer binormals = BufferUtils.createFloatBuffer(vertices.length * 3);
|
||||||
|
|
||||||
Vector3f tangent = new Vector3f();
|
Vector3f tangent = new Vector3f();
|
||||||
Vector3f binormal = new Vector3f();
|
Vector3f binormal = new Vector3f();
|
||||||
Vector3f normal = new Vector3f();
|
//Vector3f normal = new Vector3f();
|
||||||
Vector3f givenNormal = new Vector3f();
|
Vector3f givenNormal = new Vector3f();
|
||||||
|
|
||||||
Vector3f tangentUnit = new Vector3f();
|
Vector3f tangentUnit = new Vector3f();
|
||||||
@ -509,24 +510,24 @@ public class TangentBinormalGenerator {
|
|||||||
ArrayList<TriangleData> triangles = vertices[i].triangles;
|
ArrayList<TriangleData> triangles = vertices[i].triangles;
|
||||||
triangleCount += triangles.size();
|
triangleCount += triangles.size();
|
||||||
|
|
||||||
boolean flippedNormal = false;
|
// boolean flippedNormal = false;
|
||||||
for (int j = 0; j < triangles.size(); j++) {
|
for (int j = 0; j < triangles.size(); j++) {
|
||||||
TriangleData triangleData = triangles.get(j);
|
TriangleData triangleData = triangles.get(j);
|
||||||
tangent.addLocal(triangleData.tangent);
|
tangent.addLocal(triangleData.tangent);
|
||||||
binormal.addLocal(triangleData.binormal);
|
binormal.addLocal(triangleData.binormal);
|
||||||
|
|
||||||
if (givenNormal.dot(triangleData.normal) < 0) {
|
// if (givenNormal.dot(triangleData.normal) < 0) {
|
||||||
flippedNormal = true;
|
// flippedNormal = true;
|
||||||
}
|
// }
|
||||||
}
|
|
||||||
if (flippedNormal /*&& approxTangent*/) {
|
|
||||||
// Generated normal is flipped for this vertex,
|
|
||||||
// so binormal = normal.cross(tangent) will be flipped in the shader
|
|
||||||
// log.log(Level.WARNING,
|
|
||||||
// "Binormal is flipped for vertex {0}.", i);
|
|
||||||
|
|
||||||
wCoord = 1;
|
|
||||||
}
|
}
|
||||||
|
// if (flippedNormal /*&& approxTangent*/) {
|
||||||
|
// // Generated normal is flipped for this vertex,
|
||||||
|
// // so binormal = normal.cross(tangent) will be flipped in the shader
|
||||||
|
// // log.log(Level.WARNING,
|
||||||
|
// // "Binormal is flipped for vertex {0}.", i);
|
||||||
|
//
|
||||||
|
// wCoord = 1;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -587,18 +588,19 @@ public class TangentBinormalGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector3f finalTangent = new Vector3f();
|
||||||
|
Vector3f tmp = new Vector3f();
|
||||||
for (int i : vertexInfo.indices) {
|
for (int i : vertexInfo.indices) {
|
||||||
if (approxTangent) {
|
if (approxTangent) {
|
||||||
// This calculation ensures that normal and tagent have a 90 degree angle.
|
// Gram-Schmidt orthogonalize
|
||||||
// Removing this will lead to visual artifacts.
|
finalTangent.set(tangent).subtractLocal(tmp.set(givenNormal).multLocal(givenNormal.dot(tangent)));
|
||||||
givenNormal.cross(tangent, binormal);
|
finalTangent.normalizeLocal();
|
||||||
binormal.cross(givenNormal, tangent);
|
|
||||||
|
|
||||||
tangent.normalizeLocal();
|
wCoord = tmp.set(givenNormal).crossLocal(tangent).dot(binormal) <0f? -1f:1f;
|
||||||
|
|
||||||
tangents.put((i * 4), tangent.x);
|
tangents.put((i * 4), finalTangent.x);
|
||||||
tangents.put((i * 4) + 1, tangent.y);
|
tangents.put((i * 4) + 1, finalTangent.y);
|
||||||
tangents.put((i * 4) + 2, tangent.z);
|
tangents.put((i * 4) + 2, finalTangent.z);
|
||||||
tangents.put((i * 4) + 3, wCoord);
|
tangents.put((i * 4) + 3, wCoord);
|
||||||
} else {
|
} else {
|
||||||
tangents.put((i * 4), tangent.x);
|
tangents.put((i * 4), tangent.x);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user