Support for image sphere projection on a mesh.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8092 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
Kae..pl 14 years ago
parent 895e02f2a6
commit 17392d2cf8
  1. 3
      engine/src/blender/com/jme3/scene/plugins/blender/textures/UVCoordinatesGenerator.java
  2. 101
      engine/src/blender/com/jme3/scene/plugins/blender/textures/UVProjectionGenerator.java

@ -118,7 +118,8 @@ public class UVCoordinatesGenerator {
uvCoordinates = UVProjectionGenerator.tubeProjection(mesh, bt); uvCoordinates = UVProjectionGenerator.tubeProjection(mesh, bt);
break; break;
case PROJECTION_SPHERE: case PROJECTION_SPHERE:
uvCoordinates = UVProjectionGenerator.sphereProjection(mesh, bb); BoundingSphere bs = UVCoordinatesGenerator.getBoundingSphere(geometries);
uvCoordinates = UVProjectionGenerator.sphereProjection(mesh, bs);
break; break;
default: default:
throw new IllegalStateException("Unknown projection type: " + projection); throw new IllegalStateException("Unknown projection type: " + projection);

@ -3,6 +3,7 @@ package com.jme3.scene.plugins.blender.textures;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import com.jme3.bounding.BoundingBox; import com.jme3.bounding.BoundingBox;
import com.jme3.bounding.BoundingSphere;
import com.jme3.math.FastMath; import com.jme3.math.FastMath;
import com.jme3.math.Triangle; import com.jme3.math.Triangle;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
@ -172,55 +173,55 @@ import com.jme3.scene.plugins.blender.textures.UVCoordinatesGenerator.BoundingTu
* the bounding box for projecting * the bounding box for projecting
* @return UV coordinates after the projection * @return UV coordinates after the projection
*/ */
public static float[] sphereProjection(Mesh mesh, BoundingBox bb) { public static float[] sphereProjection(Mesh mesh, BoundingSphere bs) {
return null;// TODO: implement FloatBuffer positions = mesh.getFloatBuffer(VertexBuffer.Type.Position);
// Vector2f[] uvTable = new Vector2f[vertexList.size()]; float[] uvCoordinates = new float[positions.limit() / 3 * 2];
// Ray ray = new Ray(); Vector3f v = new Vector3f();
// CollisionResults cr = new CollisionResults(); float cx = bs.getCenter().x, cy = bs.getCenter().y, cz = bs.getCenter().z;
// Vector3f yVec = new Vector3f(); Vector3f uBase = new Vector3f(0, -1, 0);
// Vector3f zVec = new Vector3f(); Vector3f vBase = new Vector3f(0, 0, -1);
// for(Geometry geom : geometries) {
// if(materialHelper.hasTexture(geom.getMaterial())) {//generate only for (int i = 0, j = 0; i < positions.limit(); i += 3, j += 2) {
// when material has a texture // calculating U
// geom.getMesh().updateBound(); v.set(positions.get(i)-cx, positions.get(i + 1)-cy, 0);
// BoundingSphere bs = this.getBoundingSphere(geom.getMesh()); v.normalizeLocal();
// float r2 = bs.getRadius() * bs.getRadius(); float angle = v.angleBetween(uBase);// result between [0; PI]
// yVec.set(0, -bs.getRadius(), 0); if (v.x < 0) {// the angle should be greater than PI, we're on the other part of the image then
// zVec.set(0, 0, -bs.getRadius()); angle = FastMath.TWO_PI - angle;
// Vector3f center = bs.getCenter(); }
// ray.setOrigin(center); uvCoordinates[j] = angle / FastMath.TWO_PI;
// //we cast each vertex of the current mesh on the bounding box to
// determine the UV-coordinates // calculating V
// for(int i=0;i<geom.getMesh().getIndexBuffer().size();++i) { v.set(positions.get(i)-cx, positions.get(i + 1)-cy, positions.get(i + 2)-cz);
// int index = geom.getMesh().getIndexBuffer().get(i); v.normalizeLocal();
// angle = v.angleBetween(vBase);// result between [0; PI]
// ray.setOrigin(vertexList.get(index)); uvCoordinates[j+1] = angle / FastMath.PI;
// ray.setDirection(normalList.get(index)); }
//
// //finding collision point //looking for splitted triangles
// cr.clear(); Triangle triangle = new Triangle();
// bs.collideWith(ray, cr);//there is ALWAYS one collision for(int i=0;i<mesh.getTriangleCount();++i) {
// Vector3f p = cr.getCollision(0).getContactPoint(); mesh.getTriangle(i, triangle);
// p.subtractLocal(center); float sgn1 = Math.signum(triangle.get1().x-cx);
// //arcLength = FastMath.acos(p.dot(yVec)/(p.length * yVec.length)) * r float sgn2 = Math.signum(triangle.get2().x-cx);
// <- an arc length on the sphere (from top to the point on float sgn3 = Math.signum(triangle.get3().x-cx);
// the sphere) float xSideFactor = sgn1 + sgn2 + sgn3;
// //but yVec.length == r and p.length == r so: arcLength = float ySideFactor = Math.signum(triangle.get1().y-cy)+
// FastMath.acos(p.dot(yVec)/r^2)/r Math.signum(triangle.get2().y-cy)+
// //U coordinate is as follows: u = arcLength / PI*r Math.signum(triangle.get3().y-cy);
// //so to compute it faster we just write: u = if((xSideFactor>-3 || xSideFactor<3) && ySideFactor<0) {//the triangle is on the splitting plane
// FastMath.acos(p.dot(yVec)/r^2) / PI; //indexOfUcoord = (indexOfTriangle*3 + indexOfTrianglesVertex)*2
// float u = FastMath.acos(p.dot(yVec)/r2) / FastMath.PI; if(sgn1==1.0f) {
// //we use similiar method to compute v uvCoordinates[i*3*2] += 1.0f;
// //the only difference is that we need to cast the p vector on ZX }
// plane if(sgn2==1.0f) {
// //and use its length instead of r uvCoordinates[(i*3+1)*2] += 1.0f;
// p.y = 0; }
// float v = FastMath.acos(p.dot(zVec)/(bs.getRadius()*p.length())) / if(sgn3==1.0f) {
// FastMath.PI; uvCoordinates[(i*3+2)*2] += 1.0f;
// uvTable[index] = new Vector2f(u, v); }
// } }
// } }
// } return uvCoordinates;
} }
} }

Loading…
Cancel
Save