Normand Briere
2019-10-04 57c5b6cd8d12ffdaa3e0b099451e3c031012750a
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
 * Copyright (c) 2003-2009 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
//import com.jmex.effects.particles.*;
import com.jme.math.FastMath;
import java.io.IOException;
 
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
//import com.jme.renderer.Renderer;
//import com.jme.scene.TexCoords;
//import com.jme.scene.TriMesh;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;
import com.jme.util.geom.BufferUtils;
//import com.jmex.effects.particles.Particle.Status;
 
//
import com.jme.scene.TexCoords;
 
/**
 * ParticleMesh is a particle system that uses TriMesh as its underlying
 * geometric data.
 * 
 * @author Joshua Slack
 * @version $Id: ParticleMesh.java 4133 2009-03-19 20:40:11Z blaine.dev $
 */
public class ParticleNode extends ParticleSystem
{
    static final long serialVersionUID = 0;
 
    boolean IsStatic()
    {
        return false;
    }
//    private static final long serialVersionUID = 2L;
    private boolean useMeshTexCoords = true;
    private boolean useTriangleNormalEmit = true;
 
//    public ParticleMesh()
//    {
//    }
 
    cVector minima;
    cVector maxima;
    
    public ParticleNode(String name, int numParticles)
    {
        super(name, numParticles);
        //minima = LA.newVector(-0.5, 0, -0.5);
        //maxima = LA.newVector(0.5, 1, 0.5);
        
        CreateMaterial();
        
        live = true;
        rewind = true;
        
    //setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
    //setLightCombineMode(LightCombineMode.Off);
    //setTextureCombineMode(TextureCombineMode.Replace);
    }
 
    public ParticleNode(String name, int numParticles, com.jmex.effects.particles.ParticleSystem.ParticleType type)
    {
        super(name, numParticles, type);
    //setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
    //setLightCombineMode(LightCombineMode.Off);
    //setTextureCombineMode(TextureCombineMode.Replace);
    }
 
    public ParticleNode(String name, TriMesh geom)
    {
        super(name, 0, com.jmex.effects.particles.ParticleSystem.ParticleType.GeomMesh);
        numParticles = geom.getTriangleCount();
        psGeom = geom;
        //setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
        //setLightCombineMode(LightCombineMode.Off);
        //setTextureCombineMode(TextureCombineMode.Replace);
        initializeParticles(geom.getTriangleCount());
    }
 
    @Override
    protected void initializeParticles(int numParticles)
    {
 
        if (particleGeom != null)
        {
            //detachChild(particleGeom);
        }
        TriMesh mesh = new TriMesh(name + "_mesh");
//        {
//
//            private static final long serialVersionUID = 1L;
//
//            @Override
//            public void updateWorldVectors()
//            {
//                ; // Do nothing.
//            }
//        };
        particleGeom = mesh;
        //attachChild(mesh);
        particles = new Particle[numParticles];
        if (numParticles == 0)
        {
            return;
        }
        Vector2f[] sharedTextureData;
 
        // setup texture coords
        switch (getParticleType())
        {
            case GeomMesh:
            case Triangle:
                sharedTextureData = new Vector2f[]{
                    new Vector2f(0.0f, 0.0f),
                    new Vector2f(0.0f, 2.0f),
                    new Vector2f(2.0f, 0.0f)
                };
                break;
            case Quad:
                sharedTextureData = new Vector2f[]{
                    new Vector2f(0.0f, 0.0f),
                    new Vector2f(0.0f, 1.0f),
                    new Vector2f(1.0f, 0.0f),
                    new Vector2f(1.0f, 1.0f)
                };
                break;
            case Point: sharedTextureData = null; break;
            default:
                throw new IllegalStateException("Particle Mesh may only have particle type of ParticleType.Quad, ParticleType.GeomMesh or ParticleType.Triangle");
        }
 
        int verts = getVertsForParticleType(getParticleType());
 
        geometryCoordinates = FloatBuffer.allocate(numParticles * verts * 3); //BufferUtils.createVector3Buffer(numParticles * verts);
 
        // setup indices
        int[] indices;
        switch (getParticleType())
        {
            case Triangle:
            case GeomMesh:
                indices = new int[numParticles * 3];
                for (int j = 0; j < numParticles; j++)
                {
                    indices[0 + j * 3] = j * 3 + 2;
                    indices[1 + j * 3] = j * 3 + 1;
                    indices[2 + j * 3] = j * 3 + 0;
                }
                break;
            case Quad:
                indices = new int[numParticles * 6];
                for (int j = 0; j < numParticles; j++)
                {
                    indices[0 + j * 6] = j * 4 + 2;
                    indices[1 + j * 6] = j * 4 + 1;
                    indices[2 + j * 6] = j * 4 + 0;
 
                    indices[3 + j * 6] = j * 4 + 2;
                    indices[4 + j * 6] = j * 4 + 3;
                    indices[5 + j * 6] = j * 4 + 1;
                }
                break;
            case Point:
                indices = new int[numParticles];
                for (int j = 0; j < numParticles; j++)
                {
                    indices[j] = j;
                }
                break;
            default:
                throw new IllegalStateException("Particle Mesh may only have particle type of ParticleType.Quad, ParticleType.GeomMesh or ParticleType.Triangle");
        }
 
        appearanceColors = FloatBuffer.allocate(numParticles * verts * 4); //BufferUtils.createColorBuffer(numParticles * verts);
        
        sizesBuffer = FloatBuffer.allocate(numParticles * verts * 3); //BufferUtils.createVector3Buffer(numParticles * verts);
 
        mesh.setVertexBuffer(geometryCoordinates);
        mesh.setColorBuffer(appearanceColors);
        mesh.setSizeBuffer(sizesBuffer);
        mesh.setTextureCoords(new TexCoords(BufferUtils.createVector2Buffer(numParticles * verts)), 0);
        mesh.setIndexBuffer(BufferUtils.createIntBuffer(indices));
 
        invScale = new Vector3f();
 
        for (int k = 0; k < numParticles; k++)
        {
            particles[k] = new Particle(this);
            particles[k].init();
            particles[k].setStartIndex(k * verts);
            for (int a = verts - 1; a >= 0; a--)
            {
                int ind = (k * verts) + a;
                if (verts > 1)
                {
                    if (particleType == com.jmex.effects.particles.ParticleSystem.ParticleType.GeomMesh && useMeshTexCoords)
                    {
                        int index = ((TriMesh) psGeom).getIndexBuffer().get(ind);
                        BufferUtils.populateFromBuffer(workVect2, psGeom.getTextureCoords(0).coords, index);
                        BufferUtils.setInBuffer(workVect2, mesh.getTextureCoords(0).coords, ind);
                    } else
                    {
                        BufferUtils.setInBuffer(sharedTextureData[a],
                                mesh.getTextureCoords(0).coords, ind);
                    }
                }
                
                //BufferUtils.setInBuffer(particles[k].getCurrentColor(), appearanceColors, (ind));
                appearanceColors.set(ind, particles[k].getCurrentColor());
            }
 
        }
    //    updateRenderState();
    //    particleGeom.setCastsShadows(false);
    }
 
    /**
     * Stop emiting particles
     * Kill all available particles. (status=dead) 
     * @author clovis teixeira
     */
//    public void stopEmitting()
//    {
//        //for (int k = 0; k < numParticles; k++)
//        for (int k = 0; k < particles.length; k++)
//        {
//            if (particles[k].getStatus() == Particle.Status0.Available)
//            {
//                particles[k].setStatus(Particle.Status0.Dead);
//            }
//        }
//    }
 
//    public void draw(Renderer r)
//    {
//        Camera camera = r.getCamera();
//        for (int i = 0; i < particles.length; i++)
//        {
//            Particle particle = particles[i];
//            if (particle.getStatus() == com.jmex.effects.particles.Particle.Status.Alive)
//            {
//                particle.updateVerts(camera);
//            }
//        }
//
//        if (!particlesInWorldCoords)
//        {
//            getParticleGeometry().getWorldTranslation().set(getWorldTranslation());
//            getParticleGeometry().getWorldRotation().set(getWorldRotation());
//        } else
//        {
//            getParticleGeometry().getWorldTranslation().zero();
//            getParticleGeometry().getWorldRotation().loadIdentity();
//        }
//        getParticleGeometry().getWorldScale().set(getWorldScale());
//        getParticleGeometry().draw(r);
//    }
 
    static Sphere test = new Sphere();
    
    static
    {
        test.material = null;
        test.GenUVs();
        test.bRep.Stripify();
        test.bRep.colors = null;
    }
    
    
    void linkVerticesThis(Object3D other)
    {
        support = other;
 
        link2master = support != null;
    }
    
    void resetMasterNode(boolean smooth)
    {
    }
    
    void Reset()
    {
        initAllParticlesLocation();
        forceRespawn();
    }
    
    void Step()
    {
        if (isAllParticleInactive())
        {
            if (FastMath.nextRandomFloat() > standby)
                Reset();
        }
        
        int step = 1;
        if (CameraPane.FAST)
        {
            step *= CameraPane.STEP;
        }
        
        getParticleController().update(step);
        for (int i = 0; i < particles.length; i++)
        {
            Particle particle = particles[i];
            
            if (i > getNumParticles()) // aout 2013
                particle.setStatus(Particle.Status0.Hidden);
            
            //if (particle.getStatus() == Particle.Status0.Alive)
            {
                particle.updateVerts(null);
            }
        }
    }
    
    void DrawNode(iCameraPane display, Object3D /*Composite*/ root, boolean selected) // ??
    {
        if (display.DrawMode() == display.SELECTION || display.IsBoxMode())
            return; // hum...
        
        Object3D geo = test;
        
        if (support != null && Link2Support())
            geo = support;
        
        if (live && Globals.isLIVE() && (display.DrawMode() == display.SHADOW || !Globals.RENDERSHADOW)) // june 2013
        {
            Step();
        }
        
        getParticleGeometry().DrawParticles(display,geo,selected, random);
    }
    
    public void resetParticleVelocity(int i)
    {
        if (particleType == com.jmex.effects.particles.ParticleSystem.ParticleType.GeomMesh && useTriangleNormalEmit)
        {
            particles[i].getVelocity().set(particles[i].getTriangleModel().getNormal());
            particles[i].getVelocity().multLocal(emissionDirection);
            particles[i].getVelocity().multLocal(getInitialVelocity());
        } else
        {
            super.resetParticleVelocity(i);
        }
    }
 
    public boolean isUseMeshTexCoords()
    {
        return useMeshTexCoords;
    }
 
    public void setUseMeshTexCoords(boolean useMeshTexCoords)
    {
        this.useMeshTexCoords = useMeshTexCoords;
    }
 
    public boolean isUseTriangleNormalEmit()
    {
        return useTriangleNormalEmit;
    }
 
    public void setUseTriangleNormalEmit(boolean useTriangleNormalEmit)
    {
        this.useTriangleNormalEmit = useTriangleNormalEmit;
    }
//    
//    /**
//     * determines if a collision between this trimesh and a given spatial occurs
//     * if it has true is returned, otherwise false is returned.
//     */
//    public boolean hasCollision(Spatial scene, boolean checkTriangles) {
//        if (this == scene || !isCollidable || !scene.isCollidable()) {
//            return false;
//        }
//        if (getWorldBound().intersects(scene.getWorldBound())) {
//            if (scene instanceof Node) {
//                Node parent = (Node) scene;
//                for (int i = 0; i < parent.getQuantity(); i++) {
//                    if (hasCollision(parent.getChild(i), checkTriangles)) {
//                        return true;
//                    }
//                }
//
//                return false;
//            } 
//                
//            if (!checkTriangles) {
//                return true;
//            } 
//            
//            return hasTriangleCollision((TriMesh) scene);
//        } 
//            
//        return false;        
//    }
//
//    /**
//     * determines if this TriMesh has made contact with the give scene. The
//     * scene is recursively transversed until a trimesh is found, at which time
//     * the two trimesh OBBTrees are then compared to find the triangles that
//     * hit.
//     */
//    public void findCollisions(Spatial scene, CollisionResults results) {
//        if (this == scene || !isCollidable || !scene.isCollidable()) {
//            return;
//        }
//
//        if (getWorldBound().intersects(scene.getWorldBound())) {
//            if (scene instanceof Node) {
//                Node parent = (Node) scene;
//                for (int i = 0; i < parent.getQuantity(); i++) {
//                    findCollisions(parent.getChild(i), results);
//                }
//            } else {
//                results.addCollision(getParticleGeometry(), (Geometry) scene);
//            }
//        }
//    }
//
//    /**
//     * This function checks for intersection between this trimesh and the given
//     * one. On the first intersection, true is returned.
//     * 
//     * @param toCheck
//     *            The intersection testing mesh.
//     * @return True if they intersect.
//     */
//    public boolean hasTriangleCollision(TriMesh toCheck) {
//        TriMesh a,b;
//        for (int x = 0; x < getBatchCount(); x++) {
//            a = getBatch(x);
//            if (a == null || !a.isEnabled()) continue;
//            for (int y = 0; y < toCheck.getBatchCount(); y++) {
//                b = toCheck.getBatch(y);
//                if (b == null || !b.isEnabled()) continue;
//                if (hasTriangleCollision(toCheck, x, y))
//                    return true;
//            }
//        }
//        return false;
//    }
//
//    /**
//     * This function checks for intersection between this trimesh and the given
//     * one. On the first intersection, true is returned.
//     * 
//     * @param toCheck
//     *            The intersection testing mesh.
//     * @return True if they intersect.
//     */
//    public boolean hasTriangleCollision(TriMesh toCheck, int thisBatch, int checkBatch) {
//        CollisionTree thisCT = CollisionTreeManager.getInstance().getCollisionTree(getBatch(thisBatch));
//        CollisionTree toCheckCT = CollisionTreeManager.getInstance().getCollisionTree(toCheck.getBatch(checkBatch));
//        
//        if (thisCT == null
//                || toCheckCT == null
//                || !isCollidable || !toCheck.isCollidable())
//            return false;
//        
//        thisCT.getBounds().transform(
//                worldRotation, worldTranslation, worldScale,
//                thisCT.getWorldBounds());
//        return thisCT.intersect(toCheckCT);        
//    }
    public void write(JMEExporter e) throws IOException
    {
        super.write(e);
        OutputCapsule capsule = null; // e.getCapsule(this);
        capsule.write(useMeshTexCoords, "useMeshTexCoords", true);
        capsule.write(useTriangleNormalEmit, "useTriangleNormalEmit", true);
    }
 
    public void read(JMEImporter e) throws IOException
    {
        super.read(e);
        InputCapsule capsule = null; // e.getCapsule(this);
        useMeshTexCoords = capsule.readBoolean("useMeshTexCoords", true);
        useTriangleNormalEmit = capsule.readBoolean("useTriangleNormalEmit", true);
    }
 
//    @Override
//    public TriMesh getParticleGeometry()
//    {
//        return (TriMesh) particleGeom;
//    }
    void Invariants()
    {
        
    }
}