Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.strijar.vs;
- import com.jme3.bullet.control.RigidBodyControl;
- import com.jme3.bullet.control.SoftBodyControl;
- import com.jme3.math.Vector3f;
- import com.jme3.scene.Mesh;
- import com.jme3.scene.VertexBuffer.Type;
- import com.jme3.util.BufferUtils;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import java.util.ArrayList;
- import java.util.HashSet;
- class TriMesh extends Mesh {
- private int SUB = 5;
- private final int vertexCount;
- private final ArrayList<Vector3f> vertex = new ArrayList<Vector3f>();
- private final ArrayList<Integer> index = new ArrayList<Integer>();
- private final HashSet<Integer> sideAB = new HashSet<Integer>();
- public TriMesh(Vector3f a, Vector3f b, Vector3f c) {
- vertex.add(a); sideAB.add(0);
- vertex.add(b); sideAB.add(1);
- vertex.add(c);
- createTri(a, b, c, 0, 1, 2, true, SUB);
- // Vertex
- vertexCount = vertex.size();
- FloatBuffer vertBuf = BufferUtils.createVector3Buffer(vertexCount);
- setBuffer(Type.Position, 3, vertBuf);
- int i = 0;
- for (Vector3f item : vertex)
- BufferUtils.setInBuffer(item, vertBuf, i++);
- vertex.clear();
- // Index
- IntBuffer indexBuf = BufferUtils.createIntBuffer(index.size());
- setBuffer(Type.Index, 3, indexBuf);
- for (Integer item : index)
- indexBuf.put(item);
- index.clear();
- // Color
- FloatBuffer colorBuf = BufferUtils.createFloatBuffer(vertexCount * 4);
- setBuffer(Type.Color, 4, colorBuf);
- for (i = 0; i < vertexCount; i++) {
- if (sideAB.contains(i)) {
- if (i < 64) {
- colorBuf.put(1f);
- colorBuf.put(0f);
- colorBuf.put(0f);
- } else {
- colorBuf.put(1f);
- colorBuf.put(1f);
- colorBuf.put(1f);
- }
- } else {
- colorBuf.put(0f);
- colorBuf.put(0f);
- colorBuf.put(1f);
- }
- colorBuf.put(1f);
- }
- // Normal
- FloatBuffer normBuf = BufferUtils.createVector3Buffer(vertexCount);
- setBuffer(Type.Normal, 3, normBuf);
- for (i = 0; i < vertexCount; i++)
- BufferUtils.setInBuffer(Vector3f.UNIT_X, normBuf, i);
- updateBound();
- }
- public void attachAB(RigidBodyControl line, SoftBodyControl control) {
- for (Integer item : sideAB) {
- control.appendAnchor(item, line, false, 1f);
- }
- }
- private void createTri(Vector3f a, Vector3f b, Vector3f c, int ai, int bi, int ci, boolean abFlag, int sub) {
- if (sub == 0) {
- index.add(ai);
- index.add(bi);
- index.add(ci);
- if (abFlag) {
- sideAB.add(ai);
- sideAB.add(bi);
- }
- } else {
- Vector3f d = new Vector3f();
- Vector3f e = new Vector3f();
- Vector3f f = new Vector3f();
- int ei = vertex.size();
- int fi = ei + 1;
- int di = ei + 2;
- d.interpolateLocal(a, c, 0.5f);
- e.interpolateLocal(a, b, 0.5f);
- f.interpolateLocal(b, c, 0.5f);
- vertex.add(e);
- vertex.add(f);
- vertex.add(d);
- createTri(a, e, d, ai, ei, di, abFlag, sub-1);
- createTri(e, b, f, ei, bi, fi, abFlag, sub-1);
- createTri(d, e, f, di, ei, fi, false, sub-1);
- createTri(d, f, c, di, fi, ci, false, sub-1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment