Advertisement
Guest User

Untitled

a guest
Jun 8th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var createBaseScene = function(engineProxy, headless) {
  2.     // Scene
  3.     var scene = new BABYLON.Scene(engineProxy)
  4.     scene.clearColor = BABYLON.Color3.Purple();
  5.     // Camera
  6.     var camera = new BABYLON.ArcRotateCamera("Camera", 3 * Math.PI / 2, Math.PI / 2, 50, BABYLON.Vector3.Zero(), scene);
  7.     // Box
  8.     var box1 = BABYLON.MeshBuilder.CreateBox("Box", {size:4}, scene);
  9.     box1.position = new BABYLON.Vector3(-2, 4, 0);
  10.     var box2 = BABYLON.MeshBuilder.CreateBox("Box", {size:4}, scene);
  11.     box2.position = new BABYLON.Vector3(-2, 4, 0);    
  12.     // Ground
  13.     var ground = BABYLON.MeshBuilder.CreateBox("Ground", {width: 100, height: 1, depth: 100}, scene);
  14.     ground.position.y = -5.0;
  15.  
  16.     if(!headless) {
  17.         camera.attachControl(canvas, true);
  18.  
  19.         // Light
  20.         var light = new BABYLON.DirectionalLight("dir02", new BABYLON.Vector3(0.2, -1, 0), scene);
  21.         light.position = new BABYLON.Vector3(0, 80, 0);
  22.         // // Shadows
  23.         var shadowGenerator = new BABYLON.ShadowGenerator(2048, light);
  24.         shadowGenerator.addShadowCaster(box1);
  25.         shadowGenerator.addShadowCaster(box2);
  26.         // Materials
  27.         var materialWood = new BABYLON.StandardMaterial("wood", scene);
  28.         materialWood.diffuseTexture = new BABYLON.Texture("textures/crate.png", scene);
  29.         materialWood.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  30.         box1.material = materialWood;
  31.         box2.material = materialWood;
  32.         var groundMat = new BABYLON.StandardMaterial("groundMat", scene);
  33.         groundMat.diffuseColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  34.         groundMat.emissiveColor = new BABYLON.Color3(0.2, 0.2, 0.2);
  35.         groundMat.backFaceCulling = false;
  36.         ground.material = groundMat;
  37.         ground.receiveShadows = true;
  38.     }
  39.  
  40.     // Physics
  41.     const physics = new BABYLON.AmmoJSPlugin(false)
  42.     physics.setTimeStep(1/10)
  43.     physics.setFixedTimeStep(1/10)
  44.  
  45.     scene.enablePhysics(null, physics);
  46.     box1.physicsImpostor = new BABYLON.PhysicsImpostor(box1, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 10, friction: 10, restitution: 0.3 }, scene);
  47.     box2.physicsImpostor = new BABYLON.PhysicsImpostor(box2, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 10, friction: 10, restitution: 0.3 }, scene);
  48.     ground.physicsImpostor = new BABYLON.PhysicsImpostor(ground, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, friction: 10, restitution: 0.7 }, scene);
  49.     box1.physicsImpostor.friction = 10;
  50.     box2.physicsImpostor.friction = 10;
  51.     ground.physicsImpostor.friction = 10;
  52.  
  53.     return [scene, box1, box2, ground, camera]
  54. }
  55.  
  56. var createScene = function () {
  57.     var engine1 = engine
  58.  
  59.     var engine2 = new BABYLON.NullEngine({
  60.         renderWidth: 512,
  61.         renderHeight: 256,
  62.         textureSize: 512,
  63.         deterministicLockstep: false,
  64.         lockstepMaxSteps: 4,
  65.     })
  66.  
  67.     var [scene1, box1, box1a, ground1, camera1] = createBaseScene(engine1, false)
  68.     var [scene2, box2, box2a, ground2, camera2] = createBaseScene(engine2, true)
  69.  
  70.     var lastMousePosition = null
  71.     var grabbed = false
  72.     var grabbedBox = null
  73.  
  74.     box1.position.z = 15
  75.     box1a.position.z = 15
  76.     box2.position.z = 0
  77.     box2a.position.z = 0
  78.  
  79.     var box2Proxy = BABYLON.MeshBuilder.CreateBox("Box", {size:4}, scene1);
  80.     var box2aProxy = BABYLON.MeshBuilder.CreateBox("Box", {size:4}, scene1);
  81.  
  82.     scene1.registerAfterRender(function() {
  83.         scene2.render()
  84.  
  85.         const result = scene1.pick(
  86.             scene1.pointerX,
  87.             scene1.pointerY,
  88.             (mesh) => mesh === ground1)
  89.  
  90.         if(result && result.pickedPoint) {
  91.             lastMousePosition = result.pickedPoint
  92.         }
  93.  
  94.         if(lastMousePosition && grabbed && grabbedBox) {
  95.             const targetPosition = new BABYLON.Vector3(
  96.                 lastMousePosition.x,
  97.                 lastMousePosition.y + 5,
  98.                 lastMousePosition.z
  99.             )
  100.  
  101.             // Position
  102.             grabbedBox.physicsImpostor.applyForce((
  103.                 targetPosition
  104.                     .subtract(grabbedBox.position))
  105.                     .scale(16 * 5 * grabbedBox.physicsImpostor.mass),
  106.                 grabbedBox.getAbsolutePosition());
  107.  
  108.             // Rotation
  109.             const targetRotation = BABYLON.Quaternion.RotationAxis(BABYLON.Vector3.Up(), 0).multiply(BABYLON.Quaternion.Identity())
  110.             grabbedBox.rotationQuaternion = BABYLON.Quaternion.Slerp(grabbedBox.rotationQuaternion, targetRotation, 0.09)
  111.  
  112.             // Dampening
  113.             const linearVelocity = grabbedBox.physicsImpostor.getLinearVelocity()
  114.             const angularVelocity = grabbedBox.physicsImpostor.getAngularVelocity()
  115.  
  116.             if(linearVelocity != null)
  117.                 grabbedBox.physicsImpostor.setLinearVelocity(linearVelocity.scale(0.05))
  118.             if(angularVelocity != null)
  119.                 grabbedBox.physicsImpostor.setAngularVelocity(angularVelocity.scale(0.05))
  120.         }
  121.     })
  122.  
  123.     scene2.registerAfterRender(function() {
  124.         box2Proxy.position = box2.position
  125.         box2Proxy.rotation = box2.rotation
  126.  
  127.         box2aProxy.position = box2a.position
  128.         box2aProxy.rotation = box2a.rotation
  129.     })
  130.  
  131.     function onGrab() {
  132.         const result = scene1.pick(
  133.             scene1.pointerX,
  134.             scene1.pointerY,
  135.             (mesh) => mesh === box1 || mesh === box2Proxy)
  136.  
  137.         if(result.pickedMesh === box2Proxy)
  138.             result.pickedMesh = box2
  139.  
  140.         if(result && result.pickedMesh && !grabbed) {
  141.             grabbed = true
  142.             grabbedBox = result.pickedMesh
  143.             camera1.detachControl(canvas)
  144.         }
  145.     }
  146.  
  147.     function onRelease() {
  148.         camera1.attachControl(canvas, true);
  149.         grabbed = false
  150.     }
  151.  
  152.     canvas.addEventListener('pointerdown', (ev) => onGrab())
  153.     canvas.addEventListener('pointerup', (ev) => onRelease())
  154.  
  155.     function pulse() {
  156.         box1.physicsImpostor.applyForce(BABYLON.Vector3.Up().scale(5000), box1.getAbsolutePosition());
  157.         box2.physicsImpostor.applyForce(BABYLON.Vector3.Up().scale(5000), box2.getAbsolutePosition());
  158.     }
  159.  
  160.     var button = BABYLON.GUI.Button.CreateSimpleButton("Button", "Apply Impulse");
  161.     button.width = 0.2;
  162.     button.height = "40px";
  163.     button.color = "white";
  164.     button.background = "black";
  165.     button.top = "-10px";
  166.     button.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  167.     button.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
  168.     button.onPointerClickObservable.add(pulse);
  169.  
  170.     var ui = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI", true, scene1);
  171.     ui.addControl(button);
  172.  
  173.     return scene1
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement