computeShadowRatio.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/CesiumGS/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['./when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337', './Cartesian4-5af5bb24', './createTaskProcessorWorker'], function (when, Check, _Math, Cartographic, Cartesian4, createTaskProcessorWorker) { 'use strict';
  24. var packedDepthScale = new Cartesian4.Cartesian4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0);
  25. var scratchPacked = new Cartesian4.Cartesian4();
  26. var TEXTURE_SIZE = 1024;
  27. function getShadowRadio(cartographic, bounds, extend, spacing, oriBottom, enuPoints, pixelsArray){
  28. var longitude = cartographic.longitude;
  29. var latitude = cartographic.latitude;
  30. var height = cartographic.height;
  31. longitude = _Math.CesiumMath.toDegrees(longitude);
  32. latitude = _Math.CesiumMath.toDegrees(latitude);
  33. if(longitude < bounds[0] || longitude > bounds[2] ||
  34. latitude < bounds[1] || latitude > bounds[3]){
  35. return -1;
  36. }
  37. var bUsed = false;
  38. var nTexIndex = 0;
  39. var minDist = spacing * 0.1;
  40. for(var bottom = 0.0; bottom <= extend; bottom += spacing){
  41. if(Math.abs(oriBottom + bottom - height) < minDist){
  42. bUsed = true;
  43. break;
  44. }
  45. nTexIndex++;
  46. }
  47. if(!bUsed){
  48. return -1;
  49. }
  50. if(enuPoints.length < 0){
  51. return -1;
  52. }
  53. bUsed = false;
  54. for(var i = 0; i < enuPoints.length; i+=2){
  55. var pos1 = Cartographic.Cartesian3.fromDegrees(longitude, latitude, height);
  56. var pos2 = Cartographic.Cartesian3.fromDegrees(enuPoints[i + 0], enuPoints[i + 1], height);
  57. var dis = Cartographic.Cartesian3.distance(pos1, pos2);
  58. if(dis < minDist){
  59. bUsed = true;
  60. break;
  61. }
  62. }
  63. if(!bUsed){
  64. return -1;
  65. }
  66. var width = bounds[2] - bounds[0];
  67. var height = bounds[3] - bounds[1];
  68. var left = bounds[0] - width * 0.025;
  69. var right = bounds[1] - height * 0.025;
  70. width += width * 0.05;
  71. height += height * 0.05;
  72. var xTexcoord = parseInt((longitude - left) / width * TEXTURE_SIZE);
  73. var yTexcoord = parseInt((latitude - right) / height * TEXTURE_SIZE);
  74. xTexcoord = xTexcoord < 1 ? 1 : xTexcoord;
  75. yTexcoord = yTexcoord < 1 ? 1 : yTexcoord;
  76. var pixels = pixelsArray[nTexIndex];
  77. var result = 0;
  78. for(var i = -1; i < 2; i++){
  79. for(var j = -1; j < 2; j++){
  80. var offset = (TEXTURE_SIZE * (yTexcoord + j) + (xTexcoord + i)) * 4;
  81. scratchPacked.x = pixels[offset];
  82. scratchPacked.y = pixels[offset + 1];
  83. scratchPacked.z = pixels[offset + 2];
  84. scratchPacked.w = pixels[offset + 3];
  85. Cartesian4.Cartesian4.divideByScalar(scratchPacked, 255.0, scratchPacked);
  86. result = Math.max(result, Cartesian4.Cartesian4.dot(scratchPacked, packedDepthScale));
  87. }
  88. }
  89. result = result > 0.999 ? 1.0 : result;
  90. return result;
  91. }
  92. function computeShadowRatio(parameters, transferableObjects) {
  93. var points = parameters.points;
  94. var enuPoints = parameters.enuPoints;
  95. var bounds = parameters.bounds;
  96. var extend = parameters.extend;
  97. var spacing = parameters.spacing;
  98. var bottom = parameters.bottom;
  99. var pixelsArray = parameters.pixelsArray;
  100. var result = [];
  101. for(var j = 0,len = points.length;j < len;j++){
  102. var p = points[j];
  103. var cartographic = Cartographic.Cartographic.fromCartesian(p);
  104. var ratio = getShadowRadio(cartographic, bounds, extend, spacing, bottom, enuPoints, pixelsArray);
  105. result.push({
  106. position : Cartographic.Cartesian3.clone(p),
  107. shadowRatio : ratio
  108. });
  109. }
  110. return {
  111. resultData : result
  112. };
  113. }
  114. var computeShadowRatio$1 = createTaskProcessorWorker(computeShadowRatio);
  115. return computeShadowRatio$1;
  116. });