Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var createBaseScene = function(engineProxy, headless) {
- // Scene
- var scene = new BABYLON.Scene(engineProxy)
- scene.clearColor = BABYLON.Color3.Purple();
- // Camera
- var camera = new BABYLON.ArcRotateCamera("Camera", 3 * Math.PI / 2, Math.PI / 2, 50, BABYLON.Vector3.Zero(), scene);
- // Box
- var box1 = BABYLON.MeshBuilder.CreateBox("Box", {size:4}, scene);
- box1.position = new BABYLON.Vector3(-2, 4, 0);
- var box2 = BABYLON.MeshBuilder.CreateBox("Box", {size:4}, scene);
- box2.position = new BABYLON.Vector3(-2, 4, 0);
- // Ground
- var ground = BABYLON.MeshBuilder.CreateBox("Ground", {width: 100, height: 1, depth: 100}, scene);
- ground.position.y = -5.0;
- if(!headless) {
- camera.attachControl(canvas, true);
- // Light
- var light = new BABYLON.DirectionalLight("dir02", new BABYLON.Vector3(0.2, -1, 0), scene);
- light.position = new BABYLON.Vector3(0, 80, 0);
- // // Shadows
- var shadowGenerator = new BABYLON.ShadowGenerator(2048, light);
- shadowGenerator.addShadowCaster(box1);
- shadowGenerator.addShadowCaster(box2);
- // Materials
- var materialWood = new BABYLON.StandardMaterial("wood", scene);
- materialWood.diffuseTexture = new BABYLON.Texture("textures/crate.png", scene);
- materialWood.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
- box1.material = materialWood;
- box2.material = materialWood;
- var groundMat = new BABYLON.StandardMaterial("groundMat", scene);
- groundMat.diffuseColor = new BABYLON.Color3(0.5, 0.5, 0.5);
- groundMat.emissiveColor = new BABYLON.Color3(0.2, 0.2, 0.2);
- groundMat.backFaceCulling = false;
- ground.material = groundMat;
- ground.receiveShadows = true;
- }
- // Physics
- const physics = new BABYLON.AmmoJSPlugin(false)
- physics.setTimeStep(1/10)
- physics.setFixedTimeStep(1/10)
- scene.enablePhysics(null, physics);
- box1.physicsImpostor = new BABYLON.PhysicsImpostor(box1, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 10, friction: 10, restitution: 0.3 }, scene);
- box2.physicsImpostor = new BABYLON.PhysicsImpostor(box2, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 10, friction: 10, restitution: 0.3 }, scene);
- ground.physicsImpostor = new BABYLON.PhysicsImpostor(ground, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, friction: 10, restitution: 0.7 }, scene);
- box1.physicsImpostor.friction = 10;
- box2.physicsImpostor.friction = 10;
- ground.physicsImpostor.friction = 10;
- return [scene, box1, box2, ground, camera]
- }
- var createScene = function () {
- var engine1 = engine
- var engine2 = new BABYLON.NullEngine({
- renderWidth: 512,
- renderHeight: 256,
- textureSize: 512,
- deterministicLockstep: false,
- lockstepMaxSteps: 4,
- })
- var [scene1, box1, box1a, ground1, camera1] = createBaseScene(engine1, false)
- var [scene2, box2, box2a, ground2, camera2] = createBaseScene(engine2, true)
- var lastMousePosition = null
- var grabbed = false
- var grabbedBox = null
- box1.position.z = 15
- box1a.position.z = 15
- box2.position.z = 0
- box2a.position.z = 0
- var box2Proxy = BABYLON.MeshBuilder.CreateBox("Box", {size:4}, scene1);
- var box2aProxy = BABYLON.MeshBuilder.CreateBox("Box", {size:4}, scene1);
- scene1.registerAfterRender(function() {
- scene2.render()
- const result = scene1.pick(
- scene1.pointerX,
- scene1.pointerY,
- (mesh) => mesh === ground1)
- if(result && result.pickedPoint) {
- lastMousePosition = result.pickedPoint
- }
- if(lastMousePosition && grabbed && grabbedBox) {
- const targetPosition = new BABYLON.Vector3(
- lastMousePosition.x,
- lastMousePosition.y + 5,
- lastMousePosition.z
- )
- // Position
- grabbedBox.physicsImpostor.applyForce((
- targetPosition
- .subtract(grabbedBox.position))
- .scale(16 * 5 * grabbedBox.physicsImpostor.mass),
- grabbedBox.getAbsolutePosition());
- // Rotation
- const targetRotation = BABYLON.Quaternion.RotationAxis(BABYLON.Vector3.Up(), 0).multiply(BABYLON.Quaternion.Identity())
- grabbedBox.rotationQuaternion = BABYLON.Quaternion.Slerp(grabbedBox.rotationQuaternion, targetRotation, 0.09)
- // Dampening
- const linearVelocity = grabbedBox.physicsImpostor.getLinearVelocity()
- const angularVelocity = grabbedBox.physicsImpostor.getAngularVelocity()
- if(linearVelocity != null)
- grabbedBox.physicsImpostor.setLinearVelocity(linearVelocity.scale(0.05))
- if(angularVelocity != null)
- grabbedBox.physicsImpostor.setAngularVelocity(angularVelocity.scale(0.05))
- }
- })
- scene2.registerAfterRender(function() {
- box2Proxy.position = box2.position
- box2Proxy.rotation = box2.rotation
- box2aProxy.position = box2a.position
- box2aProxy.rotation = box2a.rotation
- })
- function onGrab() {
- const result = scene1.pick(
- scene1.pointerX,
- scene1.pointerY,
- (mesh) => mesh === box1 || mesh === box2Proxy)
- if(result.pickedMesh === box2Proxy)
- result.pickedMesh = box2
- if(result && result.pickedMesh && !grabbed) {
- grabbed = true
- grabbedBox = result.pickedMesh
- camera1.detachControl(canvas)
- }
- }
- function onRelease() {
- camera1.attachControl(canvas, true);
- grabbed = false
- }
- canvas.addEventListener('pointerdown', (ev) => onGrab())
- canvas.addEventListener('pointerup', (ev) => onRelease())
- function pulse() {
- box1.physicsImpostor.applyForce(BABYLON.Vector3.Up().scale(5000), box1.getAbsolutePosition());
- box2.physicsImpostor.applyForce(BABYLON.Vector3.Up().scale(5000), box2.getAbsolutePosition());
- }
- var button = BABYLON.GUI.Button.CreateSimpleButton("Button", "Apply Impulse");
- button.width = 0.2;
- button.height = "40px";
- button.color = "white";
- button.background = "black";
- button.top = "-10px";
- button.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
- button.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
- button.onPointerClickObservable.add(pulse);
- var ui = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI", true, scene1);
- ui.addControl(button);
- return scene1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement