Pārlūkot izejas kodu

Adding effect for particles

David Catuhe 11 gadi atpakaļ
vecāks
revīzija
e9a7f1c3b7

+ 13 - 0
Babylon/Materials/babylon.effect.js

@@ -66,12 +66,20 @@
 
             if (baseName.vertexElement) {
                 vertexSource = document.getElementById(baseName.vertexElement);
+
+                if (!vertexSource) {
+                    vertexSource = baseName.vertexElement;
+                }
             } else {
                 vertexSource = baseName.vertex || baseName;
             }
 
             if (baseName.fragmentElement) {
                 fragmentSource = document.getElementById(baseName.fragmentElement);
+
+                if (!fragmentSource) {
+                    fragmentSource = baseName.fragmentElement;
+                }
             } else {
                 fragmentSource = baseName.fragment || baseName;
             }
@@ -166,6 +174,11 @@
                 return;
             }
 
+            if (BABYLON.Effect.ShadersStore[fragment + "FragmentShader"]) {
+                callback(BABYLON.Effect.ShadersStore[fragment + "FragmentShader"]);
+                return;
+            }
+
             var fragmentShaderUrl;
 
             if (fragment[0] === ".") {

+ 13 - 0
Babylon/Materials/babylon.effect.ts

@@ -74,12 +74,20 @@
 
             if (baseName.vertexElement) {
                 vertexSource = document.getElementById(baseName.vertexElement);
+
+                if (!vertexSource) {
+                    vertexSource = baseName.vertexElement;
+                }
             } else {
                 vertexSource = baseName.vertex || baseName;
             }
 
             if (baseName.fragmentElement) {
                 fragmentSource = document.getElementById(baseName.fragmentElement);
+
+                if (!fragmentSource) {
+                    fragmentSource = baseName.fragmentElement;
+                }
             } else {
                 fragmentSource = baseName.fragment || baseName;
             }
@@ -175,6 +183,11 @@
                 return;
             }
 
+            if (BABYLON.Effect.ShadersStore[fragment + "FragmentShader"]) {
+                callback(BABYLON.Effect.ShadersStore[fragment + "FragmentShader"]);
+                return;
+            }
+
             var fragmentShaderUrl;
 
             if (fragment[0] === ".") {

+ 9 - 13
Babylon/Particles/babylon.particleSystem.js

@@ -11,10 +11,9 @@
     };
 
     var ParticleSystem = (function () {
-        function ParticleSystem(name, capacity, scene, fragmentElement) {
+        function ParticleSystem(name, capacity, scene, customEffect) {
             var _this = this;
             this.name = name;
-            this.fragmentElement = fragmentElement;
             this.renderingGroupId = 0;
             this.emitter = null;
             this.emitRate = 10;
@@ -59,6 +58,8 @@
 
             this._scene = scene;
 
+            this._customEffect = customEffect;
+
             scene.particleSystems.push(this);
 
             // VBO
@@ -205,6 +206,11 @@
         };
 
         ParticleSystem.prototype._getEffect = function () {
+            if (this._customEffect) {
+                return this._customEffect;
+            }
+            ;
+
             var defines = [];
 
             if (this._scene.clipPlane) {
@@ -215,18 +221,8 @@
             var join = defines.join("\n");
             if (this._cachedDefines != join) {
                 this._cachedDefines = join;
-                var baseName;
-
-                if (this.fragmentElement) {
-                    baseName = {
-                        vertex: "particles",
-                        fragmentElement: this.fragmentElement
-                    };
-                } else {
-                    baseName = "particles";
-                }
 
-                this._effect = this._scene.getEngine().createEffect(baseName, ["position", "color", "options"], ["invView", "view", "projection", "vClipPlane", "textureMask"], ["diffuseSampler"], join);
+                this._effect = this._scene.getEngine().createEffect("particles", ["position", "color", "options"], ["invView", "view", "projection", "vClipPlane", "textureMask"], ["diffuseSampler"], join);
             }
 
             return this._effect;

+ 9 - 13
Babylon/Particles/babylon.particleSystem.ts

@@ -67,6 +67,7 @@
         private _indexBuffer: WebGLBuffer;
         private _vertices: Float32Array;
         private _effect: Effect;
+        private _customEffect: Effect;
         private _cachedDefines: string;
 
         private _scaledColorStep = new BABYLON.Color4(0, 0, 0, 0);
@@ -81,12 +82,14 @@
         private _actualFrame = 0;
         private _scaledUpdateSpeed: number;
 
-        constructor(public name: string, capacity: number, scene: Scene, public fragmentElement?: string) {
+        constructor(public name: string, capacity: number, scene: Scene, customEffect?: Effect) {
             this.id = name;
             this._capacity = capacity;
 
             this._scene = scene;
 
+            this._customEffect = customEffect;
+
             scene.particleSystems.push(this);
 
             // VBO
@@ -235,6 +238,10 @@
         }
 
         private _getEffect(): Effect {
+            if (this._customEffect) {
+                return this._customEffect;
+            };
+
             var defines = [];
 
             if (this._scene.clipPlane) {
@@ -245,20 +252,9 @@
             var join = defines.join("\n");
             if (this._cachedDefines != join) {
                 this._cachedDefines = join;
-                var baseName;
-
-                if (this.fragmentElement) {
-                    baseName = {
-                        vertex: "particles",
-                        fragmentElement: this.fragmentElement
-                    }
-                } else {
-                    baseName = "particles";
-                }
-
 
                 this._effect = this._scene.getEngine().createEffect(
-                    baseName,
+                    "particles",
                     ["position", "color", "options"],
                     ["invView", "view", "projection", "vClipPlane", "textureMask"],
                     ["diffuseSampler"], join);

+ 6 - 2
Babylon/Physics/Plugins/babylon.oimoJSPlugin.js

@@ -1,4 +1,4 @@
-var BABYLON;
+var BABYLON;
 (function (BABYLON) {
     var OimoJSPlugin = (function () {
         function OimoJSPlugin() {
@@ -216,7 +216,11 @@ var BABYLON;
             for (var index = 0; index < this._registeredMeshes.length; index++) {
                 var registeredMesh = this._registeredMeshes[index];
                 if (registeredMesh.mesh === mesh || registeredMesh.mesh === mesh.parent) {
-                    registeredMesh.body.body.applyImpulse(contactPoint.scale(OIMO.INV_SCALE), force.scale(OIMO.INV_SCALE));
+                    // Get object mass to have a behaviour similar to cannon.js
+                    var mass = registeredMesh.body.body.massInfo.mass;
+
+                    // The force is scaled with the mass of object
+                    registeredMesh.body.body.applyImpulse(contactPoint.scale(OIMO.INV_SCALE), force.scale(OIMO.INV_SCALE * mass));
                     return;
                 }
             }

+ 12 - 12
Babylon/Physics/Plugins/babylon.oimoJSPlugin.ts

@@ -8,7 +8,7 @@ module BABYLON {
         private _checkWithEpsilon(value: number): number {
             return value < BABYLON.PhysicsEngine.Epsilon ? BABYLON.PhysicsEngine.Epsilon : value;
         }
-        
+
         public initialize(iterations?: number): void {
             this._world = new OIMO.World();
             this._world.clear();
@@ -34,7 +34,7 @@ module BABYLON {
                     var size = Math.max(
                         this._checkWithEpsilon(radiusX),
                         this._checkWithEpsilon(radiusY),
-                        this._checkWithEpsilon(radiusZ))/2;
+                        this._checkWithEpsilon(radiusZ)) / 2;
 
                     // The delta between the mesh position and the mesh bounding box center
                     var deltaPosition = mesh.position.subtract(bbox.center);
@@ -49,9 +49,9 @@ module BABYLON {
                         world: this._world
                     });
                     this._registeredMeshes.push({
-                        mesh:       mesh,
-                        body:       body,
-                        delta:  deltaPosition
+                        mesh: mesh,
+                        body: body,
+                        delta: deltaPosition
                     });
                     break;
 
@@ -79,9 +79,9 @@ module BABYLON {
                     });
 
                     this._registeredMeshes.push({
-                        mesh:   mesh,
-                        body:   body,
-                        delta:  deltaPosition
+                        mesh: mesh,
+                        body: body,
+                        delta: deltaPosition
                     });
                     break;
 
@@ -138,7 +138,7 @@ module BABYLON {
                     var size = Math.max(
                         this._checkWithEpsilon(radiusX),
                         this._checkWithEpsilon(radiusY),
-                        this._checkWithEpsilon(radiusZ))/2;
+                        this._checkWithEpsilon(radiusZ)) / 2;
                     bodyParameters = {
                         type: 'sphere',
                         /* bug with oimo : sphere needs 3 sizes in this case */
@@ -232,13 +232,13 @@ module BABYLON {
                     // Get object mass to have a behaviour similar to cannon.js
                     var mass = registeredMesh.body.body.massInfo.mass;
                     // The force is scaled with the mass of object
-                    registeredMesh.body.body.applyImpulse(contactPoint.scale(OIMO.INV_SCALE), force.scale(OIMO.INV_SCALE*mass));
+                    registeredMesh.body.body.applyImpulse(contactPoint.scale(OIMO.INV_SCALE), force.scale(OIMO.INV_SCALE * mass));
                     return;
                 }
             }
         }
 
-        public createLink (mesh1: AbstractMesh, mesh2: AbstractMesh, pivot1: Vector3, pivot2: Vector3, options?: any): boolean {
+        public createLink(mesh1: AbstractMesh, mesh2: AbstractMesh, pivot1: Vector3, pivot2: Vector3, options?: any): boolean {
             var body1 = null,
                 body2 = null;
             for (var index = 0; index < this._registeredMeshes.length; index++) {
@@ -304,7 +304,7 @@ module BABYLON {
 
                 var body = this._registeredMeshes[i].body.body;
                 var mesh = this._registeredMeshes[i].mesh;
-                var delta  = this._registeredMeshes[i].delta;
+                var delta = this._registeredMeshes[i].delta;
 
                 if (!body.sleeping) {
 

+ 10 - 0
Babylon/babylon.engine.js

@@ -935,6 +935,16 @@
             return effect;
         };
 
+        Engine.prototype.createEffectForParticles = function (fragmentName, uniformsNames, samplers, defines, fallbacks, onCompiled, onError) {
+            if (typeof uniformsNames === "undefined") { uniformsNames = []; }
+            if (typeof samplers === "undefined") { samplers = []; }
+            if (typeof defines === "undefined") { defines = ""; }
+            return this.createEffect({
+                vertex: "particles",
+                fragmentElement: fragmentName
+            }, ["position", "color", "options"], ["view", "projection"].concat(uniformsNames), ["diffuseSampler"].concat(samplers), defines, fallbacks, onCompiled, onError);
+        };
+
         Engine.prototype.createShaderProgram = function (vertexCode, fragmentCode, defines) {
             var vertexShader = compileShader(this._gl, vertexCode, "vertex", defines);
             var fragmentShader = compileShader(this._gl, fragmentCode, "fragment", defines);

+ 13 - 0
Babylon/babylon.engine.ts

@@ -943,6 +943,19 @@
             return effect;
         }
 
+        public createEffectForParticles(fragmentName: string, uniformsNames: string[]= [], samplers: string[] = [], defines = "", fallbacks?: EffectFallbacks,
+            onCompiled?: (effect: Effect) => void, onError?: (effect: Effect, errors: string) => void): Effect {
+
+            return this.createEffect(
+                {
+                    vertex: "particles",
+                    fragmentElement: fragmentName
+                },
+                ["position", "color", "options"],
+                ["view", "projection"].concat(uniformsNames),
+                ["diffuseSampler"].concat(samplers), defines, fallbacks, onCompiled, onError);
+        }
+
         public createShaderProgram(vertexCode: string, fragmentCode: string, defines: string): WebGLProgram {
             var vertexShader = compileShader(this._gl, vertexCode, "vertex", defines);
             var fragmentShader = compileShader(this._gl, fragmentCode, "fragment", defines);

+ 32 - 13
babylon.1.14-beta-debug.js

@@ -3748,6 +3748,16 @@ var BABYLON;
             return effect;
         };
 
+        Engine.prototype.createEffectForParticles = function (fragmentName, uniformsNames, samplers, defines, fallbacks, onCompiled, onError) {
+            if (typeof uniformsNames === "undefined") { uniformsNames = []; }
+            if (typeof samplers === "undefined") { samplers = []; }
+            if (typeof defines === "undefined") { defines = ""; }
+            return this.createEffect({
+                vertex: "particles",
+                fragmentElement: fragmentName
+            }, ["position", "color", "options"], ["view", "projection"].concat(uniformsNames), ["diffuseSampler"].concat(samplers), defines, fallbacks, onCompiled, onError);
+        };
+
         Engine.prototype.createShaderProgram = function (vertexCode, fragmentCode, defines) {
             var vertexShader = compileShader(this._gl, vertexCode, "vertex", defines);
             var fragmentShader = compileShader(this._gl, fragmentCode, "fragment", defines);
@@ -12305,12 +12315,20 @@ var BABYLON;
 
             if (baseName.vertexElement) {
                 vertexSource = document.getElementById(baseName.vertexElement);
+
+                if (!vertexSource) {
+                    vertexSource = baseName.vertexElement;
+                }
             } else {
                 vertexSource = baseName.vertex || baseName;
             }
 
             if (baseName.fragmentElement) {
                 fragmentSource = document.getElementById(baseName.fragmentElement);
+
+                if (!fragmentSource) {
+                    fragmentSource = baseName.fragmentElement;
+                }
             } else {
                 fragmentSource = baseName.fragment || baseName;
             }
@@ -12405,6 +12423,11 @@ var BABYLON;
                 return;
             }
 
+            if (BABYLON.Effect.ShadersStore[fragment + "FragmentShader"]) {
+                callback(BABYLON.Effect.ShadersStore[fragment + "FragmentShader"]);
+                return;
+            }
+
             var fragmentShaderUrl;
 
             if (fragment[0] === ".") {
@@ -14340,10 +14363,9 @@ var BABYLON;
     };
 
     var ParticleSystem = (function () {
-        function ParticleSystem(name, capacity, scene, fragmentElement) {
+        function ParticleSystem(name, capacity, scene, customEffect) {
             var _this = this;
             this.name = name;
-            this.fragmentElement = fragmentElement;
             this.renderingGroupId = 0;
             this.emitter = null;
             this.emitRate = 10;
@@ -14388,6 +14410,8 @@ var BABYLON;
 
             this._scene = scene;
 
+            this._customEffect = customEffect;
+
             scene.particleSystems.push(this);
 
            
@@ -14534,6 +14558,11 @@ var BABYLON;
         };
 
         ParticleSystem.prototype._getEffect = function () {
+            if (this._customEffect) {
+                return this._customEffect;
+            }
+            ;
+
             var defines = [];
 
             if (this._scene.clipPlane) {
@@ -14544,18 +14573,8 @@ var BABYLON;
             var join = defines.join("\n");
             if (this._cachedDefines != join) {
                 this._cachedDefines = join;
-                var baseName;
-
-                if (this.fragmentElement) {
-                    baseName = {
-                        vertex: "particles",
-                        fragmentElement: this.fragmentElement
-                    };
-                } else {
-                    baseName = "particles";
-                }
 
-                this._effect = this._scene.getEngine().createEffect(baseName, ["position", "color", "options"], ["invView", "view", "projection", "vClipPlane", "textureMask"], ["diffuseSampler"], join);
+                this._effect = this._scene.getEngine().createEffect("particles", ["position", "color", "options"], ["invView", "view", "projection", "vClipPlane", "textureMask"], ["diffuseSampler"], join);
             }
 
             return this._effect;

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 14 - 14
babylon.1.14-beta.js


+ 6 - 2
babylon.d.ts

@@ -144,6 +144,7 @@ declare module BABYLON {
         public draw(useTriangles: boolean, indexStart: number, indexCount: number, instancesCount?: number): void;
         public _releaseEffect(effect: Effect): void;
         public createEffect(baseName: any, attributesNames: string[], uniformsNames: string[], samplers: string[], defines: string, fallbacks?: EffectFallbacks, onCompiled?: (effect: Effect) => void, onError?: (effect: Effect, errors: string) => void): Effect;
+        public createEffectForParticles(fragmentName: string, uniformsNames?: string[], samplers?: string[], defines?: string, fallbacks?: EffectFallbacks, onCompiled?: (effect: Effect) => void, onError?: (effect: Effect, errors: string) => void): Effect;
         public createShaderProgram(vertexCode: string, fragmentCode: string, defines: string): WebGLProgram;
         public getUniforms(shaderProgram: WebGLProgram, uniformsNames: string[]): WebGLUniformLocation[];
         public getAttributes(shaderProgram: WebGLProgram, attributesNames: string[]): number[];
@@ -1942,6 +1943,7 @@ declare module BABYLON {
         public equals(otherQuaternion: Quaternion): boolean;
         public clone(): Quaternion;
         public copyFrom(other: Quaternion): void;
+        public copyFromFloats(x: number, y: number, z: number, w: number): void;
         public add(other: Quaternion): Quaternion;
         public subtract(other: Quaternion): Quaternion;
         public scale(value: number): Quaternion;
@@ -1950,6 +1952,7 @@ declare module BABYLON {
         public length(): number;
         public normalize(): void;
         public toEulerAngles(): Vector3;
+        public toEulerAnglesToRef(result: Vector3): void;
         public toRotationMatrix(result: Matrix): void;
         public fromRotationMatrix(matrix: Matrix): void;
         static Inverse(q: Quaternion): Quaternion;
@@ -2410,6 +2413,7 @@ declare module BABYLON {
         public delayLoadState: number;
         public instances: InstancedMesh[];
         public delayLoadingFile: string;
+        public _binaryInfo: any;
         public _geometry: Geometry;
         private _onBeforeRenderCallbacks;
         private _onAfterRenderCallbacks;
@@ -2626,7 +2630,6 @@ declare module BABYLON {
 declare module BABYLON {
     class ParticleSystem implements IDisposable {
         public name: string;
-        public fragmentElement: string;
         static BLENDMODE_ONEONE: number;
         static BLENDMODE_STANDARD: number;
         public id: string;
@@ -2671,6 +2674,7 @@ declare module BABYLON {
         private _indexBuffer;
         private _vertices;
         private _effect;
+        private _customEffect;
         private _cachedDefines;
         private _scaledColorStep;
         private _colorDiff;
@@ -2682,7 +2686,7 @@ declare module BABYLON {
         private _stopped;
         private _actualFrame;
         private _scaledUpdateSpeed;
-        constructor(name: string, capacity: number, scene: Scene, fragmentElement?: string);
+        constructor(name: string, capacity: number, scene: Scene, customEffect?: Effect);
         public getCapacity(): number;
         public isAlive(): boolean;
         public isStarted(): boolean;