Advertisement
Guest User

Tubulenz engine sample code

a guest
Jun 6th, 2013
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*{{ javascript("jslib/camera.js") }}*/
  2. /*{{ javascript("jslib/floor.js") }}*/
  3. /*{{ javascript("jslib/mouseforces.js") }}*/
  4. /*{{ javascript("jslib/observer.js") }}*/
  5. /*{{ javascript("jslib/utilities.js") }}*/
  6. /*{{ javascript("jslib/requesthandler.js") }}*/
  7. /*{{ javascript("jslib/services/turbulenzservices.js") }}*/
  8. /*{{ javascript("jslib/services/turbulenzbridge.js") }}*/
  9. /*{{ javascript("jslib/services/gamesession.js") }}*/
  10. /*{{ javascript("jslib/services/mappingtable.js") }}*/
  11. /*{{ javascript("scripts/htmlcontrols.js") }}*/
  12. /*global TurbulenzEngine: true */
  13. /*global RequestHandler: false */
  14. /*global window: false */
  15. /*global Camera: false */
  16. /*global CameraController: false */
  17. /*global Floor: false */
  18. /*global RequestHandler: false */
  19. /*global HTMLControls: false */
  20. /*global MouseForces: false */
  21. /*global TurbulenzServices: false */
  22. TurbulenzEngine.onload = function onloadFn() {
  23.     var errorCallback = function errorCallback(msg) {
  24.         window.alert(msg);
  25.     };
  26.     var graphicsDeviceParameters = {
  27.     };
  28.     var graphicsDevice = TurbulenzEngine.createGraphicsDevice(graphicsDeviceParameters);
  29.     if(!graphicsDevice.shadingLanguageVersion) {
  30.         errorCallback("No shading language support detected.\nPlease check your graphics drivers are up to date.");
  31.         graphicsDevice = null;
  32.         return;
  33.     }
  34.     var mathDeviceParameters = {
  35.     };
  36.     var mathDevice = TurbulenzEngine.createMathDevice(mathDeviceParameters);
  37.     var inputDeviceParameters = {
  38.     };
  39.     var inputDevice = TurbulenzEngine.createInputDevice(inputDeviceParameters);
  40.     var assetsToLoad = 3;
  41.     // Setup camera & controller
  42.     var camera = Camera.create(mathDevice);
  43.     var cameraInitialize = function cameraInitializeFn() {
  44.         var worldUp = mathDevice.v3BuildYAxis();
  45.         camera.lookAt(worldUp, worldUp, mathDevice.v3Build(0.0, 3.0, -15.0));
  46.         camera.updateViewMatrix();
  47.     };
  48.     cameraInitialize();
  49.     var cameraController = CameraController.create(graphicsDevice, inputDevice, camera);
  50.     var floor = Floor.create(graphicsDevice, mathDevice);
  51.     var requestHandlerParameters = {
  52.     };
  53.     var requestHandler = RequestHandler.create(requestHandlerParameters);
  54.     var shader3d = null;
  55.     var technique3d = null;
  56.     var shader3dLoaded = function shader3dLoadedFn(shaderText) {
  57.         if(shaderText) {
  58.             var shaderParameters = JSON.parse(shaderText);
  59.             shader3d = graphicsDevice.createShader(shaderParameters);
  60.             if(shader3d) {
  61.                 technique3d = shader3d.getTechnique("textured3D");
  62.                 assetsToLoad -= 1;
  63.             }
  64.         }
  65.     };
  66.     var sharedTechniqueParameters = graphicsDevice.createTechniqueParameters({
  67.         diffuse: null
  68.     });
  69.     /*jshint white: false*/
  70.     // Vertex buffer parameters for crate
  71.     var vertexbufferParameters = {
  72.         numVertices: 24,
  73.         attributes: [
  74.             'FLOAT3',
  75.             'SHORT2'
  76.         ],
  77.         dynamic: false,
  78.         data: [
  79.             -0.5,
  80.             -0.5,
  81.             0.5,
  82.             0,
  83.             0,
  84.             0.5,
  85.             -0.5,
  86.             0.5,
  87.             1,
  88.             0,
  89.             0.5,
  90.             0.5,
  91.             0.5,
  92.             1,
  93.             1,
  94.             -0.5,
  95.             0.5,
  96.             0.5,
  97.             0,
  98.             1,
  99.             -0.5,
  100.             0.5,
  101.             0.5,
  102.             0,
  103.             0,
  104.             0.5,
  105.             0.5,
  106.             0.5,
  107.             1,
  108.             0,
  109.             0.5,
  110.             0.5,
  111.             -0.5,
  112.             1,
  113.             1,
  114.             -0.5,
  115.             0.5,
  116.             -0.5,
  117.             0,
  118.             1,
  119.             -0.5,
  120.             0.5,
  121.             -0.5,
  122.             1,
  123.             1,
  124.             0.5,
  125.             0.5,
  126.             -0.5,
  127.             0,
  128.             1,
  129.             0.5,
  130.             -0.5,
  131.             -0.5,
  132.             0,
  133.             0,
  134.             -0.5,
  135.             -0.5,
  136.             -0.5,
  137.             1,
  138.             0,
  139.             -0.5,
  140.             -0.5,
  141.             -0.5,
  142.             0,
  143.             0,
  144.             0.5,
  145.             -0.5,
  146.             -0.5,
  147.             1,
  148.             0,
  149.             0.5,
  150.             -0.5,
  151.             0.5,
  152.             1,
  153.             1,
  154.             -0.5,
  155.             -0.5,
  156.             0.5,
  157.             0,
  158.             1,
  159.             0.5,
  160.             -0.5,
  161.             0.5,
  162.             0,
  163.             0,
  164.             0.5,
  165.             -0.5,
  166.             -0.5,
  167.             1,
  168.             0,
  169.             0.5,
  170.             0.5,
  171.             -0.5,
  172.             1,
  173.             1,
  174.             0.5,
  175.             0.5,
  176.             0.5,
  177.             0,
  178.             1,
  179.             -0.5,
  180.             -0.5,
  181.             -0.5,
  182.             0,
  183.             0,
  184.             -0.5,
  185.             -0.5,
  186.             0.5,
  187.             1,
  188.             0,
  189.             -0.5,
  190.             0.5,
  191.             0.5,
  192.             1,
  193.             1,
  194.             -0.5,
  195.             0.5,
  196.             -0.5,
  197.             0,
  198.             1
  199.         ]
  200.     };
  201.     /*jshint white: true*/
  202.     var vertexbuffer = graphicsDevice.createVertexBuffer(vertexbufferParameters);
  203.     var semantics = graphicsDevice.createSemantics([
  204.         graphicsDevice.SEMANTIC_POSITION,
  205.         graphicsDevice.SEMANTIC_TEXCOORD
  206.     ]);
  207.     /*jshint white: false*/
  208.     var indexbufferParameters = {
  209.         numIndices: 36,
  210.         format: 'USHORT',
  211.         dynamic: false,
  212.         data: [
  213.             2,
  214.             0,
  215.             1,
  216.             3,
  217.             0,
  218.             2,
  219.             6,
  220.             4,
  221.             5,
  222.             7,
  223.             4,
  224.             6,
  225.             10,
  226.             8,
  227.             9,
  228.             11,
  229.             8,
  230.             10,
  231.             14,
  232.             12,
  233.             13,
  234.             15,
  235.             12,
  236.             14,
  237.             18,
  238.             16,
  239.             17,
  240.             19,
  241.             16,
  242.             18,
  243.             22,
  244.             20,
  245.             21,
  246.             23,
  247.             20,
  248.             22
  249.         ]
  250.     };
  251.     /*jshint white: true*/
  252.     var indexbuffer = graphicsDevice.createIndexBuffer(indexbufferParameters);
  253.     var primitive = graphicsDevice.PRIMITIVE_TRIANGLES;
  254.     var numIndices = 36;
  255.     // Cache mathDevice functions
  256.     var m43MulM44 = mathDevice.m43MulM44;
  257.     var isVisibleBoxOrigin = mathDevice.isVisibleBoxOrigin;
  258.     var v4Build = mathDevice.v4Build;
  259.     var m43BuildTranslation = mathDevice.m43BuildTranslation;
  260.     var shader2d = null;
  261.     var technique2d = null;
  262.     var shader2dLoaded = function shader2dLoadedFn(shaderText) {
  263.         if(shaderText) {
  264.             var shaderParameters = JSON.parse(shaderText);
  265.             shader2d = graphicsDevice.createShader(shaderParameters);
  266.             if(shader2d) {
  267.                 technique2d = shader2d.getTechnique("constantColor2D");
  268.                 assetsToLoad -= 1;
  269.             }
  270.         }
  271.     };
  272.     var techniqueParameters2d = graphicsDevice.createTechniqueParameters({
  273.         clipSpace: null,
  274.         constantColor: mathDevice.v4Build(0, 0, 0, 1)
  275.     });
  276.     var linePrim = graphicsDevice.PRIMITIVE_LINES;
  277.     var cursorFormat = [
  278.         graphicsDevice.VERTEXFORMAT_FLOAT3
  279.     ];
  280.     var cursorSemantic = graphicsDevice.createSemantics([
  281.         graphicsDevice.SEMANTIC_POSITION
  282.     ]);
  283.     var clearColor = mathDevice.v4Build(0.95, 0.95, 1.0, 1.0);
  284.     //
  285.     // Physics
  286.     //
  287.     var physicsDeviceParameters = {
  288.     };
  289.     var physicsDevice = TurbulenzEngine.createPhysicsDevice(physicsDeviceParameters);
  290.     var dynamicsWorldParameters = {
  291.     };
  292.     var dynamicsWorld = physicsDevice.createDynamicsWorld(dynamicsWorldParameters);
  293.     // Specify the generic settings for the collision objects
  294.     var collisionMargin = 0.005;
  295.     var mass = 20.0;
  296.     var cubeExtents = mathDevice.v3Build(0.5, 0.5, 0.5);
  297.     // Floor is represented by a plane
  298.     var floorShape = physicsDevice.createPlaneShape({
  299.         normal: mathDevice.v3Build(0, 1, 0),
  300.         distance: 0,
  301.         margin: collisionMargin
  302.     });
  303.     var floorObject = physicsDevice.createCollisionObject({
  304.         shape: floorShape,
  305.         transform: mathDevice.m43BuildIdentity(),
  306.         friction: 0.5,
  307.         restitution: 0.3,
  308.         group: physicsDevice.FILTER_STATIC,
  309.         mask: physicsDevice.FILTER_ALL
  310.     });
  311.     // Adds the floor collision object to the physicsDevice
  312.     dynamicsWorld.addCollisionObject(floorObject);
  313.     var boxShape = physicsDevice.createBoxShape({
  314.         halfExtents: cubeExtents,
  315.         margin: collisionMargin
  316.     });
  317.     var inertia = boxShape.inertia;
  318.     inertia = mathDevice.v3ScalarMul(inertia, mass);
  319.     var boxBody = physicsDevice.createRigidBody({
  320.         shape: boxShape,
  321.         mass: mass,
  322.         inertia: inertia,
  323.         transform: mathDevice.m43BuildTranslation(0.0, 1.0, 0.0),
  324.         friction: 0.5,
  325.         restitution: 0.3,
  326.         angularDamping: 0.9,
  327.         frozen: false
  328.     });
  329.     dynamicsWorld.addRigidBody(boxBody);
  330.    
  331.     var doReset = false;
  332.     var reset = function resetFn() {
  333.         var transform = mathDevice.m43BuildIdentity();
  334.         var v3Zero = mathDevice.v3BuildZero();
  335.         var body = boxBody;
  336.         body.transform = mathDevice.m43BuildTranslation(0.0, 1.0, 0.0, transform);
  337.         body.linearVelocity = v3Zero;
  338.         body.angularVelocity = v3Zero;
  339.         body.active = true;
  340.         cameraInitialize();
  341.     };
  342.     // Controls
  343.     var htmlControls = HTMLControls.create();
  344.     htmlControls.addButtonControl({
  345.         id: "button01",
  346.         value: "Reset",
  347.         fn: function schedulResetFn() {
  348.             doReset = true;
  349.         }
  350.     });
  351.     htmlControls.register();
  352.     var keyCodes = inputDevice.keyCodes;
  353.     var mouseCodes = inputDevice.mouseCodes;
  354.     var onKeyUp = function physicsOnkeyupFn(keynum) {
  355.         // If the key R is released we reset the positions
  356.         if(keynum === keyCodes.R) {
  357.             reset();
  358.         }
  359.     };
  360.     // Mouse forces
  361.     var dragMin = mathDevice.v3Build(-1000, 0, -1000);
  362.     var dragMax = mathDevice.v3Build(1000, 1000, 1000);
  363.     var mouseForces = MouseForces.create(graphicsDevice, inputDevice, mathDevice, physicsDevice, dragMin, dragMax);
  364.     var onMouseDown = function (button) {
  365.         if(mouseCodes.BUTTON_0 === button || mouseCodes.BUTTON_1 === button) {
  366.             mouseForces.onmousedown();
  367.         }
  368.     };
  369.     var onMouseUp = function (button) {
  370.         if(mouseCodes.BUTTON_0 === button || mouseCodes.BUTTON_1 === button) {
  371.             mouseForces.onmouseup();
  372.         }
  373.     };
  374.     // Add event listeners
  375.     inputDevice.addEventListener("keyup", onKeyUp);
  376.     inputDevice.addEventListener("mousedown", onMouseDown);
  377.     inputDevice.addEventListener("mouseup", onMouseUp);
  378.     //
  379.     // Update
  380.     //
  381.     var update = function updateFn() {
  382.         inputDevice.update();
  383.         if(doReset) {
  384.             reset();
  385.             doReset = false;
  386.         }
  387.         if(mouseForces.pickedBody) {
  388.             // If we're dragging a body don't apply the movement to the camera
  389.             cameraController.pitch = 0;
  390.             cameraController.turn = 0;
  391.             cameraController.step = 0;
  392.         }
  393.         cameraController.update();
  394.         var aspectRatio = (graphicsDevice.width / graphicsDevice.height);
  395.         if(aspectRatio !== camera.aspectRatio) {
  396.             camera.aspectRatio = aspectRatio;
  397.             camera.updateProjectionMatrix();
  398.         }
  399.         camera.updateViewProjectionMatrix();
  400.         if(0 >= assetsToLoad) {
  401.             mouseForces.update(dynamicsWorld, camera, 0.1);
  402.             dynamicsWorld.update();
  403.         }
  404.         var vp = camera.viewProjectionMatrix;
  405.         var transform = mathDevice.m43BuildIdentity();
  406.         var wvp;
  407.         if(graphicsDevice.beginFrame()) {
  408.             graphicsDevice.clear(clearColor, 1.0, 0);
  409.             floor.render(graphicsDevice, camera);
  410.             if(0 >= assetsToLoad) {
  411.                 graphicsDevice.setStream(vertexbuffer, semantics);
  412.                 graphicsDevice.setIndexBuffer(indexbuffer);
  413.                 graphicsDevice.setTechnique(technique3d);
  414.                 graphicsDevice.setTechniqueParameters(sharedTechniqueParameters);
  415.                 boxBody.calculateTransform(transform);
  416.                 wvp = m43MulM44.call(mathDevice, transform, vp, wvp);
  417.                 if(isVisibleBoxOrigin.call(mathDevice, cubeExtents, wvp)) {
  418.                     // Use directly the active technique when just a single property changes
  419.                     technique3d.worldViewProjection = wvp;
  420.                     graphicsDevice.drawIndexed(primitive, numIndices);
  421.                 }
  422.                 if(!mouseForces.pickedBody) {
  423.                     graphicsDevice.setTechnique(technique2d);
  424.                     var screenWidth = graphicsDevice.width;
  425.                     var screenHeight = graphicsDevice.height;
  426.                     techniqueParameters2d['clipSpace'] = v4Build.call(mathDevice, 2.0 / screenWidth, -2.0 / screenHeight, -1.0, 1.0);
  427.                     graphicsDevice.setTechniqueParameters(techniqueParameters2d);
  428.                     var writer = graphicsDevice.beginDraw(linePrim, 4, cursorFormat, cursorSemantic);
  429.                     if(writer) {
  430.                         var halfWidth = screenWidth * 0.5;
  431.                         var halfHeight = screenHeight * 0.5;
  432.                         writer([
  433.                             halfWidth - 10,
  434.                             halfHeight
  435.                         ]);
  436.                         writer([
  437.                             halfWidth + 10,
  438.                             halfHeight
  439.                         ]);
  440.                         writer([
  441.                             halfWidth,
  442.                             halfHeight - 10
  443.                         ]);
  444.                         writer([
  445.                             halfWidth,
  446.                             halfHeight + 10
  447.                         ]);
  448.                         graphicsDevice.endDraw(writer);
  449.                     }
  450.                 }
  451.             }
  452.             graphicsDevice.endFrame();
  453.         }
  454.     };
  455.     var intervalID = TurbulenzEngine.setInterval(update, 1000 / 60);
  456.     var mappingTableReceived = function mappingTableReceivedFn(mappingTable) {
  457.         var textureParameters = {
  458.             src: mappingTable.getURL("textures/crate.jpg"),
  459.             mipmaps: true,
  460.             onload: function (texture) {
  461.                 sharedTechniqueParameters['diffuse'] = texture;
  462.                 assetsToLoad -= 1;
  463.             }
  464.         };
  465.         graphicsDevice.createTexture(textureParameters);
  466.         requestHandler.request({
  467.             src: mappingTable.getURL("shaders/generic3D.cgfx"),
  468.             onload: shader3dLoaded
  469.         });
  470.         requestHandler.request({
  471.             src: mappingTable.getURL("shaders/generic2D.cgfx"),
  472.             onload: shader2dLoaded
  473.         });
  474.     };
  475.     var gameSessionCreated = function gameSessionCreatedFn(gameSession) {
  476.         TurbulenzServices.createMappingTable(requestHandler, gameSession, mappingTableReceived);
  477.     };
  478.     var gameSession = TurbulenzServices.createGameSession(requestHandler, gameSessionCreated);
  479.     TurbulenzEngine.onunload = function destroyScene() {
  480.         TurbulenzEngine.clearInterval(intervalID);
  481.         if(gameSession) {
  482.             gameSession.destroy();
  483.             gameSession = null;
  484.         }
  485.         requestHandler = null;
  486.         indexbuffer = null;
  487.         vertexbuffer = null;
  488.         semantics = null;
  489.         cursorSemantic = null;
  490.         primitive = null;
  491.         linePrim = null;
  492.         technique3d = null;
  493.         shader3d = null;
  494.         technique2d = null;
  495.         shader2d = null;
  496.         techniqueParameters2d = null;
  497.         sharedTechniqueParameters = null;
  498.         mouseForces = null;
  499.         floor = null;
  500.         cameraController = null;
  501.         camera = null;
  502.         boxBodies = null;
  503.         inertia = null;
  504.         clearColor = null;
  505.         m43MulM44 = null;
  506.         m43BuildTranslation = null;
  507.         isVisibleBoxOrigin = null;
  508.         v4Build = null;
  509.         dragMax = null;
  510.         dragMin = null;
  511.         mouseCodes = null;
  512.         keyCodes = null;
  513.         TurbulenzEngine.flush();
  514.         physicsDevice = null;
  515.         inputDevice = null;
  516.         graphicsDevice = null;
  517.         mathDevice = null;
  518.     };
  519. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement