Coder Social home page Coder Social logo

node-3d / webgl-raub Goto Github PK

View Code? Open in Web Editor NEW
79.0 4.0 12.0 7.92 MB

OpenGL/WebGL for Node.js

Home Page: https://github.com/node-3d/node-3d

License: MIT License

JavaScript 21.99% C++ 78.01%
addon webgl opengl gl graphics bindings node-3d native javascript cpp

webgl-raub's Introduction

Node3D

NPM ESLint Test

npm i -s 3d-core-raub

Node3D

Desktop 3D applications with Node.js and OpenGL.

  • WebGL-like interface. Real OpenGL though.
  • Three.js compatible environment.
  • Use node modules and compiled addons: CUDA, OpenCL, etc.
  • Window control. Multiwindow applications.
  • Read/write files.
  • Crossplatform: Linux x64, Linux ARM, MacOS x64, Windows x64.

Example

Compatibility with three.js allows porting the existing JS code. The real OpenGL backend is used (not ANGLE). So it is possible to use the GL resource IDs to setup interoperation with CUDA or OpenCL. This is the most important feature of this project and why it was created in the first place.

It is quite possible to create a fully-features apps and games using this framework. For example, see Space Simulation Toolkit.

Quick start

  1. Setup the project directory:

    mkdir my-project
    cd my-project
    npm init -y
    npm i -s 3d-core-raub three
    touch index.js
  2. Paste the code and see if it works:

    const three = require('three');
    const { init, addThreeHelpers } = require('3d-core-raub');
    
    const { doc, gl, requestAnimationFrame } = init({ isGles3: true });
    addThreeHelpers(three, gl);
    
    const renderer = new three.WebGLRenderer();
    renderer.setPixelRatio( doc.devicePixelRatio );
    renderer.setSize( doc.innerWidth, doc.innerHeight );
    
    const camera = new three.PerspectiveCamera(70, doc.innerWidth / doc.innerHeight, 1, 1000);
    camera.position.z = 2;
    const scene = new three.Scene();
    
    const geometry = new three.BoxGeometry();
    const material = new three.MeshBasicMaterial({ color: 0xFACE8D });
    const mesh = new three.Mesh( geometry, material );
    scene.add(mesh);
    
    doc.addEventListener('resize', () => {
    	camera.aspect = doc.innerWidth / doc.innerHeight;
    	camera.updateProjectionMatrix();
    	renderer.setSize(doc.innerWidth, doc.innerHeight);
    });
    
    const animate = () => {
    	requestAnimationFrame(animate);
    	const time = Date.now();
    	mesh.rotation.x = time * 0.0005;
    	mesh.rotation.y = time * 0.001;
    	
    	renderer.render(scene, camera);
    };
    
    animate();
  3. See docs and examples: 3d-core-raub.

  4. Take a look at Three.js examples.

Node3D Modules

  1. Dependency - carries one or more precompiled binary and/or C++ headers.

  2. Addon - provides native bindings.

  3. Plugin - a high-level Node3D module for extending the 3d-core features. For example:

    import { init, addThreeHelpers } from '3d-core-raub';
    import { init as initQml } from '3d-qml-raub';
    const __dirname = dirname(fileURLToPath(import.meta.url));
    const {
    	doc, Image: Img, gl, glfw,
    } = init({
    	isGles3: true, isWebGL2: true, mode: 'borderless',
    });
    addThreeHelpers(three, gl);
    const {
    	QmlOverlay, Property, Method, View, loop, release, textureFromId,
    } = initQml({ doc, gl, cwd: __dirname, three });
  4. Other:

Contributing to Node3D

Bugs and enhancements are tracked as GitHub issues. You can also create an issue on a specific repository of Node3D.

Issues

  • Use a clear and descriptive title.
  • Describe the desired enhancement / problem.
  • Provide examples to demonstrate the issue.
  • If the problem involves a crash, provide its trace log.

Pull Requests

  • Do not include issue numbers in the PR title.
  • Commits use the present tense ("Add feature" not "Added feature").
  • Commits use the imperative mood ("Move cursor to..." not "Moves cursor to...").
  • File System
    • Only lowercase in file/directory names.
    • Words are separated with dashes.
    • If there is an empty directory to be kept, place an empty .keep file inside.
    • Use SemVer versioning pattern.

License

Node3D can be used commercially. You don't have to pay for Node3D or any of its third-party libraries.

Node3D modules have their own code licensed under MIT, meaning "I've just put it here, do what you want, have fun". Some modules have separately licensed third-party software in them. For instance, deps-freeimage-raub carries the FreeImage binaries and headers, and those are the property of their respective owners, and are licensed under FIPL terms (but free to use anyway).

All such cases are explained in README.md per project in question.

webgl-raub's People

Contributors

automatonsystems avatar aximili-dev avatar creationix avatar djulien avatar jiahansu avatar lmeyerov avatar lukaaash avatar maratyszcza avatar mikeseven avatar nicholasbishop avatar raub avatar samzanemesis avatar timknip2 avatar tmpvar avatar trxcllnt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

webgl-raub's Issues

Comparison with headless-gl

Hello! Thank you for this project!
We are using https://github.com/stackgl/headless-gl to render images on node js with pixijs.
It does not support webgl2, so we are trying to find a replacement.
Does your project allow to use webgl2?
Does it provide similar performance to https://github.com/stackgl/headless-gl ?

In order to utilize nvidia gpu we build headless-gl passing

'libraries': [
              '-lEGL',
              '-lGLESv2'
 ],

to linker and it utilizes egl provided by nvidia drivers.
stackgl/headless-gl@master...Lumen5:headless-gl:master

Is it possible to do the same with your library?

Thank you!

Is all extension unavailable?

When I used webgl1 extension, there was an undefined method error and no method bindings in the source code. Is webgl extension not available?

  const ext = gl.getExtension("OES_vertex_array_object"),
    vao = ext.createVertexArrayOES();
  ext.bindVertexArrayOES(vao);

darwin-arm64.zip: 404, not found

Hello. Thanks again for this excellent work! I'm on a MacBook Air M2 these days, and when I run npm install in https://github.com/lume/glas, I now get this error:

npm ERR! code 255
npm ERR! path /Users/trusktr/src/lume+lume/packages/glas/node_modules/deps-opengl-raub
npm ERR! command failed
npm ERR! command sh -c -- node install
npm ERR! https://github.com/node-3d/deps-opengl-raub/releases/download/4.1.0/darwin-arm64.zip
npm ERR! Response status was 404

I'm guessing this isn't compiled for MacBook Air M2 architecture yet?

I thought I was able to install successfully before though.

getExtension breaks with no output

const webgl = require('node-webgl-raub');
webgl.getExtension && webgl.getExtension('WEBGL_color_buffer_float');

const canvas = document.createElement('canvas'); // === doc
const gl = canvas.getContext('webgl'); 
console.log( gl.getExtension('WEBGL_color_buffer_float') );

How to enable argument coercion like the browser's WebGL APIs?

JS_METHOD(vertexAttribPointer) { NAPI_ENV;
REQ_INT32_ARG(0, indx);
REQ_INT32_ARG(1, size);
REQ_INT32_ARG(2, type);
LET_BOOL_ARG(3, normalized);
REQ_INT32_ARG(4, stride);
REQ_OFFS_ARG(5, offset);
GLvoid *vertices = reinterpret_cast<GLvoid*>(offset);
glVertexAttribPointer(indx, size, type, normalized, stride, vertices);
RET_UNDEFINED;
}

I was trying to pass numbers for argument 3 normalized (f.e. 0 or 1) instead of booleans, and this causes the error:

vertexAttrib4fv error
Error: Argument 3 must be of type `Bool` or be `null`/`undefined`

So on the JS side I had to cast my values (which I don't have to do in a browser) like so:

gl.vertexAttribPointer(..., !!normalized, ...)

The browser automatically coerces values, so no need ton manually convert them.

Is there some way to fix this?

Incorrect typescript definitions

The index.d.ts file has some semicolons which cause the following error to be throw when trying to compile a typescript project that uses this library:

node_modules/webgl-raub/index.d.ts:24:4 - error TS1036: Statements are not allowed in ambient contexts.

I've forked the repo and created a fix to be able to use it in my own projects, I've also submitted a pull request with the fix

Missing props for WebGL needed for ThreeJS

Ok,

I am trying to write a React wrapper for glfw-raub and webgl-raub... so i can then use them in react-three-fiber.
I am using 3d-core-raub as a reference implementation

I am using threejs v0.128.x

Got an error that gl.canvas was undefined here.
https://github.com/mrdoob/three.js/blob/da14c33181a90419820c04b5201d66b1401c1581/src/renderers/webgl/WebGLState.js#L1117

^^^ WebGLRenderingContext is supposed to have a reference to the canvas that created it, i guess. i set this manually and got past this error.

ThreeJS expects a WebGLRenderingContext, but you use your WebGL class in all your examples. Its fine if it is the same object but for typescript maybe they could be masked behind getters that return that right context.

in your examples you set viewportWidth & Height, but those properties don't exist...
also tried to use drawingBufferWidth & height and they were null.

Just notes to help anyone else with this issue.

Error: Cannot find module './bin-linux/segfault'

Hello again. I must say, this package is pretty neat! I've gotten lume/glas (port of Three.js to AssemblyScript) to run inside unit tests ran with as-pect (a test runner for AssemblyScript). I polyfilled WebGL on the Node.js side with webgl-raub, then used ASWebGLue (WebGL bindings for AssemblyScript) to set up the JS WebGL glue code and for the WebGL API inside AssemblyScript code, and it works great in Linux (headlessly with xvfb-maybe for unit testing, but it will work great with a window for actual applications). Really awesome that webgl-raub exists to allow such things.

I'm trying to get this setup working in Windows now. First I'm trying in WSL Bash, and when I try to run unit tests I run into this error:

Error: Cannot find module './bin-linux/segfault'
Require stack:
- /mnt/d/src/lume+lume/packages/glas/node_modules/segfault-raub/index.js
- /mnt/d/src/lume+lume/packages/glas/node_modules/webgl-raub/core.js
- /mnt/d/src/lume+lume/packages/glas/node_modules/webgl-raub/js/webgl.js
- /mnt/d/src/lume+lume/packages/glas/node_modules/webgl-raub/index.js

When I check to see what is in the segfault-raub package, I see there is not bin-linux/ folder:

$ ls -l node_modules/segfault-raub/
total 12
-rwxrwxrwx 1 trusktr trusktr  260 Oct 26  1985 index.js
-rwxrwxrwx 1 trusktr trusktr  232 Oct 26  1985 install.js
-rwxrwxrwx 1 trusktr trusktr 1538 Oct 26  1985 LICENSE
-rwxrwxrwx 1 trusktr trusktr 1860 Mar 12 21:27 package.json
-rwxrwxrwx 1 trusktr trusktr 1857 Oct 26  1985 README.md

Any idea what is happening?

Headless rendering

Hi, this is really cool. Is there any way to render headless, without the use of a GUI? It would be nice to be able to get a frame buffers stream in JS.

context.drawImage is not a function

Hi Raub! Thank you so much for your great work here.
I'm reaching to you because i have noticed an error. When the texture is not Pow of 2, this error is being triggered:

THREE.WebGLRenderer 89
C:\Users\elmer\Repos\3d-core-raub\node_modules\node-threejs-raub\build\three.js:18538
                                context.drawImage( image, 0, 0, canvas.width, canvas.height );
                                        ^

TypeError: context.drawImage is not a function
    at makePowerOfTwo (C:\Users\elmer\Repos\3d-core-raub\node_modules\node-threejs-raub\build\three.js:18538:13)
    at uploadTexture (C:\Users\elmer\Repos\3d-core-raub\node_modules\node-threejs-raub\build\three.js:18917:13)
    at WebGLTextures.setTexture2D (C:\Users\elmer\Repos\3d-core-raub\node_modules\node-threejs-raub\build\three.js:18699:6)
    at WebGLRenderer.setTexture2D (C:\Users\elmer\Repos\3d-core-raub\node_modules\node-threejs-raub\build\three.js:23541:14)
    at SingleUniform.setValueT1 [as setValue] (C:\Users\elmer\Repos\3d-core-raub\node_modules\node-threejs-raub\build\three.js:4937:12)
    at Function.WebGLUniforms.upload (C:\Users\elmer\Repos\3d-core-raub\node_modules\node-threejs-raub\build\three.js:5278:7)
    at setProgram (C:\Users\elmer\Repos\3d-core-raub\node_modules\node-threejs-raub\build\three.js:23088:19)
    at WebGLRenderer.renderBufferDirect (C:\Users\elmer\Repos\3d-core-raub\node_modules\node-threejs-raub\build\three.js:21873:18)
    at renderObject (C:\Users\elmer\Repos\3d-core-raub\node_modules\node-threejs-raub\build\three.js:22632:11)
    at renderObjects (C:\Users\elmer\Repos\3d-core-raub\node_modules\node-threejs-raub\build\three.js:22603:6)

I was going to add this bug on the 3d-core-raub repo but after some debugging i noticed the error is on this package. Looks like the context (webgl) doesn't have the drawImage function, and after reviewing the code looks like its actually true. We can easily replicate this error by changing the dimensions of the crate.gif file and run the crate example.

Do you have a workaround for this issue?

Thanks, really appreciate the work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.