Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two.js and Three.js in same canvas Element #226

Closed
jackbach opened this issue Jun 20, 2017 · 5 comments
Closed

Two.js and Three.js in same canvas Element #226

jackbach opened this issue Jun 20, 2017 · 5 comments
Assignees
Labels

Comments

@jackbach
Copy link

Hey There ✌️✨

I'm trying to render Two.js and Three.js in the same WebGL canvas.

What I'm trying to do:

  1. disable autoclear in both Two.js and Three.js renderers
  2. render Two.js scene to canvas
  3. render Three.js scene to canvas
  4. Clear and go to (2)

I think I'm getting there but i have this error thrown by two.js

WebGL: INVALID_OPERATION: uniformMatrix3fv: location is not from current program

You can see it live in this codepen

Any idea on how to fix this? Thanks 😄

By the way, is this a good idea, or would it be better to use different canvas with alpha?

@jonobr1 jonobr1 self-assigned this Jun 20, 2017
@jonobr1
Copy link
Owner

jonobr1 commented Jun 20, 2017

This is a great question. I've never seen anyone mix the two as you are doing... Unfortunately, at this time both Two.js and Three.js internally create their own GL Programs. The result is that you can't mix and match things on the same context.

You can mix and match primitive objects as in the Puzzle Piece Example and you can also render Two.js to a canvas and then apply that to a Three.js scene either through a texture or Render Target. I'm in transit now, but will update this with links when I'm back at a computer with stable internet.

@jackbach
Copy link
Author

jackbach commented Jun 26, 2017 via email

@jonobr1
Copy link
Owner

jonobr1 commented Jul 7, 2017

Interesting! In theory there should be a way, but you might have to modify the Two.js source because at the moment there isn't a way to get the GL program from within the closure of Two.js' WebGL Renderer...

This is kind of random, but have you tried instantiating Three.js first and then passing the canvas to a new instance of Two.js? I don't imagine it will yield any better results, but I'd be interested to know what the errors are -- if they're the same or different.

@jonobr1
Copy link
Owner

jonobr1 commented Jul 7, 2017

Actually, scratch all of that. You can get access to the program via: two.renderer.program. So, if you can get the program out of Three.js (which I think you have to modify the source for in order to do this). You can set the two.renderer.program to it. Wherever you set that reference also add this code once:

    // look up where the vertex data needs to go.
    program.position = gl.getAttribLocation(this.program, 'a_position');
    program.matrix = gl.getUniformLocation(this.program, 'u_matrix');
    program.textureCoords = gl.getAttribLocation(this.program, 'a_textureCoords');

    var gl = two.renderer.ctx;

    // Copied from Three.js WebGLRenderer
    gl.disable(gl.DEPTH_TEST);

    // Setup some initial statements of the gl context
    gl.enable(gl.BLEND);

    // https://code.google.com/p/chromium/issues/detail?id=316393
    // gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, gl.TRUE);

    gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD);
    gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA,
      gl.ONE, gl.ONE_MINUS_SRC_ALPHA );

And in theory you should be able to render a Two.js scene to that canvas. Don't know how it'll affect the Three.js rendering... Please let me know if you try this out!

@jonobr1
Copy link
Owner

jonobr1 commented Sep 18, 2017

Moving forward to find the solution for cross library use of the Two.js GL context we'll be having discussion on this thread: #249

@jonobr1 jonobr1 closed this as completed Sep 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants