aboutsummaryrefslogtreecommitdiff
path: root/engine/src/blender/com/jme3/scene/plugins/blender/textures/UVProjectionGenerator.java
blob: 4412d608afa4a05f82bd7f106a1f36e50d592530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package com.jme3.scene.plugins.blender.textures;

import com.jme3.bounding.BoundingBox;
import com.jme3.bounding.BoundingSphere;
import com.jme3.math.FastMath;
import com.jme3.math.Triangle;
import com.jme3.math.Vector3f;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer;
import com.jme3.scene.plugins.blender.textures.UVCoordinatesGenerator.BoundingTube;
import java.nio.FloatBuffer;

/**
 * This class helps with projection calculations.
 * 
 * @author Marcin Roguski (Kaelthas)
 */
/* package */class UVProjectionGenerator {
	/**
	 * Flat projection for 2D textures.
	 * 
	 * @param mesh
	 *            mesh that is to be projected
	 * @param bb
	 *            the bounding box for projecting
	 * @return UV coordinates after the projection
	 */
	public static float[] flatProjection(Mesh mesh, BoundingBox bb) {
		if (bb == null) {
			bb = UVCoordinatesGenerator.getBoundingBox(mesh);
		}
		Vector3f min = bb.getMin(null);
		float[] ext = new float[] { bb.getXExtent() * 2.0f, bb.getYExtent() * 2.0f };
		FloatBuffer positions = mesh.getFloatBuffer(VertexBuffer.Type.Position);
		float[] uvCoordinates = new float[positions.limit() / 3 * 2];
		for (int i = 0, j = 0; i < positions.limit(); i += 3, j += 2) {
			uvCoordinates[j] = (positions.get(i) - min.x) / ext[0];
			uvCoordinates[j + 1] = (positions.get(i + 1) - min.y) / ext[1];
			// skip the Z-coordinate
		}
		return uvCoordinates;
	}

	/**
	 * Cube projection for 2D textures.
	 * 
	 * @param mesh
	 *            mesh that is to be projected
	 * @param bb
	 *            the bounding box for projecting
	 * @return UV coordinates after the projection
	 */
	public static float[] cubeProjection(Mesh mesh, BoundingBox bb) {
		Triangle triangle = new Triangle();
		Vector3f x = new Vector3f(1, 0, 0);
		Vector3f y = new Vector3f(0, 1, 0);
		Vector3f z = new Vector3f(0, 0, 1);
		Vector3f min = bb.getMin(null);
		float[] ext = new float[] { bb.getXExtent() * 2.0f, bb.getYExtent() * 2.0f, bb.getZExtent() * 2.0f };

		float[] uvCoordinates = new float[mesh.getTriangleCount() * 6];// 6 == 3 * 2
		float borderAngle = (float) Math.sqrt(2.0f) / 2.0f;
		for (int i = 0, pointIndex = 0; i < mesh.getTriangleCount(); ++i) {
			mesh.getTriangle(i, triangle);
			Vector3f n = triangle.getNormal();
			float dotNX = Math.abs(n.dot(x));
			float dorNY = Math.abs(n.dot(y));
			float dotNZ = Math.abs(n.dot(z));
			if (dotNX > borderAngle) {
				if (dotNZ < borderAngle) {// discard X-coordinate
					uvCoordinates[pointIndex++] = (triangle.get1().y - min.y) / ext[1];
					uvCoordinates[pointIndex++] = (triangle.get1().z - min.z) / ext[2];
					uvCoordinates[pointIndex++] = (triangle.get2().y - min.y) / ext[1];
					uvCoordinates[pointIndex++] = (triangle.get2().z - min.z) / ext[2];
					uvCoordinates[pointIndex++] = (triangle.get3().y - min.y) / ext[1];
					uvCoordinates[pointIndex++] = (triangle.get3().z - min.z) / ext[2];
				} else {// discard Z-coordinate
					uvCoordinates[pointIndex++] = (triangle.get1().x - min.x) / ext[0];
					uvCoordinates[pointIndex++] = (triangle.get1().y - min.y) / ext[1];
					uvCoordinates[pointIndex++] = (triangle.get2().x - min.x) / ext[0];
					uvCoordinates[pointIndex++] = (triangle.get2().y - min.y) / ext[1];
					uvCoordinates[pointIndex++] = (triangle.get3().x - min.x) / ext[0];
					uvCoordinates[pointIndex++] = (triangle.get3().y - min.y) / ext[1];
				}
			} else {
				if (dorNY > borderAngle) {// discard Y-coordinate
					uvCoordinates[pointIndex++] = (triangle.get1().x - min.x) / ext[0];
					uvCoordinates[pointIndex++] = (triangle.get1().z - min.z) / ext[2];
					uvCoordinates[pointIndex++] = (triangle.get2().x - min.x) / ext[0];
					uvCoordinates[pointIndex++] = (triangle.get2().z - min.z) / ext[2];
					uvCoordinates[pointIndex++] = (triangle.get3().x - min.x) / ext[0];
					uvCoordinates[pointIndex++] = (triangle.get3().z - min.z) / ext[2];
				} else {// discard Z-coordinate
					uvCoordinates[pointIndex++] = (triangle.get1().x - min.x) / ext[0];
					uvCoordinates[pointIndex++] = (triangle.get1().y - min.y) / ext[1];
					uvCoordinates[pointIndex++] = (triangle.get2().x - min.x) / ext[0];
					uvCoordinates[pointIndex++] = (triangle.get2().y - min.y) / ext[1];
					uvCoordinates[pointIndex++] = (triangle.get3().x - min.x) / ext[0];
					uvCoordinates[pointIndex++] = (triangle.get3().y - min.y) / ext[1];
				}
			}
			triangle.setNormal(null);// clear the previous normal vector
		}
		return uvCoordinates;
	}

	/**
	 * Tube projection for 2D textures.
	 * 
	 * @param mesh
	 *            mesh that is to be projected
	 * @param bt
	 *            the bounding tube for projecting
	 * @return UV coordinates after the projection
	 */
	public static float[] tubeProjection(Mesh mesh, BoundingTube bt) {
		FloatBuffer positions = mesh.getFloatBuffer(VertexBuffer.Type.Position);
		float[] uvCoordinates = new float[positions.limit() / 3 * 2];
		Vector3f v = new Vector3f();
		float cx = bt.getCenter().x, cy = bt.getCenter().y;
		Vector3f uBase = new Vector3f(0, -1, 0);
		
		float vBase = bt.getCenter().z - bt.getHeight() * 0.5f;
		for (int i = 0, j = 0; i < positions.limit(); i += 3, j += 2) {
			// calculating U
			v.set(positions.get(i)-cx, positions.get(i + 1)-cy, 0);
			v.normalizeLocal();
			float angle = v.angleBetween(uBase);// result between [0; PI]
			if (v.x < 0) {// the angle should be greater than PI, we're on the other part of the image then
				angle = FastMath.TWO_PI - angle;
			}
			uvCoordinates[j] = angle / FastMath.TWO_PI;

			// calculating V
			float z = positions.get(i + 2);
			uvCoordinates[j + 1] = (z - vBase) / bt.getHeight();
		}
		
		//looking for splitted triangles
		Triangle triangle = new Triangle();
		for(int i=0;i<mesh.getTriangleCount();++i) {
			mesh.getTriangle(i, triangle);
			float sgn1 = Math.signum(triangle.get1().x-cx);
			float sgn2 = Math.signum(triangle.get2().x-cx);
			float sgn3 = Math.signum(triangle.get3().x-cx);
			float xSideFactor = sgn1 + sgn2 + sgn3;
			float ySideFactor = Math.signum(triangle.get1().y-cy)+
					   Math.signum(triangle.get2().y-cy)+
					   Math.signum(triangle.get3().y-cy);
			if((xSideFactor>-3 || xSideFactor<3) && ySideFactor<0) {//the triangle is on the splitting plane
				//indexOfUcoord = (indexOfTriangle*3 + indexOfTrianglesVertex)*2
				if(sgn1==1.0f) {
					uvCoordinates[i*3*2] += 1.0f;
				}
				if(sgn2==1.0f) {
					uvCoordinates[(i*3+1)*2] += 1.0f;
				}
				if(sgn3==1.0f) {
					uvCoordinates[(i*3+2)*2] += 1.0f;
				}
			}
		}
		return uvCoordinates;
	}

	/**
	 * Sphere projection for 2D textures.
	 * 
	 * @param mesh
	 *            mesh that is to be projected
	 * @param bb
	 *            the bounding box for projecting
	 * @return UV coordinates after the projection
	 */
	public static float[] sphereProjection(Mesh mesh, BoundingSphere bs) {
		FloatBuffer positions = mesh.getFloatBuffer(VertexBuffer.Type.Position);
		float[] uvCoordinates = new float[positions.limit() / 3 * 2];
		Vector3f v = new Vector3f();
		float cx = bs.getCenter().x, cy = bs.getCenter().y, cz = bs.getCenter().z;
		Vector3f uBase = new Vector3f(0, -1, 0);
		Vector3f vBase = new Vector3f(0, 0, -1);

		for (int i = 0, j = 0; i < positions.limit(); i += 3, j += 2) {
			// calculating U
			v.set(positions.get(i)-cx, positions.get(i + 1)-cy, 0);
			v.normalizeLocal();
			float angle = v.angleBetween(uBase);// result between [0; PI]
			if (v.x < 0) {// the angle should be greater than PI, we're on the other part of the image then
				angle = FastMath.TWO_PI - angle;
			}
			uvCoordinates[j] = angle / FastMath.TWO_PI;

			// calculating V
			v.set(positions.get(i)-cx, positions.get(i + 1)-cy, positions.get(i + 2)-cz);
			v.normalizeLocal();
			angle = v.angleBetween(vBase);// result between [0; PI]
			uvCoordinates[j+1] = angle / FastMath.PI;
		}
		
		//looking for splitted triangles
		Triangle triangle = new Triangle();
		for(int i=0;i<mesh.getTriangleCount();++i) {
			mesh.getTriangle(i, triangle);
			float sgn1 = Math.signum(triangle.get1().x-cx);
			float sgn2 = Math.signum(triangle.get2().x-cx);
			float sgn3 = Math.signum(triangle.get3().x-cx);
			float xSideFactor = sgn1 + sgn2 + sgn3;
			float ySideFactor = Math.signum(triangle.get1().y-cy)+
					   Math.signum(triangle.get2().y-cy)+
					   Math.signum(triangle.get3().y-cy);
			if((xSideFactor>-3 || xSideFactor<3) && ySideFactor<0) {//the triangle is on the splitting plane
				//indexOfUcoord = (indexOfTriangle*3 + indexOfTrianglesVertex)*2
				if(sgn1==1.0f) {
					uvCoordinates[i*3*2] += 1.0f;
				}
				if(sgn2==1.0f) {
					uvCoordinates[(i*3+1)*2] += 1.0f;
				}
				if(sgn3==1.0f) {
					uvCoordinates[(i*3+2)*2] += 1.0f;
				}
			}
		}
		return uvCoordinates;
	}
}