Jump to content

Rendering an iframe on Babylon


Recommended Posts

I was wondering if anybody knows how could I go about rendering an iframe over a plane.

As far as I understand that's not possible with textures, like we do with images or videos, but I found http://learningthreejs.com/blog/2013/04/30/closing-the-gap-between-html-and-webgl/ , where he archived the result I'm looking for with threejs. I was wondering if there is something like the CSS3DRender for Babylon.

 

Link to post
Share on other sites

Yeah, it seems this is not as trivial as I would like it to be. I will look into making the CSS3D hack with Babylon and see how far I get with that, the positioning and projecting doesn't seem that hard given the fact that all Babylon cameras provide easy access to their projection matrices, I still have no clue how to make the blending work though, not to mention that this approach doesn't work for VR.

But the future looks bright, it seems that with the whole webVR thing exploding these days, there are good chances we will get to see iframes to textures soon: 

 

 

Link to post
Share on other sites

You can turn html into an svg and you can load and svg into a dynamic texture.

 

Step 1:  Grab CORS content with a php file to inject it into a div/iframe that now has local origins. (You can skip this if your content is coming from the same origin)
Step 2:  Convert Content to and SVG and load into a temp canvas
Step 3:  Grab Temp Canvas Context as Image data
Step 4: create Dynamic Texture and load that imageData
Step 5: Assign texture to material.

Link to post
Share on other sites

To get you started:
 

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
    <title>iFrame as Texture</title>	
	<style>
        html, body {
            overflow: hidden;
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }
        #renderCanvas {
            width: 100%;
            height: 100%;
            touch-action: none;
        }
    </style>	
	<script src="https://cdn.babylonjs.com/babylon.worker.js"></script>
	<script src="http://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>	
</head>
<body>
<canvas id="renderCanvas"></canvas>
<div id='getData'>
I AM A SVG!!! <br>
WOOT WOOT! <BR>
THIs should work....
</div>
<script>
document.addEventListener("DOMContentLoaded", () => {
  var canvas = document.getElementById("renderCanvas"); // Get the canvas element 
	var engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine
        var createScene = function () {

                        var scene = new BABYLON.Scene(engine);

                        var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 2, BABYLON.Vector3.Zero(), scene);
                        camera.attachControl(canvas, true);

                        var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), scene);
                        var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(0, 1, -1), scene);
							light1.intensity = 0.65;
							light2.intensity = 0.45;

                        var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter:2}, scene);
						setTimeout(()=>{
						console.log('MAKING Texture');						
						html2canvas(document.getElementById('getData'), {}).then(_canvas => {						
							var _ctx = _canvas.getContext('2d');
							sphere.material = new BABYLON.StandardMaterial('mat', scene);							
							var dt = new BABYLON.DynamicTexture('dt', {width:_canvas.width, height:_canvas.height}, scene, false, 1);
							var dctx = dt._context;
							var _iDat = _ctx.getImageData(0,0,_canvas.width, _canvas.height);							
							dctx.putImageData(_iDat, 0, 0);								
							dt.update(false);							
							console.log(dt);							
							sphere.material.diffuseTexture = dt;														
						});						
						},0);
						
                        return scene;
                };     

            var scene = createScene(); 

            engine.runRenderLoop(function () { 
                    scene.render();
            });

            window.addEventListener("resize", function () { 
                    engine.resize();
            });

},false);
</script>		
</body>
</html>

 

Link to post
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...