Parcourir la source

"AAAAAAAAAAAAAAAAhhhhhaaaa" step 8

David Catuhe il y a 7 ans
Parent
commit
c319ca1cbf
65 fichiers modifiés avec 25139 ajouts et 24174 suppressions
  1. 1863 1822
      dist/preview release/babylon.d.ts
  2. 40 40
      dist/preview release/babylon.js
  3. 519 245
      dist/preview release/babylon.max.js
  4. 1863 1822
      dist/preview release/babylon.module.d.ts
  5. 41 41
      dist/preview release/babylon.worker.js
  6. 9747 9710
      dist/preview release/customConfigurations/minimalGLTFViewer/babylon.d.ts
  7. 38 38
      dist/preview release/customConfigurations/minimalGLTFViewer/babylon.js
  8. 368 169
      dist/preview release/customConfigurations/minimalGLTFViewer/babylon.max.js
  9. 9747 9710
      dist/preview release/customConfigurations/minimalGLTFViewer/babylon.module.d.ts
  10. 32 32
      dist/preview release/gui/babylon.gui.d.ts
  11. 32 32
      dist/preview release/gui/babylon.gui.module.d.ts
  12. 8 2
      src/Audio/babylon.sound.ts
  13. 1 1
      src/Behaviors/Cameras/babylon.bouncingBehavior.ts
  14. 1 1
      src/Behaviors/Cameras/babylon.framingBehavior.ts
  15. 16 10
      src/Collisions/babylon.collisionCoordinator.ts
  16. 4 2
      src/Culling/Octrees/babylon.octree.ts
  17. 1 1
      src/Debug/babylon.skeletonViewer.ts
  18. 31 18
      src/Engine/babylon.engine.ts
  19. 3 1
      src/Gamepad/Controllers/babylon.poseEnabledController.ts
  20. 9 0
      src/Gamepad/Controllers/babylon.windowsMotionController.ts
  21. 15 7
      src/Layer/babylon.highlightlayer.ts
  22. 1 1
      src/LensFlare/babylon.lensFlareSystem.ts
  23. 6 2
      src/Lights/Shadows/babylon.shadowGenerator.ts
  24. 16 12
      src/Materials/PBR/babylon.pbrBaseMaterial.ts
  25. 3 2
      src/Materials/Textures/Procedurals/babylon.customProceduralTexture.ts
  26. 49 24
      src/Materials/Textures/Procedurals/babylon.proceduralTexture.ts
  27. 23 16
      src/Materials/Textures/babylon.colorGradingTexture.ts
  28. 15 4
      src/Materials/Textures/babylon.cubeTexture.ts
  29. 15 8
      src/Materials/Textures/babylon.dynamicTexture.ts
  30. 1 1
      src/Materials/Textures/babylon.internalTexture.ts
  31. 13 5
      src/Materials/Textures/babylon.mirrorTexture.ts
  32. 7 5
      src/Materials/Textures/babylon.multiRenderTarget.ts
  33. 3 2
      src/Materials/Textures/babylon.rawTexture.ts
  34. 10 2
      src/Materials/Textures/babylon.refractionTexture.ts
  35. 10 5
      src/Materials/Textures/babylon.videoTexture.ts
  36. 2 1
      src/Materials/babylon.imageProcessingConfiguration.ts
  37. 1 1
      src/Materials/babylon.material.ts
  38. 7 6
      src/Materials/babylon.materialHelper.ts
  39. 1 1
      src/Materials/babylon.pushMaterial.ts
  40. 4 1
      src/Materials/babylon.standardMaterial.ts
  41. 1 1
      src/Materials/babylon.uniformBuffer.ts
  42. 11 13
      src/Mesh/babylon.abstractMesh.ts
  43. 4 4
      src/Mesh/babylon.csg.ts
  44. 27 3
      src/Mesh/babylon.geometry.ts
  45. 5 0
      src/Mesh/babylon.groundMesh.ts
  46. 1 1
      src/Mesh/babylon.instancedMesh.ts
  47. 2 2
      src/Mesh/babylon.linesMesh.ts
  48. 175 68
      src/Mesh/babylon.mesh.ts
  49. 84 70
      src/Mesh/babylon.mesh.vertexData.ts
  50. 116 100
      src/Mesh/babylon.meshBuilder.ts
  51. 13 1
      src/Mesh/babylon.meshSimplification.ts
  52. 1 1
      src/Mesh/babylon.polygonMesh.ts
  53. 5 5
      src/Mesh/babylon.subMesh.ts
  54. 3 2
      src/Mesh/babylon.vertexBuffer.ts
  55. 3 3
      src/Morph/babylon.morphTarget.ts
  56. 8 6
      src/Morph/babylon.morphTargetManager.ts
  57. 2 2
      src/Particles/babylon.gpuParticleSystem.ts
  58. 21 17
      src/Particles/babylon.particleSystem.ts
  59. 27 26
      src/Particles/babylon.solidParticle.ts
  60. 46 39
      src/Particles/babylon.solidParticleSystem.ts
  61. 5 1
      src/Tools/babylon.decorators.ts
  62. 2 2
      src/babylon.node.ts
  63. 4 2
      src/babylon.scene.ts
  64. 2 1
      src/babylon.types.ts
  65. 5 1
      tslint.json

Fichier diff supprimé car celui-ci est trop grand
+ 1863 - 1822
dist/preview release/babylon.d.ts


Fichier diff supprimé car celui-ci est trop grand
+ 40 - 40
dist/preview release/babylon.js


Fichier diff supprimé car celui-ci est trop grand
+ 519 - 245
dist/preview release/babylon.max.js


Fichier diff supprimé car celui-ci est trop grand
+ 1863 - 1822
dist/preview release/babylon.module.d.ts


Fichier diff supprimé car celui-ci est trop grand
+ 41 - 41
dist/preview release/babylon.worker.js


Fichier diff supprimé car celui-ci est trop grand
+ 9747 - 9710
dist/preview release/customConfigurations/minimalGLTFViewer/babylon.d.ts


Fichier diff supprimé car celui-ci est trop grand
+ 38 - 38
dist/preview release/customConfigurations/minimalGLTFViewer/babylon.js


Fichier diff supprimé car celui-ci est trop grand
+ 368 - 169
dist/preview release/customConfigurations/minimalGLTFViewer/babylon.max.js


Fichier diff supprimé car celui-ci est trop grand
+ 9747 - 9710
dist/preview release/customConfigurations/minimalGLTFViewer/babylon.module.d.ts


+ 32 - 32
dist/preview release/gui/babylon.gui.d.ts

@@ -36,7 +36,7 @@ declare module BABYLON.GUI {
         readonly layer: Layer;
         readonly rootContainer: Container;
         focusedControl: IFocusableControl;
-        constructor(name: string, width: number, height: number, scene: Scene, generateMipMaps?: boolean, samplingMode?: number);
+        constructor(name: string, width: number | undefined, height: number | undefined, scene: Scene, generateMipMaps?: boolean, samplingMode?: number);
         executeOnAllControls(func: (control: Control) => void, container?: Container): void;
         markAsDirty(): void;
         addControl(control: Control): AdvancedDynamicTexture;
@@ -126,7 +126,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Control {
-        name: string;
+        name: string | undefined;
         private _alpha;
         private _alphaSet;
         private _zIndex;
@@ -254,7 +254,7 @@ declare module BABYLON.GUI {
         readonly linkOffsetYInPixels: number;
         readonly centerX: number;
         readonly centerY: number;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         getLocalCoordinates(globalCoordinates: Vector2): Vector2;
         getLocalCoordinatesToRef(globalCoordinates: Vector2, result: Vector2): Control;
@@ -315,13 +315,13 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Container extends Control {
-        name: string;
+        name: string | undefined;
         protected _children: Control[];
         protected _measureForChildren: Measure;
         protected _background: string;
         background: string;
         readonly children: Control[];
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         getChildByName(name: string): Control;
         getChildByType(name: string, type: string): Control;
@@ -344,7 +344,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class StackPanel extends Container {
-        name: string;
+        name: string | undefined;
         private _isVertical;
         private _manualWidth;
         private _manualHeight;
@@ -353,7 +353,7 @@ declare module BABYLON.GUI {
         isVertical: boolean;
         width: string | number;
         height: string | number;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         protected _preMeasure(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
     }
@@ -362,12 +362,12 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Rectangle extends Container {
-        name: string;
+        name: string | undefined;
         private _thickness;
         private _cornerRadius;
         thickness: number;
         cornerRadius: number;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         protected _localDraw(context: CanvasRenderingContext2D): void;
         protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
@@ -379,10 +379,10 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Ellipse extends Container {
-        name: string;
+        name: string | undefined;
         private _thickness;
         thickness: number;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         protected _localDraw(context: CanvasRenderingContext2D): void;
         protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
@@ -393,7 +393,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Line extends Control {
-        name: string;
+        name: string | undefined;
         private _lineWidth;
         private _x1;
         private _y1;
@@ -413,7 +413,7 @@ declare module BABYLON.GUI {
         verticalAlignment: number;
         private readonly _effectiveX2;
         private readonly _effectiveY2;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
         _measure(): void;
@@ -425,7 +425,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Slider extends Control {
-        name: string;
+        name: string | undefined;
         private _thumbWidth;
         private _minimum;
         private _maximum;
@@ -443,7 +443,7 @@ declare module BABYLON.GUI {
         minimum: number;
         maximum: number;
         value: number;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
         private _pointerIsDown;
@@ -457,7 +457,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Checkbox extends Control {
-        name: string;
+        name: string | undefined;
         private _isChecked;
         private _background;
         private _checkSizeRatio;
@@ -467,7 +467,7 @@ declare module BABYLON.GUI {
         checkSizeRatio: number;
         background: string;
         isChecked: boolean;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
         _onPointerDown(target: Control, coordinates: Vector2, buttonIndex: number): boolean;
@@ -477,7 +477,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class RadioButton extends Control {
-        name: string;
+        name: string | undefined;
         private _isChecked;
         private _background;
         private _checkSizeRatio;
@@ -488,7 +488,7 @@ declare module BABYLON.GUI {
         checkSizeRatio: number;
         background: string;
         isChecked: boolean;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
         _onPointerDown(target: Control, coordinates: Vector2, buttonIndex: number): boolean;
@@ -498,7 +498,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class TextBlock extends Control {
-        name: string;
+        name: string | undefined;
         private _text;
         private _textWrapping;
         private _textHorizontalAlignment;
@@ -515,23 +515,23 @@ declare module BABYLON.GUI {
         text: string;
         textHorizontalAlignment: number;
         textVerticalAlignment: number;
-        constructor(name?: string, text?: string);
+        constructor(name?: string | undefined, text?: string);
         protected _getTypeName(): string;
         private _drawText(text, textWidth, y, context);
         _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
         protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
-        protected _parseLine(line: string, context: CanvasRenderingContext2D): object;
-        protected _parseLineWithTextWrapping(line: string, context: CanvasRenderingContext2D): object;
+        protected _parseLine(line: string | undefined, context: CanvasRenderingContext2D): object;
+        protected _parseLineWithTextWrapping(line: string | undefined, context: CanvasRenderingContext2D): object;
         protected _renderLines(context: CanvasRenderingContext2D): void;
         dispose(): void;
     }
 }
 
 
-declare var DOMImage: new (width?: number, height?: number) => HTMLImageElement;
+declare var DOMImage: new (width?: number | undefined, height?: number | undefined) => HTMLImageElement;
 declare module BABYLON.GUI {
     class Image extends Control {
-        name: string;
+        name: string | undefined;
         private _domImage;
         private _imageWidth;
         private _imageHeight;
@@ -552,7 +552,7 @@ declare module BABYLON.GUI {
         domImage: HTMLImageElement;
         private _onImageLoaded();
         source: string;
-        constructor(name?: string, url?: string);
+        constructor(name?: string | undefined, url?: string);
         protected _getTypeName(): string;
         synchronizeSizeWithContent(): void;
         _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
@@ -570,12 +570,12 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Button extends Rectangle {
-        name: string;
+        name: string | undefined;
         pointerEnterAnimation: () => void;
         pointerOutAnimation: () => void;
         pointerDownAnimation: () => void;
         pointerUpAnimation: () => void;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         _processPicking(x: number, y: number, type: number, buttonIndex: number): boolean;
         _onPointerEnter(target: Control): boolean;
@@ -592,7 +592,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class ColorPicker extends Control {
-        name: string;
+        name: string | undefined;
         private _colorWheelCanvas;
         private _value;
         private _tmpColor;
@@ -609,7 +609,7 @@ declare module BABYLON.GUI {
         width: string | number;
         height: string | number;
         size: string | number;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         private _updateSquareProps();
         private _drawGradientSquare(hueValue, left, top, width, height, context);
@@ -631,7 +631,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class InputText extends Control implements IFocusableControl {
-        name: string;
+        name: string | undefined;
         private _text;
         private _placeholderText;
         private _background;
@@ -663,7 +663,7 @@ declare module BABYLON.GUI {
         placeholderColor: string;
         placeholderText: string;
         text: string;
-        constructor(name?: string, text?: string);
+        constructor(name?: string | undefined, text?: string);
         onBlur(): void;
         onFocus(): void;
         protected _getTypeName(): string;

+ 32 - 32
dist/preview release/gui/babylon.gui.module.d.ts

@@ -41,7 +41,7 @@ declare module BABYLON.GUI {
         readonly layer: Layer;
         readonly rootContainer: Container;
         focusedControl: IFocusableControl;
-        constructor(name: string, width: number, height: number, scene: Scene, generateMipMaps?: boolean, samplingMode?: number);
+        constructor(name: string, width: number | undefined, height: number | undefined, scene: Scene, generateMipMaps?: boolean, samplingMode?: number);
         executeOnAllControls(func: (control: Control) => void, container?: Container): void;
         markAsDirty(): void;
         addControl(control: Control): AdvancedDynamicTexture;
@@ -131,7 +131,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Control {
-        name: string;
+        name: string | undefined;
         private _alpha;
         private _alphaSet;
         private _zIndex;
@@ -259,7 +259,7 @@ declare module BABYLON.GUI {
         readonly linkOffsetYInPixels: number;
         readonly centerX: number;
         readonly centerY: number;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         getLocalCoordinates(globalCoordinates: Vector2): Vector2;
         getLocalCoordinatesToRef(globalCoordinates: Vector2, result: Vector2): Control;
@@ -320,13 +320,13 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Container extends Control {
-        name: string;
+        name: string | undefined;
         protected _children: Control[];
         protected _measureForChildren: Measure;
         protected _background: string;
         background: string;
         readonly children: Control[];
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         getChildByName(name: string): Control;
         getChildByType(name: string, type: string): Control;
@@ -349,7 +349,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class StackPanel extends Container {
-        name: string;
+        name: string | undefined;
         private _isVertical;
         private _manualWidth;
         private _manualHeight;
@@ -358,7 +358,7 @@ declare module BABYLON.GUI {
         isVertical: boolean;
         width: string | number;
         height: string | number;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         protected _preMeasure(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
     }
@@ -367,12 +367,12 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Rectangle extends Container {
-        name: string;
+        name: string | undefined;
         private _thickness;
         private _cornerRadius;
         thickness: number;
         cornerRadius: number;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         protected _localDraw(context: CanvasRenderingContext2D): void;
         protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
@@ -384,10 +384,10 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Ellipse extends Container {
-        name: string;
+        name: string | undefined;
         private _thickness;
         thickness: number;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         protected _localDraw(context: CanvasRenderingContext2D): void;
         protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
@@ -398,7 +398,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Line extends Control {
-        name: string;
+        name: string | undefined;
         private _lineWidth;
         private _x1;
         private _y1;
@@ -418,7 +418,7 @@ declare module BABYLON.GUI {
         verticalAlignment: number;
         private readonly _effectiveX2;
         private readonly _effectiveY2;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
         _measure(): void;
@@ -430,7 +430,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Slider extends Control {
-        name: string;
+        name: string | undefined;
         private _thumbWidth;
         private _minimum;
         private _maximum;
@@ -448,7 +448,7 @@ declare module BABYLON.GUI {
         minimum: number;
         maximum: number;
         value: number;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
         private _pointerIsDown;
@@ -462,7 +462,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Checkbox extends Control {
-        name: string;
+        name: string | undefined;
         private _isChecked;
         private _background;
         private _checkSizeRatio;
@@ -472,7 +472,7 @@ declare module BABYLON.GUI {
         checkSizeRatio: number;
         background: string;
         isChecked: boolean;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
         _onPointerDown(target: Control, coordinates: Vector2, buttonIndex: number): boolean;
@@ -482,7 +482,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class RadioButton extends Control {
-        name: string;
+        name: string | undefined;
         private _isChecked;
         private _background;
         private _checkSizeRatio;
@@ -493,7 +493,7 @@ declare module BABYLON.GUI {
         checkSizeRatio: number;
         background: string;
         isChecked: boolean;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
         _onPointerDown(target: Control, coordinates: Vector2, buttonIndex: number): boolean;
@@ -503,7 +503,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class TextBlock extends Control {
-        name: string;
+        name: string | undefined;
         private _text;
         private _textWrapping;
         private _textHorizontalAlignment;
@@ -520,23 +520,23 @@ declare module BABYLON.GUI {
         text: string;
         textHorizontalAlignment: number;
         textVerticalAlignment: number;
-        constructor(name?: string, text?: string);
+        constructor(name?: string | undefined, text?: string);
         protected _getTypeName(): string;
         private _drawText(text, textWidth, y, context);
         _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
         protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
-        protected _parseLine(line: string, context: CanvasRenderingContext2D): object;
-        protected _parseLineWithTextWrapping(line: string, context: CanvasRenderingContext2D): object;
+        protected _parseLine(line: string | undefined, context: CanvasRenderingContext2D): object;
+        protected _parseLineWithTextWrapping(line: string | undefined, context: CanvasRenderingContext2D): object;
         protected _renderLines(context: CanvasRenderingContext2D): void;
         dispose(): void;
     }
 }
 
 
-declare var DOMImage: new (width?: number, height?: number) => HTMLImageElement;
+declare var DOMImage: new (width?: number | undefined, height?: number | undefined) => HTMLImageElement;
 declare module BABYLON.GUI {
     class Image extends Control {
-        name: string;
+        name: string | undefined;
         private _domImage;
         private _imageWidth;
         private _imageHeight;
@@ -557,7 +557,7 @@ declare module BABYLON.GUI {
         domImage: HTMLImageElement;
         private _onImageLoaded();
         source: string;
-        constructor(name?: string, url?: string);
+        constructor(name?: string | undefined, url?: string);
         protected _getTypeName(): string;
         synchronizeSizeWithContent(): void;
         _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void;
@@ -575,12 +575,12 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class Button extends Rectangle {
-        name: string;
+        name: string | undefined;
         pointerEnterAnimation: () => void;
         pointerOutAnimation: () => void;
         pointerDownAnimation: () => void;
         pointerUpAnimation: () => void;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         _processPicking(x: number, y: number, type: number, buttonIndex: number): boolean;
         _onPointerEnter(target: Control): boolean;
@@ -597,7 +597,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class ColorPicker extends Control {
-        name: string;
+        name: string | undefined;
         private _colorWheelCanvas;
         private _value;
         private _tmpColor;
@@ -614,7 +614,7 @@ declare module BABYLON.GUI {
         width: string | number;
         height: string | number;
         size: string | number;
-        constructor(name?: string);
+        constructor(name?: string | undefined);
         protected _getTypeName(): string;
         private _updateSquareProps();
         private _drawGradientSquare(hueValue, left, top, width, height, context);
@@ -636,7 +636,7 @@ declare module BABYLON.GUI {
 
 declare module BABYLON.GUI {
     class InputText extends Control implements IFocusableControl {
-        name: string;
+        name: string | undefined;
         private _text;
         private _placeholderText;
         private _background;
@@ -668,7 +668,7 @@ declare module BABYLON.GUI {
         placeholderColor: string;
         placeholderText: string;
         text: string;
-        constructor(name?: string, text?: string);
+        constructor(name?: string | undefined, text?: string);
         onBlur(): void;
         onFocus(): void;
         protected _getTypeName(): string;

+ 8 - 2
src/Audio/babylon.sound.ts

@@ -561,8 +561,14 @@ module BABYLON {
             }
         }
 
-        private _onRegisterAfterWorldMatrixUpdate(connectedMesh: AbstractMesh) {
-            this.setPosition(connectedMesh.getBoundingInfo().boundingSphere.centerWorld);
+        private _onRegisterAfterWorldMatrixUpdate(connectedMesh: AbstractMesh): void {
+            let boundingInfo = connectedMesh.getBoundingInfo();
+
+            if (!boundingInfo) {
+                return;
+            }
+
+            this.setPosition(boundingInfo.boundingSphere.centerWorld);
             if (Engine.audioEngine.canUseWebAudio && this._isDirectional && this.isPlaying) {
                 this._updateDirection();
             }

+ 1 - 1
src/Behaviors/Cameras/babylon.bouncingBehavior.ts

@@ -64,7 +64,7 @@ module BABYLON {
 					}
 
 					mesh.computeWorldMatrix(true);
-					let diagonal = mesh.getBoundingInfo().diagonalLength;
+					let diagonal = (<BoundingInfo>mesh.getBoundingInfo()).diagonalLength;
 
 					this.lowerRadiusTransitionRange = diagonal * 0.05;
 					this.upperRadiusTransitionRange = diagonal * 0.05;

+ 1 - 1
src/Behaviors/Cameras/babylon.framingBehavior.ts

@@ -220,7 +220,7 @@ module BABYLON {
 		public zoomOnMesh(mesh: AbstractMesh, focusOnOriginXZ: boolean = false, onAnimationEnd: Nullable<() => void> = null): void {
 			mesh.computeWorldMatrix(true);
 
-			let boundingBox = mesh.getBoundingInfo().boundingBox;
+			let boundingBox = (<BoundingInfo>mesh.getBoundingInfo()).boundingBox;
 			this.zoomOnBoundingInfo(boundingBox.minimumWorld, boundingBox.maximumWorld, focusOnOriginXZ, onAnimationEnd);
 		}
 

+ 16 - 10
src/Collisions/babylon.collisionCoordinator.ts

@@ -139,6 +139,8 @@ module BABYLON {
             var submeshes: Array<SerializedSubMesh> = [];
             if (mesh.subMeshes) {
                 submeshes = mesh.subMeshes.map(function (sm, idx) {
+                    let boundingInfo = <BoundingInfo>sm.getBoundingInfo();
+
                     return {
                         position: idx,
                         verticesStart: sm.verticesStart,
@@ -146,30 +148,34 @@ module BABYLON {
                         indexStart: sm.indexStart,
                         indexCount: sm.indexCount,
                         hasMaterial: !!sm.getMaterial(),
-                        sphereCenter: sm.getBoundingInfo().boundingSphere.centerWorld.asArray(),
-                        sphereRadius: sm.getBoundingInfo().boundingSphere.radiusWorld,
-                        boxMinimum: sm.getBoundingInfo().boundingBox.minimumWorld.asArray(),
-                        boxMaximum: sm.getBoundingInfo().boundingBox.maximumWorld.asArray()
+                        sphereCenter: boundingInfo.boundingSphere.centerWorld.asArray(),
+                        sphereRadius: boundingInfo.boundingSphere.radiusWorld,
+                        boxMinimum: boundingInfo.boundingBox.minimumWorld.asArray(),
+                        boxMaximum: boundingInfo.boundingBox.maximumWorld.asArray()
                     }
                 });
             }
 
             var geometryId: Nullable<string> = null;
             if (mesh instanceof Mesh) {
-                geometryId = (<Mesh>mesh).geometry ? (<Mesh>mesh).geometry.id : null;
+                let geometry = (<Mesh>mesh).geometry;
+                geometryId = geometry ? geometry.id : null;
             } else if (mesh instanceof InstancedMesh) {
-                geometryId = ((<InstancedMesh>mesh).sourceMesh && (<InstancedMesh>mesh).sourceMesh.geometry) ? (<InstancedMesh>mesh).sourceMesh.geometry.id : null;
+                let geometry = (<InstancedMesh>mesh).sourceMesh.geometry;
+                geometryId = geometry ? geometry.id : null;
             }
 
+            let boundingInfo = <BoundingInfo>mesh.getBoundingInfo();
+
             return {
                 uniqueId: mesh.uniqueId,
                 id: mesh.id,
                 name: mesh.name,
                 geometryId: geometryId,
-                sphereCenter: mesh.getBoundingInfo().boundingSphere.centerWorld.asArray(),
-                sphereRadius: mesh.getBoundingInfo().boundingSphere.radiusWorld,
-                boxMinimum: mesh.getBoundingInfo().boundingBox.minimumWorld.asArray(),
-                boxMaximum: mesh.getBoundingInfo().boundingBox.maximumWorld.asArray(),
+                sphereCenter: boundingInfo.boundingSphere.centerWorld.asArray(),
+                sphereRadius: boundingInfo.boundingSphere.radiusWorld,
+                boxMinimum: boundingInfo.boundingBox.minimumWorld.asArray(),
+                boxMaximum: boundingInfo.boundingBox.maximumWorld.asArray(),
                 worldMatrixFromCache: mesh.worldMatrixFromCache.asArray(),
                 subMeshes: submeshes,
                 checkCollisions: mesh.checkCollisions

+ 4 - 2
src/Culling/Octrees/babylon.octree.ts

@@ -96,13 +96,15 @@
         }
 
         public static CreationFuncForMeshes = (entry: AbstractMesh, block: OctreeBlock<AbstractMesh>): void => {
-            if (!entry.isBlocked && entry.getBoundingInfo().boundingBox.intersectsMinMax(block.minPoint, block.maxPoint)) {
+            let boundingInfo = entry.getBoundingInfo();
+            if (!entry.isBlocked && boundingInfo && boundingInfo.boundingBox.intersectsMinMax(block.minPoint, block.maxPoint)) {
                 block.entries.push(entry);
             }
         }
 
         public static CreationFuncForSubMeshes = (entry: SubMesh, block: OctreeBlock<SubMesh>): void => {
-            if (entry.getBoundingInfo().boundingBox.intersectsMinMax(block.minPoint, block.maxPoint)) {
+            let boundingInfo = entry.getBoundingInfo();
+            if (boundingInfo && boundingInfo.boundingBox.intersectsMinMax(block.minPoint, block.maxPoint)) {
                 block.entries.push(entry);
             }
         }

+ 1 - 1
src/Debug/babylon.skeletonViewer.ts

@@ -114,7 +114,7 @@
             }
 
             if (!this._debugMesh) {
-                this._debugMesh = BABYLON.MeshBuilder.CreateLineSystem("", { lines: this._debugLines, updatable: true }, this._scene);
+                this._debugMesh = BABYLON.MeshBuilder.CreateLineSystem("", { lines: this._debugLines, updatable: true, instance: null }, this._scene);
                 this._debugMesh.renderingGroupId = this.renderingGroupId;
             } else {
                 BABYLON.MeshBuilder.CreateLineSystem("", { lines: this._debugLines, updatable: true, instance: this._debugMesh }, this._scene);

+ 31 - 18
src/Engine/babylon.engine.ts

@@ -2137,13 +2137,15 @@
                     }
 
                     var buffer = vertexBuffer.getBuffer();
-                    this.vertexAttribPointer(buffer, order, vertexBuffer.getSize(), this._gl.FLOAT, false, vertexBuffer.getStrideSize() * 4, vertexBuffer.getOffset() * 4);
-
-                    if (vertexBuffer.getIsInstanced()) {
-                        this._gl.vertexAttribDivisor(order, vertexBuffer.getInstanceDivisor());
-                        if (!this._vaoRecordInProgress) {
-                            this._currentInstanceLocations.push(order);
-                            this._currentInstanceBuffers.push(buffer);
+                    if (buffer) {
+                        this.vertexAttribPointer(buffer, order, vertexBuffer.getSize(), this._gl.FLOAT, false, vertexBuffer.getStrideSize() * 4, vertexBuffer.getOffset() * 4);
+
+                        if (vertexBuffer.getIsInstanced()) {
+                            this._gl.vertexAttribDivisor(order, vertexBuffer.getInstanceDivisor());
+                            if (!this._vaoRecordInProgress) {
+                                this._currentInstanceLocations.push(order);
+                                this._currentInstanceBuffers.push(buffer);
+                            }
                         }
                     }
                 }
@@ -3104,7 +3106,11 @@
             return internalFormat;
         }
 
-        public updateRawTexture(texture: InternalTexture, data: ArrayBufferView, format: number, invertY: boolean, compression: Nullable<string> = null): void {
+        public updateRawTexture(texture: Nullable<InternalTexture>, data: Nullable<ArrayBufferView>, format: number, invertY: boolean, compression: Nullable<string> = null): void {
+            if (!texture) {
+                return;
+            }
+
             var internalFormat = this._getInternalFormat(format);
             this._bindTextureDirectly(this._gl.TEXTURE_2D, texture);
             this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY === undefined ? 1 : (invertY ? 1 : 0));
@@ -3120,7 +3126,7 @@
                 this._gl.pixelStorei(this._gl.UNPACK_ALIGNMENT, 1);
             }
 
-            if (compression) {
+            if (compression && data) {
                 this._gl.compressedTexImage2D(this._gl.TEXTURE_2D, 0, (<any>this.getCaps().s3tc)[compression], texture.width, texture.height, 0, data);
             } else {
                 this._gl.texImage2D(this._gl.TEXTURE_2D, 0, internalFormat, texture.width, texture.height, 0, internalFormat, this._gl.UNSIGNED_BYTE, data);
@@ -3134,7 +3140,7 @@
             texture.isReady = true;
         }
 
-        public createRawTexture(data: ArrayBufferView, width: number, height: number, format: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number, compression: Nullable<string> = null): InternalTexture {
+        public createRawTexture(data: Nullable<ArrayBufferView>, width: number, height: number, format: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number, compression: Nullable<string> = null): InternalTexture {
             var texture = new InternalTexture(this, InternalTexture.DATASOURCE_RAW);
             texture.baseWidth = width;
             texture.baseHeight = height;
@@ -3221,7 +3227,12 @@
             texture.samplingMode = samplingMode;
         }
 
-        public updateDynamicTexture(texture: InternalTexture, canvas: HTMLCanvasElement, invertY: boolean, premulAlpha: boolean = false, format?: number): void {
+        public updateDynamicTexture(texture: Nullable<InternalTexture>, canvas: HTMLCanvasElement, invertY: boolean, premulAlpha: boolean = false, format?: number): void {
+
+            if (!texture) {
+                return;
+            }
+
             this._bindTextureDirectly(this._gl.TEXTURE_2D, texture);
             this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 1 : 0);
             if (premulAlpha) {
@@ -3240,8 +3251,8 @@
             texture.isReady = true;
         }
 
-        public updateVideoTexture(texture: InternalTexture, video: HTMLVideoElement, invertY: boolean): void {
-            if (texture._isDisabled) {
+        public updateVideoTexture(texture: Nullable<InternalTexture>, video: HTMLVideoElement, invertY: boolean): void {
+            if (!texture || texture._isDisabled) {
                 return;
             }
 
@@ -3705,7 +3716,9 @@
             return texture;
         }
 
-        public createPrefilteredCubeTexture(rootUrl: string, scene: Nullable<Scene>, scale: number, offset: number, onLoad: (internalTexture: Nullable<InternalTexture>) => void, onError: Nullable<(message?: string, exception?: any) => void> = null, format?: number, forcedExtension: any = null): InternalTexture {
+        public createPrefilteredCubeTexture(rootUrl: string, scene: Nullable<Scene>, scale: number, offset: number, 
+                                            onLoad: Nullable<(internalTexture: Nullable<InternalTexture>) => void> = null, 
+                                            onError: Nullable<(message?: string, exception?: any) => void> = null, format?: number, forcedExtension: any = null): InternalTexture {
             var callback = (loadData: any) => {
                 if (!loadData) {
                     if (onLoad) {
@@ -4135,7 +4148,7 @@
             return texture;
         };
 
-        public updateRawTexture3D(texture: InternalTexture, data: ArrayBufferView, format: number, invertY: boolean, compression: Nullable<string> = null): void {
+        public updateRawTexture3D(texture: InternalTexture, data: Nullable<ArrayBufferView>, format: number, invertY: boolean, compression: Nullable<string> = null): void {
             var internalFormat = this._getInternalFormat(format);
             this._bindTextureDirectly(this._gl.TEXTURE_3D, texture);
             this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY === undefined ? 1 : (invertY ? 1 : 0));
@@ -4151,7 +4164,7 @@
                 this._gl.pixelStorei(this._gl.UNPACK_ALIGNMENT, 1);
             }
 
-            if (compression) {
+            if (compression && data) {
                 this._gl.compressedTexImage3D(this._gl.TEXTURE_3D, 0, (<any>this.getCaps().s3tc)[compression], texture.width, texture.height, texture.depth, 0, data);
             } else {
                 this._gl.texImage3D(this._gl.TEXTURE_3D, 0, internalFormat, texture.width, texture.height, texture.depth, 0, internalFormat, this._gl.UNSIGNED_BYTE, data);
@@ -4165,7 +4178,7 @@
             texture.isReady = true;
         }
 
-        public createRawTexture3D(data: ArrayBufferView, width: number, height: number, depth: number, format: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number, compression: Nullable<string> = null): InternalTexture {
+        public createRawTexture3D(data: Nullable<ArrayBufferView>, width: number, height: number, depth: number, format: number, generateMipMaps: boolean, invertY: boolean, samplingMode: number, compression: Nullable<string> = null): InternalTexture {
             var texture = new InternalTexture(this, InternalTexture.DATASOURCE_RAW3D);
             texture.baseWidth = width;
             texture.baseHeight = height;
@@ -4429,7 +4442,7 @@
 
             let internalTexture: InternalTexture;
             if (texture.isReady()) {
-                internalTexture = texture.getInternalTexture();
+                internalTexture = <InternalTexture>texture.getInternalTexture();
             }
             else if (texture.isCube) {
                 internalTexture = this.emptyCubeTexture;

+ 3 - 1
src/Gamepad/Controllers/babylon.poseEnabledController.ts

@@ -80,7 +80,9 @@ module BABYLON {
             
             if (this._mesh) {
                 this._mesh.position.copyFrom(this._calculatedPosition);
-                this._mesh.rotationQuaternion.copyFrom(this._calculatedRotation);
+                if (this._mesh.rotationQuaternion) {
+                    this._mesh.rotationQuaternion.copyFrom(this._calculatedRotation);
+                }
             }
         }
 

+ 9 - 0
src/Gamepad/Controllers/babylon.windowsMotionController.ts

@@ -138,6 +138,11 @@ module BABYLON {
             }
 
             var meshInfo = this._loadedMeshInfo.buttonMeshes[buttonName];
+
+            if (!meshInfo.unpressed.rotationQuaternion || !meshInfo.pressed.rotationQuaternion || !meshInfo.value.rotationQuaternion) {
+                return;
+            }
+
             BABYLON.Quaternion.SlerpToRef(
                 meshInfo.unpressed.rotationQuaternion, 
                 meshInfo.pressed.rotationQuaternion, 
@@ -160,6 +165,10 @@ module BABYLON {
                 return;
             }
 
+            if (!meshInfo.min.rotationQuaternion || !meshInfo.max.rotationQuaternion || !meshInfo.value.rotationQuaternion) {
+                return;
+            }            
+
             // Convert from gamepad value range (-1 to +1) to lerp range (0 to 1)
             let lerpValue = axisValue * 0.5 + 0.5;
             BABYLON.Quaternion.SlerpToRef(

+ 15 - 7
src/Layer/babylon.highlightlayer.ts

@@ -419,17 +419,21 @@ module BABYLON {
 
             // Custom render function
             var renderSubMesh = (subMesh: SubMesh): void => {
-
                 if (!this._meshes) {
                     return;
                 }
 
+                var material = subMesh.getMaterial();
                 var mesh = subMesh.getRenderingMesh();
                 var scene = this._scene;
                 var engine = scene.getEngine();
 
+                if (!material) {
+                    return;
+                }
+
                 // Culling
-                engine.setState(subMesh.getMaterial().backFaceCulling);
+                engine.setState(material.backFaceCulling);
 
                 // Managing instances
                 var batch = mesh._getInstancesRenderList(subMesh._id);
@@ -445,7 +449,6 @@ module BABYLON {
                 var hardwareInstancedRendering = (engine.getCaps().instancedArrays) && (batch.visibleInstances[subMesh._id] !== null) && (batch.visibleInstances[subMesh._id] !== undefined);
 
                 var highlightLayerMesh = this._meshes[mesh.uniqueId];
-                var material = subMesh.getMaterial();
                 var emissiveTexture: Nullable<Texture> = null;
                 if (highlightLayerMesh && highlightLayerMesh.glowEmissiveOnly && material) {
                     emissiveTexture = (<any>material).emissiveTexture;
@@ -491,7 +494,7 @@ module BABYLON {
                     }
 
                     // Bones
-                    if (mesh.useBones && mesh.computeBonesUsingShaders) {
+                    if (mesh.useBones && mesh.computeBonesUsingShaders && mesh.skeleton) {
                         this._glowMapGenerationEffect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
                     }
 
@@ -545,7 +548,13 @@ module BABYLON {
          * @return true if ready otherwise, false
          */
         private isReady(subMesh: SubMesh, useInstances: boolean, emissiveTexture: Nullable<Texture>): boolean {
-            if (!subMesh.getMaterial().isReady(subMesh.getMesh(), useInstances)) {
+            let material = subMesh.getMaterial();
+
+            if (!material) {
+                return false;
+            }
+
+            if (!material.isReady(subMesh.getMesh(), useInstances)) {
                 return false;
             }
 
@@ -554,7 +563,6 @@ module BABYLON {
             var attribs = [VertexBuffer.PositionKind];
 
             var mesh = subMesh.getMesh();
-            var material = subMesh.getMaterial();
             var uv1 = false;
             var uv2 = false;
 
@@ -607,7 +615,7 @@ module BABYLON {
                     attribs.push(VertexBuffer.MatricesWeightsExtraKind);
                 }
                 defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
-                defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
+                defines.push("#define BonesPerMesh " + (mesh.skeleton ? (mesh.skeleton.bones.length + 1) : 0));
             } else {
                 defines.push("#define NUM_BONE_INFLUENCERS 0");
             }

+ 1 - 1
src/LensFlare/babylon.lensFlareSystem.ts

@@ -23,7 +23,7 @@
             this.id = name;
             scene.lensFlareSystems.push(this);
 
-            this.meshesSelectionPredicate = m => scene.activeCamera !== null && m.material && m.isVisible && m.isEnabled() && m.isBlocker && ((m.layerMask & scene.activeCamera.layerMask) != 0);
+            this.meshesSelectionPredicate = m => <boolean>(scene.activeCamera && m.material && m.isVisible && m.isEnabled() && m.isBlocker && ((m.layerMask & scene.activeCamera.layerMask) != 0));
 
             var engine = scene.getEngine();
 

+ 6 - 2
src/Lights/Shadows/babylon.shadowGenerator.ts

@@ -525,9 +525,14 @@
             var mesh = subMesh.getRenderingMesh();
             var scene = this._scene;
             var engine = scene.getEngine();
+            let material = subMesh.getMaterial();
+
+            if (!material) {
+                return;
+            }
 
             // Culling
-            engine.setState(subMesh.getMaterial().backFaceCulling);
+            engine.setState(material.backFaceCulling);
 
             // Managing instances
             var batch = mesh._getInstancesRenderList(subMesh._id);
@@ -539,7 +544,6 @@
             if (this.isReady(subMesh, hardwareInstancedRendering)) {
                 engine.enableEffect(this._effect);
                 mesh._bind(subMesh, this._effect, Material.TriangleFillMode);
-                var material = subMesh.getMaterial();
 
                 this._effect.setFloat2("biasAndScale", this.bias, this.depthScale);
 

+ 16 - 12
src/Materials/PBR/babylon.pbrBaseMaterial.ts

@@ -389,7 +389,7 @@
          * from cos thetav and roughness: 
          * http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
          */
-        protected _environmentBRDFTexture: BaseTexture = null;
+        protected _environmentBRDFTexture: Nullable<BaseTexture> = null;
 
         /**
          * Force the shader to compute irradiance in the fragment shader in order to take bump in account.
@@ -411,13 +411,13 @@
         /**
          * Keep track of the image processing observer to allow dispose and replace.
          */
-        private _imageProcessingObserver: Observer<ImageProcessingConfiguration>;
+        private _imageProcessingObserver: Nullable<Observer<ImageProcessingConfiguration>>;
 
         /**
          * Attaches a new image processing configuration to the PBR Material.
          * @param configuration 
          */
-        protected _attachImageProcessingConfiguration(configuration: ImageProcessingConfiguration): void {
+        protected _attachImageProcessingConfiguration(configuration: Nullable<ImageProcessingConfiguration>): void {
             if (configuration === this._imageProcessingConfiguration) {
                 return;
             }
@@ -812,7 +812,7 @@
             MaterialHelper.PrepareDefinesForMisc(mesh, scene, this._useLogarithmicDepth, this.pointsCloud, this.fogEnabled, defines);
 
             // Values that need to be evaluated on every frame
-            MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances, this._forceAlphaTest);
+            MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false, this._forceAlphaTest);
 
              // Attribs
             if (MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true, true)) {
@@ -968,7 +968,7 @@
                 this.buildUniformLayout();
             }
 
-            if (!subMesh.effect.isReady()) {
+            if (!subMesh.effect || !subMesh.effect.isReady()) {
                 return false;
             }
 
@@ -1041,6 +1041,11 @@
             }
 
             var effect = subMesh.effect;
+
+            if (!effect) {
+                return;
+            }
+
             this._activeEffect = effect;
 
             // Matrices
@@ -1051,11 +1056,12 @@
             // Bones
             MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
 
+            let reflectionTexture: Nullable<BaseTexture> = null;
             if (mustRebind) {
                 this._uniformBuffer.bindToEffect(effect, "Material");
 
                 this.bindViewProjection(effect);
-                var reflectionTexture = this._getReflectionTexture();
+                reflectionTexture = this._getReflectionTexture();
                 var refractionTexture = this._getRefractionTexture();
                                
                 if (!this._uniformBuffer.useUbo || !this.isFrozen || !this._uniformBuffer.isSync) {
@@ -1081,8 +1087,8 @@
                             this._uniformBuffer.updateMatrix("reflectionMatrix", reflectionTexture.getReflectionTextureMatrix());
                             this._uniformBuffer.updateFloat2("vReflectionInfos", reflectionTexture.level, 0);
 
-                            if (defines.USESPHERICALFROMREFLECTIONMAP) {
-                                var polynomials = reflectionTexture.sphericalPolynomial;
+                            var polynomials = reflectionTexture.sphericalPolynomial;
+                            if (defines.USESPHERICALFROMREFLECTIONMAP && polynomials) {
                                 this._activeEffect.setFloat3("vSphericalX", polynomials.x.x, polynomials.x.y, polynomials.x.z);
                                 this._activeEffect.setFloat3("vSphericalY", polynomials.y.x, polynomials.y.y, polynomials.y.z);
                                 this._activeEffect.setFloat3("vSphericalZ", polynomials.z.x, polynomials.z.y, polynomials.z.z);
@@ -1259,7 +1265,7 @@
                 // Colors
                 scene.ambientColor.multiplyToRef(this._ambientColor, this._globalAmbientColor);
 
-                var eyePosition = scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.globalPosition;
+                var eyePosition = scene._mirroredCameraPosition ? scene._mirroredCameraPosition : (<Camera>scene.activeCamera).globalPosition;
                 var invertNormal = (scene.useRightHandedSystem === (scene._mirroredCameraPosition != null));
                 effect.setFloat4("vEyePosition",
                     eyePosition.x,
@@ -1298,8 +1304,6 @@
             this._uniformBuffer.update();
 
             this._afterBind(mesh);
-
-            scene = null;
         }
 
         public getAnimatables(): IAnimatable[] {
@@ -1355,7 +1359,7 @@
             return this.getScene().environmentTexture;
         }
 
-        private _getRefractionTexture(): BaseTexture {
+        private _getRefractionTexture(): Nullable<BaseTexture> {
             if (this._refractionTexture) {
                 return this._refractionTexture;
             }

+ 3 - 2
src/Materials/Textures/Procedurals/babylon.customProceduralTexture.ts

@@ -83,8 +83,9 @@ module BABYLON {
         }
 
         public render(useCameraPostProcess?: boolean): void {
-            if (this._animate) {
-                this._time += this.getScene().getAnimationRatio() * 0.03;
+            let scene = this.getScene();
+            if (this._animate && scene) {
+                this._time += scene.getAnimationRatio() * 0.03;
                 this.updateShaderUniforms();
             }
 

+ 49 - 24
src/Materials/Textures/Procedurals/babylon.proceduralTexture.ts

@@ -8,8 +8,8 @@
 
         public onGenerated: () => void;
 
-        private _vertexBuffers: { [key: string]: VertexBuffer } = {};
-        private _indexBuffer: WebGLBuffer;
+        private _vertexBuffers: { [key: string]: Nullable<VertexBuffer> } = {};
+        private _indexBuffer: Nullable<WebGLBuffer>;
         private _effect: Effect;
 
         private _uniforms = new Array<string>();
@@ -25,15 +25,18 @@
         private _vectors3: {[key: string]: Vector3} = {};
         private _matrices: {[key: string]: Matrix} = {};
 
-        private _fallbackTexture: Texture;
+        private _fallbackTexture: Nullable<Texture>;
 
         private _fallbackTextureUsed = false;
+        private _engine: Engine;
 
-        constructor(name: string, size: any, fragment: any, scene: Scene, fallbackTexture?: Texture, generateMipMaps = true, public isCube = false) {
+        constructor(name: string, size: any, fragment: any, scene: Scene, fallbackTexture: Nullable<Texture> = null, generateMipMaps = true, public isCube = false) {
             super(null, scene, !generateMipMaps);
 
             scene._proceduralTextures.push(this);
 
+            this._engine = scene.getEngine();
+
             this.name = name;
             this.isRenderTarget = true;
             this._size = size;
@@ -43,14 +46,12 @@
 
             this._fallbackTexture = fallbackTexture;
 
-            var engine = scene.getEngine();
-
             if (isCube) {
-                this._texture = engine.createRenderTargetCubeTexture(size, { generateMipMaps: generateMipMaps });
+                this._texture = this._engine.createRenderTargetCubeTexture(size, { generateMipMaps: generateMipMaps });
                 this.setFloat("face", 0);
             }
             else {
-                this._texture = engine.createRenderTargetTexture(size, generateMipMaps);
+                this._texture = this._engine.createRenderTargetTexture(size, generateMipMaps);
             }
 
             // VBO
@@ -60,13 +61,13 @@
             vertices.push(-1, -1);
             vertices.push(1, -1);
 
-            this._vertexBuffers[VertexBuffer.PositionKind] = new VertexBuffer(engine, vertices, VertexBuffer.PositionKind, false, false, 2);
+            this._vertexBuffers[VertexBuffer.PositionKind] = new VertexBuffer(this._engine, vertices, VertexBuffer.PositionKind, false, false, 2);
 
             this._createIndexBuffer();
         }
 
         private _createIndexBuffer(): void {
-            var engine = this.getScene().getEngine();
+            var engine = this._engine;
 
             // Indices
             var indices = [];
@@ -82,7 +83,12 @@
         }
 
         public _rebuild(): void {
-            this._vertexBuffers[VertexBuffer.PositionKind]._rebuild();
+            let vb = this._vertexBuffers[VertexBuffer.PositionKind];
+
+            if (vb) {
+                vb._rebuild();
+            }
+            
             this._createIndexBuffer();
 
             if (this.refreshRate === RenderTargetTexture.REFRESHRATE_RENDER_ONCE) {
@@ -93,14 +99,15 @@
         public reset(): void {
             if (this._effect === undefined) {
                 return;
-            }
-            var engine = this.getScene().getEngine();
+            }      
+            
+            var engine = this._engine;
             engine._releaseEffect(this._effect);
         }
 
 
         public isReady(): boolean {
-            var engine = this.getScene().getEngine();
+            var engine = this._engine;
             var shaders;
 
             if (!this._fragment) {
@@ -122,12 +129,15 @@
                 [VertexBuffer.PositionKind],
                 this._uniforms,
                 this._samplers,
-                "", null, null, () => {
+                "", undefined, undefined, () => {
                     this.releaseInternalTexture();
 
                     if (this._fallbackTexture) {
                         this._texture = this._fallbackTexture._texture;
-                        this._texture.incrementReferences();
+
+                        if (this._texture) {
+                            this._texture.incrementReferences();
+                        }
                     }
 
                     this._fallbackTextureUsed = true;
@@ -187,7 +197,7 @@
             }
 
             this.releaseInternalTexture();
-            this._texture = this.getScene().getEngine().createRenderTargetTexture(size, generateMipMaps);
+            this._texture = this._engine.createRenderTargetTexture(size, generateMipMaps);
         }
 
         private _checkUniform(uniformName: string): void {
@@ -254,9 +264,14 @@
             return this;
         }
 
-        public render(useCameraPostProcess?: boolean) {
+        public render(useCameraPostProcess?: boolean): void {
             var scene = this.getScene();
-            var engine = scene.getEngine();
+
+            if (!scene) {
+                return;
+            }
+
+            var engine = this._engine;
 
             // Render
             engine.enableEffect(this._effect);
@@ -301,7 +316,11 @@
             // Matrix      
             for (name in this._matrices) {
                 this._effect.setMatrix(name, this._matrices[name]);
-            }            
+            }           
+            
+            if (!this._texture) {
+                return;
+            }
 
             if (this.isCube) {
                 for (var face = 0; face < 6; face++) {
@@ -346,7 +365,7 @@
 
         public clone(): ProceduralTexture {
             var textureSize = this.getSize();
-            var newTexture = new ProceduralTexture(this.name, textureSize.width, this._fragment, this.getScene(), this._fallbackTexture, this._generateMipMaps);
+            var newTexture = new ProceduralTexture(this.name, textureSize.width, this._fragment, <Scene>this.getScene(), this._fallbackTexture, this._generateMipMaps);
 
             // Base texture
             newTexture.hasAlpha = this.hasAlpha;
@@ -359,10 +378,16 @@
         }
 
         public dispose(): void {
-            var index = this.getScene()._proceduralTextures.indexOf(this);
+            let scene = this.getScene();
+
+            if (!scene) {
+                return;
+            }
+
+            var index = scene._proceduralTextures.indexOf(this);
 
             if (index >= 0) {
-                this.getScene()._proceduralTextures.splice(index, 1);
+                scene._proceduralTextures.splice(index, 1);
             }
 
             var vertexBuffer = this._vertexBuffers[VertexBuffer.PositionKind];
@@ -371,7 +396,7 @@
                 this._vertexBuffers[VertexBuffer.PositionKind] = null;
             }
 
-            if (this._indexBuffer && this.getScene().getEngine()._releaseBuffer(this._indexBuffer)) {
+            if (this._indexBuffer && this._engine._releaseBuffer(this._indexBuffer)) {
                 this._indexBuffer = null;
             }
 

+ 23 - 16
src/Materials/Textures/babylon.colorGradingTexture.ts

@@ -24,6 +24,8 @@ module BABYLON {
          */
         private static _noneEmptyLineRegex = /\S+/;
 
+        private _engine: Engine;
+
         /**
          * Instantiates a ColorGradingTexture from the following parameters.
          * 
@@ -37,12 +39,13 @@ module BABYLON {
                 return;
             }
 
+            this._engine = scene.getEngine();
             this._textureMatrix = Matrix.Identity();
             this.name = url;
             this.url = url;
             this.hasAlpha = false;
             this.isCube = false;
-            this.is3D = scene.getEngine().webGLVersion > 1;
+            this.is3D = this._engine.webGLVersion > 1;
             this.wrapU = Texture.CLAMP_ADDRESSMODE;
             this.wrapV = Texture.CLAMP_ADDRESSMODE;
             this.wrapR = Texture.CLAMP_ADDRESSMODE;
@@ -72,7 +75,7 @@ module BABYLON {
          * Occurs when the file being loaded is a .3dl LUT file.
          */
         private load3dlTexture() {
-            var engine = this.getScene().getEngine();
+            var engine = this._engine;
             var texture: InternalTexture;
             if (engine.webGLVersion === 1) {
                 texture = engine.createRawTexture(null, 1, 1, BABYLON.Engine.TEXTUREFORMAT_RGBA, false, false, Texture.BILINEAR_SAMPLINGMODE);
@@ -84,8 +87,8 @@ module BABYLON {
             this._texture = texture;
 
             var callback = (text: string) => {
-                var data: Uint8Array;
-                var tempData: Float32Array;
+                var data: Nullable<Uint8Array> = null;
+                var tempData: Nullable<Float32Array> = null;
 
                 var line: string;
                 var lines = text.split('\n');
@@ -121,9 +124,11 @@ module BABYLON {
 
                         var pixelStorageIndex = (pixelIndexW + pixelIndexSlice * size + pixelIndexH * size * size) * 4;
 
-                        tempData[pixelStorageIndex + 0] = r;
-                        tempData[pixelStorageIndex + 1] = g;
-                        tempData[pixelStorageIndex + 2] = b;
+                        if (tempData) {
+                            tempData[pixelStorageIndex + 0] = r;
+                            tempData[pixelStorageIndex + 1] = g;
+                            tempData[pixelStorageIndex + 2] = b;
+                        }
 
                         pixelIndexSlice++;
                         if (pixelIndexSlice % size == 0) {
@@ -137,13 +142,15 @@ module BABYLON {
                     }
                 }
 
-                for (let i = 0; i < tempData.length; i++) {
-                    if (i > 0 && (i+1) % 4 === 0) {
-                        data[i] = 255;
-                    }
-                    else {
-                        var value = tempData[i];
-                        data[i] = (value / maxColor * 255);
+                if (tempData && data) {
+                    for (let i = 0; i < tempData.length; i++) {
+                        if (i > 0 && (i+1) % 4 === 0) {
+                            data[i] = 255;
+                        }
+                        else {
+                            var value = tempData[i];
+                            data[i] = (value / maxColor * 255);
+                        }
                     }
                 }
 
@@ -174,7 +181,7 @@ module BABYLON {
          * Clones the color gradind texture.
          */
         public clone(): ColorGradingTexture {
-            var newTexture = new ColorGradingTexture(this.url, this.getScene());
+            var newTexture = new ColorGradingTexture(this.url, <Scene>this.getScene());
 
             // Base texture
             newTexture.level = this.level;
@@ -205,7 +212,7 @@ module BABYLON {
          * @param rootUrl The root url of the data assets to load
          * @return A color gradind texture
          */
-        public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): ColorGradingTexture {
+        public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): Nullable<ColorGradingTexture> {
             var texture = null;
             if (parsedTexture.name && !parsedTexture.isRenderTarget) {
                 texture = new BABYLON.ColorGradingTexture(parsedTexture.name, scene);

+ 15 - 4
src/Materials/Textures/babylon.cubeTexture.ts

@@ -18,7 +18,8 @@
             return new CubeTexture(url, scene, null, false, null, null, null, undefined, true, forcedExtension);
         }
 
-        constructor(rootUrl: string, scene: Scene, extensions?: string[], noMipmap?: boolean, files?: string[], onLoad: () => void = null, onError: (message?: string, exception?: any) => void = null, format: number = Engine.TEXTUREFORMAT_RGBA, prefiltered = false, forcedExtension: any = null) {
+        constructor(rootUrl: string, scene: Scene, extensions: Nullable<string[]> = null, noMipmap: boolean = false, files: Nullable<string[]> = null,
+            onLoad: Nullable<() => void> = null, onError: Nullable<(message?: string, exception?: any) => void> = null, format: number = Engine.TEXTUREFORMAT_RGBA, prefiltered = false, forcedExtension: any = null) {
             super(scene);
 
             this.name = rootUrl;
@@ -82,15 +83,20 @@
                 return;
             }
 
+            let scene = this.getScene();
+
+            if (!scene) {
+                return;
+            }
             this.delayLoadState = Engine.DELAYLOADSTATE_LOADED;
             this._texture = this._getFromCache(this.url, this._noMipmap);
 
             if (!this._texture) {
                 if (this._prefiltered) {
-                    this._texture = this.getScene().getEngine().createPrefilteredCubeTexture(this.url, this.getScene(), this.lodGenerationScale, this.lodGenerationOffset, undefined, undefined, this._format);
+                    this._texture = scene.getEngine().createPrefilteredCubeTexture(this.url, scene, this.lodGenerationScale, this.lodGenerationOffset, undefined, undefined, this._format);
                 }
                 else {
-                    this._texture = this.getScene().getEngine().createCubeTexture(this.url, this.getScene(), this._files, this._noMipmap, undefined, undefined, this._format);
+                    this._texture = scene.getEngine().createCubeTexture(this.url, scene, this._files, this._noMipmap, undefined, undefined, this._format);
                 }
             }
         }
@@ -122,7 +128,12 @@
 
         public clone(): CubeTexture {
             return SerializationHelper.Clone(() => {
-                return new CubeTexture(this.url, this.getScene(), this._extensions, this._noMipmap, this._files);
+                let scene = this.getScene();
+                
+                if (!scene) {
+                    return this;
+                }
+                return new CubeTexture(this.url, scene, this._extensions, this._noMipmap, this._files);
             }, this);
         }
     }

+ 15 - 8
src/Materials/Textures/babylon.dynamicTexture.ts

@@ -5,12 +5,13 @@ module BABYLON {
         private _generateMipMaps: boolean;
         private _canvas: HTMLCanvasElement;
         private _context: CanvasRenderingContext2D;
+        private _engine: Engine;
 
         constructor(name: string, options: any, scene: Scene, generateMipMaps: boolean, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, format: number = Engine.TEXTUREFORMAT_RGBA) {
             super(null, scene, !generateMipMaps, undefined, samplingMode, undefined, undefined, undefined, undefined, format);
 
             this.name = name;
-            var engine = this.getScene().getEngine();
+            this._engine = (<Scene>this.getScene()).getEngine();
             this.wrapU = Texture.CLAMP_ADDRESSMODE;
             this.wrapV = Texture.CLAMP_ADDRESSMODE;
 
@@ -18,14 +19,14 @@ module BABYLON {
 
             if (options.getContext) {
                 this._canvas = options;
-                this._texture = engine.createDynamicTexture(options.width, options.height, generateMipMaps, samplingMode);
+                this._texture = this._engine.createDynamicTexture(options.width, options.height, generateMipMaps, samplingMode);
             } else {
                 this._canvas = document.createElement("canvas");
 
                 if (options.width) {
-                    this._texture = engine.createDynamicTexture(options.width, options.height, generateMipMaps, samplingMode);
+                    this._texture = this._engine.createDynamicTexture(options.width, options.height, generateMipMaps, samplingMode);
                 } else {
-                    this._texture = engine.createDynamicTexture(options, options, generateMipMaps, samplingMode);
+                    this._texture = this._engine.createDynamicTexture(options, options, generateMipMaps, samplingMode);
                 }
             }
 
@@ -33,7 +34,7 @@ module BABYLON {
 
             this._canvas.width = textureSize.width;
             this._canvas.height = textureSize.height;
-            this._context = this._canvas.getContext("2d");
+            this._context = <CanvasRenderingContext2D>this._canvas.getContext("2d");
         }
 
         public get canRescale(): boolean {
@@ -46,7 +47,7 @@ module BABYLON {
 
             this.releaseInternalTexture();
 
-            this._texture = this.getScene().getEngine().createDynamicTexture(textureSize.width, textureSize.height, this._generateMipMaps, this._samplingMode);
+            this._texture = this._engine.createDynamicTexture(textureSize.width, textureSize.height, this._generateMipMaps, this._samplingMode);
         }
 
         public scale(ratio: number): void {
@@ -77,7 +78,7 @@ module BABYLON {
         }
 
         public update(invertY?: boolean): void {
-            this.getScene().getEngine().updateDynamicTexture(this._texture, this._canvas, invertY === undefined ? true : invertY, undefined, this._format);
+            this._engine.updateDynamicTexture(this._texture, this._canvas, invertY === undefined ? true : invertY, undefined, this._format || undefined);
         }
 
         public drawText(text: string, x: number, y: number, font: string, color: string, clearColor: string, invertY?: boolean, update = true) {
@@ -106,8 +107,14 @@ module BABYLON {
         }
 
         public clone(): DynamicTexture {
+            let scene = this.getScene();
+
+            if (!scene) {
+                return this;
+            }
+
             var textureSize = this.getSize();
-            var newTexture = new DynamicTexture(this.name, textureSize, this.getScene(), this._generateMipMaps);
+            var newTexture = new DynamicTexture(this.name, textureSize, scene, this._generateMipMaps);
 
             // Base texture
             newTexture.hasAlpha = this.hasAlpha;

+ 1 - 1
src/Materials/Textures/babylon.internalTexture.ts

@@ -34,7 +34,7 @@ module BABYLON {
         // Private
         public _dataSource = InternalTexture.DATASOURCE_UNKNOWN;
         public _buffer: Nullable<ArrayBuffer | HTMLImageElement>;
-        public _bufferView: ArrayBufferView;
+        public _bufferView: Nullable<ArrayBufferView>;
         public _bufferViewArray: Nullable<ArrayBufferView[]>;
         public _size: number;
         public _extension: string;

+ 13 - 5
src/Materials/Textures/babylon.mirrorTexture.ts

@@ -75,7 +75,7 @@ module BABYLON {
 
                 scene.getEngine().cullBackFaces = false;
 
-                scene._mirroredCameraPosition = Vector3.TransformCoordinates(scene.activeCamera.globalPosition, this._mirrorMatrix);
+                scene._mirroredCameraPosition = Vector3.TransformCoordinates((<Camera>scene.activeCamera).globalPosition, this._mirrorMatrix);
             });
 
             this.onAfterRenderObservable.add(() => {
@@ -91,14 +91,14 @@ module BABYLON {
             this.clearPostProcesses(true);
 
             if (this._blurKernelX && this._blurKernelY) {
-                var engine = this.getScene().getEngine();
+                var engine = (<Scene>this.getScene()).getEngine();
 
                 var textureType = engine.getCaps().textureFloatRender ? Engine.TEXTURETYPE_FLOAT : Engine.TEXTURETYPE_HALF_FLOAT;
 
                 this._blurX = new BABYLON.BlurPostProcess("horizontal blur", new BABYLON.Vector2(1.0, 0), this._blurKernelX, this._blurRatio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, engine, false, textureType);
                 this._blurX.autoClear = false;
 
-                if (this._blurRatio === 1 && this.samples < 2) {
+                if (this._blurRatio === 1 && this.samples < 2 && this._texture) {
                     this._blurX.outputTexture = this._texture;
                 } else {
                     this._blurX.alwaysForcePOT = true;
@@ -114,11 +114,17 @@ module BABYLON {
         }   
 
         public clone(): MirrorTexture {
+            let scene = this.getScene();
+
+            if (!scene) {
+                return this;
+            }
+
             var textureSize = this.getSize();
             var newTexture = new MirrorTexture(
                 this.name,
                 textureSize.width,
-                this.getScene(),
+                scene,
                 this._renderTargetOptions.generateMipMaps,
                 this._renderTargetOptions.type,
                 this._renderTargetOptions.samplingMode,
@@ -131,7 +137,9 @@ module BABYLON {
 
             // Mirror Texture
             newTexture.mirrorPlane = this.mirrorPlane.clone();
-            newTexture.renderList = this.renderList.slice(0);
+            if (this.renderList) {
+                newTexture.renderList = this.renderList.slice(0);
+            }
 
             return newTexture;
         }

+ 7 - 5
src/Materials/Textures/babylon.multiRenderTarget.ts

@@ -13,10 +13,10 @@ module BABYLON {
         private _internalTextures: InternalTexture[];
         private _textures: Texture[];
         private _count: number;
+        private _engine: Engine;
 
         public get isSupported(): boolean {
-            var engine = this.getScene().getEngine();
-            return engine.webGLVersion > 1 || engine.getCaps().drawBuffersExtension;
+            return this._engine.webGLVersion > 1 || this._engine.getCaps().drawBuffersExtension;
         }
 
         private _multiRenderTargetOptions: IMultiRenderTargetOptions;
@@ -54,6 +54,8 @@ module BABYLON {
 
             super(name, size, scene, generateMipMaps, doNotChangeAspectRatio);
 
+            this._engine = scene.getEngine();            
+
             if (!this.isSupported) {
                 this.dispose();
                 return;
@@ -109,7 +111,7 @@ module BABYLON {
         }
 
         private _createInternalTextures(): void {
-            this._internalTextures = this.getScene().getEngine().createMultipleRenderTarget(this._size , this._multiRenderTargetOptions);
+            this._internalTextures = this._engine.createMultipleRenderTarget(this._size , this._multiRenderTargetOptions);
         }
 
         private _createTextures(): void {
@@ -134,13 +136,13 @@ module BABYLON {
             }
             
             for (var i = 0 ; i < this._internalTextures.length; i++) {
-                this._samples = this.getScene().getEngine().updateRenderTargetTextureSampleCount(this._internalTextures[i], value);
+                this._samples = this._engine.updateRenderTargetTextureSampleCount(this._internalTextures[i], value);
             }
         }
 
         public resize(size: any) {
             this.releaseInternalTextures();
-            this._internalTextures = this.getScene().getEngine().createMultipleRenderTarget(size, this._multiRenderTargetOptions);
+            this._internalTextures = this._engine.createMultipleRenderTarget(size, this._multiRenderTargetOptions);
             this._createInternalTextures();
         }
 

+ 3 - 2
src/Materials/Textures/babylon.rawTexture.ts

@@ -1,8 +1,9 @@
 module BABYLON {
     export class RawTexture extends Texture {
+        private _engine: Engine;
         constructor(data: ArrayBufferView, width: number, height: number, public format: number, scene: Scene, generateMipMaps: boolean = true, invertY: boolean = false, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE) {
             super(null, scene, !generateMipMaps, invertY);
-
+            this._engine = scene.getEngine();
             this._texture = scene.getEngine().createRawTexture(data, width, height, format, generateMipMaps, invertY, samplingMode);
 
             this.wrapU = Texture.CLAMP_ADDRESSMODE;
@@ -10,7 +11,7 @@
         }
 
         public update(data: ArrayBufferView): void {
-            this.getScene().getEngine().updateRawTexture(this._texture, data, this.format, this._invertY);
+            this._engine.updateRawTexture(this._texture, data, this.format, this._invertY);
         }
 
         // Statics

+ 10 - 2
src/Materials/Textures/babylon.refractionTexture.ts

@@ -24,8 +24,14 @@ module BABYLON {
         }
 
         public clone(): RefractionTexture {
+            let scene = this.getScene();
+
+            if (!scene) {
+                return this;
+            }
+
             var textureSize = this.getSize();
-            var newTexture = new RefractionTexture(this.name, textureSize.width, this.getScene(), this._generateMipMaps);
+            var newTexture = new RefractionTexture(this.name, textureSize.width, scene, this._generateMipMaps);
 
             // Base texture
             newTexture.hasAlpha = this.hasAlpha;
@@ -33,7 +39,9 @@ module BABYLON {
 
             // Refraction Texture
             newTexture.refractionPlane = this.refractionPlane.clone();
-            newTexture.renderList = this.renderList.slice(0);
+            if (this.renderList) {
+                newTexture.renderList = this.renderList.slice(0);
+            }
             newTexture.depth = this.depth;
 
             return newTexture;

+ 10 - 5
src/Materials/Textures/babylon.videoTexture.ts

@@ -6,6 +6,8 @@
         private _lastUpdate: number;
         private _generateMipMaps: boolean
         private _setTextureReady: () => void;
+        private _engine: Engine;
+
         /**
          * Creates a video texture.
          * Sample : https://doc.babylonjs.com/tutorials/01._Advanced_Texturing
@@ -18,7 +20,7 @@
         constructor(name: string, urlsOrVideo: string[] | HTMLVideoElement, scene: Scene, generateMipMaps = false, invertY = false, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE) {
             super(null, scene, !generateMipMaps, invertY);
 
-            var urls: string[];
+            var urls: Nullable<string[]> = null;
             this.name = name;
 
             if (urlsOrVideo instanceof HTMLVideoElement) {
@@ -31,10 +33,11 @@
                 this.video.loop = true;
             }
 
+            this._engine = (<Scene>this.getScene()).getEngine();
             this._generateMipMaps = generateMipMaps;
             this._samplingMode = samplingMode;
 
-            if (!this.getScene().getEngine().needPOTTextures ||(Tools.IsExponentOfTwo(this.video.videoWidth) && Tools.IsExponentOfTwo(this.video.videoHeight))) {
+            if (!this._engine.needPOTTextures ||(Tools.IsExponentOfTwo(this.video.videoWidth) && Tools.IsExponentOfTwo(this.video.videoHeight))) {
                 this.wrapU = Texture.WRAP_ADDRESSMODE;
                 this.wrapV = Texture.WRAP_ADDRESSMODE;
             } else {
@@ -60,11 +63,13 @@
         }
 
         private __setTextureReady(): void {
-            this._texture.isReady = true;
+            if (this._texture) {
+                this._texture.isReady = true;
+            }
         }
 
         private _createTexture(): void {
-            this._texture = this.getScene().getEngine().createDynamicTexture(this.video.videoWidth, this.video.videoHeight, this._generateMipMaps, this._samplingMode);
+            this._texture = this._engine.createDynamicTexture(this.video.videoWidth, this.video.videoHeight, this._generateMipMaps, this._samplingMode);
 
             if (this._autoLaunch) {
                 this._autoLaunch = false;
@@ -87,7 +92,7 @@
             }
 
             this._lastUpdate = now;
-            this.getScene().getEngine().updateVideoTexture(this._texture, this.video, this._invertY);
+            this._engine.updateVideoTexture(this._texture, this.video, this._invertY);
             return true;
         }
 

+ 2 - 1
src/Materials/babylon.imageProcessingConfiguration.ts

@@ -345,6 +345,7 @@ module BABYLON {
                 defines.IMAGEPROCESSINGPOSTPROCESS = this.applyByPostProcess;
                 return;
             }
+
             defines.VIGNETTE = this.vignetteEnabled;
             defines.VIGNETTEBLENDMODEMULTIPLY = (this.vignetteBlendMode === ImageProcessingConfiguration._VIGNETTEMODE_MULTIPLY);
             defines.VIGNETTEBLENDMODEOPAQUE = !defines.VIGNETTEBLENDMODEMULTIPLY;
@@ -353,7 +354,7 @@ module BABYLON {
             defines.EXPOSURE = (this.exposure !== 1.0);
             defines.COLORCURVES = (this.colorCurvesEnabled && !!this.colorCurves);
             defines.COLORGRADING = (this.colorGradingEnabled && !!this.colorGradingTexture);
-            defines.COLORGRADING3D = defines.COLORGRADING && (this.colorGradingTexture.getScene().getEngine().webGLVersion > 1);
+            defines.COLORGRADING3D = defines.COLORGRADING && ((<Scene>this.colorGradingTexture.getScene()).getEngine().webGLVersion > 1);
             defines.SAMPLER3DGREENDEPTH = this.colorGradingWithGreenDepth;
             defines.SAMPLER3DBGRMAP = this.colorGradingBGR;
             defines.IMAGEPROCESSINGPOSTPROCESS = this.applyByPostProcess;

+ 1 - 1
src/Materials/babylon.material.ts

@@ -462,7 +462,7 @@
             this._wasPreviouslyReady = false;
         }
 
-        public _preBind(effect?: Effect, overrideOrientation? : number): boolean {
+        public _preBind(effect?: Effect, overrideOrientation : Nullable<number> = null): boolean {
             var engine = this._scene.getEngine();
 
             var orientation = (overrideOrientation == null) ? this.sideOrientation : overrideOrientation;

+ 7 - 6
src/Materials/babylon.materialHelper.ts

@@ -99,8 +99,8 @@
             }
 
             if (useMorphTargets) {
-                if ((<any>mesh).morphTargetManager) {
-                    var manager = (<Mesh>mesh).morphTargetManager;
+                var manager = (<Mesh>mesh).morphTargetManager;
+                if (manager) {
                     defines["MORPHTARGETS_TANGENT"] = manager.supportsTangents && defines["TANGENT"];
                     defines["MORPHTARGETS_NORMAL"] = manager.supportsNormals && defines["NORMAL"] ;
                     defines["MORPHTARGETS"] = (manager.numInfluencers > 0);
@@ -303,8 +303,8 @@
             if (influencers > 0 && Engine.LastCreatedEngine) {
                 var maxAttributesCount = Engine.LastCreatedEngine.getCaps().maxVertexAttribs;
                 var manager = (<Mesh>mesh).morphTargetManager;
-                var normal = manager.supportsNormals && defines["NORMAL"];
-                var tangent = manager.supportsTangents && defines["TANGENT"];
+                var normal = manager && manager.supportsNormals && defines["NORMAL"];
+                var tangent = manager && manager.supportsTangents && defines["TANGENT"];
                 for (var index = 0; index < influencers; index++) {
                     attribs.push(VertexBuffer.PositionKind + index);
 
@@ -404,11 +404,12 @@
         }
 
         public static BindMorphTargetParameters(abstractMesh: AbstractMesh, effect: Effect): void {
-            if (!abstractMesh || !(<Mesh>abstractMesh).morphTargetManager) {
+            let manager = (<Mesh>abstractMesh).morphTargetManager;
+            if (!abstractMesh || !manager) {
                 return;
             }
 
-            effect.setFloatArray("morphTargetInfluences", (<Mesh>abstractMesh).morphTargetManager.influences);
+            effect.setFloatArray("morphTargetInfluences", manager.influences);
         }
 
         public static BindLogDepth(defines: any, effect: Effect, scene: Scene): void {

+ 1 - 1
src/Materials/babylon.pushMaterial.ts

@@ -36,7 +36,7 @@
             this.bindForSubMesh(world, mesh, mesh.subMeshes[0]);
         }
 
-        protected _afterBind(mesh: Mesh, effect?: Effect): void {
+        protected _afterBind(mesh: Mesh, effect: Nullable<Effect> = null): void {
             super._afterBind(mesh);
             this.getScene()._cachedEffect = effect;
         }

+ 4 - 1
src/Materials/babylon.standardMaterial.ts

@@ -885,7 +885,7 @@ module BABYLON {
                 this.buildUniformLayout();
             }
 
-            if (!subMesh.effect.isReady()) {
+            if (!subMesh.effect || !subMesh.effect.isReady()) {
                 return false;
             }
 
@@ -958,6 +958,9 @@ module BABYLON {
             }
 
             var effect = subMesh.effect;
+            if (!effect) {
+                return;
+            }
             this._activeEffect = effect;
 
             // Matrices        

+ 1 - 1
src/Materials/babylon.uniformBuffer.ts

@@ -577,7 +577,7 @@ module BABYLON {
          * @param {string} name Name of the sampler.
          * @param {Texture} texture
          */
-        public setTexture(name: string, texture: BaseTexture) {
+        public setTexture(name: string, texture: Nullable<BaseTexture>) {
             this._currentEffect.setTexture(name, texture);
         }
 

+ 11 - 13
src/Mesh/babylon.abstractMesh.ts

@@ -448,7 +448,7 @@
         }
 
         // Constructor
-        constructor(name: string, scene: Scene) {
+        constructor(name: string, scene: Nullable<Scene> = null) {
             super(name, scene);
 
             this.getScene().addMesh(this);
@@ -2077,8 +2077,6 @@
          * Returns the AbstractMesh.  
          */
         public setParent(mesh: Nullable<AbstractMesh>): AbstractMesh {
-
-            var child = this;
             var parent = (<AbstractMesh>mesh);
 
             if (mesh == null) {
@@ -2087,17 +2085,17 @@
                 var position = Tmp.Vector3[0];
                 var scale = Tmp.Vector3[1];
 
-                child.getWorldMatrix().decompose(scale, rotation, position);
+                this.getWorldMatrix().decompose(scale, rotation, position);
 
-                if (child.rotationQuaternion) {
-                    child.rotationQuaternion.copyFrom(rotation);
+                if (this.rotationQuaternion) {
+                    this.rotationQuaternion.copyFrom(rotation);
                 } else {
-                    rotation.toEulerAnglesToRef(child.rotation);
+                    rotation.toEulerAnglesToRef(this.rotation);
                 }
 
-                child.position.x = position.x;
-                child.position.y = position.y;
-                child.position.z = position.z;
+                this.position.x = position.x;
+                this.position.y = position.y;
+                this.position.z = position.z;
 
             } else {
 
@@ -2105,11 +2103,11 @@
                 var m1 = Tmp.Matrix[0];
 
                 parent.getWorldMatrix().invertToRef(m1);
-                Vector3.TransformCoordinatesToRef(child.position, m1, position);
+                Vector3.TransformCoordinatesToRef(this.position, m1, position);
 
-                child.position.copyFrom(position);
+                this.position.copyFrom(position);
             }
-            child.parent = parent;
+            this.parent = parent;
             return this;
         }
 

+ 4 - 4
src/Mesh/babylon.csg.ts

@@ -306,10 +306,10 @@
                 throw 'BABYLON.CSG: Wrong Mesh type, must be BABYLON.Mesh';
             }
 
-            var indices = mesh.getIndices(),
-                positions = mesh.getVerticesData(VertexBuffer.PositionKind),
-                normals = mesh.getVerticesData(VertexBuffer.NormalKind),
-                uvs = mesh.getVerticesData(VertexBuffer.UVKind);
+            var indices = <IndicesArray>mesh.getIndices(),
+                positions = <FloatArray>mesh.getVerticesData(VertexBuffer.PositionKind),
+                normals = <FloatArray>mesh.getVerticesData(VertexBuffer.NormalKind),
+                uvs = <FloatArray>mesh.getVerticesData(VertexBuffer.UVKind);
 
             var subMeshes = mesh.subMeshes;
 

+ 27 - 3
src/Mesh/babylon.geometry.ts

@@ -46,6 +46,14 @@
             this.updateBoundingInfo(true, null);
         }
 
+        public static CreateGeometryForMesh(mesh: Mesh): Geometry {
+            let geometry = new Geometry(Geometry.RandomId(), mesh.getScene());
+            
+            geometry.applyToMesh(mesh);
+
+            return geometry;
+        }
+
         constructor(id: string, scene: Scene, vertexData?: VertexData, updatable: boolean = false, mesh: Nullable<Mesh> = null) {
             this.id = id;
             this._engine = scene.getEngine();
@@ -151,7 +159,7 @@
             this._vertexBuffers[kind] = buffer;
 
             if (kind === VertexBuffer.PositionKind) {
-                var data = buffer.getData();
+                var data = <FloatArray>buffer.getData();
                 var stride = buffer.getStrideSize();
 
                 this._totalVertices = data.length / stride;
@@ -271,7 +279,7 @@
             if (!vertexBuffer) {
                 return null;
             }
-            var orig = vertexBuffer.getData();
+            var orig = <FloatArray>vertexBuffer.getData();
             if (!forceCopy && (!copyWhenShared || this._meshes.length === 1)) {
                 return orig;
             } else {
@@ -284,6 +292,22 @@
             }
         }
 
+        /**
+         * Returns a boolean defining if the vertex data for the requested `kind` is updatable.
+         * Possible `kind` values :
+         * - BABYLON.VertexBuffer.PositionKind
+         * - BABYLON.VertexBuffer.UVKind
+         * - BABYLON.VertexBuffer.UV2Kind
+         * - BABYLON.VertexBuffer.UV3Kind
+         * - BABYLON.VertexBuffer.UV4Kind
+         * - BABYLON.VertexBuffer.UV5Kind
+         * - BABYLON.VertexBuffer.UV6Kind
+         * - BABYLON.VertexBuffer.ColorKind
+         * - BABYLON.VertexBuffer.MatricesIndicesKind
+         * - BABYLON.VertexBuffer.MatricesIndicesExtraKind
+         * - BABYLON.VertexBuffer.MatricesWeightsKind
+         * - BABYLON.VertexBuffer.MatricesWeightsExtraKind
+         */        
         public isVertexBufferUpdatable(kind: string): boolean {
             let vb = this._vertexBuffers[kind];
 
@@ -460,7 +484,7 @@
 
         private updateExtend(data: Nullable<FloatArray> = null, stride? : number) {
             if (!data) {
-                data = this._vertexBuffers[VertexBuffer.PositionKind].getData();
+                data = <FloatArray>this._vertexBuffers[VertexBuffer.PositionKind].getData();
             }
 
             this._extend = Tools.ExtractMinAndMax(data, 0, this._totalVertices, this.boundingBias, stride);

+ 5 - 0
src/Mesh/babylon.groundMesh.ts

@@ -161,6 +161,11 @@ module BABYLON {
         // Returns the GroundMesh.  
         private _computeHeightQuads(): GroundMesh {
             var positions = this.getVerticesData(VertexBuffer.PositionKind);
+
+            if (!positions) {
+                return this;
+            }
+
             var v1 = Tmp.Vector3[3];
             var v2 = Tmp.Vector3[2];
             var v3 = Tmp.Vector3[1];

+ 1 - 1
src/Mesh/babylon.instancedMesh.ts

@@ -167,7 +167,7 @@
             return this._sourceMesh.getIndices();
         }
 
-        public get _positions(): Vector3[] {
+        public get _positions(): Nullable<Vector3[]> {
             return this._sourceMesh._positions;
         }
 

+ 2 - 2
src/Mesh/babylon.linesMesh.ts

@@ -34,7 +34,7 @@ module BABYLON {
         private _intersectionThreshold: number;
         private _colorShader: ShaderMaterial;
 
-        constructor(name: string, scene: Scene, parent: Nullable<Node> = null, source?: LinesMesh, doNotCloneChildren?: boolean, public useVertexColor? : boolean) {
+        constructor(name: string, scene: Nullable<Scene> = null, parent: Nullable<Node> = null, source?: LinesMesh, doNotCloneChildren?: boolean, public useVertexColor? : boolean) {
             super(name, scene, parent, source, doNotCloneChildren);
 
             if (source) {
@@ -56,7 +56,7 @@ module BABYLON {
                 options.needAlphaBlending = true;
             }
 
-            this._colorShader = new ShaderMaterial("colorShader", scene, "color", options);
+            this._colorShader = new ShaderMaterial("colorShader", this.getScene(), "color", options);
         }
 
         /**

+ 175 - 68
src/Mesh/babylon.mesh.ts

@@ -1,7 +1,7 @@
 module BABYLON {
     export class _InstancesBatch {
         public mustReturn = false;
-        public visibleInstances = new Array<Array<InstancedMesh>>();
+        public visibleInstances = new Array<Nullable<Array<InstancedMesh>>>();
         public renderSelf = new Array<boolean>();
     }
 
@@ -101,13 +101,13 @@
         public onLODLevelSelection: (distance: number, mesh: Mesh, selectedLevel: Mesh) => void;
 
         // Morph
-        private _morphTargetManager: MorphTargetManager;
+        private _morphTargetManager: Nullable<MorphTargetManager>;
 
-        public get morphTargetManager(): MorphTargetManager {
+        public get morphTargetManager(): Nullable<MorphTargetManager> {
             return this._morphTargetManager;
         }
 
-        public set morphTargetManager(value: MorphTargetManager) {
+        public set morphTargetManager(value: Nullable<MorphTargetManager>) {
             if (this._morphTargetManager === value) {
                 return;
             }
@@ -124,7 +124,7 @@
         private _renderIdForInstances = new Array<number>();
         private _batchCache = new _InstancesBatch();
         private _instancesBufferSize = 32 * 16 * 4; // let's start with a maximum of 32 instances
-        private _instancesBuffer: Buffer;
+        private _instancesBuffer: Nullable<Buffer>;
         private _instancesData: Float32Array;
         private _overridenInstanceCount: number;
 
@@ -160,9 +160,11 @@
          *                  This will make creation of children, recursive.
          * @param {boolean} clonePhysicsImpostor When cloning, include cloning mesh physics impostor, default True.
          */
-        constructor(name: string, scene: Scene, parent: Nullable<Node> = null, source?: Mesh, doNotCloneChildren?: boolean, clonePhysicsImpostor: boolean = true) {
+        constructor(name: string, scene: Nullable<Scene> = null, parent: Nullable<Node> = null, source: Nullable<Mesh> = null, doNotCloneChildren?: boolean, clonePhysicsImpostor: boolean = true) {
             super(name, scene);
 
+            scene = this.getScene();
+
             if (source) {
                 // Source mesh
                 this._source = source;
@@ -492,6 +494,33 @@
             }
             return this._geometry.isVerticesDataPresent(kind);
         }
+
+
+        /**
+         * Returns a boolean defining if the vertex data for the requested `kind` is updatable.
+         * Possible `kind` values :
+         * - BABYLON.VertexBuffer.PositionKind
+         * - BABYLON.VertexBuffer.UVKind
+         * - BABYLON.VertexBuffer.UV2Kind
+         * - BABYLON.VertexBuffer.UV3Kind
+         * - BABYLON.VertexBuffer.UV4Kind
+         * - BABYLON.VertexBuffer.UV5Kind
+         * - BABYLON.VertexBuffer.UV6Kind
+         * - BABYLON.VertexBuffer.ColorKind
+         * - BABYLON.VertexBuffer.MatricesIndicesKind
+         * - BABYLON.VertexBuffer.MatricesIndicesExtraKind
+         * - BABYLON.VertexBuffer.MatricesWeightsKind
+         * - BABYLON.VertexBuffer.MatricesWeightsExtraKind
+         */
+        public isVertexBufferUpdatable(kind: string): boolean {
+            if (!this._geometry) {
+                if (this._delayInfo) {
+                    return this._delayInfo.indexOf(kind) !== -1;
+                }
+                return false;
+            }
+            return this._geometry.isVertexBufferUpdatable(kind);
+        }        
         /**
          * Returns a string : the list of existing `kinds` of Vertex Data for this mesh.  
          * Possible `kind` values :
@@ -783,9 +812,7 @@
          */
         public setVerticesBuffer(buffer: VertexBuffer): Mesh {
             if (!this._geometry) {
-                var scene = this.getScene();
-
-                new Geometry(Geometry.RandomId(), scene).applyToMesh(this);
+                this._geometry = Geometry.CreateGeometryForMesh(this);
             }
 
             this._geometry.setVerticesBuffer(buffer);
@@ -839,11 +866,21 @@
          */
         public updateMeshPositions(positionFunction: (data: FloatArray) => void, computeNormals: boolean = true): Mesh {
             var positions = this.getVerticesData(VertexBuffer.PositionKind);
+            if (!positions) {
+                return this;
+            }
+
             positionFunction(positions);
             this.updateVerticesData(VertexBuffer.PositionKind, positions, false, false);
+
             if (computeNormals) {
                 var indices = this.getIndices();
                 var normals = this.getVerticesData(VertexBuffer.NormalKind);
+
+                if (!normals) {
+                    return this;
+                }
+
                 VertexData.ComputeNormals(positions, indices, normals);
                 this.updateVerticesData(VertexBuffer.NormalKind, normals, false, false);
             }
@@ -915,6 +952,10 @@
         }
 
         public _bind(subMesh: SubMesh, effect: Effect, fillMode: number): Mesh {
+            if (!this._geometry) {
+                return this;
+            }
+
             var engine = this.getScene().getEngine();
 
             // Wireframe
@@ -928,7 +969,7 @@
                         indexToBind = null;
                         break;
                     case Material.WireFrameFillMode:
-                        indexToBind = subMesh.getLinesIndexBuffer(this.getIndices(), engine);
+                        indexToBind = subMesh.getLinesIndexBuffer(<IndicesArray>this.getIndices(), engine);
                         break;
                     default:
                     case Material.TriangleFillMode:
@@ -975,6 +1016,9 @@
 
             if (scene._isAlternateRenderingEnabled && !alternate) {
                 let effect = subMesh.effect || this._effectiveMaterial.getEffect();
+                if (!effect || !scene.activeCamera) {
+                    return this;
+                }
                 scene._switchToAlternateCameraConfiguration(true);
                 this._effectiveMaterial.bindView(effect);
                 this._effectiveMaterial.bindViewProjection(effect);
@@ -1048,7 +1092,8 @@
                     selfRenderId = Math.max(this._visibleInstances.selfDefaultRenderId, currentRenderId);
                 }
 
-                if (this._batchCache.visibleInstances[subMeshId] && this._batchCache.visibleInstances[subMeshId].length) {
+                let visibleInstancesForSubMesh = this._batchCache.visibleInstances[subMeshId];
+                if (visibleInstancesForSubMesh && visibleInstancesForSubMesh.length) {
                     if (this._renderIdForInstances[subMeshId] === currentRenderId) {
                         this._batchCache.mustReturn = true;
                         return this._batchCache;
@@ -1067,6 +1112,10 @@
 
         public _renderWithInstances(subMesh: SubMesh, fillMode: number, batch: _InstancesBatch, effect: Effect, engine: Engine): Mesh {
             var visibleInstances = batch.visibleInstances[subMesh._id];
+            if (!visibleInstances) {
+                return this;
+            }
+
             var matricesCount = visibleInstances.length + 1;
             var bufferSize = matricesCount * 16 * 4;
 
@@ -1140,9 +1189,11 @@
                     this._draw(subMesh, fillMode, this._overridenInstanceCount);
                 }
 
-                if (batch.visibleInstances[subMesh._id]) {
-                    for (var instanceIndex = 0; instanceIndex < batch.visibleInstances[subMesh._id].length; instanceIndex++) {
-                        var instance = batch.visibleInstances[subMesh._id][instanceIndex];
+                let visibleInstancesForSubMesh = batch.visibleInstances[subMesh._id];
+
+                if (visibleInstancesForSubMesh) {                
+                    for (var instanceIndex = 0; instanceIndex < visibleInstancesForSubMesh.length; instanceIndex++) {
+                        var instance = visibleInstancesForSubMesh[instanceIndex];
 
                         // World
                         var world = instance.getWorldMatrix();
@@ -1190,12 +1241,14 @@
             var hardwareInstancedRendering = (engine.getCaps().instancedArrays) && (batch.visibleInstances[subMesh._id] !== null) && (batch.visibleInstances[subMesh._id] !== undefined);
 
             // Material
-            this._effectiveMaterial = subMesh.getMaterial();
+            let material = subMesh.getMaterial();
 
-            if (!this._effectiveMaterial) {
+            if (!material) {
                 return this;
             }
 
+            this._effectiveMaterial = material
+
             if (this._effectiveMaterial.storeEffectOnSubMeshes) {
                 if (!this._effectiveMaterial.isReadyForSubMesh(this, subMesh, hardwareInstancedRendering)) {
                     return this;
@@ -1217,13 +1270,17 @@
                 engine.setDepthWrite(savedDepthWrite);
             }
 
-            var effect: Effect;
+            var effect: Nullable<Effect>;
             if (this._effectiveMaterial.storeEffectOnSubMeshes) {
                 effect = subMesh.effect;
             } else {
                 effect = this._effectiveMaterial.getEffect();
             }
 
+            if (!effect) {
+                return this;
+            }
+
             var reverse = this._effectiveMaterial._preBind(effect, this.overrideMaterialSideOrientation);
 
             if (this._effectiveMaterial.forceDepthWrite) {
@@ -1435,25 +1492,26 @@
 
             this._resetPointsArrayCache();
 
-            var data = this.getVerticesData(VertexBuffer.PositionKind);
+            var data = <FloatArray>this.getVerticesData(VertexBuffer.PositionKind);
+
             var temp = new Array<number>();
             var index: number;
             for (index = 0; index < data.length; index += 3) {
                 Vector3.TransformCoordinates(Vector3.FromArray(data, index), transform).toArray(temp, index);
             }
 
-            this.setVerticesData(VertexBuffer.PositionKind, temp, this.getVertexBuffer(VertexBuffer.PositionKind).isUpdatable());
+            this.setVerticesData(VertexBuffer.PositionKind, temp, (<VertexBuffer>this.getVertexBuffer(VertexBuffer.PositionKind)).isUpdatable());
 
             // Normals
             if (!this.isVerticesDataPresent(VertexBuffer.NormalKind)) {
                 return this;
             }
-            data = this.getVerticesData(VertexBuffer.NormalKind);
+            data = <FloatArray>this.getVerticesData(VertexBuffer.NormalKind);
             temp = [];
             for (index = 0; index < data.length; index += 3) {
                 Vector3.TransformNormal(Vector3.FromArray(data, index), transform).normalize().toArray(temp, index);
             }
-            this.setVerticesData(VertexBuffer.NormalKind, temp, this.getVertexBuffer(VertexBuffer.NormalKind).isUpdatable());
+            this.setVerticesData(VertexBuffer.NormalKind, temp, (<VertexBuffer>this.getVertexBuffer(VertexBuffer.NormalKind)).isUpdatable());
 
             // flip faces?
             if (transform.m[0] * transform.m[5] * transform.m[10] < 0) { this.flipFaces(); }
@@ -1486,7 +1544,7 @@
         }
 
         // Cache
-        public get _positions(): Vector3[] {
+        public get _positions(): Nullable<Vector3[]> {
             if (this._geometry) {
                 return this._geometry._positions;
             }
@@ -1524,7 +1582,7 @@
          * This also frees the memory allocated under the hood to all the buffers used by WebGL.
          */
         public dispose(doNotRecurse?: boolean): void {
-            this.morphTargetManager = undefined;
+            this.morphTargetManager = null;
 
             if (this._geometry) {
                 this._geometry.releaseForMesh(this, true);
@@ -1580,7 +1638,7 @@
             var onload = (img: HTMLImageElement) => {
                 // Getting height map data
                 var canvas = document.createElement("canvas");
-                var context = canvas.getContext("2d");
+                var context = <CanvasRenderingContext2D>canvas.getContext("2d");
                 var heightMapWidth = img.width;
                 var heightMapHeight = img.height;
                 canvas.width = heightMapWidth;
@@ -1624,9 +1682,9 @@
                 return this;
             }
 
-            var positions = this.getVerticesData(VertexBuffer.PositionKind);
-            var normals = this.getVerticesData(VertexBuffer.NormalKind);
-            var uvs = this.getVerticesData(VertexBuffer.UVKind);
+            var positions = <FloatArray>this.getVerticesData(VertexBuffer.PositionKind);
+            var normals = <FloatArray>this.getVerticesData(VertexBuffer.NormalKind);
+            var uvs = <number[]>this.getVerticesData(VertexBuffer.UVKind);
             var position = Vector3.Zero();
             var normal = Vector3.Zero();
             var uv = Vector2.Zero();
@@ -1683,7 +1741,7 @@
             var kind: string;
             for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
                 kind = kinds[kindIndex];
-                var vertexBuffer = this.getVertexBuffer(kind);
+                var vertexBuffer = <VertexBuffer>this.getVertexBuffer(kind);
 
                 if (kind === VertexBuffer.NormalKind) {
                     updatableNormals = vertexBuffer.isUpdatable();
@@ -1693,14 +1751,14 @@
                 }
 
                 vbs[kind] = vertexBuffer;
-                data[kind] = vbs[kind].getData();
+                data[kind] = <FloatArray>vbs[kind].getData();
                 newdata[kind] = [];
             }
 
             // Save previous submeshes
             var previousSubmeshes = this.subMeshes.slice(0);
 
-            var indices = this.getIndices();
+            var indices = <IndicesArray>this.getIndices();
             var totalIndices = this.getTotalIndices();
 
             // Generating unique vertices per face
@@ -1781,16 +1839,16 @@
             var kind: string;
             for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
                 kind = kinds[kindIndex];
-                var vertexBuffer = this.getVertexBuffer(kind);
+                var vertexBuffer = <VertexBuffer>this.getVertexBuffer(kind);
                 vbs[kind] = vertexBuffer;
-                data[kind] = vbs[kind].getData();
+                data[kind] = <FloatArray>vbs[kind].getData();
                 newdata[kind] = [];
             }
 
             // Save previous submeshes
             var previousSubmeshes = this.subMeshes.slice(0);
 
-            var indices = this.getIndices();
+            var indices = <IndicesArray>this.getIndices();
             var totalIndices = this.getTotalIndices();
 
             // Generating unique vertices per face
@@ -1844,18 +1902,20 @@
         public flipFaces(flipNormals: boolean = false): Mesh {
             var vertex_data = VertexData.ExtractFromMesh(this);
             var i: number;
-            if (flipNormals && this.isVerticesDataPresent(VertexBuffer.NormalKind)) {
+            if (flipNormals && this.isVerticesDataPresent(VertexBuffer.NormalKind) && vertex_data.normals) {
                 for (i = 0; i < vertex_data.normals.length; i++) {
                     vertex_data.normals[i] *= -1;
                 }
             }
 
-            var temp;
-            for (i = 0; i < vertex_data.indices.length; i += 3) {
-                // reassign indices
-                temp = vertex_data.indices[i + 1];
-                vertex_data.indices[i + 1] = vertex_data.indices[i + 2];
-                vertex_data.indices[i + 2] = temp;
+            if (vertex_data.indices) {
+                var temp;
+                for (i = 0; i < vertex_data.indices.length; i += 3) {
+                    // reassign indices
+                    temp = vertex_data.indices[i + 1];
+                    vertex_data.indices[i + 1] = vertex_data.indices[i + 2];
+                    vertex_data.indices[i + 2] = temp;
+                }
             }
 
             vertex_data.applyToMesh(this);
@@ -1919,8 +1979,13 @@
          * @param successCallback an optional success callback to be called after the optimization finished.   
          */
         public optimizeIndices(successCallback?: (mesh?: Mesh) => void): Mesh {
-            var indices = this.getIndices();
+            var indices = <IndicesArray>this.getIndices();
             var positions = this.getVerticesData(VertexBuffer.PositionKind);
+
+            if (!positions || !indices) {
+                return this;
+            }
+
             var vectorPositions = new Array<Vector3>();
             for (var pos = 0; pos < positions.length; pos = pos + 3) {
                 vectorPositions.push(Vector3.FromArray(positions, pos));
@@ -2031,12 +2096,13 @@
 
             // Physics
             //TODO implement correct serialization for physics impostors.
-            if (this.getPhysicsImpostor()) {
-                var impostor = this.getPhysicsImpostor();
+            
+            let impostor = this.getPhysicsImpostor();
+            if (impostor) {
                 serializationObject.physicsMass = impostor.getParam("mass");
                 serializationObject.physicsFriction = impostor.getParam("friction");
                 serializationObject.physicsRestitution = impostor.getParam("mass");
-                serializationObject.physicsImpostor = this.getPhysicsImpostor().type;
+                serializationObject.physicsImpostor = impostor.type;
             }
 
             // Metadata
@@ -2099,15 +2165,16 @@
 
             this._markSubMeshesAsAttributesDirty();
 
-            if (this._morphTargetManager && this._morphTargetManager.vertexCount) {
-                if (this._morphTargetManager.vertexCount !== this.getTotalVertices()) {
+            let morphTargetManager = this._morphTargetManager;
+            if (morphTargetManager && morphTargetManager.vertexCount) {
+                if (morphTargetManager.vertexCount !== this.getTotalVertices()) {
                     Tools.Error("Mesh is incompatible with morph targets. Targets and mesh must all have the same vertices count.");
-                    this.morphTargetManager = undefined;
+                    this.morphTargetManager = null;
                     return;
                 }
 
-                for (var index = 0; index < this.morphTargetManager.numInfluencers; index++) {
-                    var morphTarget = this.morphTargetManager.getActiveTarget(index);
+                for (var index = 0; index < morphTargetManager.numInfluencers; index++) {
+                    var morphTarget = morphTargetManager.getActiveTarget(index);
                     this.geometry.setVerticesData(VertexBuffer.PositionKind + index, morphTarget.getPositions(), false, 3);
 
                     if (morphTarget.hasNormals) {
@@ -2404,7 +2471,7 @@
          * Detail here : http://doc.babylonjs.com/tutorials/02._Discover_Basic_Elements#side-orientation    
          * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.  
          */
-        public static CreateRibbon(name: string, pathArray: Vector3[][], closeArray: boolean, closePath: boolean, offset: number, scene?: Scene, updatable?: boolean, sideOrientation?: number, instance?: Mesh): Mesh {
+        public static CreateRibbon(name: string, pathArray: Vector3[][], closeArray: boolean = false, closePath: boolean, offset: number, scene?: Scene, updatable: boolean = false, sideOrientation?: number, instance?: Mesh): Mesh {
             return MeshBuilder.CreateRibbon(name, {
                 pathArray: pathArray,
                 closeArray: closeArray,
@@ -2424,7 +2491,7 @@
          * Detail here : http://doc.babylonjs.com/tutorials/02._Discover_Basic_Elements#side-orientation    
          * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.  
          */
-        public static CreateDisc(name: string, radius: number, tessellation: number, scene?: Scene, updatable?: boolean, sideOrientation?: number): Mesh {
+        public static CreateDisc(name: string, radius: number, tessellation: number, scene: Nullable<Scene> = null, updatable?: boolean, sideOrientation?: number): Mesh {
             var options = {
                 radius: radius,
                 tessellation: tessellation,
@@ -2442,7 +2509,7 @@
          * Detail here : http://doc.babylonjs.com/tutorials/02._Discover_Basic_Elements#side-orientation    
          * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.  
          */
-        public static CreateBox(name: string, size: number, scene?: Scene, updatable?: boolean, sideOrientation?: number): Mesh {
+        public static CreateBox(name: string, size: number, scene: Nullable<Scene> = null, updatable?: boolean, sideOrientation?: number): Mesh {
             var options = {
                 size: size,
                 sideOrientation: sideOrientation,
@@ -2588,7 +2655,7 @@
          * When updating an instance, remember that only point positions can change, not the number of points.      
          * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.  
          */
-        public static CreateDashedLines(name: string, points: Vector3[], dashSize: number, gapSize: number, dashNb: number, scene?: Scene, updatable?: boolean, instance?: LinesMesh): LinesMesh {
+        public static CreateDashedLines(name: string, points: Vector3[], dashSize: number, gapSize: number, dashNb: number, scene: Nullable<Scene> = null, updatable?: boolean, instance?: LinesMesh): LinesMesh {
             var options = {
                 points: points,
                 dashSize: dashSize,
@@ -2652,7 +2719,7 @@
          * Detail here : http://doc.babylonjs.com/tutorials/02._Discover_Basic_Elements#side-orientation    
          * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.  
          */
-        public static ExtrudeShape(name: string, shape: Vector3[], path: Vector3[], scale: number, rotation: number, cap: number, scene?: Scene, updatable?: boolean, sideOrientation?: number, instance?: Mesh): Mesh {
+        public static ExtrudeShape(name: string, shape: Vector3[], path: Vector3[], scale: number, rotation: number, cap: number, scene: Nullable<Scene> = null, updatable?: boolean, sideOrientation?: number, instance?: Mesh): Mesh {
             var options = {
                 shape: shape,
                 path: path,
@@ -2925,13 +2992,15 @@
          * @returns original positions used for CPU skinning.  Useful for integrating Morphing with skeletons in same mesh.
          */
         public setPositionsForCPUSkinning(): Float32Array {
-            var source: FloatArray;
             if (!this._sourcePositions) {
-                source = this.getVerticesData(VertexBuffer.PositionKind);
+                let source = this.getVerticesData(VertexBuffer.PositionKind);
+                if (!source) {
+                    return this._sourcePositions;
+                }
 
                 this._sourcePositions = new Float32Array(<any>source);
 
-                if (!this.getVertexBuffer(VertexBuffer.PositionKind).isUpdatable()) {
+                if (!this.isVertexBufferUpdatable(VertexBuffer.PositionKind)) {
                     this.setVerticesData(VertexBuffer.PositionKind, source, true);
                 }
             }
@@ -2942,13 +3011,16 @@
          * @returns original normals used for CPU skinning.  Useful for integrating Morphing with skeletons in same mesh.
          */
         public setNormalsForCPUSkinning(): Float32Array {
-            var source: FloatArray;
             if (!this._sourceNormals) {
-                source = this.getVerticesData(VertexBuffer.NormalKind);
+                let source = this.getVerticesData(VertexBuffer.NormalKind);
+
+                if (!source) {
+                    return this._sourceNormals;
+                }
 
                 this._sourceNormals = new Float32Array(<any>source);
 
-                if (!this.getVertexBuffer(VertexBuffer.NormalKind).isUpdatable()) {
+                if (!this.isVertexBufferUpdatable(VertexBuffer.NormalKind)) {
                     this.setVerticesData(VertexBuffer.NormalKind, source, true);
                 }
             }
@@ -2997,23 +3069,41 @@
 
             // positionsData checks for not being Float32Array will only pass at most once
             var positionsData = this.getVerticesData(VertexBuffer.PositionKind);
+
+            if (!positionsData) {
+                return this;
+            }
+
             if (!(positionsData instanceof Float32Array)) {
                 positionsData = new Float32Array(positionsData);
             }
 
             // normalsData checks for not being Float32Array will only pass at most once
             var normalsData = this.getVerticesData(VertexBuffer.NormalKind);
+
+            if (!normalsData) {
+                return this;
+            }
+
             if (!(normalsData instanceof Float32Array)) {
                 normalsData = new Float32Array(normalsData);
             }
 
             var matricesIndicesData = this.getVerticesData(VertexBuffer.MatricesIndicesKind);
             var matricesWeightsData = this.getVerticesData(VertexBuffer.MatricesWeightsKind);
+            
+            if (!matricesWeightsData || !matricesIndicesData) {
+                return this;
+            }
 
             var needExtras = this.numBoneInfluencers > 4;
             var matricesIndicesExtraData = needExtras ? this.getVerticesData(VertexBuffer.MatricesIndicesExtraKind) : null;
             var matricesWeightsExtraData = needExtras ? this.getVerticesData(VertexBuffer.MatricesWeightsExtraKind) : null;
 
+            if (!matricesWeightsExtraData || !matricesIndicesExtraData) {
+                return this;
+            }            
+
             var skeletonMatrices = skeleton.getTransformMatrices(this);
 
             var tempVector3 = Vector3.Zero();
@@ -3064,11 +3154,18 @@
          * This min and max Vector3 are the minimum and maximum vectors of each mesh bounding box from the passed array, in the World system
          */
         public static MinMax(meshes: AbstractMesh[]): { min: Vector3; max: Vector3 } {
-            var minVector: Vector3 = null;
-            var maxVector: Vector3 = null;
+            var minVector: Nullable<Vector3> = null;
+            var maxVector: Nullable<Vector3> = null;
+            
             meshes.forEach(function (mesh, index, array) {
-                var boundingBox = mesh.getBoundingInfo().boundingBox;
-                if (!minVector) {
+                let boundingInfo = mesh.getBoundingInfo();
+
+                if (!boundingInfo) {
+                    return;
+                }
+
+                let boundingBox = boundingInfo.boundingBox;
+                if (!minVector || !maxVector) {
                     minVector = boundingBox.minimumWorld;
                     maxVector = boundingBox.maximumWorld;
                 } else {
@@ -3077,6 +3174,13 @@
                 }
             });
 
+            if (!minVector || !maxVector) {
+                return {
+                    min: Vector3.Zero(),
+                    max: Vector3.Zero()
+                }
+            }
+
             return {
                 min: minVector,
                 max: maxVector
@@ -3098,7 +3202,7 @@
          * @param {Mesh} meshSubclass - When set, vertices inserted into this Mesh.  Meshes can then be merged into a Mesh sub-class.
          * @param {boolean} subdivideWithSubMeshes - When true (false default), subdivide mesh to his subMesh array with meshes source.
          */
-        public static MergeMeshes(meshes: Array<Mesh>, disposeSource = true, allow32BitsIndices?: boolean, meshSubclass?: Mesh, subdivideWithSubMeshes?: boolean): Mesh {
+        public static MergeMeshes(meshes: Array<Mesh>, disposeSource = true, allow32BitsIndices?: boolean, meshSubclass?: Mesh, subdivideWithSubMeshes?: boolean): Nullable<Mesh> {
             var index: number;
             if (!allow32BitsIndices) {
                 var totalVertices = 0;
@@ -3117,10 +3221,10 @@
             }
 
             // Merge
-            var vertexData: VertexData;
+            var vertexData: Nullable<VertexData> = null;
             var otherVertexData: VertexData;
             var indiceArray: Array<number> = new Array<number>();
-            var source: Mesh;
+            var source: Nullable<Mesh> = null;
             for (index = 0; index < meshes.length; index++) {
                 if (meshes[index]) {
                     meshes[index].computeWorldMatrix(true);
@@ -3140,10 +3244,13 @@
                 }
             }
 
+            source = <Mesh>source;
+
             if (!meshSubclass) {
                 meshSubclass = new Mesh(source.name + "_merged", source.getScene());
             }
-            vertexData.applyToMesh(meshSubclass);
+
+            (<VertexData>vertexData).applyToMesh(meshSubclass);
 
             // Setting properties
             meshSubclass.material = source.material;

+ 84 - 70
src/Mesh/babylon.mesh.vertexData.ts

@@ -1,6 +1,4 @@
 module BABYLON {
-    export type IndicesArray = number[] | Int32Array | Uint32Array | Uint16Array;
-
     export interface IGetSetVerticesData
     {
         isVerticesDataPresent(kind: string): boolean;
@@ -12,21 +10,21 @@
     }
 
     export class VertexData {
-        public positions: FloatArray;
-        public normals: FloatArray;
-        public tangents: FloatArray;
-        public uvs: FloatArray;
-        public uvs2: FloatArray;
-        public uvs3: FloatArray;
-        public uvs4: FloatArray;
-        public uvs5: FloatArray;
-        public uvs6: FloatArray;
-        public colors: FloatArray;
-        public matricesIndices: FloatArray;
-        public matricesWeights: FloatArray;
-        public matricesIndicesExtra: FloatArray;
-        public matricesWeightsExtra: FloatArray;
-        public indices: IndicesArray;
+        public positions: Nullable<FloatArray>;
+        public normals: Nullable<FloatArray>;
+        public tangents: Nullable<FloatArray>;
+        public uvs: Nullable<FloatArray>;
+        public uvs2: Nullable<FloatArray>;
+        public uvs3: Nullable<FloatArray>;
+        public uvs4: Nullable<FloatArray>;
+        public uvs5: Nullable<FloatArray>;
+        public uvs6: Nullable<FloatArray>;
+        public colors: Nullable<FloatArray>;
+        public matricesIndices: Nullable<FloatArray>;
+        public matricesWeights: Nullable<FloatArray>;
+        public matricesIndicesExtra: Nullable<FloatArray>;
+        public matricesWeightsExtra: Nullable<FloatArray>;
+        public indices: Nullable<IndicesArray>;
 
         public set(data: FloatArray, kind: string) {
             switch (kind) {
@@ -113,7 +111,7 @@
             return this;
         }
 
-        private _applyTo(meshOrGeometry: IGetSetVerticesData, updatable?: boolean): VertexData {
+        private _applyTo(meshOrGeometry: IGetSetVerticesData, updatable: boolean = false): VertexData {
             if (this.positions) {
                 meshOrGeometry.setVerticesData(VertexBuffer.PositionKind, this.positions, updatable);
             }
@@ -311,6 +309,10 @@
 
             this.positions = this._mergeElement(this.positions, other.positions);
 
+            if (!this.positions) {
+                return this;
+            }
+
             var count = this.positions.length / 3;
 
             this.normals = this._mergeElement(this.normals, other.normals, count * 3);
@@ -329,13 +331,13 @@
             return this;
         }
 
-        private _mergeElement(source: FloatArray, other: FloatArray, length = 0): FloatArray {
+        private _mergeElement(source: Nullable<FloatArray>, other: Nullable<FloatArray>, length = 0): Nullable<FloatArray> {
             if (!other && !source) {
                 return null;
             }
 
             if (!other) {
-                return this._mergeElement(source, new Float32Array(source.length), length);
+                return this._mergeElement(source, new Float32Array((<FloatArray>source).length), length);
             }
 
             if (!source) {
@@ -606,8 +608,8 @@
             // vertical distances (v)
             var path1: Vector3[];
             var path2: Vector3[];
-            var vertex1: Vector3;
-            var vertex2: Vector3;
+            var vertex1: Nullable<Vector3> = null;
+            var vertex2: Nullable<Vector3> = null;
             for (i = 0; i < minlg + closePathCorr; i++) {
                 vTotalDistance[i] = 0;
                 vs[i] = [0];
@@ -627,7 +629,8 @@
                     vs[i].push(dist);
                     vTotalDistance[i] = dist;
                 }
-                if (closeArray) {
+
+                if (closeArray && vertex2 && vertex1) {
                     path1 = pathArray[p];
                     path2 = pathArray[0];
                     if (i === minlg) {   // closePath
@@ -721,8 +724,9 @@
             VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
 
             // Colors
+            let colors: Nullable<Float32Array> = null;
             if (customColors) {
-                var colors = new Float32Array(customColors.length * 4);
+                colors = new Float32Array(customColors.length * 4);
                 for (var c = 0; c < customColors.length; c++) {
                     colors[c * 4] = customColors[c].r;
                     colors[c * 4 + 1] = customColors[c].g;
@@ -741,7 +745,7 @@
             vertexData.positions = positions32;
             vertexData.normals = normals32;
             vertexData.uvs = uvs32;
-            if (customColors) {
+            if (colors) {
                 vertexData.set(colors, VertexBuffer.ColorKind);
             }
 
@@ -775,7 +779,7 @@
             var depth = options.depth || options.size || 1;
             var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
             var faceUV: Vector4[] = options.faceUV || new Array<Vector4>(6);
-            var faceColors: Color4[] = options.faceColors;
+            var faceColors = options.faceColors;
             var colors = [];
 
             // default face colors and UV if undefined
@@ -869,8 +873,8 @@
             var diameterX: number = options.diameterX || options.diameter || 1;
             var diameterY: number = options.diameterY || options.diameter || 1;
             var diameterZ: number = options.diameterZ || options.diameter || 1;
-            var arc: number = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
-            var slice: number = (options.slice <= 0) ? 1.0 : options.slice || 1.0;
+            var arc: number = options.arc && (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
+            var slice: number = options.slice && (options.slice <= 0) ? 1.0 : options.slice || 1.0;
             var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
 
             var radius = new Vector3(diameterX / 2, diameterY / 2, diameterZ / 2);
@@ -942,12 +946,12 @@
             var diameterBottom: number = (options.diameterBottom === 0) ? 0 : options.diameterBottom || options.diameter || 1;
             var tessellation: number = options.tessellation || 24;
             var subdivisions: number = options.subdivisions || 1;
-            var hasRings: boolean = options.hasRings;
-            var enclose: boolean = options.enclose;
-            var arc: number = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
+            var hasRings: boolean = options.hasRings ? true : false;
+            var enclose: boolean = options.enclose ? true : false
+            var arc: number = options.arc && (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
             var sideOrientation: number = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
             var faceUV: Vector4[] = options.faceUV || new Array<Vector4>(3);
-            var faceColors: Color4[] = options.faceColors;
+            var faceColors = options.faceColors;
             // default face colors and UV if undefined
             var quadNb: number = (arc !== 1 && enclose) ? 2 : 0;
             var ringNb: number = (hasRings) ? subdivisions : 1;
@@ -1090,11 +1094,15 @@
             var s: number;
             i = 0;
             for (s = 0; s < subdivisions; s++) {
+                let i0: number = 0;
+                let i1: number = 0;
+                let i2: number = 0;
+                let i3: number = 0;
                 for (j = 0; j < tessellation; j++) {
-                    var i0 = i * (e + 1) + j;
-                    var i1 = (i + 1) * (e + 1) + j;
-                    var i2 = i * (e + 1) + (j + 1);
-                    var i3 = (i + 1) * (e + 1) + (j + 1);
+                    i0 = i * (e + 1) + j;
+                    i1 = (i + 1) * (e + 1) + j;
+                    i2 = i * (e + 1) + (j + 1);
+                    i3 = (i + 1) * (e + 1) + (j + 1);
                     indices.push(i0, i1, i2);
                     indices.push(i3, i2, i1);
                 }
@@ -1119,7 +1127,7 @@
                 var circleVector;
                 var i: number;
                 var u: Vector4 = (isTop) ? faceUV[surfaceNb - 1] : faceUV[0];
-                var c: Color4;
+                var c: Nullable<Color4> = null;
                 if (faceColors) {
                     c = (isTop) ? faceColors[surfaceNb - 1] : faceColors[0];
                 }
@@ -1130,7 +1138,7 @@
                 positions.push(center.x, center.y, center.z);
                 normals.push(0, isTop ? 1 : -1, 0);
                 uvs.push(u.x + (u.z - u.x) * 0.5, u.y + (u.w - u.y) * 0.5);
-                if (faceColors) {
+                if (c) {
                     colors.push(c.r, c.g, c.b, c.a);
                 }
 
@@ -1144,7 +1152,7 @@
                     positions.push(circleVector.x, circleVector.y, circleVector.z);
                     normals.push(0, isTop ? 1 : -1, 0);
                     uvs.push(u.x + (u.z - u.x) * textureCoordinate.x, u.y + (u.w - u.y) * textureCoordinate.y);
-                    if (faceColors) {
+                    if (c) {
                         colors.push(c.r, c.g, c.b, c.a);
                     }
                 }
@@ -1594,7 +1602,7 @@
 
             var radius = options.radius || 0.5;
             var tessellation = options.tessellation || 64;
-            var arc: number = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
+            var arc: number = options.arc && (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
             var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
 
             // positions and uvs
@@ -1641,7 +1649,7 @@
          */
         public static CreatePolygon(polygon: Mesh, sideOrientation: number, fUV?:Vector4[], fColors?: Color4[], frontUVs?: Vector4, backUVs?: Vector4) {
 			var faceUV: Vector4[] = fUV || new Array<Vector4>(3);
-            var faceColors: Color4[] = fColors;
+            var faceColors = fColors;
             var colors = [];
 
             // default face colors and UV if undefined
@@ -1654,11 +1662,11 @@
                 }
             }
             
-            var positions = polygon.getVerticesData(VertexBuffer.PositionKind);
-			var normals = polygon.getVerticesData(VertexBuffer.NormalKind);
-			var uvs = polygon.getVerticesData(VertexBuffer.UVKind);
-			var indices = polygon.getIndices();
-
+            var positions = <FloatArray>polygon.getVerticesData(VertexBuffer.PositionKind);
+			var normals = <FloatArray>polygon.getVerticesData(VertexBuffer.NormalKind);
+			var uvs = <FloatArray>polygon.getVerticesData(VertexBuffer.UVKind);
+            var indices = <IndicesArray>polygon.getIndices();
+            
             // set face colours and textures
             var idx: number = 0;
             var face: number = 0;
@@ -2002,8 +2010,8 @@
                 face: [[15, 18, 21], [12, 20, 16], [6, 10, 2], [3, 0, 1], [9, 7, 13], [2, 8, 4, 0], [0, 4, 5, 1], [1, 5, 11, 7], [7, 11, 17, 13], [13, 17, 22, 18], [18, 22, 24, 21], [21, 24, 23, 20], [20, 23, 19, 16], [16, 19, 14, 10], [10, 14, 8, 2], [15, 9, 13, 18], [12, 15, 21, 20], [6, 12, 16, 10], [3, 6, 2, 0], [9, 3, 1, 7], [9, 15, 12, 6, 3], [22, 17, 11, 5, 4, 8, 14, 19, 23, 24]]
             };
 
-            var type: number = (options.type < 0 || options.type >= polyhedra.length) ? 0 : options.type || 0;
-            var size: number = options.size;
+            var type: number = options.type && (options.type < 0 || options.type >= polyhedra.length) ? 0 : options.type || 0;
+            var size = options.size;
             var sizeX: number = options.sizeX || size || 1;
             var sizeY: number = options.sizeY || size || 1;
             var sizeZ: number = options.sizeZ || size || 1;
@@ -2237,15 +2245,21 @@
             var computeFacetPositions = false;
             var computeFacetPartitioning = false;
             var faceNormalSign = 1;
+            let ratio = 0;
             if (options) {
                 computeFacetNormals = (options.facetNormals) ? true : false;
                 computeFacetPositions = (options.facetPositions) ? true : false;
                 computeFacetPartitioning = (options.facetPartitioning) ? true : false;
                 faceNormalSign = (options.useRightHandedSystem === true) ? -1 : 1;
+                ratio = options.ratio || 0;
             }
 
             // facetPartitioning reinit if needed
-            if (computeFacetPartitioning) {
+            let xSubRatio = 0;
+            let ySubRatio = 0;
+            let zSubRatio = 0;
+            let subSq = 0;
+            if (computeFacetPartitioning && options && options.bbSize) {
                 var ox = 0;                 // X partitioning index for facet position
                 var oy = 0;                 // Y partinioning index for facet position
                 var oz = 0;                 // Z partinioning index for facet position
@@ -2265,10 +2279,10 @@
 
                 var bbSizeMax = (options.bbSize.x > options.bbSize.y) ? options.bbSize.x : options.bbSize.y;
                 bbSizeMax = (bbSizeMax > options.bbSize.z) ? bbSizeMax : options.bbSize.z;
-                var xSubRatio = options.subDiv.X * options.ratio / options.bbSize.x;
-                var ySubRatio = options.subDiv.Y * options.ratio / options.bbSize.y;
-                var zSubRatio = options.subDiv.Z * options.ratio / options.bbSize.z;
-                var subSq = options.subDiv.max * options.subDiv.max;
+                xSubRatio = options.subDiv.X * ratio / options.bbSize.x;
+                ySubRatio = options.subDiv.Y * ratio / options.bbSize.y;
+                zSubRatio = options.subDiv.Z * ratio / options.bbSize.z;
+                subSq = options.subDiv.max * options.subDiv.max;
                 options.facetPartitioning.length = 0;
             }
 
@@ -2311,34 +2325,34 @@
                 faceNormaly /= length;
                 faceNormalz /= length;
 
-                if (computeFacetNormals) {
+                if (computeFacetNormals && options) {
                     options.facetNormals[index].x = faceNormalx;
                     options.facetNormals[index].y = faceNormaly;
                     options.facetNormals[index].z = faceNormalz;
                 }
 
-                if (computeFacetPositions) {
+                if (computeFacetPositions && options) {
                     // compute and the facet barycenter coordinates in the array facetPositions 
                     options.facetPositions[index].x = (positions[v1x] + positions[v2x] + positions[v3x]) / 3.0;
                     options.facetPositions[index].y = (positions[v1y] + positions[v2y] + positions[v3y]) / 3.0;
                     options.facetPositions[index].z = (positions[v1z] + positions[v2z] + positions[v3z]) / 3.0;
                 }
 
-                if (computeFacetPartitioning) {
+                if (computeFacetPartitioning && options) {
                     // store the facet indexes in arrays in the main facetPartitioning array :
                     // compute each facet vertex (+ facet barycenter) index in the partiniong array
-                    ox = Math.floor((options.facetPositions[index].x - options.bInfo.minimum.x * options.ratio) * xSubRatio);
-                    oy = Math.floor((options.facetPositions[index].y - options.bInfo.minimum.y * options.ratio) * ySubRatio);
-                    oz = Math.floor((options.facetPositions[index].z - options.bInfo.minimum.z * options.ratio) * zSubRatio);
-                    b1x = Math.floor((positions[v1x] - options.bInfo.minimum.x * options.ratio) * xSubRatio);
-                    b1y = Math.floor((positions[v1y] - options.bInfo.minimum.y * options.ratio) * ySubRatio);
-                    b1z = Math.floor((positions[v1z] - options.bInfo.minimum.z * options.ratio) * zSubRatio);
-                    b2x = Math.floor((positions[v2x] - options.bInfo.minimum.x * options.ratio) * xSubRatio);
-                    b2y = Math.floor((positions[v2y] - options.bInfo.minimum.y * options.ratio) * ySubRatio);
-                    b2z = Math.floor((positions[v2z] - options.bInfo.minimum.z * options.ratio) * zSubRatio);
-                    b3x = Math.floor((positions[v3x] - options.bInfo.minimum.x * options.ratio) * xSubRatio);
-                    b3y = Math.floor((positions[v3y] - options.bInfo.minimum.y * options.ratio) * ySubRatio);
-                    b3z = Math.floor((positions[v3z] - options.bInfo.minimum.z * options.ratio) * zSubRatio);
+                    ox = Math.floor((options.facetPositions[index].x - options.bInfo.minimum.x * ratio) * xSubRatio);
+                    oy = Math.floor((options.facetPositions[index].y - options.bInfo.minimum.y * ratio) * ySubRatio);
+                    oz = Math.floor((options.facetPositions[index].z - options.bInfo.minimum.z * ratio) * zSubRatio);
+                    b1x = Math.floor((positions[v1x] - options.bInfo.minimum.x * ratio) * xSubRatio);
+                    b1y = Math.floor((positions[v1y] - options.bInfo.minimum.y * ratio) * ySubRatio);
+                    b1z = Math.floor((positions[v1z] - options.bInfo.minimum.z * ratio) * zSubRatio);
+                    b2x = Math.floor((positions[v2x] - options.bInfo.minimum.x * ratio) * xSubRatio);
+                    b2y = Math.floor((positions[v2y] - options.bInfo.minimum.y * ratio) * ySubRatio);
+                    b2z = Math.floor((positions[v2z] - options.bInfo.minimum.z * ratio) * zSubRatio);
+                    b3x = Math.floor((positions[v3x] - options.bInfo.minimum.x * ratio) * xSubRatio);
+                    b3y = Math.floor((positions[v3y] - options.bInfo.minimum.y * ratio) * ySubRatio);
+                    b3z = Math.floor((positions[v3z] - options.bInfo.minimum.z * ratio) * zSubRatio);
 
                     block_idx_v1 = b1x + options.subDiv.max * b1y + subSq * b1z;
                     block_idx_v2 = b2x + options.subDiv.max * b2y + subSq * b2z;
@@ -2443,8 +2457,8 @@
                     for (u = 0; u < lu; u++) {
                         uvs[u + lu] = uvs[u];                       
                     }
-                    var frontUVs = frontUVs ? frontUVs : new Vector4(0.0, 0.0, 1.0, 1.0);
-                    var backUVs = backUVs ? backUVs : new Vector4(0.0, 0.0, 1.0, 1.0); 
+                    frontUVs = frontUVs ? frontUVs : new Vector4(0.0, 0.0, 1.0, 1.0);
+                    backUVs = backUVs ? backUVs : new Vector4(0.0, 0.0, 1.0, 1.0); 
                     u = 0;
                     for (i = 0; i < lu / 2; i++) {    
                         uvs[u] = frontUVs.x + (frontUVs.z - frontUVs.x) * uvs[u];

+ 116 - 100
src/Mesh/babylon.meshBuilder.ts

@@ -1,6 +1,6 @@
 module BABYLON {
     export class MeshBuilder {
-        private static updateSideOrientation(orientation: number, scene: Scene): number {
+        private static updateSideOrientation(orientation?: number): number {
             if (orientation == Mesh.DOUBLESIDE) {
                 return Mesh.DOUBLESIDE;
             }
@@ -24,12 +24,12 @@
          * Detail here : http://doc.babylonjs.com/tutorials/02._Discover_Basic_Elements#side-orientation    
          * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.  
          */
-        public static CreateBox(name: string, options: { size?: number, width?: number, height?: number, depth?: number, faceUV?: Vector4[], faceColors?: Color4[], sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, updatable?: boolean }, scene: Scene): Mesh {
+        public static CreateBox(name: string, options: { size?: number, width?: number, height?: number, depth?: number, faceUV?: Vector4[], faceColors?: Color4[], sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, updatable?: boolean }, scene: Nullable<Scene> = null): Mesh {
             var box = new Mesh(name, scene);
 
-            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
+            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
             box._originalBuilderSideOrientation = options.sideOrientation;
-            
+
             var vertexData = VertexData.CreateBox(options);
 
             vertexData.applyToMesh(box, options.updatable);
@@ -53,9 +53,9 @@
         public static CreateSphere(name: string, options: { segments?: number, diameter?: number, diameterX?: number, diameterY?: number, diameterZ?: number, arc?: number, slice?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, updatable?: boolean }, scene: any): Mesh {
             var sphere = new Mesh(name, scene);
 
-            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
+            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
             sphere._originalBuilderSideOrientation = options.sideOrientation;
-            
+
             var vertexData = VertexData.CreateSphere(options);
 
             vertexData.applyToMesh(sphere, options.updatable);
@@ -74,12 +74,12 @@
          * Detail here : http://doc.babylonjs.com/tutorials/02._Discover_Basic_Elements#side-orientation    
          * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.  
          */
-        public static CreateDisc(name: string, options: { radius?: number, tessellation?: number, arc?: number, updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }, scene: Scene): Mesh {
+        public static CreateDisc(name: string, options: { radius?: number, tessellation?: number, arc?: number, updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }, scene: Nullable<Scene> = null): Mesh {
             var disc = new Mesh(name, scene);
 
-            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
+            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
             disc._originalBuilderSideOrientation = options.sideOrientation;
-            
+
             var vertexData = VertexData.CreateDisc(options);
 
             vertexData.applyToMesh(disc, options.updatable);
@@ -102,9 +102,9 @@
         public static CreateIcoSphere(name: string, options: { radius?: number, radiusX?: number, radiusY?: number, radiusZ?: number, flat?: boolean, subdivisions?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, updatable?: boolean }, scene: Scene): Mesh {
             var sphere = new Mesh(name, scene);
 
-            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
+            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
             sphere._originalBuilderSideOrientation = options.sideOrientation;
-            
+
             var vertexData = VertexData.CreateIcoSphere(options);
 
             vertexData.applyToMesh(sphere, options.updatable);
@@ -134,11 +134,11 @@
          * Moreover, you can use the parameter `color` with `instance` (to update the ribbon), only if you previously used it at creation time.  
          * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.  
          */
-        public static CreateRibbon(name: string, options: { pathArray: Vector3[][], closeArray?: boolean, closePath?: boolean, offset?: number, updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, instance?: Mesh, invertUV?: boolean, uvs?: Vector2[], colors?: Color4[] }, scene?: Scene): Mesh {
+        public static CreateRibbon(name: string, options: { pathArray: Vector3[][], closeArray?: boolean, closePath?: boolean, offset?: number, updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, instance?: Mesh, invertUV?: boolean, uvs?: Vector2[], colors?: Color4[] }, scene: Nullable<Scene> = null): Mesh {
             var pathArray = options.pathArray;
             var closeArray = options.closeArray;
             var closePath = options.closePath;
-            var sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
+            var sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
             var instance = options.instance;
             var updatable = options.updatable;
 
@@ -150,7 +150,7 @@
                 var positionFunction = (positions: FloatArray) => {
                     var minlg = pathArray[0].length;
                     var i = 0;
-                    var ns = (instance._originalBuilderSideOrientation === Mesh.DOUBLESIDE) ? 2 : 1;
+                    var ns = ((<Mesh>instance)._originalBuilderSideOrientation === Mesh.DOUBLESIDE) ? 2 : 1;
                     for (var si = 1; si <= ns; si++) {
                         for (var p = 0; p < pathArray.length; p++) {
                             var path = pathArray[p];
@@ -191,13 +191,13 @@
                         }
                     }
                 };
-                var positions = instance.getVerticesData(VertexBuffer.PositionKind);
+                var positions = <FloatArray>instance.getVerticesData(VertexBuffer.PositionKind);
                 positionFunction(positions);
                 instance._boundingInfo = new BoundingInfo(Tmp.Vector3[0], Tmp.Vector3[1]);
                 instance._boundingInfo.update(instance._worldMatrix);
                 instance.updateVerticesData(VertexBuffer.PositionKind, positions, false, false);
                 if (options.colors) {
-                    var colors = instance.getVerticesData(VertexBuffer.ColorKind);
+                    var colors = <FloatArray>instance.getVerticesData(VertexBuffer.ColorKind);
                     for (var c = 0; c < options.colors.length; c++) {
                         colors[c * 4] = options.colors[c].r;
                         colors[c * 4 + 1] = options.colors[c].g;
@@ -207,7 +207,7 @@
                     instance.updateVerticesData(VertexBuffer.ColorKind, colors, false, false);
                 }
                 if (options.uvs) {
-                    var uvs = instance.getVerticesData(VertexBuffer.UVKind);
+                    var uvs = <FloatArray>instance.getVerticesData(VertexBuffer.UVKind);
                     for (var i = 0; i < options.uvs.length; i++) {
                         uvs[i * 2] = options.uvs[i].x;
                         uvs[i * 2 + 1] = options.uvs[i].y;
@@ -216,7 +216,7 @@
                 }
                 if (!instance.areNormalsFrozen || instance.isFacetDataEnabled) {
                     var indices = instance.getIndices();
-                    var normals = instance.getVerticesData(VertexBuffer.NormalKind);
+                    var normals = <FloatArray>instance.getVerticesData(VertexBuffer.NormalKind);
                     var params = instance.isFacetDataEnabled ? instance.getFacetDataParameters() : null;
                     VertexData.ComputeNormals(positions, indices, normals, params);
 
@@ -290,10 +290,10 @@
          */
         public static CreateCylinder(name: string, options: { height?: number, diameterTop?: number, diameterBottom?: number, diameter?: number, tessellation?: number, subdivisions?: number, arc?: number, faceColors?: Color4[], faceUV?: Vector4[], updatable?: boolean, hasRings?: boolean, enclose?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }, scene: any): Mesh {
             var cylinder = new Mesh(name, scene);
-            
-            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
+
+            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
             cylinder._originalBuilderSideOrientation = options.sideOrientation;
-            
+
             var vertexData = VertexData.CreateCylinder(options);
 
             vertexData.applyToMesh(cylinder, options.updatable);
@@ -315,9 +315,9 @@
         public static CreateTorus(name: string, options: { diameter?: number, thickness?: number, tessellation?: number, updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }, scene: any): Mesh {
             var torus = new Mesh(name, scene);
 
-            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
+            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
             torus._originalBuilderSideOrientation = options.sideOrientation;
-            
+
             var vertexData = VertexData.CreateTorus(options);
 
             vertexData.applyToMesh(torus, options.updatable);
@@ -340,9 +340,9 @@
         public static CreateTorusKnot(name: string, options: { radius?: number, tube?: number, radialSegments?: number, tubularSegments?: number, p?: number, q?: number, updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }, scene: any): Mesh {
             var torusKnot = new Mesh(name, scene);
 
-            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
+            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
             torusKnot._originalBuilderSideOrientation = options.sideOrientation;
-            
+
             var vertexData = VertexData.CreateTorusKnot(options);
 
             vertexData.applyToMesh(torusKnot, options.updatable);
@@ -362,7 +362,7 @@
          * When updating an instance, remember that only line point positions can change, not the number of points, neither the number of lines.      
          * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.  
          */
-        public static CreateLineSystem(name: string, options: { lines: Vector3[][], updatable: boolean, instance?: LinesMesh }, scene: Nullable<Scene>): LinesMesh {
+        public static CreateLineSystem(name: string, options: { lines: Vector3[][], updatable: boolean, instance: Nullable<LinesMesh> }, scene: Nullable<Scene>): LinesMesh {
             var instance = options.instance;
             var lines = options.lines;
 
@@ -382,7 +382,7 @@
                 instance.updateMeshPositions(positionFunction, false);
                 return instance;
             }
-            
+
             // line system creation
             var lineSystem = new LinesMesh(name, scene);
             var vertexData = VertexData.CreateLineSystem(options);
@@ -418,7 +418,7 @@
          * When updating an instance, remember that only point positions can change, not the number of points.      
          * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.  
          */
-        public static CreateDashedLines(name: string, options: { points: Vector3[], dashSize?: number, gapSize?: number, dashNb?: number, updatable?: boolean, instance?: LinesMesh }, scene: Scene): LinesMesh {
+        public static CreateDashedLines(name: string, options: { points: Vector3[], dashSize?: number, gapSize?: number, dashNb?: number, updatable?: boolean, instance?: LinesMesh }, scene: Nullable<Scene> = null): LinesMesh {
             var points = options.points;
             var instance = options.instance;
             var gapSize = options.gapSize || 1;
@@ -498,18 +498,18 @@
          * The optional parameter `invertUV` (boolean, default false) swaps in the geometry the U and V coordinates to apply a texture.  
          * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.  
          */
-        public static ExtrudeShape(name: string, options: { shape: Vector3[], path: Vector3[], scale?: number, rotation?: number, cap?: number, updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, instance?: Mesh, invertUV?: boolean }, scene: Scene): Mesh {
+        public static ExtrudeShape(name: string, options: { shape: Vector3[], path: Vector3[], scale?: number, rotation?: number, cap?: number, updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, instance?: Mesh, invertUV?: boolean }, scene: Nullable<Scene> = null): Mesh {
             var path = options.path;
             var shape = options.shape;
             var scale = options.scale || 1;
             var rotation = options.rotation || 0;
             var cap = (options.cap === 0) ? 0 : options.cap || Mesh.NO_CAP;
             var updatable = options.updatable;
-            var sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
-            var instance = options.instance;
+            var sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
+            var instance = options.instance || null;
             var invertUV = options.invertUV || false;
 
-            return MeshBuilder._ExtrudeShapeGeneric(name, shape, path, scale, rotation, null, null, false, false, cap, false, scene, updatable, sideOrientation, instance, invertUV, options.frontUVs, options.backUVs);
+            return MeshBuilder._ExtrudeShapeGeneric(name, shape, path, scale, rotation, null, null, false, false, cap, false, scene, updatable ? true : false, sideOrientation, instance, invertUV, options.frontUVs || null, options.backUVs || null);
         }
 
         /**
@@ -557,10 +557,10 @@
             var ribbonClosePath = options.ribbonClosePath || false;
             var cap = (options.cap === 0) ? 0 : options.cap || Mesh.NO_CAP;
             var updatable = options.updatable;
-            var sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
+            var sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
             var instance = options.instance;
             var invertUV = options.invertUV || false;
-            return MeshBuilder._ExtrudeShapeGeneric(name, shape, path, null, null, scaleFunction, rotationFunction, ribbonCloseArray, ribbonClosePath, cap, true, scene, updatable, sideOrientation, instance, invertUV, options.frontUVs, options.backUVs);
+            return MeshBuilder._ExtrudeShapeGeneric(name, shape, path, null, null, scaleFunction, rotationFunction, ribbonCloseArray, ribbonClosePath, cap, true, scene, updatable ? true : false, sideOrientation, instance || null, invertUV, options.frontUVs || null, options.backUVs || null);
         }
 
         /**
@@ -588,7 +588,7 @@
             var radius = options.radius || 1;
             var tessellation = options.tessellation || 64;
             var updatable = options.updatable;
-            var sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
+            var sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
             var cap = options.cap || Mesh.NO_CAP;
             var pi2 = Math.PI * 2;
             var paths = new Array();
@@ -635,9 +635,9 @@
         public static CreatePlane(name: string, options: { size?: number, width?: number, height?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, updatable?: boolean, sourcePlane?: Plane }, scene: Scene): Mesh {
             var plane = new Mesh(name, scene);
 
-            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
+            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
             plane._originalBuilderSideOrientation = options.sideOrientation;
-            
+
             var vertexData = VertexData.CreatePlane(options);
 
             vertexData.applyToMesh(plane, options.updatable);
@@ -724,7 +724,7 @@
         public static CreateGroundFromHeightMap(name: string, url: string, options: { width?: number, height?: number, subdivisions?: number, minHeight?: number, maxHeight?: number, colorFilter?: Color3, updatable?: boolean, onReady?: (mesh: GroundMesh) => void }, scene: Scene): GroundMesh {
             var width = options.width || 10.0;
             var height = options.height || 10.0;
-            var subdivisions = options.subdivisions || 1|0;
+            var subdivisions = options.subdivisions || 1 | 0;
             var minHeight = options.minHeight || 0.0;
             var maxHeight = options.maxHeight || 1.0;
             var filter = options.colorFilter || new Color3(0.3, 0.59, 0.11);
@@ -747,6 +747,11 @@
                 // Getting height map data
                 var canvas = document.createElement("canvas");
                 var context = canvas.getContext("2d");
+
+                if (!context) {
+                    throw new Error("Unable to get 2d context for CreateGroundFromHeightMap");
+                }
+
                 var bufferWidth = img.width;
                 var bufferHeight = img.height;
                 canvas.width = bufferWidth;
@@ -788,37 +793,37 @@
          * If you create a double-sided mesh, you can choose what parts of the texture image to crop and stick respectively on the front and the back sides with the parameters `frontUVs` and `backUVs` (Vector4).  
          * Remember you can only change the shape positions, not their number when updating a polygon.
          */
-        public static CreatePolygon(name: string, options: {shape: Vector3[], holes?: Vector3[][], depth?: number, faceUV?: Vector4[], faceColors?: Color4[], updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4}, scene: Scene): Mesh {
-            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
-			var shape = options.shape;
-			var holes = options.holes || [];
-			var depth = options.depth || 0;
-			var contours: Array<Vector2> = [];
-			var hole: Array<Vector2> = [];
-            
-			for(var i=0; i < shape.length; i++) {
-				contours[i] = new Vector2(shape[i].x, shape[i].z);
-			}
-			var epsilon = 0.00000001;
-			if(contours[0].equalsWithEpsilon(contours[contours.length - 1], epsilon)) {
+        public static CreatePolygon(name: string, options: { shape: Vector3[], holes?: Vector3[][], depth?: number, faceUV?: Vector4[], faceColors?: Color4[], updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }, scene: Scene): Mesh {
+            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
+            var shape = options.shape;
+            var holes = options.holes || [];
+            var depth = options.depth || 0;
+            var contours: Array<Vector2> = [];
+            var hole: Array<Vector2> = [];
+
+            for (var i = 0; i < shape.length; i++) {
+                contours[i] = new Vector2(shape[i].x, shape[i].z);
+            }
+            var epsilon = 0.00000001;
+            if (contours[0].equalsWithEpsilon(contours[contours.length - 1], epsilon)) {
                 contours.pop();
-			}
-			
-			var polygonTriangulation = new PolygonMeshBuilder(name, contours, scene);
-			for(var hNb = 0; hNb < holes.length; hNb++) {
-				hole = [];
-				for(var hPoint = 0; hPoint < holes[hNb].length; hPoint++) {
-					hole.push(new Vector2(holes[hNb][hPoint].x, holes[hNb][hPoint].z));
-				}
-				polygonTriangulation.addHole(hole);
-			}
-			var polygon = polygonTriangulation.build(options.updatable, depth);
+            }
+
+            var polygonTriangulation = new PolygonMeshBuilder(name, contours, scene);
+            for (var hNb = 0; hNb < holes.length; hNb++) {
+                hole = [];
+                for (var hPoint = 0; hPoint < holes[hNb].length; hPoint++) {
+                    hole.push(new Vector2(holes[hNb][hPoint].x, holes[hNb][hPoint].z));
+                }
+                polygonTriangulation.addHole(hole);
+            }
+            var polygon = polygonTriangulation.build(options.updatable, depth);
             polygon._originalBuilderSideOrientation = options.sideOrientation;
-			var vertexData = VertexData.CreatePolygon(polygon, options.sideOrientation, options.faceUV, options.faceColors, options.frontUVs, options.backUVs);
-            vertexData.applyToMesh(polygon, options.updatable);			
-			
+            var vertexData = VertexData.CreatePolygon(polygon, options.sideOrientation, options.faceUV, options.faceColors, options.frontUVs, options.backUVs);
+            vertexData.applyToMesh(polygon, options.updatable);
+
             return polygon;
-		};
+        };
 
         /**
          * Creates an extruded polygon mesh, with depth in the Y direction. 
@@ -826,10 +831,10 @@
          * Please read this tutorial : http://doc.babylonjs.com/tutorials/CreateBox_Per_Face_Textures_And_Colors  
 		*/
 
-		public static ExtrudePolygon(name: string, options: {shape: Vector3[], holes?: Vector3[][], depth?: number, faceUV?: Vector4[], faceColors?: Color4[], updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4}, scene: Scene): Mesh {
-			return MeshBuilder.CreatePolygon(name, options, scene);
-		};
-         
+        public static ExtrudePolygon(name: string, options: { shape: Vector3[], holes?: Vector3[][], depth?: number, faceUV?: Vector4[], faceColors?: Color4[], updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }, scene: Scene): Mesh {
+            return MeshBuilder.CreatePolygon(name, options, scene);
+        };
+
 
         /**
          * Creates a tube mesh.    
@@ -866,17 +871,17 @@
             if (options.radius !== undefined) {
                 radius = options.radius;
             };
-            var tessellation = options.tessellation || 64|0;
-            var radiusFunction = options.radiusFunction;
+            var tessellation = options.tessellation || 64 | 0;
+            var radiusFunction = options.radiusFunction || null;
             var cap = options.cap || Mesh.NO_CAP;
             var invertUV = options.invertUV || false;
             var updatable = options.updatable;
-            var sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
-            options.arc = (options.arc <= 0.0 || options.arc > 1.0) ? 1.0 : options.arc || 1.0;
+            var sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
+            options.arc = options.arc && (options.arc <= 0.0 || options.arc > 1.0) ? 1.0 : options.arc || 1.0;
 
             // tube geometry
-            var tubePathArray = (path: Vector3[], path3D: Path3D, circlePaths: Vector3[][], radius: number, tessellation: number, 
-                                radiusFunction: { (i: number, distance: number): number; }, cap: number, arc: number) => {
+            var tubePathArray = (path: Vector3[], path3D: Path3D, circlePaths: Vector3[][], radius: number, tessellation: number,
+                radiusFunction: Nullable<{ (i: number, distance: number): number; }>, cap: number, arc: number) => {
                 var tangents = path3D.getTangents();
                 var normals = path3D.getNormals();
                 var distances = path3D.getDistances();
@@ -942,7 +947,7 @@
                 var arc = options.arc || (<any>instance).arc;
                 path3D = ((<any>instance).path3D).update(path);
                 pathArray = tubePathArray(path, path3D, (<any>instance).pathArray, radius, (<any>instance).tessellation, radiusFunction, (<any>instance).cap, arc);
-                instance = MeshBuilder.CreateRibbon(null, { pathArray: pathArray, instance: instance });
+                instance = MeshBuilder.CreateRibbon("", { pathArray: pathArray, instance: instance });
                 (<any>instance).path3D = path3D;
                 (<any>instance).pathArray = pathArray;
                 (<any>instance).arc = arc;
@@ -988,9 +993,9 @@
         public static CreatePolyhedron(name: string, options: { type?: number, size?: number, sizeX?: number, sizeY?: number, sizeZ?: number, custom?: any, faceUV?: Vector4[], faceColors?: Color4[], flat?: boolean, updatable?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }, scene: Scene): Mesh {
             var polyhedron = new Mesh(name, scene);
 
-            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation, scene);
+            options.sideOrientation = MeshBuilder.updateSideOrientation(options.sideOrientation);
             polyhedron._originalBuilderSideOrientation = options.sideOrientation;
-            
+
             var vertexData = VertexData.CreatePolyhedron(options);
 
             vertexData.applyToMesh(polyhedron, options.updatable);
@@ -1008,7 +1013,7 @@
          * The parameter `angle` (float in radian, default 0) sets the angle to rotate the decal.  
          */
         public static CreateDecal(name: string, sourceMesh: AbstractMesh, options: { position?: Vector3, normal?: Vector3, size?: Vector3, angle?: number }): Mesh {
-            var indices = sourceMesh.getIndices();
+            var indices = <IndicesArray>sourceMesh.getIndices();
             var positions = sourceMesh.getVerticesData(VertexBuffer.PositionKind);
             var normals = sourceMesh.getVerticesData(VertexBuffer.NormalKind);
             var position = options.position || Vector3.Zero();
@@ -1019,7 +1024,7 @@
             // Getting correct rotation
             if (!normal) {
                 var target = new Vector3(0, 0, 1);
-                var camera = sourceMesh.getScene().activeCamera;
+                var camera = <Camera>sourceMesh.getScene().activeCamera;
                 var cameraWorldTarget = Vector3.TransformCoordinates(target, camera.getWorldMatrix());
 
                 normal = camera.globalPosition.subtract(cameraWorldTarget);
@@ -1044,8 +1049,12 @@
             var currentVertexDataIndex = 0;
 
             var extractDecalVector3 = (indexId: number): PositionNormalVertex => {
-                var vertexId = indices[indexId];
                 var result = new PositionNormalVertex();
+                if (!indices || !positions || !normals) {
+                    return result;
+                }
+
+                var vertexId = indices[indexId];
                 result.position = new Vector3(positions[vertexId * 3], positions[vertexId * 3 + 1], positions[vertexId * 3 + 2]);
 
                 // Send vector to decal local world
@@ -1057,7 +1066,7 @@
 
                 return result;
             }; // Inspired by https://github.com/mrdoob/three.js/blob/eee231960882f6f3b6113405f524956145148146/examples/js/geometries/DecalGeometry.js
-            var clip = (vertices: PositionNormalVertex[], axis: Vector3): PositionNormalVertex[]=> {
+            var clip = (vertices: PositionNormalVertex[], axis: Vector3): PositionNormalVertex[] => {
                 if (vertices.length === 0) {
                     return vertices;
                 }
@@ -1079,7 +1088,10 @@
                     var v2Out: boolean;
                     var v3Out: boolean;
                     var total = 0;
-                    var nV1: PositionNormalVertex, nV2: PositionNormalVertex, nV3: PositionNormalVertex, nV4: PositionNormalVertex;
+                    let nV1: Nullable<PositionNormalVertex> = null;
+                    let nV2: Nullable<PositionNormalVertex> = null;
+                    let nV3: Nullable<PositionNormalVertex> = null;
+                    let nV4: Nullable<PositionNormalVertex> = null;
 
                     var d1 = Vector3.Dot(vertices[index].position, axis) - clipSize;
                     var d2 = Vector3.Dot(vertices[index + 1].position, axis) - clipSize;
@@ -1128,13 +1140,15 @@
                                 nV4 = clipVertices(vertices[index + 2], nV2);
                             }
 
-                            result.push(nV1.clone());
-                            result.push(nV2.clone());
-                            result.push(nV3);
+                            if (nV1 && nV2 && nV3 && nV4) {
+                                result.push(nV1.clone());
+                                result.push(nV2.clone());
+                                result.push(nV3);
 
-                            result.push(nV4);
-                            result.push(nV3.clone());
-                            result.push(nV2.clone());
+                                result.push(nV4);
+                                result.push(nV3.clone());
+                                result.push(nV2.clone());
+                            }
                             break;
                         case 2:
                             if (!v1Out) {
@@ -1214,20 +1228,22 @@
         }
 
         // Privates
-        private static _ExtrudeShapeGeneric(name: string, shape: Vector3[], curve: Vector3[], scale: number, rotation: number, scaleFunction: { (i: number, distance: number): number; }, rotateFunction: { (i: number, distance: number): number; }, rbCA: boolean, rbCP: boolean, cap: number, custom: boolean, scene: Scene, updtbl: boolean, side: number, instance: Mesh, invertUV: boolean, frontUVs: Vector4, backUVs: Vector4): Mesh {
+        private static _ExtrudeShapeGeneric(name: string, shape: Vector3[], curve: Vector3[], scale: Nullable<number>, rotation: Nullable<number>, scaleFunction: Nullable<{ (i: number, distance: number): number; }>,
+            rotateFunction: Nullable<{ (i: number, distance: number): number; }>, rbCA: boolean, rbCP: boolean, cap: number, custom: boolean,
+            scene: Nullable<Scene>, updtbl: boolean, side: number, instance: Nullable<Mesh>, invertUV: boolean, frontUVs: Nullable<Vector4>, backUVs: Nullable<Vector4>): Mesh {
             // extrusion geometry
-            var extrusionPathArray = (shape: Vector3[], curve: Vector3[], path3D: Path3D, shapePaths: Vector3[][], scale: number, rotation: number, 
-                                        scaleFunction:{ (i: number, distance: number): number; } , rotateFunction:{ (i: number, distance: number): number; } , cap: number, custom: boolean) => {
+            var extrusionPathArray = (shape: Vector3[], curve: Vector3[], path3D: Path3D, shapePaths: Vector3[][], scale: Nullable<number>, rotation: Nullable<number>,
+                scaleFunction: Nullable<{ (i: number, distance: number): number; }>, rotateFunction: Nullable<{ (i: number, distance: number): number; }>, cap: number, custom: boolean) => {
                 var tangents = path3D.getTangents();
                 var normals = path3D.getNormals();
                 var binormals = path3D.getBinormals();
                 var distances = path3D.getDistances();
 
                 var angle = 0;
-                var returnScale: { (i: number, distance: number): number; } = () => { return scale; };
-                var returnRotation: { (i: number, distance: number): number; } = () => { return rotation; };
-                var rotate: { (i: number, distance: number): number; } = custom ? rotateFunction : returnRotation;
-                var scl: { (i: number, distance: number): number; } = custom ? scaleFunction : returnScale;
+                var returnScale: { (i: number, distance: number): number; } = () => { return scale !== null ? scale : 1; };
+                var returnRotation: { (i: number, distance: number): number; } = () => { return rotation!== null ? rotation : 0; };
+                var rotate: { (i: number, distance: number): number; } = custom && rotateFunction ? rotateFunction : returnRotation;
+                var scl: { (i: number, distance: number): number; } = custom && scaleFunction ? scaleFunction : returnScale;
                 var index = (cap === Mesh.NO_CAP || cap === Mesh.CAP_END) ? 0 : 2;
                 var rotationMatrix: Matrix = Tmp.Matrix[0];
 
@@ -1248,7 +1264,7 @@
                     index++;
                 }
                 // cap
-                var capPath = (shapePath: Vector3[])  => {
+                var capPath = (shapePath: Vector3[]) => {
                     var pointCap = Array<Vector3>();
                     var barycenter = Vector3.Zero();
                     var i: number;
@@ -1288,7 +1304,7 @@
             if (instance) { // instance update
                 path3D = ((<any>instance).path3D).update(curve);
                 pathArray = extrusionPathArray(shape, curve, (<any>instance).path3D, (<any>instance).pathArray, scale, rotation, scaleFunction, rotateFunction, (<any>instance).cap, custom);
-                instance = Mesh.CreateRibbon(null, pathArray, null, null, null, scene, null, null, instance);
+                instance = Mesh.CreateRibbon("", pathArray, false, false, 0, scene || undefined, false, 0, instance);
 
                 return instance;
             }
@@ -1297,7 +1313,7 @@
             var newShapePaths = new Array<Array<Vector3>>();
             cap = (cap < 0 || cap > 3) ? 0 : cap;
             pathArray = extrusionPathArray(shape, curve, path3D, newShapePaths, scale, rotation, scaleFunction, rotateFunction, cap, custom);
-            var extrudedGeneric = MeshBuilder.CreateRibbon(name, {pathArray: pathArray, closeArray: rbCA, closePath: rbCP, updatable: updtbl, sideOrientation: side, invertUV: invertUV, frontUVs: frontUVs, backUVs: backUVs}, scene);
+            var extrudedGeneric = MeshBuilder.CreateRibbon(name, { pathArray: pathArray, closeArray: rbCA, closePath: rbCP, updatable: updtbl, sideOrientation: side, invertUV: invertUV, frontUVs: frontUVs || undefined, backUVs: backUVs || undefined }, scene);
             (<any>extrudedGeneric).pathArray = pathArray;
             (<any>extrudedGeneric).path3D = path3D;
             (<any>extrudedGeneric).cap = cap;

+ 13 - 1
src/Mesh/babylon.meshSimplification.ts

@@ -392,6 +392,10 @@
             var vertexReferences: Array<number> = [];
 
             var vertexInit = (i: number) => {
+                if (!positionData) {
+                    return;
+                }
+
                 var offset = i + submesh.verticesStart;
                 var position = Vector3.FromArray(positionData, offset * 3);
 
@@ -407,6 +411,10 @@
             AsyncLoop.SyncAsyncForLoop(totalVertices,(this.syncIterations / 4) >> 0, vertexInit,() => {
 
                 var indicesInit = (i: number) => {
+                    if (!indices) {
+                        return;
+                    }
+
                     var offset = (submesh.indexStart / 3) + i;
                     var pos = (offset * 3);
                     var i0 = indices[pos + 0];
@@ -483,6 +491,10 @@
                 vertex.id = vertexCount;
                 if (vertex.triangleCount) {
                     vertex.originalOffsets.forEach(originalOffset => {
+                        if (!normalData) {
+                            return;
+                        }
+
                         newPositionData.push(vertex.position.x);
                         newPositionData.push(vertex.position.y);
                         newPositionData.push(vertex.position.z);
@@ -510,7 +522,7 @@
             this._reconstructedMesh.subMeshes = [];
 
             var newIndicesArray: number[] = <number[]>this._reconstructedMesh.getIndices(); //[];
-            var originalIndices = this._mesh.getIndices();
+            var originalIndices = <IndicesArray>this._mesh.getIndices();
             for (i = 0; i < newTriangles.length; ++i) {
                 t = newTriangles[i]; //now get the new referencing point for each vertex
                 [0, 1, 2].forEach(idx => {

+ 1 - 1
src/Mesh/babylon.polygonMesh.ts

@@ -147,7 +147,7 @@ module BABYLON {
             return this;
         }
 
-        build(updatable: boolean = false, depth?:number): Mesh {
+        build(updatable: boolean = false, depth:number = 0): Mesh {
             var result = new Mesh(this._name, this._scene);
 
             var normals = new Array<number>();

+ 5 - 5
src/Mesh/babylon.subMesh.ts

@@ -35,7 +35,7 @@
         public _distanceToCamera: number;
         public _id: number;
 
-        private _currentMaterial: Material;
+        private _currentMaterial: Nullable<Material>;
 
         public static AddToMesh(materialIndex: number, verticesStart: number, verticesCount: number, indexStart: number, indexCount: number, mesh: AbstractMesh, renderingMesh?: Mesh, createBoundingBox: boolean = true): SubMesh {
             return new SubMesh(materialIndex, verticesStart, verticesCount, indexStart, indexCount, mesh, renderingMesh, createBoundingBox);
@@ -98,7 +98,7 @@
         /**
          * Returns the submesh material.  
          */
-        public getMaterial(): Material {
+        public getMaterial(): Nullable<Material> {
             var rootMaterial = this._renderingMesh.material;
 
             if (rootMaterial && (<MultiMaterial>rootMaterial).getSubMaterial) {
@@ -128,7 +128,7 @@
         public refreshBoundingInfo(): SubMesh {
             this._lastColliderWorldVertices = null;
 
-            if (this.IsGlobal) {
+            if (this.IsGlobal || !this._renderingMesh || !this._renderingMesh.geometry) {
                 return this;
             }
             var data = this._renderingMesh.getVerticesData(VertexBuffer.PositionKind);
@@ -138,7 +138,7 @@
                 return this;
             }
 
-            var indices = this._renderingMesh.getIndices();
+            var indices = <IndicesArray>this._renderingMesh.getIndices();
             var extend: { minimum: Vector3, maximum: Vector3 };
 
             //is this the only submesh?
@@ -364,7 +364,7 @@
             var maxVertexIndex = -Number.MAX_VALUE;
 
             renderingMesh = (<Mesh>(renderingMesh || <Mesh>mesh));
-            var indices = renderingMesh.getIndices();
+            var indices = <IndicesArray>renderingMesh.getIndices();
 
             for (var index = startIndex; index < startIndex + indexCount; index++) {
                 var vertexIndex = indices[index];

+ 3 - 2
src/Mesh/babylon.vertexBuffer.ts

@@ -35,6 +35,7 @@
                         break;
                     case VertexBuffer.MatricesWeightsKind:
                     case VertexBuffer.MatricesWeightsExtraKind:
+                    default:
                         stride = 4;
                         break;
                 }
@@ -86,14 +87,14 @@
         /**
          * Returns an array of numbers or a Float32Array containing the VertexBuffer data.  
          */
-        public getData(): FloatArray {
+        public getData(): Nullable<FloatArray> {
             return this._buffer.getData();
         }
 
         /**
          * Returns the WebGLBuffer associated to the VertexBuffer.  
          */
-        public getBuffer(): WebGLBuffer {
+        public getBuffer(): Nullable<WebGLBuffer> {
             return this._buffer.getBuffer();
         }
 

+ 3 - 3
src/Morph/babylon.morphTarget.ts

@@ -118,13 +118,13 @@ module BABYLON {
 
             var result = new MorphTarget(name, influence);
 
-            result.setPositions(mesh.getVerticesData(VertexBuffer.PositionKind));
+            result.setPositions(<FloatArray>mesh.getVerticesData(VertexBuffer.PositionKind));
 
             if (mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
-                result.setNormals(mesh.getVerticesData(VertexBuffer.NormalKind));
+                result.setNormals(<FloatArray>mesh.getVerticesData(VertexBuffer.NormalKind));
             }
             if (mesh.isVerticesDataPresent(VertexBuffer.TangentKind)) {
-                result.setTangents(mesh.getVerticesData(VertexBuffer.TangentKind));
+                result.setTangents(<FloatArray>mesh.getVerticesData(VertexBuffer.TangentKind));
             }
 
             return result;

+ 8 - 6
src/Morph/babylon.morphTargetManager.ts

@@ -1,9 +1,9 @@
 module BABYLON {
     export class MorphTargetManager {
         private _targets = new Array<MorphTarget>();
-        private _targetObservable = new Array<Observer<boolean>>();
+        private _targetObservable = new Array<Nullable<Observer<boolean>>>();
         private _activeTargets = new SmartArray<MorphTarget>(16);
-        private _scene: Scene;
+        private _scene: Nullable<Scene>;
         private _influences: Float32Array;
         private _supportsNormals = false;
         private _supportsTangents = false;
@@ -11,16 +11,18 @@ module BABYLON {
         private _uniqueId = 0;
         private _tempInfluences = new Array<number>();
 
-        public constructor(scene?: Scene) {
+        public constructor(scene: Nullable<Scene> = null) {
             if (!scene) {
                 scene = Engine.LastCreatedScene;
             }
 
             this._scene = scene;
 
-            this._scene.morphTargetManagers.push(this);
+            if (this._scene) {
+                this._scene.morphTargetManagers.push(this);
 
-            this._uniqueId = scene.getUniqueId();
+                this._uniqueId = this._scene.getUniqueId();
+            }
         }
 
         public get uniqueId(): number {
@@ -129,7 +131,7 @@ module BABYLON {
                 this._influences[index] = this._tempInfluences[index];
             }
             
-            if (needUpdate) {
+            if (needUpdate && this._scene) {
                 // Flag meshes as dirty to resync with the active targets
                 for (var mesh of this._scene.meshes) {
                     if ((<any>mesh).morphTargetManager === this) {

+ 2 - 2
src/Particles/babylon.gpuParticleSystem.ts

@@ -2,7 +2,7 @@
     export class GPUParticleSystem implements IDisposable, IParticleSystem {
         // Members
         public id: string;
-        public emitter: AbstractMesh | Vector3 = null;       
+        public emitter: Nullable<AbstractMesh | Vector3> = null;       
         public renderingGroupId = 0;        
         public layerMask: number = 0x0FFFFFFF;
 
@@ -50,7 +50,7 @@
         }
 
         //TODO: Clone / Parse / serialize
-        public clone(name: string, newEmitter: any): GPUParticleSystem {
+        public clone(name: string, newEmitter: any): Nullable<GPUParticleSystem> {
             return null;
         }
 

+ 21 - 17
src/Particles/babylon.particleSystem.ts

@@ -12,14 +12,14 @@
     export interface IParticleSystem {
         id: string;
         name: string;
-        emitter: AbstractMesh | Vector3;
+        emitter: Nullable<AbstractMesh | Vector3>; 
         renderingGroupId: number;
         layerMask: number;
         isStarted(): boolean;
         animate(): void;
         render(): number;
         dispose(): void;
-        clone(name: string, newEmitter: any): IParticleSystem;
+        clone(name: string, newEmitter: any): Nullable<IParticleSystem>;
         serialize(): any;
 
         rebuild(): void
@@ -35,7 +35,7 @@
 
         public id: string;
         public renderingGroupId = 0;
-        public emitter: AbstractMesh | Vector3 = null;
+        public emitter: Nullable<AbstractMesh | Vector3> = null;
         public emitRate = 10;
         public manualEmitCount = -1;
         public updateSpeed = 0.01;
@@ -53,7 +53,7 @@
         public minAngularSpeed = 0;
         public maxAngularSpeed = 0;
 
-        public particleTexture: Texture;
+        public particleTexture: Nullable<Texture>;
 
         public layerMask: number = 0x0FFFFFFF;
 
@@ -69,7 +69,7 @@
         */
         public onDisposeObservable = new Observable<ParticleSystem>();
 
-        private _onDisposeObserver: Observer<ParticleSystem>;
+        private _onDisposeObserver: Nullable<Observer<ParticleSystem>>;
         public set onDispose(callback: () => void) {
             if (this._onDisposeObserver) {
                 this.onDisposeObservable.remove(this._onDisposeObserver);
@@ -78,7 +78,7 @@
         }
 
         public updateFunction: (particles: Particle[]) => void;
-        public onAnimationEnd: () => void = null;
+        public onAnimationEnd: Nullable<() => void> = null;
 
         public blendMode = ParticleSystem.BLENDMODE_ONEONE;
 
@@ -103,11 +103,11 @@
         private _stockParticles = new Array<Particle>();
         private _newPartsExcess = 0;
         private _vertexData: Float32Array;
-        private _vertexBuffer: Buffer;
+        private _vertexBuffer: Nullable<Buffer>;
         private _vertexBuffers: { [key: string]: VertexBuffer } = {};
-        private _indexBuffer: WebGLBuffer;
+        private _indexBuffer: Nullable<WebGLBuffer>;
         private _effect: Effect;
-        private _customEffect: Effect;
+        private _customEffect: Nullable<Effect>;
         private _cachedDefines: string;
 
         private _scaledColorStep = new Color4(0, 0, 0, 0);
@@ -137,7 +137,7 @@
         }
         // end of sheet animation
 
-        constructor(public name: string, capacity: number, scene: Scene, customEffect?: Effect, private _isAnimationSheetEnabled: boolean = false, epsilon: number = 0.01) {
+        constructor(public name: string, capacity: number, scene: Scene, customEffect: Nullable<Effect> = null, private _isAnimationSheetEnabled: boolean = false, epsilon: number = 0.01) {
             this.id = name;
             this._capacity = capacity;
 
@@ -238,7 +238,7 @@
         }
 
         public recycleParticle(particle: Particle): void {
-            var lastParticle = this.particles.pop();
+            var lastParticle = <Particle>this.particles.pop();
 
             if (lastParticle !== particle) {
                 lastParticle.copyTo(particle);
@@ -336,7 +336,7 @@
                 }
 
                 if (this._stockParticles.length !== 0) {
-                    particle = this._stockParticles.pop();
+                    particle = <Particle>this._stockParticles.pop();
                     particle.age = 0;
                     particle.cellIndex = this.startSpriteCellID;
                 } else {
@@ -484,10 +484,12 @@
                 offset += 4;
             }
 
-            this._vertexBuffer.update(this._vertexData);
+            if (this._vertexBuffer) {
+                this._vertexBuffer.update(this._vertexData);
+            }
         }
 
-        public appendParticleVertexes: (offset: number, particle: Particle) => void = null;
+        public appendParticleVertexes: Nullable<(offset: number, particle: Particle) => void> = null;
 
         private appenedParticleVertexesWithSheet(offset: number, particle: Particle) {
             this._appendParticleVertexWithAnimation(offset++, particle, 0, 0);
@@ -506,7 +508,9 @@
         public rebuild(): void {
             this._createIndexBuffer();
 
-            this._vertexBuffer._rebuild();
+            if (this._vertexBuffer) {
+                this._vertexBuffer._rebuild();
+            }
         }
 
         public render(): number {
@@ -591,7 +595,7 @@
 
         // Clone
         public clone(name: string, newEmitter: any): ParticleSystem {
-            var custom: Effect = null;
+            var custom: Nullable<Effect> = null;
             var program: any = null;
             if (this.customShader != null) {
                 program = this.customShader;
@@ -673,7 +677,7 @@
 
         public static Parse(parsedParticleSystem: any, scene: Scene, rootUrl: string): ParticleSystem {
             var name = parsedParticleSystem.name;
-            var custom: Effect = null;
+            var custom: Nullable<Effect> = null;
             var program: any = null;
             if (parsedParticleSystem.customShader) {
                 program = parsedParticleSystem.customShader;

+ 27 - 26
src/Particles/babylon.solidParticle.ts

@@ -1,25 +1,25 @@
 module BABYLON {
     
         export class SolidParticle {
-            public idx: number = 0;                         // particle global index
-            public color = new Color4(1.0, 1.0, 1.0, 1.0);  // color
-            public position = Vector3.Zero();               // position
-            public rotation = Vector3.Zero();               // rotation
-            public rotationQuaternion: Quaternion;          // quaternion, will overwrite rotation
-            public scaling = Vector3.One();                 // scaling
-            public uvs = new Vector4(0.0, 0.0, 1.0, 1.0);   // uvs
-            public velocity = Vector3.Zero();               // velocity
-            public alive = true;                            // alive
-            public isVisible = true;                        // visibility
-            public _pos: number = 0;                        // index of this particle in the global "positions" array
-            public _ind: number = 0;                        // index of this particle in the global "indices" array
-            public _model: ModelShape;                      // model shape reference
-            public shapeId: number = 0;                     // model shape id
-            public idxInShape: number = 0;                  // index of the particle in its shape id
-            public _modelBoundingInfo: BoundingInfo;        // reference to the shape model BoundingInfo object
-            public _boundingInfo: BoundingInfo;             // particle BoundingInfo
-            public _sps: SolidParticleSystem;               // reference to the SPS what the particle belongs to
-            public _stillInvisible: boolean = false;         // still set as invisible in order to skip useless computations
+            public idx: number = 0;                                             // particle global index
+            public color: Nullable<Color4> = new Color4(1.0, 1.0, 1.0, 1.0);    // color
+            public position = Vector3.Zero();                                   // position
+            public rotation = Vector3.Zero();                                   // rotation
+            public rotationQuaternion: Nullable<Quaternion>;                    // quaternion, will overwrite rotation
+            public scaling = Vector3.One();                                     // scaling
+            public uvs = new Vector4(0.0, 0.0, 1.0, 1.0);                       // uvs
+            public velocity = Vector3.Zero();                                   // velocity
+            public alive = true;                                                // alive
+            public isVisible = true;                                            // visibility
+            public _pos: number = 0;                                            // index of this particle in the global "positions" array
+            public _ind: number = 0;                                            // index of this particle in the global "indices" array
+            public _model: ModelShape;                                          // model shape reference
+            public shapeId: number = 0;                                         // model shape id
+            public idxInShape: number = 0;                                      // index of the particle in its shape id
+            public _modelBoundingInfo: BoundingInfo;                            // reference to the shape model BoundingInfo object
+            public _boundingInfo: BoundingInfo;                                 // particle BoundingInfo
+            public _sps: SolidParticleSystem;                                   // reference to the SPS what the particle belongs to
+            public _stillInvisible: boolean = false;                            // still set as invisible in order to skip useless computations
     
             /**
              * Creates a Solid Particle object.
@@ -32,11 +32,11 @@ module BABYLON {
              * `idxInShape` (integer) is the index of the particle in the current model (ex: the 10th box of addShape(box, 30))
              * `modelBoundingInfo` is the reference to the model BoundingInfo used for intersection computations.
              */
-            constructor(particleIndex: number, positionIndex: number, indiceIndex: number, model: ModelShape, shapeId: number, idxInShape: number, sps: SolidParticleSystem, modelBoundingInfo?: BoundingInfo) {
+            constructor(particleIndex: number, positionIndex: number, indiceIndex: number, model: Nullable<ModelShape>, shapeId: number, idxInShape: number, sps: SolidParticleSystem, modelBoundingInfo: Nullable<BoundingInfo> = null) {
                 this.idx = particleIndex;
                 this._pos = positionIndex;
                 this._ind = indiceIndex;
-                this._model = model;
+                this._model = <ModelShape>model;
                 this.shapeId = shapeId;
                 this.idxInShape = idxInShape;
                 this._sps = sps;
@@ -60,11 +60,11 @@ module BABYLON {
             /**
              * legacy support, changed quaternion to rotationQuaternion
              */ 
-            public get quaternion(): Quaternion {
+            public get quaternion(): Nullable<Quaternion> {
                 return this.rotationQuaternion;
             }
     
-            public set quaternion(q: Quaternion) {
+            public set quaternion(q: Nullable<Quaternion>) {
                 this.rotationQuaternion = q;
             }
     
@@ -89,14 +89,15 @@ module BABYLON {
             public _shape: Vector3[];                   // flat array of model positions
             public _shapeUV: number[];                  // flat array of model UVs
             public _indicesLength: number = 0;          // length of the shape in the model indices array
-            public _positionFunction: (particle: SolidParticle, i: number, s: number) => void;
-            public _vertexFunction: (particle: SolidParticle, vertex: Vector3, i: number) => void;
+            public _positionFunction: Nullable<(particle: SolidParticle, i: number, s: number) => void>;
+            public _vertexFunction: Nullable<(particle: SolidParticle, vertex: Vector3, i: number) => void>;
     
             /**
              * Creates a ModelShape object. This is an internal simplified reference to a mesh used as for a model to replicate particles from by the SPS.
              * SPS internal tool, don't use it manually.  
              */
-            constructor(id: number, shape: Vector3[], indicesLength: number, shapeUV: number[], posFunction: (particle: SolidParticle, i: number, s: number) => void, vtxFunction: (particle: SolidParticle, vertex: Vector3, i: number) => void) {
+            constructor(id: number, shape: Vector3[], indicesLength: number, shapeUV: number[], 
+                            posFunction: Nullable<(particle: SolidParticle, i: number, s: number) => void>, vtxFunction: Nullable<(particle: SolidParticle, vertex: Vector3, i: number) => void>) {
                 this.shapeID = id;
                 this._shape = shape;
                 this._indicesLength = indicesLength;

+ 46 - 39
src/Particles/babylon.solidParticleSystem.ts

@@ -73,7 +73,7 @@
             private _alwaysVisible: boolean = false;
             private _depthSort: boolean = false;
             private _shapeCounter: number = 0;
-            private _copy: SolidParticle = new SolidParticle(null, null, null, null, null, null, null);
+            private _copy: SolidParticle = new SolidParticle(0, 0, 0, null, 0, 0, this);
             private _shape: Vector3[];
             private _shapeUV: number[];
             private _color: Color4 = new Color4(0, 0, 0, 0);
@@ -141,10 +141,10 @@
                 this.name = name;
                 this._scene = scene || Engine.LastCreatedScene;
                 this._camera = <TargetCamera>scene.activeCamera;
-                this._pickable = options ? options.isPickable : false;
-                this._depthSort = options ? options.enableDepthSort : false;
-                this._particlesIntersect = options ? options.particleIntersection : false;
-                this._bSphereOnly= options ? options.boundingSphereOnly : false;
+                this._pickable = options ? <boolean>options.isPickable : false;
+                this._depthSort = options ? <boolean>options.enableDepthSort : false;
+                this._particlesIntersect = options ? <boolean>options.particleIntersection : false;
+                this._bSphereOnly= options ? <boolean>options.boundingSphereOnly : false;
                 this._bSphereRadiusFactor = (options && options.bSphereRadiusFactor) ? options.bSphereRadiusFactor : 1.0;
                 if (options && options.updatable) {
                     this._updatable = options.updatable;
@@ -202,10 +202,10 @@
                 this.mesh.isPickable = this._pickable;
     
                 // free memory
-                this._positions = null;
-                this._normals = null;
-                this._uvs = null;
-                this._colors = null;
+                (<any>this._positions) = null;
+                (<any>this._normals) = null;
+                (<any>this._uvs) = null;
+                (<any>this._colors) = null;
     
                 if (!this._updatable) {
                     this.particles.length = 0;
@@ -225,13 +225,13 @@
             */
             public digest(mesh: Mesh, options?: { facetNb?: number; number?: number; delta?: number }): SolidParticleSystem {
                 var size: number = (options && options.facetNb) || 1;
-                var number: number = (options && options.number);
+                var number: number = (options && options.number) || 0;
                 var delta: number = (options && options.delta) || 0;
-                var meshPos = mesh.getVerticesData(VertexBuffer.PositionKind);
-                var meshInd = mesh.getIndices();
-                var meshUV = mesh.getVerticesData(VertexBuffer.UVKind);
-                var meshCol = mesh.getVerticesData(VertexBuffer.ColorKind);
-                var meshNor = mesh.getVerticesData(VertexBuffer.NormalKind);
+                var meshPos = <FloatArray>mesh.getVerticesData(VertexBuffer.PositionKind);
+                var meshInd = <IndicesArray>mesh.getIndices();
+                var meshUV = <FloatArray>mesh.getVerticesData(VertexBuffer.UVKind);
+                var meshCol = <FloatArray>mesh.getVerticesData(VertexBuffer.ColorKind);
+                var meshNor = <FloatArray>mesh.getVerticesData(VertexBuffer.NormalKind);
     
                 var f: number = 0;                              // facet counter
                 var totalFacets: number = meshInd.length / 3;   // a facet is a triangle, so 3 indices
@@ -477,7 +477,7 @@
             }
     
             // adds a new particle object in the particles array
-            private _addParticle(idx: number, idxpos: number, idxind: number, model: ModelShape, shapeId: number, idxInShape: number, bInfo?: BoundingInfo): SolidParticle {
+            private _addParticle(idx: number, idxpos: number, idxind: number, model: ModelShape, shapeId: number, idxInShape: number, bInfo: Nullable<BoundingInfo> = null): SolidParticle {
                 var sp = new SolidParticle(idx, idxpos, idxind, model, shapeId, idxInShape, this, bInfo);
                 this.particles.push(sp);
                 return sp;
@@ -492,11 +492,11 @@
             * `vertexFunction` is an optional javascript function to called for each vertex of each particle on SPS creation
             */
             public addShape(mesh: Mesh, nb: number, options?: { positionFunction?: any; vertexFunction?: any }): number {
-                var meshPos = mesh.getVerticesData(VertexBuffer.PositionKind);
-                var meshInd = mesh.getIndices();
-                var meshUV = mesh.getVerticesData(VertexBuffer.UVKind);
-                var meshCol = mesh.getVerticesData(VertexBuffer.ColorKind);
-                var meshNor = mesh.getVerticesData(VertexBuffer.NormalKind);
+                var meshPos = <FloatArray>mesh.getVerticesData(VertexBuffer.PositionKind);
+                var meshInd = <IndicesArray>mesh.getIndices();
+                var meshUV = <FloatArray>mesh.getVerticesData(VertexBuffer.UVKind);
+                var meshCol = <FloatArray>mesh.getVerticesData(VertexBuffer.ColorKind);
+                var meshNor = <FloatArray>mesh.getVerticesData(VertexBuffer.NormalKind);
                 var bbInfo;
                 if (this._particlesIntersect) {
                     bbInfo = mesh.getBoundingInfo();
@@ -522,10 +522,10 @@
                         sp = this._addParticle(idx, currentPos, currentInd, modelShape, this._shapeCounter, i, bbInfo);
                         sp.position.copyFrom(currentCopy.position);
                         sp.rotation.copyFrom(currentCopy.rotation);
-                        if (currentCopy.rotationQuaternion) {
+                        if (currentCopy.rotationQuaternion && sp.rotationQuaternion) {
                             sp.rotationQuaternion.copyFrom(currentCopy.rotationQuaternion);
                         }
-                        if (currentCopy.color) {
+                        if (currentCopy.color && sp.color) {
                             sp.color.copyFrom(currentCopy.color);
                         }
                         sp.scaling.copyFrom(currentCopy.scaling);
@@ -674,8 +674,10 @@
                         Vector3.FromFloatsToRef(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE, this._maximum);
                     }
                     else {      // only some particles are updated, then use the current existing BBox basis. Note : it can only increase.
-                        this._minimum.copyFrom(this.mesh._boundingInfo.boundingBox.minimum);
-                        this._maximum.copyFrom(this.mesh._boundingInfo.boundingBox.maximum);
+                        if (this.mesh._boundingInfo) {
+                            this._minimum.copyFrom(this.mesh._boundingInfo.boundingBox.minimum);
+                            this._maximum.copyFrom(this.mesh._boundingInfo.boundingBox.maximum);
+                        }
                     }
                 }
     
@@ -793,7 +795,7 @@
                                 this._normals32[idx + 2] = this._cam_axisX.z * this._rotated.x + this._cam_axisY.z * this._rotated.y + this._cam_axisZ.z * this._rotated.z;                          
                             }
     
-                            if (this._computeParticleColor) {
+                            if (this._computeParticleColor && this._particle.color) {
                                 this._colors32[colidx] = this._particle.color.r;
                                 this._colors32[colidx + 1] = this._particle.color.g;
                                 this._colors32[colidx + 2] = this._particle.color.b;
@@ -820,7 +822,7 @@
                             this._normals32[idx] = 0.0;
                             this._normals32[idx + 1] = 0.0;
                             this._normals32[idx + 2] = 0.0;
-                            if (this._computeParticleColor) {
+                            if (this._computeParticleColor && this._particle.color) {
                                 this._colors32[colidx] = this._particle.color.r;
                                 this._colors32[colidx + 1] = this._particle.color.g;
                                 this._colors32[colidx + 2] = this._particle.color.b;
@@ -964,17 +966,17 @@
                 this.mesh.dispose();
                 this.vars = null;
                 // drop references to internal big arrays for the GC
-                this._positions = null;
-                this._indices = null;
-                this._normals = null;
-                this._uvs = null;
-                this._colors = null;
-                this._positions32 = null;
-                this._normals32 = null;
-                this._fixedNormal32 = null;
-                this._uvs32 = null;
-                this._colors32 = null;
-                this.pickedParticles = null;
+                (<any>this._positions) = null;
+                (<any>this._indices) = null;
+                (<any>this._normals) = null;
+                (<any>this._uvs) = null;
+                (<any>this._colors) = null;
+                (<any>this._positions32) = null;
+                (<any>this._normals32) = null;
+                (<any>this._fixedNormal32) = null;
+                (<any>this._uvs32) = null;
+                (<any>this._colors32) = null;
+                (<any>this.pickedParticles) = null;
             }
     
             /**
@@ -1021,7 +1023,12 @@
             */
             public set isVisibilityBoxLocked(val: boolean) {
                 this._isVisibilityBoxLocked = val;
-                this.mesh.getBoundingInfo().isLocked = val;
+
+                let boundingInfo = this.mesh.getBoundingInfo();
+
+                if (boundingInfo) {
+                    boundingInfo.isLocked = val;
+                }
             }
     
             public get isVisibilityBoxLocked(): boolean {

+ 5 - 1
src/Tools/babylon.decorators.ts

@@ -238,9 +238,13 @@
             return serializationObject;
         }
 
-        public static Parse<T>(creationFunction: () => T, source: any, scene: Nullable<Scene>, rootUrl: string = ""): T {
+        public static Parse<T>(creationFunction: () => T, source: any, scene: Nullable<Scene>, rootUrl: Nullable<string> = null): T {
             var destination = creationFunction();
 
+            if (!rootUrl) {
+                rootUrl = "";
+            }
+
             // Tags
             if (Tags) {
                 Tags.AddTagsTo(destination, source.tags);

+ 2 - 2
src/babylon.node.ts

@@ -90,10 +90,10 @@ module BABYLON {
          * @param {string} name - the name and id to be given to this node
          * @param {BABYLON.Scene} the scene this node will be added to
          */
-        constructor(name: string, scene: Scene) {
+        constructor(name: string, scene: Nullable<Scene> = null) {
             this.name = name;
             this.id = name;
-            this._scene = scene || Engine.LastCreatedScene;
+            this._scene = <Scene>(scene || Engine.LastCreatedScene);
             this.uniqueId = this._scene.getUniqueId();
             this._initCache();
         }

+ 4 - 2
src/babylon.scene.ts

@@ -437,7 +437,7 @@
         private _currentInternalStep: number = 0;
 
         // Mirror
-        public _mirroredCameraPosition: Vector3;
+        public _mirroredCameraPosition: Nullable<Vector3>;
 
         // Keyboard
 
@@ -760,6 +760,7 @@
 
         public _toBeDisposed = new SmartArray<Nullable<IDisposable>>(256);
         private _pendingData = new Array();
+        private _isDisposed = false;
 
         private _activeMeshes = new SmartArray<AbstractMesh>(256);
         private _processedMaterials = new SmartArray<Material>(256);
@@ -3729,10 +3730,11 @@
             }
 
             this._engine.wipeCaches();
+            this._isDisposed = true;
         }
 
         public get isDisposed(): boolean {
-            return !this._engine;
+            return this._isDisposed;
         }
 
         // Release sounds & sounds tracks

+ 2 - 1
src/babylon.types.ts

@@ -3,4 +3,5 @@ type float = number;
 type double = number;
 type int = number;
 
-type FloatArray = number[] | Float32Array;
+type FloatArray = number[] | Float32Array;
+type IndicesArray = number[] | Int32Array | Uint32Array | Uint16Array;

+ 5 - 1
tslint.json

@@ -1,3 +1,7 @@
 {
-	"rules": {	}
+    "rules": {  
+        "no-invalid-this": true,
+        "no-this-assignment": true,
+        "completed-docs": true
+    }
 }