Преглед изворни кода

ArcRotateCamera, Mesh and Vector3 new methods and modified OurDemoController of BuildOurOwnBabylonJS

Added zoomOn and focusOn methods to BABYLON.ArcRotateCamera: it takes an array of meshes of parameters (can be undefined to use all scene meshes) on which the camera must zoomOn or only focusOn.
Added MinMax static method to BABYLON.Mesh: it takes an array of meshes of parameters (can be undefined to use all scene meshes) and retrieves the min and max vectors.
Added Center static method to BABYLON.Mesh: it takes an array of meshes of parameters (can be undefined to use all scene meshes) and retrieves the center of the bounding box wrapping all meshes.
Added MinimizeInPlace and MaximizeInPlace methods to BABYLON.Vector3 in the aim to speed up BABYLON.Mesh.MinMax
Added Center static method to BABYLON.Vector3

Now, the Show action of OurDemoController takes two arguments: 'viewName' and 'folder'. 'folder' is the path relative to 'Views/OurDemo' and 'viewName' is the name of the view in this folder. 'folder' is passed to the view so it can be used to retrieve the babylonJS file for example (see sample.cshtml)
Added sample.cshtml to quickly test babylonJS files against your modified version of BabylonJS
Gwenaël Hagenmuller пре 11 година
родитељ
комит
27b37df6f7

+ 32 - 0
Babylon/Cameras/babylon.arcRotateCamera.js

@@ -370,5 +370,37 @@ var BABYLON = BABYLON || {};
 
         return this._viewMatrix;
     };
+
+    BABYLON.ArcRotateCamera.ZOOM_ON_FACTOR = 1;
+    BABYLON.ArcRotateCamera.prototype.zoomOn = function (meshes) {
+        meshes = meshes || this._scene.meshes;
+
+        var minMaxVector = BABYLON.Mesh.MinMax(meshes);
+        var distance = BABYLON.Vector3.Distance(minMaxVector.min, minMaxVector.max);
+
+        this.radius = distance * BABYLON.ArcRotateCamera.ZOOM_ON_FACTOR;
+
+        this.focusOn({min: minMaxVector.min, max: minMaxVector.max, distance: distance});
+    };
+
+    BABYLON.ArcRotateCamera.prototype.focusOn = function (meshesOrMinMaxVectorAndDistance) {
+        var meshesOrMinMaxVector;
+        var distance;
+
+        if (meshesOrMinMaxVectorAndDistance.min === undefined) { // meshes
+            meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance || this._scene.meshes;
+            meshesOrMinMaxVector = BABYLON.Mesh.MinMax(meshesOrMinMaxVector);
+            distance = BABYLON.Vector3.Distance(meshesOrMinMaxVector.min, meshesOrMinMaxVector.max);
+        }
+        else { //minMaxVector and distance
+            meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance;
+            distance = meshesOrMinMaxVectorAndDistance.distance;
+        }
+        
+        this.target = BABYLON.Mesh.Center(meshesOrMinMaxVector);
+        
+        this.maxZ = distance * 2;
+    };
+
 })();
 

+ 24 - 8
Babylon/Math/babylon.math.js

@@ -647,6 +647,18 @@ var BABYLON = BABYLON || {};
         result.z = this.z / otherVector.z;
     };
 
+    BABYLON.Vector3.prototype.MinimizeInPlace = function (other) {
+        if (other.x < this.x) this.x = other.x;
+        if (other.y < this.y) this.y = other.y;
+        if (other.z < this.z) this.z = other.z;
+    };
+
+    BABYLON.Vector3.prototype.MaximizeInPlace = function (other) {
+        if (other.x > this.x) this.x = other.x;
+        if (other.y > this.y) this.y = other.y;
+        if (other.z > this.z) this.z = other.z;
+    };
+
     // Properties
     BABYLON.Vector3.prototype.length = function () {
         return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
@@ -889,17 +901,15 @@ var BABYLON = BABYLON || {};
     };
 
     BABYLON.Vector3.Minimize = function (left, right) {
-        var x = (left.x < right.x) ? left.x : right.x;
-        var y = (left.y < right.y) ? left.y : right.y;
-        var z = (left.z < right.z) ? left.z : right.z;
-        return new BABYLON.Vector3(x, y, z);
+        var min = left.clone();
+        min.MinimizeInPlace(right);
+        return min;
     };
 
     BABYLON.Vector3.Maximize = function (left, right) {
-        var x = (left.x > right.x) ? left.x : right.x;
-        var y = (left.y > right.y) ? left.y : right.y;
-        var z = (left.z > right.z) ? left.z : right.z;
-        return new BABYLON.Vector3(x, y, z);
+        var max = left.clone();
+        max.MaximizeInPlace(right);
+        return max;
     };
 
     BABYLON.Vector3.Distance = function (value1, value2) {
@@ -914,6 +924,12 @@ var BABYLON = BABYLON || {};
         return (x * x) + (y * y) + (z * z);
     };
 
+    BABYLON.Vector3.Center = function (value1, value2) {
+        var center = value1.add(value2);
+        center.scaleInPlace(0.5);
+        return center;
+    };
+
     ////////////////////////////////// Quaternion //////////////////////////////////
 
     BABYLON.Quaternion = function (initialX, initialY, initialZ, initialW) {

+ 26 - 0
Babylon/Mesh/babylon.mesh.js

@@ -1592,4 +1592,30 @@ var BABYLON = BABYLON || {};
             normals[index * 3 + 2] = normal.z;
         }
     };
+
+    BABYLON.Mesh.MinMax = function(meshes) {
+        var minVector;
+        var maxVector;
+        for(var i in meshes) {
+            var mesh = meshes[i];
+            var boundingBox = mesh.getBoundingInfo().boundingBox;
+            if (!minVector) {
+                minVector = boundingBox.minimumWorld;
+                maxVector = boundingBox.maximumWorld;
+                continue;
+            }
+            minVector.MinimizeInPlace(boundingBox.minimumWorld);
+            maxVector.MaximizeInPlace(boundingBox.maximumWorld);
+        }
+
+        return {
+            min: minVector,
+            max: maxVector
+        };
+    };
+
+    BABYLON.Mesh.Center = function(meshesOrMinMaxVector) {
+        var minMaxVector = meshesOrMinMaxVector.min !== undefined ? meshesOrMinMaxVector : BABYLON.Mesh.MinMax(meshesOrMinMaxVector);
+        return BABYLON.Vector3.Center(minMaxVector.min, minMaxVector.max);
+    };
 })();

+ 1 - 1
Tools/BuildOurOwnBabylonJS/BuildOurOwnBabylonJSServer/BuildOurOwnBabylonJSServer.csproj

@@ -66,6 +66,7 @@
   <ItemGroup>
     <Compile Include="Controllers\BabylonJSDemoController.cs" />
     <Compile Include="Controllers\OurDemoController.cs" />
+    <Compile Include="ViewModel\OurDemoViewModel.cs" />
     <Compile Include="WebViewPageExtensions.cs" />
     <Compile Include="Controllers\HomeController.cs" />
     <Compile Include="Controllers\BuildOurOwnBabylonJSController.cs" />
@@ -154,7 +155,6 @@
       <Private>True</Private>
     </ProjectReference>
   </ItemGroup>
-  <ItemGroup />
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

+ 28 - 2
Tools/BuildOurOwnBabylonJS/BuildOurOwnBabylonJSServer/Controllers/OurDemoController.cs

@@ -3,14 +3,40 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.Mvc;
+using System.IO;
+using BuildOurOwnBabylonJSServer.ViewModels;
 
 namespace BuildOurOwnBabylonJSServer.Controllers
 {
     public class OurDemoController : Controller
     {
-        public ActionResult Show(string demo)
+        public ActionResult Show(string viewName, 
+            string folder = "")
         {
-            return View(demo);
+            var form = Request.Form;
+            var queryString = Request.QueryString;
+            var dictionary = new Dictionary<string, string>(form.Count + queryString.Count);
+
+            var keys = form.AllKeys;
+            
+            foreach(var k in keys)
+            {
+                if (k == "viewName" || k == "folder")
+                    continue;
+                dictionary.Add(k, form.GetValues(k).First());
+            }
+
+            keys = queryString.AllKeys;
+
+            foreach (var k in keys)
+            {
+                if (k == "viewName" || k == "folder")
+                    continue;
+                dictionary.Add(k, queryString.GetValues(k).First());
+            }
+
+            return View(Path.Combine(folder, viewName),
+                new OurDemoViewModel { Folder = folder, Dictionary = dictionary });
         }
     }
 }

+ 15 - 0
Tools/BuildOurOwnBabylonJS/BuildOurOwnBabylonJSServer/ViewModel/OurDemoViewModel.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Web.Mvc;
+using System.IO;
+
+namespace BuildOurOwnBabylonJSServer.ViewModels
+{
+    public class OurDemoViewModel
+    {
+        public string Folder { get; set; }
+        public IDictionary<string, string> Dictionary { get; set; }
+    }
+}

+ 47 - 0
Tools/BuildOurOwnBabylonJS/BuildOurOwnBabylonJSServer/Views/OurDemo/sample.cshtml

@@ -0,0 +1,47 @@
+@model OurDemoViewModel
+@using BuildOurOwnBabylonJSServer.ViewModels
+@using System.Text.RegularExpressions
+
+@{
+    ViewBag.Title = "Our Own BabylonJS";
+    ViewBag.BabylonJSFolder = "..\\..\\..\\";
+
+    var myRegex = new Regex(@"[\\]", RegexOptions.Singleline);
+
+    Model.Folder = myRegex.Replace(Model.Folder, @"/");
+    Model.Folder = Model.Folder.Trim('/');
+
+    if (!String.IsNullOrEmpty(Model.Folder))
+    {
+        Model.Folder += "/";
+    }
+}
+
+<script type="text/javascript">
+    $(document).ready(function () {
+        BABYLON.SceneLoader.Load("/Content/@Model.Folder", "@Model.Dictionary["fileBJS"]" + ".babylon", OURBABYLON.engine, function (scene) {
+
+            scene.activeCamera = new BABYLON.ArcRotateCamera("defaultCamera", 0, 0, 100, BABYLON.Vector3.Zero(), scene);
+
+            scene.activeCamera.zoomOn();
+            scene.activeCamera.attachControl(OURBABYLON.canvas);
+
+            var material = new BABYLON.StandardMaterial("default", scene);
+            material.emissiveColor = new BABYLON.Color3(0.7, 0.7, 0.7);
+
+            for (var m in scene.meshes) {
+                scene.meshes[m].material = material;
+            }
+
+            // Render loop
+            var renderLoop = function () {
+                scene.render();
+            };
+
+            // Launch render loop
+            scene.getEngine().runRenderLoop(renderLoop);
+
+            OURBABYLON.currentScene = scene;
+        });
+    });
+</script>