nicktendo Posted July 14, 2017 Share Posted July 14, 2017 Would it be possible to create a BabylonJS scene within the context of a NodeJS server simply to recreate the environment of a player and verify valid movement/collisions? The BabylonJS scene on the server would obviously not need to paint anything anywhere, but would simply need to calculate mesh positions/collisions/etc. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted July 14, 2017 Share Posted July 14, 2017 Hello I have the plan to work on a headless version of babylon.js that could be used in this scenario: https://github.com/BabylonJS/Babylon.js/issues/2529 HeadClot, iiceman and nicktendo 3 Quote Link to comment Share on other sites More sharing options...
nicktendo Posted July 14, 2017 Author Share Posted July 14, 2017 @Deltakosh Thank you for the reply Is there a "hack" way to accomplish this with the current version of BJS? Quote Link to comment Share on other sites More sharing options...
nicktendo Posted July 14, 2017 Author Share Posted July 14, 2017 @Deltakosh I came across a thread in which @RaananW suggested simply overriding the render function as such: http://www.babylonjs-playground.com/#1WNTNW Would this be the best way to run a "headless" version of BJS at current? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted July 14, 2017 Share Posted July 14, 2017 This won't work as you still need a canvas to initialize the engine Quote Link to comment Share on other sites More sharing options...
nicktendo Posted July 14, 2017 Author Share Posted July 14, 2017 @Deltakosh How about this: https://github.com/Automattic/node-canvas Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted July 14, 2017 Share Posted July 14, 2017 Don't think either as there is no webgl context Quote Link to comment Share on other sites More sharing options...
nicktendo Posted July 14, 2017 Author Share Posted July 14, 2017 @Deltakosh How about this : https://github.com/stackgl/headless-gl Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted July 17, 2017 Share Posted July 17, 2017 This one looks really promising! nicktendo 1 Quote Link to comment Share on other sites More sharing options...
nicktendo Posted July 18, 2017 Author Share Posted July 18, 2017 @Deltakosh I'll experiment with using headless-gl as the WebGL context for BJS later today and update with my results.. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted July 18, 2017 Share Posted July 18, 2017 Please do. This would be useful for everyone Quote Link to comment Share on other sites More sharing options...
nicktendo Posted July 24, 2017 Author Share Posted July 24, 2017 @Deltakosh A quick update, I've successfully gotten BJS to run within a NodeJS app. The dependencies are as follows: gl: https://www.npmjs.com/package/gl canvas: https://www.npmjs.com/package/canvas jsdom: https://www.npmjs.com/package/jsdom A version of NodeJS that utilizes Node Module version 48 to be compatible with canvas: https://nodejs.org/en/download/releases/ A few modifications had to be made to Babylon.js: 1. Define the following variables at the top of Babylon.js: var window = {}; window.location = {}; window.location.href = ""; var navigator = {}; 2. Add an argument to the engine constructor to pass a window variable from jsdom: function Engine(canvas, antialias, nodeWindow, options, adaptToDeviceRatio) { window = nodeWindow; document = window.document; navigator = window.navigator; 3. Comment out the following code inside Effect.prototype._loadVertexShader: if (vertex instanceof HTMLElement) { var vertexCode = BABYLON.Tools.GetDOMTextContent(vertex); callback(vertexCode); return; } Without this comment an error is thrown when attempting to resolve 'HTMLElement', I'm sure there's a more comprehensive solution to this problem. Here is the NodeJS app: var gl = require('gl')(1920, 1080, { preserveDrawingBuffer: true }); var canvas = require('canvas'); const jsdom = require('jsdom'); const { JSDOM } = jsdom; var BABYLON = require('./babylon.max.js'); var dom = new JSDOM(); var window = dom.window; canvas.getContext = function(thecontext,options){ return gl; } canvas.addEventListener = function (event,options){ return 1; } var engine = new BABYLON.Engine(canvas, true, window); var scene = new BABYLON.Scene(engine); // Enable Collisions scene.collisionsEnabled = true; var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene); var sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene); sphere.checkCollisions = true; sphere.position.y = 5; var speed = 0.01; scene.registerBeforeRender(function() { console.log(sphere.position.x); sphere.position.x += speed; }); engine.runRenderLoop(function () { scene.render(); }); console.log("ran") As of now the default vertex shader isn't able to be loaded properly, but as this is a headless implementation shaders probably aren't very relevant. I'll ultimately find a permanent solution for this error. I'm sure there is a significant amount of simplification that could be done to the engine implementation overall since it's running in a headless configuration, but this seems to be working for now. I wanted to minimize my modifications to the engine so I could use as close to a duplicate implementation of the browser version as possible I've attached the NodeJS project files for reference. babylon.max.js index.js package.json GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.