Selaa lähdekoodia

Merge remote-tracking branch 'refs/remotes/BabylonJS/master'

László Matuska 9 vuotta sitten
vanhempi
commit
1c2c72cad5

+ 22 - 10
Exporters/Unity 5/Unity3D2Babylon/SceneBuilder.Materials.cs

@@ -88,7 +88,6 @@ namespace Unity3D2Babylon
         private void CopyTexture(string texturePath, Texture2D texture2D, BabylonTexture babylonTexture, bool isLightmap = false)
         {
             bool needToDelete = false;
-            var useJPG = !texture2D.alphaIsTransparency;
 
             // Convert unsupported file extensions
             if (texturePath.EndsWith(".psd") || texturePath.EndsWith(".tif") || texturePath.EndsWith(".exr"))
@@ -112,11 +111,8 @@ namespace Unity3D2Babylon
                     textureImporter.grayscaleToAlpha = false;
 
                     AssetDatabase.ImportAsset(texturePath);
-
-                    texturePath = Path.Combine(Path.GetTempPath(), Path.GetFileName(texturePath));
-                    var extension = useJPG ? ".jpg" : ".png";
-                    texturePath = texturePath.Replace(".psd", extension).Replace(".tif", extension).Replace(".exr", extension);
-
+                    
+                    var usePNG = texture2D.alphaIsTransparency;
                     var tempTexture = new Texture2D(texture2D.width, texture2D.height, TextureFormat.ARGB32, false);
 
                     if (isLightmap)
@@ -130,12 +126,22 @@ namespace Unity3D2Babylon
                         }
                         tempTexture.SetPixels(pixels);
                     }
-                    else {
+                    else
+                    {
+                        Color[] pixels = texture2D.GetPixels(0, 0, texture2D.width, texture2D.height);
+                        for (int index = 0; index < pixels.Length; index++)
+                        {
+                            usePNG |= pixels[index].a <= 0.99999f;
+                        }
+                        
                         tempTexture.SetPixels32(texture2D.GetPixels32());
                     }
                     tempTexture.Apply();
 
-                    File.WriteAllBytes(texturePath, useJPG ? tempTexture.EncodeToJPG() : tempTexture.EncodeToPNG());
+                    texturePath = Path.Combine(Path.GetTempPath(), Path.GetFileName(texturePath));
+                    var extension = usePNG ? ".png": ".jpg";
+                    texturePath = texturePath.Replace(".psd", extension).Replace(".tif", extension).Replace(".exr", extension);
+                    File.WriteAllBytes(texturePath, usePNG ? tempTexture.EncodeToPNG() : tempTexture.EncodeToJPG());
 
                     needToDelete = true;
 
@@ -154,6 +160,14 @@ namespace Unity3D2Babylon
                     Debug.LogException(ex);
                 }
             }
+            else if (texture2D.alphaIsTransparency || texturePath.EndsWith(".png"))
+            {
+                babylonTexture.hasAlpha = true;
+            }
+            else
+            {
+                babylonTexture.hasAlpha = false;
+            }
 
             var textureName = Path.GetFileName(texturePath);
             babylonTexture.name = textureName;
@@ -594,8 +608,6 @@ namespace Unity3D2Babylon
             var texture2D = texture as Texture2D;
             if (texture2D)
             {
-                babylonTexture.hasAlpha = texture2D.alphaIsTransparency;
-
                 CopyTexture(texturePath, texture2D, babylonTexture, isLightmap);
             }
             else

+ 2 - 0
src/Canvas2d/babylon.prim2dBase.ts

@@ -2748,6 +2748,8 @@
         protected _spreadActualOpacityChanged() {
             for (let child of this._children) {
                 child._setFlags(SmartPropertyPrim.flagActualOpacityDirty);
+                child._updateRenderMode();
+                child.onPrimBecomesDirty();
                 child._spreadActualOpacityChanged();
             }
         }

+ 1 - 1
src/Canvas2d/babylon.renderablePrim2d.ts

@@ -359,7 +359,7 @@
          * The setter should be used only by implementers of new primitive type.
          */
         public get isTransparent(): boolean {
-            return (this._opacity<1) || this._shouldUseAlphaFromTexture() || this._isPrimTransparent();
+            return (this.actualOpacity<1) || this._shouldUseAlphaFromTexture() || this._isPrimTransparent();
         }
 
         public get renderMode(): number {

+ 33 - 15
src/Mesh/babylon.groundMesh.ts

@@ -4,7 +4,9 @@
 
         private _worldInverse = new Matrix();
         private _heightQuads: { slope: Vector2; facet1: Vector4; facet2: Vector4 }[];
-        public _subdivisions: number;
+        
+        public _subdivisionsX: number;
+        public _subdivisionsY: number;
         public _width: number;
         public _height: number;
         public _minX: number;
@@ -17,12 +19,21 @@
         }
 
         public get subdivisions(): number {
-            return this._subdivisions;
+            return Math.min(this._subdivisionsX, this._subdivisionsY);
+        }
+
+        public get subdivisionsX(): number {
+            return this._subdivisionsX;
+        }
+
+        public get subdivisionsY(): number {
+            return this._subdivisionsY;
         }
 
         public optimize(chunksCount: number, octreeBlocksSize = 32): void {
-            this._subdivisions = chunksCount;
-            this.subdivide(this._subdivisions);
+            this._subdivisionsX = chunksCount;
+            this._subdivisionsY = chunksCount;
+            this.subdivide(chunksCount);
             this.createOrUpdateSubmeshesOctree(octreeBlocksSize);
         }
 
@@ -103,9 +114,11 @@
         // Returns the element "facet" from the heightQuads array relative to (x, z) local coordinates
         private _getFacetAt(x: number, z: number): Vector4 {
             // retrieve col and row from x, z coordinates in the ground local system
-            var col = Math.floor((x + this._maxX) * this._subdivisions / this._width);
-            var row = Math.floor(-(z + this._maxZ) * this._subdivisions / this._height + this._subdivisions);
-            var quad = this._heightQuads[row * this._subdivisions + col];
+            var subdivisionsX = this._subdivisionsX;
+            var subdivisionsY = this._subdivisionsY;
+            var col = Math.floor((x + this._maxX) * this._subdivisionsX / this._width);
+            var row = Math.floor(-(z + this._maxZ) * this._subdivisionsY / this._height + this._subdivisionsY);
+            var quad = this._heightQuads[row * this._subdivisionsX + col];
             var facet;
             if (z < quad.slope.x * x + quad.slope.y) {
                 facet = quad.facet1;
@@ -121,11 +134,13 @@
         // facet1 : Vector4(a, b, c, d) = first facet 3D plane equation : ax + by + cz + d = 0
         // facet2 :  Vector4(a, b, c, d) = second facet 3D plane equation : ax + by + cz + d = 0
         private _initHeightQuads(): void {
+            var subdivisionsX = this._subdivisionsX;
+            var subdivisionsY = this._subdivisionsY;
             this._heightQuads = new Array();
-            for (var row = 0; row < this._subdivisions; row++) {
-                for (var col = 0; col < this._subdivisions; col++) {
+            for (var row = 0; row < subdivisionsY; row++) {
+                for (var col = 0; col < subdivisionsX; col++) {
                     var quad = { slope: BABYLON.Vector2.Zero(), facet1: new BABYLON.Vector4(0, 0, 0, 0), facet2: new BABYLON.Vector4(0, 0, 0, 0) };
-                    this._heightQuads[row * this._subdivisions + col] = quad;
+                    this._heightQuads[row * subdivisionsX + col] = quad;
                 }
             }
         }
@@ -153,11 +168,14 @@
             var d1 = 0;     // facet plane equation : ax + by + cz + d = 0
             var d2 = 0;
 
-            for (var row = 0; row < this._subdivisions; row++) {
-                for (var col = 0; col < this._subdivisions; col++) {
+            var subdivisionsX = this._subdivisionsX;
+            var subdivisionsY = this._subdivisionsY;
+
+            for (var row = 0; row < subdivisionsY; row++) {
+                for (var col = 0; col < subdivisionsX; col++) {
                     i = col * 3;
-                    j = row * (this._subdivisions + 1) * 3;
-                    k = (row + 1) * (this._subdivisions + 1) * 3;
+                    j = row * (subdivisionsX + 1) * 3;
+                    k = (row + 1) * (subdivisionsX + 1) * 3;
                     v1.x = positions[j + i];
                     v1.y = positions[j + i + 1];
                     v1.z = positions[j + i + 2];
@@ -190,7 +208,7 @@
                     d1 = -(norm1.x * v1.x + norm1.y * v1.y + norm1.z * v1.z);
                     d2 = -(norm2.x * v2.x + norm2.y * v2.y + norm2.z * v2.z);
 
-                    var quad = this._heightQuads[row * this._subdivisions + col];
+                    var quad = this._heightQuads[row * subdivisionsX + col];
                     quad.slope.copyFromFloats(cd, h);
                     quad.facet1.copyFromFloats(norm1.x, norm1.y, norm1.z, d1);
                     quad.facet2.copyFromFloats(norm2.x, norm2.y, norm2.z, d2);

+ 15 - 14
src/Mesh/babylon.mesh.vertexData.ts

@@ -1180,7 +1180,7 @@
             return vertexData;
         }
 
-        public static CreateGround(options: { width?: number, height?: number, subdivisions?: number }): VertexData {
+        public static CreateGround(options: { width?: number, height?: number, subdivisions?: number, subdivisionsX?: number, subdivisionsY?: number }): VertexData {
             var indices = [];
             var positions = [];
             var normals = [];
@@ -1189,28 +1189,29 @@
 
             var width: number = options.width || 1;
             var height: number = options.height || 1;
-            var subdivisions: number = options.subdivisions || 1;
+            var subdivisionsX: number = options.subdivisionsX || options.subdivisions || 1;
+            var subdivisionsY: number = options.subdivisionsY || options.subdivisions || 1;
 
-            for (row = 0; row <= subdivisions; row++) {
-                for (col = 0; col <= subdivisions; col++) {
-                    var position = new Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
+            for (row = 0; row <= subdivisionsY; row++) {
+                for (col = 0; col <= subdivisionsX; col++) {
+                    var position = new Vector3((col * width) / subdivisionsX - (width / 2.0), 0, ((subdivisionsY - row) * height) / subdivisionsY - (height / 2.0));
                     var normal = new Vector3(0, 1.0, 0);
 
                     positions.push(position.x, position.y, position.z);
                     normals.push(normal.x, normal.y, normal.z);
-                    uvs.push(col / subdivisions, 1.0 - row / subdivisions);
+                    uvs.push(col / subdivisionsX, 1.0 - row / subdivisionsX);
                 }
             }
 
-            for (row = 0; row < subdivisions; row++) {
-                for (col = 0; col < subdivisions; col++) {
-                    indices.push(col + 1 + (row + 1) * (subdivisions + 1));
-                    indices.push(col + 1 + row * (subdivisions + 1));
-                    indices.push(col + row * (subdivisions + 1));
+            for (row = 0; row < subdivisionsY; row++) {
+                for (col = 0; col < subdivisionsX; col++) {
+                    indices.push(col + 1 + (row + 1) * (subdivisionsX + 1));
+                    indices.push(col + 1 + row * (subdivisionsX + 1));
+                    indices.push(col + row * (subdivisionsX + 1));
 
-                    indices.push(col + (row + 1) * (subdivisions + 1));
-                    indices.push(col + 1 + (row + 1) * (subdivisions + 1));
-                    indices.push(col + row * (subdivisions + 1));
+                    indices.push(col + (row + 1) * (subdivisionsX + 1));
+                    indices.push(col + 1 + (row + 1) * (subdivisionsX + 1));
+                    indices.push(col + row * (subdivisionsX + 1));
                 }
             }
 

+ 5 - 3
src/Mesh/babylon.meshBuilder.ts

@@ -583,10 +583,11 @@
          * The parameter `subdivisions` (positive integer) sets the number of subdivisions per side.       
          * 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 CreateGround(name: string, options: { width?: number, height?: number, subdivisions?: number, updatable?: boolean }, scene: any): Mesh {
+        public static CreateGround(name: string, options: { width?: number, height?: number, subdivisions?: number, subdivisionsX?: number, subdivisionsY?: number, updatable?: boolean }, scene: any): Mesh {
             var ground = new GroundMesh(name, scene);
             ground._setReady(false);
-            ground._subdivisions = options.subdivisions || 1;
+            ground._subdivisionsX = options.subdivisionsX || options.subdivisions || 1;
+            ground._subdivisionsY = options.subdivisionsY || options.subdivisions || 1;
             ground._width = options.width || 1;
             ground._height = options.height || 1;
             ground._maxX = ground._width / 2;
@@ -651,7 +652,8 @@
             var onReady = options.onReady;
 
             var ground = new GroundMesh(name, scene);
-            ground._subdivisions = subdivisions;
+            ground._subdivisionsX = subdivisions;
+            ground._subdivisionsY = subdivisions;
             ground._width = width;
             ground._height = height;
             ground._maxX = ground._width / 2.0;