فهرست منبع

better random function

David Catuhe 7 سال پیش
والد
کامیت
ad9db78ffb
3فایلهای تغییر یافته به همراه14 افزوده شده و 7 حذف شده
  1. 1 0
      dist/preview release/what's new.md
  2. 11 6
      src/Particles/babylon.gpuParticleSystem.ts
  3. 2 1
      src/Shaders/gpuUpdateParticles.vertex.fx

+ 1 - 0
dist/preview release/what's new.md

@@ -2,6 +2,7 @@
 
 ## Major updates
 
+-Support for [GPU particles](DOC HERE) ([deltakosh](https://github.com/deltakosh))
 - Improved building process: We now run a full visual validation test for each pull request. Furthermore, code comments and what's new updates are now mandatory ([sebavan](https://github.com/sebavan))
 - Introduced texture binding atlas. This optimization allows the engine to reuse texture bindings instead of rebinding textures when they are not on constant sampler indexes ([deltakosh](https://github.com/deltakosh))
 - New [AnimationGroup class](http://doc.babylonjs.com/how_to/group) to control simultaneously multiple animations with different targets ([deltakosh](https://github.com/deltakosh))

+ 11 - 6
src/Particles/babylon.gpuParticleSystem.ts

@@ -160,7 +160,7 @@
 
             let updateEffectOptions: EffectCreationOptions = {
                 attributes: ["position", "age", "life", "seed", "color", "direction"],
-                uniformsNames: ["timeDelta", "generalRandom", "emitterWM", "lifeTime", "color1", "color2"],
+                uniformsNames: ["currentCount", "timeDelta", "generalRandom", "emitterWM", "lifeTime", "color1", "color2"],
                 uniformBuffersNames: [],
                 samplers:["randomSampler"],
                 defines: "",
@@ -177,13 +177,14 @@
             this._renderEffect = new Effect("gpuRenderParticles", ["position", "age", "life", "color", "offset", "uv"], ["view", "projection"], ["textureSampler"], this._scene.getEngine());
 
             // Random data
+            var maxTextureSize = this._engine.getCaps().maxTextureSize;
             var d = [];
-            for (var i = 0; i < 4096; ++i) {
+            for (var i = 0; i < maxTextureSize; ++i) {
                 d.push(Math.random());
                 d.push(Math.random());
                 d.push(Math.random());
             }
-            this._randomTexture = new RawTexture(new Float32Array(d), 4096, 1, Engine.TEXTUREFORMAT_RGB32F, this._scene, false, false, Texture.NEAREST_SAMPLINGMODE, Engine.TEXTURETYPE_FLOAT)
+            this._randomTexture = new RawTexture(new Float32Array(d), maxTextureSize, 1, Engine.TEXTUREFORMAT_RGB32F, this._scene, false, false, Texture.NEAREST_SAMPLINGMODE, Engine.TEXTURETYPE_FLOAT)
             this._randomTexture.wrapU = Texture.WRAP_ADDRESSMODE;
             this._randomTexture.wrapV = Texture.WRAP_ADDRESSMODE;
         }
@@ -264,9 +265,12 @@
             }
 
             // Sprite data
-            var spriteData = new Float32Array([1, 1,  1, 1,  -1, 1,  0, 1,
-                -1, -1,  0, 0,   1, 1,  1, 1,
-                -1, -1,  0, 0,   1, -1, 1, 0]);
+            var spriteData = new Float32Array([1, 1,  1, 1,  
+                                              -1, 1,  0, 1,
+                                             -1, -1,  0, 0,   
+                                             1, 1,  1, 1,
+                                            -1, -1,  0, 0,   
+                                            1, -1,  1, 0]);
 
             // Buffers
             this._buffer0 = new Buffer(engine, data, false, this._attributesStrideSize);
@@ -304,6 +308,7 @@
             this._engine.enableEffect(this._updateEffect);
             this._engine.setState(false);    
             
+            this._updateEffect.setFloat("currentCount", this._currentActiveCount);
             this._updateEffect.setFloat("timeDelta", this._timeDelta);
             this._updateEffect.setFloat("generalRandom", Math.random());
             this._updateEffect.setTexture("randomSampler", this._randomTexture);

+ 2 - 1
src/Shaders/gpuUpdateParticles.vertex.fx

@@ -1,5 +1,6 @@
 #version 300 es
 
+uniform float currentCount;
 uniform float timeDelta;
 uniform float generalRandom;
 uniform mat4 emitterWM;
@@ -25,7 +26,7 @@ out vec4 outColor;
 out vec3 outDirection;
 
 vec3 getRandomVec3(float offset) {
-  return texture(randomSampler, vec2(float(gl_VertexID) * offset, 0)).rgb;
+  return texture(randomSampler, vec2(float(gl_VertexID) * offset / currentCount, 0)).rgb;
 }
 
 void main() {