123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- var createScene = function () {
- var scene = new BABYLON.Scene(engine);
- var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, new BABYLON.Vector3(0, 0, 0), scene);
- camera.setPosition(new BABYLON.Vector3(20, 200, 400));
- camera.lowerBetaLimit = 0.1;
- camera.upperBetaLimit = (Math.PI / 2) * 0.99;
- camera.lowerRadiusLimit = 150;
- scene.clearColor = new BABYLON.Color3(0, 0, 0);
- // Light
- var light = new BABYLON.PointLight("omni", new BABYLON.Vector3(0, 50, 0), scene);
- // Ground
- var ground = BABYLON.Mesh.CreateGround("ground", 1000, 1000, 1, scene, false);
- var groundMaterial = new BABYLON.StandardMaterial("ground", scene);
- groundMaterial.specularColor = BABYLON.Color3.Black();
- ground.material = groundMaterial;
- // Meshes
- var redSphere = BABYLON.Mesh.CreateSphere("red", 32, 20, scene);
- var redMat = new BABYLON.StandardMaterial("ground", scene);
- redMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
- redMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
- redMat.emissiveColor = BABYLON.Color3.Red();
- redSphere.material = redMat;
- redSphere.position.y = 10;
- redSphere.position.x -= 100;
- var greenBox = BABYLON.Mesh.CreateBox("green", 20, scene);
- var greenMat = new BABYLON.StandardMaterial("ground", scene);
- greenMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
- greenMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
- greenMat.emissiveColor = BABYLON.Color3.Green();
- greenBox.material = greenMat;
- greenBox.position.z -= 100;
- greenBox.position.y = 10;
- var blueBox = BABYLON.Mesh.CreateBox("blue", 20, scene);
- var blueMat = new BABYLON.StandardMaterial("ground", scene);
- blueMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
- blueMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
- blueMat.emissiveColor = BABYLON.Color3.Blue();
- blueBox.material = blueMat;
- blueBox.position.x += 100;
- blueBox.position.y = 10;
- var purpleDonut = BABYLON.Mesh.CreateTorus("red", 30, 10, 32, scene);
- var purpleMat = new BABYLON.StandardMaterial("ground", scene);
- purpleMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
- purpleMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
- purpleMat.emissiveColor = BABYLON.Color3.Purple();
- purpleDonut.material = purpleMat;
- purpleDonut.position.y = 10;
- purpleDonut.position.z += 100;
- // Events
- var canvas = engine.getRenderingCanvas();
- var startingPoint;
- var currentMesh;
- var getGroundPosition = function () {
- // Use a predicate to get position on the ground
- var pickinfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh == ground; });
- if (pickinfo.hit) {
- return pickinfo.pickedPoint;
- }
- return null;
- }
- var onPointerDown = function (evt) {
- if (evt.button !== 0) {
- return;
- }
- // check if we are under a mesh
- var pickInfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh !== ground; });
- if (pickInfo.hit) {
- currentMesh = pickInfo.pickedMesh;
- startingPoint = getGroundPosition(evt);
- if (startingPoint) { // we need to disconnect camera from canvas
- setTimeout(function () {
- camera.detachControl(canvas);
- }, 0);
- }
- }
- }
- var onPointerUp = function () {
- if (startingPoint) {
- camera.attachControl(canvas, true);
- startingPoint = null;
- return;
- }
- }
- var onPointerMove = function (evt) {
- if (!startingPoint) {
- return;
- }
- var current = getGroundPosition(evt);
- if (!current) {
- return;
- }
- var diff = current.subtract(startingPoint);
- currentMesh.position.addInPlace(diff);
- startingPoint = current;
- }
- canvas.addEventListener("pointerdown", onPointerDown, false);
- canvas.addEventListener("pointerup", onPointerUp, false);
- canvas.addEventListener("pointermove", onPointerMove, false);
- scene.onDispose = function () {
- canvas.removeEventListener("pointerdown", onPointerDown);
- canvas.removeEventListener("pointerup", onPointerUp);
- canvas.removeEventListener("pointermove", onPointerMove);
- }
- return scene;
- };
|