three.shim.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. import * as THREE from "../../libs/three.js/build/three.module.js";
  2. THREE.WebGLRenderer.prototype.paramThreeToGL = function(e) {
  3. var t, i = this.extensions, r = this.getContext();//context;
  4. if (e === THREE.RepeatWrapping)
  5. return r.REPEAT;
  6. if (e === THREE.ClampToEdgeWrapping)
  7. return r.CLAMP_TO_EDGE;
  8. if (e === THREE.MirroredRepeatWrapping)
  9. return r.MIRRORED_REPEAT;
  10. if (e === THREE.NearestFilter)
  11. return r.NEAREST;
  12. if (e === THREE.NearestMipMapNearestFilter)
  13. return r.NEAREST_MIPMAP_NEAREST;
  14. if (e === THREE.NearestMipMapLinearFilter)
  15. return r.NEAREST_MIPMAP_LINEAR;
  16. if (e === THREE.LinearFilter)
  17. return r.LINEAR;
  18. if (e === THREE.LinearMipMapNearestFilter)
  19. return r.LINEAR_MIPMAP_NEAREST;
  20. if (e === THREE.LinearMipMapLinearFilter)
  21. return r.LINEAR_MIPMAP_LINEAR;
  22. if (e === THREE.UnsignedByteType)
  23. return r.UNSIGNED_BYTE;
  24. if (e === THREE.UnsignedShort4444Type)
  25. return r.UNSIGNED_SHORT_4_4_4_4;
  26. if (e === THREE.UnsignedShort5551Type)
  27. return r.UNSIGNED_SHORT_5_5_5_1;
  28. if (e === THREE.UnsignedShort565Type)
  29. return r.UNSIGNED_SHORT_5_6_5;
  30. if (e === THREE.ByteType)
  31. return r.BYTE;
  32. if (e === THREE.ShortType)
  33. return r.SHORT;
  34. if (e === THREE.UnsignedShortType)
  35. return r.UNSIGNED_SHORT;
  36. if (e === THREE.IntType)
  37. return r.INT;
  38. if (e === THREE.UnsignedIntType)
  39. return r.UNSIGNED_INT;
  40. if (e === THREE.FloatType)
  41. return r.FLOAT;
  42. if (t = i.get("OES_texture_half_float"),
  43. null !== t && e === THREE.HalfFloatType)
  44. return t.HALF_FLOAT_OES;
  45. if (e === THREE.AlphaFormat)
  46. return r.ALPHA;
  47. if (e === THREE.RGBFormat)
  48. return r.RGB;
  49. if (e === THREE.RGBAFormat)
  50. return r.RGBA;
  51. if (e === THREE.LuminanceFormat)
  52. return r.LUMINANCE;
  53. if (e === THREE.LuminanceAlphaFormat)
  54. return r.LUMINANCE_ALPHA;
  55. if (e === THREE.AddEquation)
  56. return r.FUNC_ADD;
  57. if (e === THREE.SubtractEquation)
  58. return r.FUNC_SUBTRACT;
  59. if (e === THREE.ReverseSubtractEquation)
  60. return r.FUNC_REVERSE_SUBTRACT;
  61. if (e === THREE.ZeroFactor)
  62. return r.ZERO;
  63. if (e === THREE.OneFactor)
  64. return r.ONE;
  65. if (e === THREE.SrcColorFactor)
  66. return r.SRC_COLOR;
  67. if (e === THREE.OneMinusSrcColorFactor)
  68. return r.ONE_MINUS_SRC_COLOR;
  69. if (e === THREE.SrcAlphaFactor)
  70. return r.SRC_ALPHA;
  71. if (e === THREE.OneMinusSrcAlphaFactor)
  72. return r.ONE_MINUS_SRC_ALPHA;
  73. if (e === THREE.DstAlphaFactor)
  74. return r.DST_ALPHA;
  75. if (e === THREE.OneMinusDstAlphaFactor)
  76. return r.ONE_MINUS_DST_ALPHA;
  77. if (e === THREE.DstColorFactor)
  78. return r.DST_COLOR;
  79. if (e === THREE.OneMinusDstColorFactor)
  80. return r.ONE_MINUS_DST_COLOR;
  81. if (e === THREE.SrcAlphaSaturateFactor)
  82. return r.SRC_ALPHA_SATURATE;
  83. if (t = i.get("WEBGL_compressed_texture_s3tc"),
  84. null !== t) {
  85. if (e === THREE.RGB_S3TC_DXT1_Format)
  86. return t.COMPRESSED_RGB_S3TC_DXT1_EXT;
  87. if (e === THREE.RGBA_S3TC_DXT1_Format)
  88. return t.COMPRESSED_RGBA_S3TC_DXT1_EXT;
  89. if (e === THREE.RGBA_S3TC_DXT3_Format)
  90. return t.COMPRESSED_RGBA_S3TC_DXT3_EXT;
  91. if (e === THREE.RGBA_S3TC_DXT5_Format)
  92. return t.COMPRESSED_RGBA_S3TC_DXT5_EXT
  93. }
  94. if (t = i.get("WEBGL_compressed_texture_pvrtc"),
  95. null !== t) {
  96. if (e === THREE.RGB_PVRTC_4BPPV1_Format)
  97. return t.COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  98. if (e === THREE.RGB_PVRTC_2BPPV1_Format)
  99. return t.COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  100. if (e === THREE.RGBA_PVRTC_4BPPV1_Format)
  101. return t.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  102. if (e === THREE.RGBA_PVRTC_2BPPV1_Format)
  103. return t.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
  104. }
  105. if (t = i.get("WEBGL_compressed_texture_etc1"),
  106. null !== t && e === THREE.RGB_ETC1_Format)
  107. return t.COMPRESSED_RGB_ETC1_WEBGL;
  108. if (t = i.get("EXT_blend_minmax"),
  109. null !== t) {
  110. if (e === THREE.MinEquation)
  111. return t.MIN_EXT;
  112. if (e === THREE.MaxEquation)
  113. return t.MAX_EXT
  114. }
  115. return 0
  116. }
  117. THREE.EventDispatcher.prototype.addEventListener = function(type, listener, {importance=0, once}={}){ //add importance
  118. if ( this._listeners === undefined ) this._listeners = {};
  119. const listeners = this._listeners;
  120. if ( listeners[ type ] === undefined ) {
  121. listeners[ type ] = [];
  122. }
  123. /* if(type == 'flyingDone'){
  124. console.log('addEventListener flyingDone')
  125. } */
  126. if ( !listeners[ type ].some(e=>e.listener == listener ) ) {
  127. listeners[type].push({ listener, importance, once});
  128. listeners[type] = listeners[type].sort((e,a)=> a.importance - e.importance)//add
  129. }
  130. }
  131. THREE.EventDispatcher.prototype.hasEventListener = function(type, listener){
  132. if ( this._listeners === undefined ) return false;
  133. const listeners = this._listeners;
  134. return listeners[ type ] !== undefined && listeners[ type ].some(e=>e.listener == listener )
  135. }
  136. THREE.EventDispatcher.prototype.removeEventListener = function(type, listener){
  137. if ( this._listeners === undefined ) return;
  138. const listeners = this._listeners;
  139. const listenerArray = listeners[ type ];
  140. if ( listenerArray !== undefined ) {
  141. /* const index = listenerArray.indexOf( listener );
  142. if ( index !== - 1 ) {
  143. listenerArray.splice( index, 1 );
  144. } */
  145. let item = listenerArray.find(e=>e.listener == listener)
  146. item && listenerArray.splice(listenerArray.indexOf(item), 1);
  147. }
  148. }
  149. THREE.EventDispatcher.prototype.removeEventListeners = function(type){ //add
  150. if(this._listeners && this._listeners[type] !== undefined){
  151. delete this._listeners[type];
  152. }
  153. }
  154. THREE.EventDispatcher.prototype.removeAllListeners = function(){ //add
  155. this._listeners = {};
  156. }
  157. THREE.EventDispatcher.prototype.dispatchEvent = function(event){
  158. if(typeof event == 'string'){//add
  159. event = {type:event}
  160. }
  161. if ( this._listeners === undefined ) return;
  162. const listeners = this._listeners;
  163. const listenerArray = listeners[ event.type ];
  164. if ( listenerArray !== undefined ) {
  165. event.target = this;
  166. // Make a copy, in case listeners are removed while iterating.
  167. for(let {listener, once} of listenerArray.slice(0)){
  168. if(once){
  169. this.removeEventListener(event.type,listener)
  170. }
  171. let result = listener.call(this, event); //add stopContinue
  172. if(result && result.stopContinue){
  173. break
  174. }
  175. }
  176. }
  177. }
  178. THREE.EventDispatcher.prototype.traverse = function(callback){
  179. let result = callback( this );
  180. if(result && result.stopContinue){//xzw add
  181. return
  182. }
  183. const children = this.children;
  184. if(children){
  185. for ( let i = 0, l = children.length; i < l; i ++ ) {
  186. children[ i ].traverse( callback );
  187. }
  188. }
  189. }
  190. THREE.Object3D.prototype.traverse = function ( callback ) {
  191. let result = callback( this );
  192. if(result && result.stopContinue){//xzw add
  193. return
  194. }
  195. const children = this.children;
  196. for ( let i = 0, l = children.length; i < l; i ++ ) {
  197. children[ i ] && children[ i ].traverse( callback );
  198. }
  199. }
  200. THREE.Material.prototype.setValues = function ( values ) {
  201. if ( values === undefined ) return;
  202. for ( const key in values ) {
  203. const newValue = values[ key ];
  204. if ( newValue === undefined ) {
  205. console.warn( 'THREE.Material: \'' + key + '\' parameter is undefined.' );
  206. continue;
  207. }
  208. // for backward compatability if shading is set in the constructor
  209. if ( key === 'shading' ) {
  210. console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' );
  211. this.flatShading = ( newValue === FlatShading ) ? true : false;
  212. continue;
  213. }
  214. const currentValue = this[ key ];
  215. /* if ( currentValue === undefined ) { //-----主要删了这段,否则很难和 set 一起使用
  216. //console.warn( 'THREE.' + this.type + ': \'' + key + '\' is not a property of this material.' );
  217. continue;
  218. } */
  219. if ( currentValue && currentValue.isColor ) {
  220. currentValue.set( newValue );
  221. } else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) {
  222. currentValue.copy( newValue );
  223. } else {
  224. this[ key ] = newValue;
  225. }
  226. }
  227. }
  228. function ascSort( a, b ) {
  229. return a.distance - b.distance;
  230. }
  231. function intersectObject( object, raycaster, intersects, recursive, ignoreUnvisible ) {
  232. if(ignoreUnvisible && !object.visible)return //add
  233. if ( object.layers.test( raycaster.layers ) ) {
  234. object.raycast( raycaster, intersects );
  235. }
  236. if ( recursive === true ) {
  237. const children = object.children;
  238. for ( let i = 0, l = children.length; i < l; i ++ ) {
  239. intersectObject( children[ i ], raycaster, intersects, true, ignoreUnvisible);
  240. }
  241. }
  242. }
  243. THREE.Raycaster.prototype.intersectObject = function ( object, recursive, optionalTarget, ignoreUnvisible ) {
  244. const intersects = optionalTarget || [];
  245. intersectObject( object, this, intersects, recursive, ignoreUnvisible );
  246. intersects.sort( ascSort );
  247. return intersects;
  248. }
  249. THREE.Raycaster.prototype.intersectObjects = function ( objects, recursive, optionalTarget, ignoreUnvisible ) {//add ignoreUnvisible 跳过不可见
  250. const intersects = optionalTarget || [];
  251. if ( Array.isArray( objects ) === false ) {
  252. console.warn( 'THREE.Raycaster.intersectObjects: objects is not an Array.' );
  253. return intersects;
  254. }
  255. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  256. intersectObject( objects[ i ], this, intersects, recursive, ignoreUnvisible );
  257. }
  258. intersects.sort( ascSort );
  259. return intersects;
  260. }
  261. THREE.Object3D.prototype.realVisible = function(){
  262. let v = true
  263. let parent = this
  264. let lastParent
  265. while(parent){
  266. if(parent.visible === false){
  267. v = false
  268. break;
  269. }
  270. lastParent = parent
  271. parent = parent.parent
  272. }
  273. if(v && !(lastParent instanceof THREE.Scene)){//已被删除
  274. v = false
  275. }
  276. return v
  277. }