Guest User

Untitled

a guest
Nov 20th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. var THREEx = THREEx || {}
  2.  
  3. THREEx.Portal360 = function(videoImageURL, doorWidth, doorHeight){
  4.  
  5. var doorCenter = new THREE.Group
  6. doorCenter.position.y = doorHeight/2
  7. this.object3d = doorCenter
  8.  
  9. //////////////////////////////////////////////////////////////////////////////
  10. // build texture360
  11. //////////////////////////////////////////////////////////////////////////////
  12. var isVideo = videoImageURL.match(/.(mp4|webm|ogv)/i) ? true : false
  13. if( isVideo ){
  14. var video = document.createElement( 'video' )
  15. video.width = 640;
  16. video.height = 360;
  17. video.loop = true;
  18. video.muted = true;
  19. video.src = videoImageURL;
  20. video.crossOrigin = 'anonymous'
  21. video.setAttribute( 'webkit-playsinline', 'webkit-playsinline' );
  22. video.play();
  23.  
  24. var texture360 = new THREE.VideoTexture( video );
  25. texture360.minFilter = THREE.LinearFilter;
  26. texture360.format = THREE.RGBFormat;
  27. texture360.flipY = false;
  28. }else{
  29. var texture360 = new THREE.TextureLoader().load(videoImageURL)
  30. texture360.minFilter = THREE.NearestFilter;
  31. texture360.format = THREE.RGBFormat;
  32. texture360.flipY = false;
  33. }
  34.  
  35. //////////////////////////////////////////////////////////////////////////////
  36. // build mesh
  37. //////////////////////////////////////////////////////////////////////////////
  38.  
  39. // create insideMesh which is visible IIF inside the portal
  40. var insideMesh = this._buildInsideMesh(texture360, doorWidth, doorHeight)
  41. doorCenter.add(insideMesh)
  42. this.insideMesh = insideMesh
  43.  
  44. // create outsideMesh which is visible IIF outside the portal
  45. var outsideMesh = this._buildOutsideMesh(texture360, doorWidth, doorHeight)
  46. doorCenter.add(outsideMesh)
  47. this.outsideMesh = outsideMesh
  48.  
  49. // create frameMesh for the frame of the portal
  50. var frameMesh = this._buildRectangularFrame(doorWidth/100, doorWidth, doorHeight)
  51. doorCenter.add(frameMesh)
  52. }
  53. //////////////////////////////////////////////////////////////////////////////
  54. // Code Separator
  55. //////////////////////////////////////////////////////////////////////////////
  56. THREEx.Portal360.buildTransparentMaterial = function(){
  57. // if there is a cached version, return it
  58. if( THREEx.Portal360.buildTransparentMaterial.material ){
  59. return THREEx.Portal360.buildTransparentMaterial.material
  60. }
  61. var material = new THREE.MeshBasicMaterial({
  62. colorWrite: false // only write to z-buf
  63. })
  64. // an alternative to reach the same visual - this one seems way slower tho. My guess is it is hitting a slow-path in gpu
  65. // var material = new THREE.MeshBasicMaterial();
  66. // material.color.set('black')
  67. // material.opacity = 0;
  68. // material.blending = THREE.NoBlending;
  69.  
  70. // cache the material
  71. THREEx.Portal360.buildTransparentMaterial.material = material
  72.  
  73. return material
  74. }
  75.  
  76. //////////////////////////////////////////////////////////////////////////////
  77. // Build various cache
  78. //////////////////////////////////////////////////////////////////////////////
  79. THREEx.Portal360.buildSquareCache = function(){
  80. var container = new THREE.Group
  81. // add outter cube - invisibility cloak
  82. var geometry = new THREE.PlaneGeometry(50,50);
  83. var material = THREEx.Portal360.buildTransparentMaterial()
  84.  
  85. var mesh = new THREE.Mesh( geometry, material);
  86. mesh.position.x = geometry.parameters.width/2 + 0.5
  87. mesh.position.y = -geometry.parameters.height/2 + 0.5
  88. container.add(mesh)
  89.  
  90. var mesh = new THREE.Mesh( geometry, material);
  91. mesh.position.x = -geometry.parameters.width/2 + 0.5
  92. mesh.position.y = -geometry.parameters.height/2 - 0.5
  93. container.add(mesh)
  94.  
  95. var mesh = new THREE.Mesh( geometry, material);
  96. mesh.position.x = -geometry.parameters.width/2 - 0.5
  97. mesh.position.y = geometry.parameters.height/2 - 0.5
  98. container.add(mesh)
  99.  
  100. var mesh = new THREE.Mesh( geometry, material);
  101. mesh.position.x = +geometry.parameters.width/2 - 0.5
  102. mesh.position.y = geometry.parameters.height/2 + 0.5
  103. container.add(mesh)
  104.  
  105. return container
  106. }
  107.  
  108. //////////////////////////////////////////////////////////////////////////////
  109. // build meshes
  110. //////////////////////////////////////////////////////////////////////////////
  111.  
  112. /**
  113. * create insideMesh which is visible IIF inside the portal
  114. */
  115. THREEx.Portal360.prototype._buildInsideMesh = function(texture360, doorWidth, doorHeight){
  116. var doorInsideCenter = new THREE.Group
  117.  
  118. // var squareCache = THREEx.Portal360.buildSquareCache()
  119. // squareCache.scale.y = doorWidth
  120. // squareCache.scale.y = doorHeight
  121. // doorInsideCenter.add( squareCache )
  122.  
  123. var geometry = new THREE.PlaneGeometry(doorWidth, doorHeight)
  124. var material = THREEx.Portal360.buildTransparentMaterial()
  125. // var material = new THREE.MeshNormalMaterial()
  126. var mesh = new THREE.Mesh( geometry, material)
  127. mesh.rotation.y = Math.PI
  128. // mesh.position.z = 0.03
  129. doorInsideCenter.add( mesh )
  130.  
  131.  
  132. //////////////////////////////////////////////////////////////////////////////
  133. // add 360 sphere
  134. //////////////////////////////////////////////////////////////////////////////
  135. // add 360 texture
  136. // TODO put that in a this.data
  137. var radius360Sphere = 10
  138. // var radius360Sphere = 1
  139.  
  140. var geometry = new THREE.SphereGeometry( radius360Sphere, 16, 16).rotateZ(Math.PI)
  141. var material = new THREE.MeshBasicMaterial( {
  142. map: texture360,
  143. // opacity: 0.9,
  144. side: THREE.DoubleSide,
  145. });
  146. // var material = new THREE.MeshNormalMaterial()
  147. var sphere360Mesh = new THREE.Mesh( geometry, material );
  148. sphere360Mesh.position.z = -0.1
  149. sphere360Mesh.rotation.y = Math.PI
  150. doorInsideCenter.add(sphere360Mesh)
  151.  
  152. return doorInsideCenter
  153. }
  154.  
  155. /**
  156. * create outsideMesh which is visible IIF outside the portal
  157. */
  158. THREEx.Portal360.prototype._buildOutsideMesh = function(texture360, doorWidth, doorHeight){
  159. var doorOutsideCenter = new THREE.Group
  160.  
  161. //////////////////////////////////////////////////////////////////////////////
  162. // add squareCache
  163. //////////////////////////////////////////////////////////////////////////////
  164. var squareCache = THREEx.Portal360.buildSquareCache()
  165. squareCache.scale.y = doorWidth
  166. squareCache.scale.y = doorHeight
  167. doorOutsideCenter.add( squareCache )
  168.  
  169. //////////////////////////////////////////////////////////////////////////////
  170. // add 360 sphere
  171. //////////////////////////////////////////////////////////////////////////////
  172. // add 360 texture
  173. var radius360Sphere = 10
  174. // var radius360Sphere = 1
  175.  
  176. // build half sphere geometry
  177. var geometry = new THREE.SphereGeometry( radius360Sphere, 16, 16, Math.PI, Math.PI, 0, Math.PI).rotateZ(Math.PI)
  178. // fix UVs
  179. geometry.faceVertexUvs[0].forEach(function(faceUvs){
  180. faceUvs.forEach(function(uv){
  181. uv.x /= 2
  182. })
  183. })
  184. geometry.uvsNeedUpdate = true
  185. var material = new THREE.MeshBasicMaterial( {
  186. map: texture360,
  187. // opacity: 0.9,
  188. side: THREE.BackSide,
  189. });
  190. // var geometry = new THREE.SphereGeometry( radius360Sphere, 16, 16);
  191. // var material = new THREE.MeshNormalMaterial()
  192. var sphere360Mesh = new THREE.Mesh( geometry, material );
  193. sphere360Mesh.position.z = -0.1
  194. doorOutsideCenter.add(sphere360Mesh)
  195.  
  196. return doorOutsideCenter
  197. }
  198.  
  199. /**
  200. * create frameMesh for the frame of the portal
  201. */
  202. THREEx.Portal360.prototype._buildRectangularFrame = function(radius, width, height){
  203. var container = new THREE.Group
  204. var material = new THREE.MeshNormalMaterial()
  205. var material = new THREE.MeshPhongMaterial({
  206. color: 'silver',
  207. emissive: 'green'
  208. })
  209.  
  210. var geometryBeamVertical = new THREE.CylinderGeometry(radius, radius, height - radius)
  211.  
  212. // mesh right
  213. var meshRight = new THREE.Mesh(geometryBeamVertical, material)
  214. meshRight.position.x = width/2
  215. container.add(meshRight)
  216.  
  217. // mesh right
  218. var meshLeft = new THREE.Mesh(geometryBeamVertical, material)
  219. meshLeft.position.x = -width/2
  220. container.add(meshLeft)
  221.  
  222. var geometryBeamHorizontal = new THREE.CylinderGeometry(radius, radius, width - radius).rotateZ(Math.PI/2)
  223.  
  224. // mesh top
  225. var meshTop = new THREE.Mesh(geometryBeamHorizontal, material)
  226. meshTop.position.y = height/2
  227. container.add(meshTop)
  228.  
  229. // mesh bottom
  230. var meshBottom = new THREE.Mesh(geometryBeamHorizontal, material)
  231. meshBottom.position.y = -height/2
  232. container.add(meshBottom)
  233.  
  234. return container
  235. }
  236.  
  237. //////////////////////////////////////////////////////////////////////////////
  238. // update function
  239. //////////////////////////////////////////////////////////////////////////////
  240.  
  241. THREEx.Portal360.prototype.update = function () {
  242. // determine if the user is isOutsidePortal
  243. var localPosition = new THREE.Vector3
  244. this.object3d.worldToLocal(localPosition)
  245. var isOutsidePortal = localPosition.z >= 0 ? true : false
  246.  
  247. // handle mesh visibility based on isOutsidePortal
  248. if( isOutsidePortal ){
  249. this.outsideMesh.visible = true
  250. this.insideMesh.visible = false
  251. }else{
  252. this.outsideMesh.visible = false
  253. this.insideMesh.visible = true
  254. }
  255. }
Add Comment
Please, Sign In to add comment