Blame view

js/threejs/renderers/CSS2DRenderer.js 2.25 KB
670b6d6f8   tuan   2016/09/22 - Copy...
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
  /**
   * @author mrdoob / http://mrdoob.com/
   */
  
  THREE.CSS2DObject = function ( element ) {
  
  	THREE.Object3D.call( this );
  
  	this.element = element;
  	this.element.style.position = 'absolute';
  
  	this.addEventListener( 'removed', function ( event ) {
  
  		if ( this.element.parentNode !== null ) {
  
  			this.element.parentNode.removeChild( this.element );
  
  		}
  
  	} );
  
  };
  
  THREE.CSS2DObject.prototype = Object.create( THREE.Object3D.prototype );
  THREE.CSS2DObject.prototype.constructor = THREE.CSS2DObject;
  
  //
  
  THREE.CSS2DRenderer = function () {
  
  	console.log( 'THREE.CSS2DRenderer', THREE.REVISION );
  
  	var _width, _height;
  	var _widthHalf, _heightHalf;
  
  	var vector = new THREE.Vector3();
  	var viewMatrix = new THREE.Matrix4();
  	var viewProjectionMatrix = new THREE.Matrix4();
  
  	var domElement = document.createElement( 'div' );
  	domElement.style.overflow = 'hidden';
  
  	this.domElement = domElement;
  
  	this.setSize = function ( width, height ) {
  
  		_width = width;
  		_height = height;
  
  		_widthHalf = _width / 2;
  		_heightHalf = _height / 2;
  
  		domElement.style.width = width + 'px';
  		domElement.style.height = height + 'px';
  
  	};
  
  	var renderObject = function ( object, camera ) {
  
  		if ( object instanceof THREE.CSS2DObject ) {
  
  			vector.setFromMatrixPosition( object.matrixWorld );
  			vector.applyProjection( viewProjectionMatrix );
  
  			var element = object.element;
  			var style = 'translate(-50%,-50%) translate(' + ( vector.x * _widthHalf + _widthHalf ) + 'px,' + ( - vector.y * _heightHalf + _heightHalf ) + 'px)';
  
  			element.style.WebkitTransform = style;
  			element.style.MozTransform = style;
  			element.style.oTransform = style;
  			element.style.transform = style;
  
  			if ( element.parentNode !== domElement ) {
  
  				domElement.appendChild( element );
  
  			}
  
  		}
  
  		for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  
  			renderObject( object.children[ i ], camera );
  
  		}
  
  	};
  
  	this.render = function ( scene, camera ) {
  
  		scene.updateMatrixWorld();
  
  		if ( camera.parent === null ) camera.updateMatrixWorld();
  
  		camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  
  		viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
  		viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, viewMatrix );
  
  		renderObject( scene, camera );
  
  	};
  
  };