CSS3DStereoRenderer.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * Based on http://www.emagix.net/academic/mscs-project/item/camera-sync-with-css3-and-webgl-threejs
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author yomotsu / https://yomotsu.net/
  5. */
  6. THREE.CSS3DObject = function(element) {
  7. THREE.Object3D.call(this);
  8. this.element = element;
  9. this.element.style.position = 'absolute';
  10. this.addEventListener('removed', function() {
  11. if (this.element.parentNode !== null) {
  12. this.element.parentNode.removeChild(this.element);
  13. }
  14. });
  15. };
  16. THREE.CSS3DObject.prototype = Object.create(THREE.Object3D.prototype);
  17. THREE.CSS3DObject.prototype.constructor = THREE.CSS3DObject;
  18. THREE.CSS3DSprite = function(element) {
  19. THREE.CSS3DObject.call(this, element);
  20. };
  21. THREE.CSS3DSprite.prototype = Object.create(THREE.CSS3DObject.prototype);
  22. THREE.CSS3DSprite.prototype.constructor = THREE.CSS3DSprite;
  23. //
  24. THREE.CSS3DRenderer = function() {
  25. console.log('THREE.CSS3DRenderer', THREE.REVISION);
  26. var _width, _height;
  27. var _widthHalf, _heightHalf;
  28. var matrix = new THREE.Matrix4();
  29. var cache = {
  30. camera: { fov: 0, style: '' },
  31. objects: new WeakMap()
  32. };
  33. var domElement = document.createElement('div');
  34. domElement.style.overflow = 'hidden';
  35. this.domElement = domElement;
  36. var cameraElement = document.createElement('div');
  37. cameraElement.style.WebkitTransformStyle = 'preserve-3d';
  38. cameraElement.style.transformStyle = 'preserve-3d';
  39. domElement.appendChild(cameraElement);
  40. var isIE = /Trident/i.test(navigator.userAgent);
  41. this.getSize = function() {
  42. return {
  43. width: _width,
  44. height: _height
  45. };
  46. };
  47. this.setSize = function(width, height) {
  48. _width = width;
  49. _height = height;
  50. _widthHalf = _width / 2;
  51. _heightHalf = _height / 2;
  52. domElement.style.width = width + 'px';
  53. domElement.style.height = height + 'px';
  54. cameraElement.style.width = width + 'px';
  55. cameraElement.style.height = height + 'px';
  56. };
  57. function epsilon(value) {
  58. return Math.abs(value) < 1e-10 ? 0 : value;
  59. }
  60. function getCameraCSSMatrix(matrix) {
  61. var elements = matrix.elements;
  62. return 'matrix3d(' +
  63. epsilon(elements[0]) + ',' +
  64. epsilon(-elements[1]) + ',' +
  65. epsilon(elements[2]) + ',' +
  66. epsilon(elements[3]) + ',' +
  67. epsilon(elements[4]) + ',' +
  68. epsilon(-elements[5]) + ',' +
  69. epsilon(elements[6]) + ',' +
  70. epsilon(elements[7]) + ',' +
  71. epsilon(elements[8]) + ',' +
  72. epsilon(-elements[9]) + ',' +
  73. epsilon(elements[10]) + ',' +
  74. epsilon(elements[11]) + ',' +
  75. epsilon(elements[12]) + ',' +
  76. epsilon(-elements[13]) + ',' +
  77. epsilon(elements[14]) + ',' +
  78. epsilon(elements[15]) +
  79. ')';
  80. }
  81. function getObjectCSSMatrix(matrix, cameraCSSMatrix) {
  82. var elements = matrix.elements;
  83. var matrix3d = 'matrix3d(' +
  84. epsilon(elements[0]) + ',' +
  85. epsilon(elements[1]) + ',' +
  86. epsilon(elements[2]) + ',' +
  87. epsilon(elements[3]) + ',' +
  88. epsilon(-elements[4]) + ',' +
  89. epsilon(-elements[5]) + ',' +
  90. epsilon(-elements[6]) + ',' +
  91. epsilon(-elements[7]) + ',' +
  92. epsilon(elements[8]) + ',' +
  93. epsilon(elements[9]) + ',' +
  94. epsilon(elements[10]) + ',' +
  95. epsilon(elements[11]) + ',' +
  96. epsilon(elements[12]) + ',' +
  97. epsilon(elements[13]) + ',' +
  98. epsilon(elements[14]) + ',' +
  99. epsilon(elements[15]) +
  100. ')';
  101. if (isIE) {
  102. return 'translate(-50%,-50%)' +
  103. 'translate(' + _widthHalf + 'px,' + _heightHalf + 'px)' +
  104. cameraCSSMatrix +
  105. matrix3d;
  106. }
  107. return 'translate(-50%,-50%)' + matrix3d;
  108. }
  109. function renderObject(object, camera, cameraCSSMatrix) {
  110. if (object instanceof THREE.CSS3DObject) {
  111. var style;
  112. if (object instanceof THREE.CSS3DSprite) {
  113. // http://swiftcoder.wordpress.com/2008/11/25/constructing-a-billboard-matrix/
  114. matrix.copy(camera.matrixWorldInverse);
  115. matrix.transpose();
  116. matrix.copyPosition(object.matrixWorld);
  117. matrix.scale(object.scale);
  118. matrix.elements[3] = 0;
  119. matrix.elements[7] = 0;
  120. matrix.elements[11] = 0;
  121. matrix.elements[15] = 1;
  122. style = getObjectCSSMatrix(matrix, cameraCSSMatrix);
  123. } else {
  124. style = getObjectCSSMatrix(object.matrixWorld, cameraCSSMatrix);
  125. }
  126. var element = object.element;
  127. var cachedStyle = cache.objects.get(object);
  128. if (cachedStyle === undefined || cachedStyle !== style) {
  129. element.style.WebkitTransform = style;
  130. element.style.transform = style;
  131. var objectData = { style: style };
  132. if (isIE) {
  133. objectData.distanceToCameraSquared = getDistanceToSquared(camera, object);
  134. }
  135. cache.objects.set(object, objectData);
  136. }
  137. if (element.parentNode !== cameraElement) {
  138. cameraElement.appendChild(element);
  139. }
  140. }
  141. for (var i = 0, l = object.children.length; i < l; i++) {
  142. renderObject(object.children[i], camera, cameraCSSMatrix);
  143. }
  144. }
  145. var getDistanceToSquared = function() {
  146. var a = new THREE.Vector3();
  147. var b = new THREE.Vector3();
  148. return function(object1, object2) {
  149. a.setFromMatrixPosition(object1.matrixWorld);
  150. b.setFromMatrixPosition(object2.matrixWorld);
  151. return a.distanceToSquared(b);
  152. };
  153. }();
  154. function filterAndFlatten(scene) {
  155. var result = [];
  156. scene.traverse(function(object) {
  157. if (object instanceof THREE.CSS3DObject) result.push(object);
  158. });
  159. return result;
  160. }
  161. function zOrder(scene) {
  162. var sorted = filterAndFlatten(scene).sort(function(a, b) {
  163. var distanceA = cache.objects.get(a).distanceToCameraSquared;
  164. var distanceB = cache.objects.get(b).distanceToCameraSquared;
  165. return distanceA - distanceB;
  166. });
  167. var zMax = sorted.length;
  168. for (var i = 0, l = sorted.length; i < l; i++) {
  169. sorted[i].element.style.zIndex = zMax - i;
  170. }
  171. }
  172. this.render = function(scene, camera) {
  173. var fov = camera.projectionMatrix.elements[5] * _heightHalf;
  174. if (cache.camera.fov !== fov) {
  175. if (camera.isPerspectiveCamera) {
  176. domElement.style.WebkitPerspective = fov + 'px';
  177. domElement.style.perspective = fov + 'px';
  178. }
  179. cache.camera.fov = fov;
  180. }
  181. scene.updateMatrixWorld();
  182. if (camera.parent === null) camera.updateMatrixWorld();
  183. var cameraCSSMatrix = camera.isOrthographicCamera ?
  184. 'scale(' + fov + ')' + getCameraCSSMatrix(camera.matrixWorldInverse) :
  185. 'translateZ(' + fov + 'px)' + getCameraCSSMatrix(camera.matrixWorldInverse);
  186. var style = cameraCSSMatrix +
  187. 'translate(' + _widthHalf + 'px,' + _heightHalf + 'px)';
  188. if (cache.camera.style !== style && !isIE) {
  189. cameraElement.style.WebkitTransform = style;
  190. cameraElement.style.transform = style;
  191. cache.camera.style = style;
  192. }
  193. renderObject(scene, camera, cameraCSSMatrix);
  194. if (isIE) {
  195. // IE10 and 11 does not support 'preserve-3d'.
  196. // Thus, z-order in 3D will not work.
  197. // We have to calc z-order manually and set CSS z-index for IE.
  198. // FYI: z-index can't handle object intersection
  199. zOrder(scene);
  200. }
  201. };
  202. };