04-27 14:46
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[WebGL] Embedded HTML 본문

Programming/WebGL(ThreeJS)

[WebGL] Embedded HTML

cinema4dr12 2015. 8. 7. 22:41

이번 예제는 HTML 페이지를 Plane 오브젝트에 Embedding 하는 방법에 대한 것이다.

Download Project

13-01-embedded-html-basic.zip


Click here to view with full screen mode


Operations

Mouse Left Button Click & Drag: Camera Rotating

Mouse Wheel: Camera Zoom In & Out

Mouse Right Button Click & Drag: Camera Panning

main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// standard global variables
var container, scene, camera, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
 
// custom global variables
var rendererCSS;
 
init();
animate();
 
// FUNCTIONS
function init()
{
    // SCENE - WebGL
    scene = new THREE.Scene();
 
    // CAMERA
    var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
    var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
    camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
    scene.add(camera);
    camera.position.set( -958.0034764777636413.81443253358533357.751964735692 );
    camera.rotation.set( -0.04403099677788493-0.2567823882704446-0.011189306356995925 );
    camera.lookAt(new THREE.Vector3( 05000 ));
 
    // RENDERER - WebGL
    if ( Detector.webgl )
        renderer = new THREE.WebGLRenderer( {antialias:true} );
    else
        renderer = new THREE.CanvasRenderer(); 
    renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
    container = document.getElementById'ThreeJS' );
    container.appendChild( renderer.domElement );
 
    // RENDERER - CSS
    rendererCSS    = new THREE.CSS3DRenderer();
    rendererCSS.setSize( window.innerWidth, window.innerHeight );
    rendererCSS.domElement.style.position = 'absolute';
    rendererCSS.domElement.style.top      = 0;
    rendererCSS.domElement.style.margin      = 0;
    rendererCSS.domElement.style.padding  = 0;
    document.body.appendChild( rendererCSS.domElement );
    // when window resizes, also resize this renderer
    THREEx.WindowResize(rendererCSS, camera);
    renderer.domElement.style.position = 'absolute';
    renderer.domElement.style.top      = 0;
    // make sure original renderer appears on top of CSS renderer
    renderer.domElement.style.zIndex   = 1;
    rendererCSS.domElement.appendChild( renderer.domElement );
 
    // EVENTS
    THREEx.WindowResize(renderer, camera);
    THREEx.FullScreen.bindKey({ charCode : 'f'.charCodeAt(0) });
 
    // CONTROLS
    controls = new THREE.OrbitControls( camera, renderer.domElement );
 
    // STATS
    stats = new Stats();
    stats.domElement.style.position = 'absolute';
    stats.domElement.style.bottom = '0px';
    stats.domElement.style.zIndex = 100;
    container.appendChild( stats.domElement );
 
    // LIGHT
    var light = new THREE.PointLight(0xffffff);
    light.position.set(0,250,0);
    light.shadowDarkness = 0.95;
    light.intensity = 2;
    light.castShadow = true;
    scene.add(light);
 
    // FLOOR
 
    // SKYBOX/FOG
    var skyBoxGeometry = new THREE.CubeGeometry( 100001000010000 );
    var skyBoxMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, side: THREE.DoubleSide } );
    var skyBox = new THREE.Mesh( skyBoxGeometry, skyBoxMaterial );
    scene.add(skyBox);
 
    ////////////
    // CUSTOM //
    ////////////
    
    var planeMaterial   = new THREE.MeshBasicMaterial({color: 0x000000, opacity: 0.1, side: THREE.DoubleSide });
    var planeWidth = 1920;
    var planeHeight = 1080;
    var planeGeometry = new THREE.PlaneGeometry( planeWidth, planeHeight );
 
    // create planeMesh1
    var planeMesh1 = new THREE.Mesh( planeGeometry, planeMaterial );
    planeMesh1.position.y += planeHeight/2;
    scene.add(planeMesh1);
 
    // create planeMesh2
    var planeGeometry2 = new THREE.PlaneGeometry( planeWidth, planeHeight );
    var planeMesh2 = new THREE.Mesh( planeGeometry, planeMaterial );
    planeMesh2.position.y += planeHeight/2;
    planeMesh2.position.x = -2000;
    scene.add(planeMesh2);
 
    // create planeMesh3 
    var planeGeometry3 = new THREE.PlaneGeometry( planeWidth, planeHeight );
    var planeMesh3 = new THREE.Mesh( planeGeometry3, planeMaterial );
    planeMesh3.position.y += planeHeight/2;
    planeMesh3.position.x = 2000;
    scene.add(planeMesh3);
    
    // create a new scene to hold CSS
    cssScene = new THREE.Scene();
 
    // create the iframe to contain webpage
    var element1 = document.createElement('iframe');
    var element2 = document.createElement('iframe');
    var element3 = document.createElement('iframe');
    // webpage to be loaded into iframe
    element1.src    = "http://www.threejs.org";
    element2.src    = "http://www.chromeexperiments.com/webgl/";
    element3.src    = "http://helloracer.com/";
    // width of iframe in pixels
    var elementWidth = 1920;
    // force iframe to have same relative dimensions as planeGeometry
    var aspectRatio = planeHeight / planeWidth;
    var elementHeight = elementWidth * aspectRatio;
    element1.style.width  = elementWidth + "px";
    element1.style.height = elementHeight + "px";
    element2.style.width  = elementWidth + "px";
    element2.style.height = elementHeight + "px";
    element3.style.width  = elementWidth + "px";
    element3.style.height = elementHeight + "px";
    
    // create a CSS3DObject to display element
    var cssObject1 = new THREE.CSS3DObject( element1 );
    var cssObject2 = new THREE.CSS3DObject( element2 );
    var cssObject3 = new THREE.CSS3DObject( element3 );
    // synchronize cssObject position/rotation with planeMesh position/rotation 
    cssObject1.position = planeMesh1.position;
    cssObject1.rotation = planeMesh1.rotation;
    cssObject1.scale = planeMesh1.scale;
    cssObject2.position = planeMesh2.position;
    cssObject2.rotation = planeMesh2.rotation;
    cssObject2.scale = planeMesh2.scale;
    cssObject3.position = planeMesh3.position;
    cssObject3.rotation = planeMesh3.rotation;
    cssObject3.scale = planeMesh3.scale;
    // resize cssObject to same size as planeMesh (plus a border)
    var percentBorder = 0.0;
    cssObject1.scale.x /= (1 + percentBorder) * (elementWidth / planeWidth);
    cssObject1.scale.y /= (1 + percentBorder) * (elementWidth / planeWidth);
    cssObject2.scale.x /= (1 + percentBorder) * (elementWidth / planeWidth);
    cssObject2.scale.y /= (1 + percentBorder) * (elementWidth / planeWidth);
    cssObject3.scale.x /= (1 + percentBorder) * (elementWidth / planeWidth);
    cssObject3.scale.y /= (1 + percentBorder) * (elementWidth / planeWidth);
    cssScene.add(cssObject1);
    cssScene.add(cssObject2);
    cssScene.add(cssObject3);
}
 
function animate()
{
    requestAnimationFrame( animate );
    render();        
    update();
}
 
function update()
{
    if ( keyboard.pressed("z") )
    { 
        // do something
    }
    
    controls.update();
    stats.update();
}
 
function render()
{
    // remember to call both renderers - WebGL Render + CSS Render!
    rendererCSS.render( cssScene, camera );
    renderer.render( scene, camera );
}
cs


'Programming > WebGL(ThreeJS)' 카테고리의 다른 글

[WebGL] Vignette Shader  (0) 2015.08.07
[WebGL] Sephia Shader  (0) 2015.08.07
[WebGL] Content Awareness  (0) 2015.08.07
[WebGL] Mouse Dragging  (0) 2015.08.07
[WebGL] Leap Motion Controller 2  (0) 2015.08.07
Comments