babylon.outlineRenderer.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var OutlineRenderer = (function () {
  4. function OutlineRenderer(scene) {
  5. this._scene = scene;
  6. }
  7. OutlineRenderer.prototype.render = function (subMesh, batch, useOverlay) {
  8. if (useOverlay === void 0) { useOverlay = false; }
  9. var scene = this._scene;
  10. var engine = this._scene.getEngine();
  11. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null) && (batch.visibleInstances[subMesh._id] !== undefined);
  12. if (!this.isReady(subMesh, hardwareInstancedRendering)) {
  13. return;
  14. }
  15. var mesh = subMesh.getRenderingMesh();
  16. var material = subMesh.getMaterial();
  17. engine.enableEffect(this._effect);
  18. this._effect.setFloat("offset", useOverlay ? 0 : mesh.outlineWidth);
  19. this._effect.setColor4("color", useOverlay ? mesh.overlayColor : mesh.outlineColor, useOverlay ? mesh.overlayAlpha : 1.0);
  20. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  21. // Bones
  22. var useBones = mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind);
  23. if (useBones) {
  24. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  25. }
  26. mesh._bind(subMesh, this._effect, BABYLON.Material.TriangleFillMode);
  27. // Alpha test
  28. if (material && material.needAlphaTesting()) {
  29. var alphaTexture = material.getAlphaTestTexture();
  30. this._effect.setTexture("diffuseSampler", alphaTexture);
  31. this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  32. }
  33. if (hardwareInstancedRendering) {
  34. mesh._renderWithInstances(subMesh, BABYLON.Material.TriangleFillMode, batch, this._effect, engine);
  35. }
  36. else {
  37. if (batch.renderSelf[subMesh._id]) {
  38. this._effect.setMatrix("world", mesh.getWorldMatrix());
  39. // Draw
  40. mesh._draw(subMesh, BABYLON.Material.TriangleFillMode);
  41. }
  42. if (batch.visibleInstances[subMesh._id]) {
  43. for (var instanceIndex = 0; instanceIndex < batch.visibleInstances[subMesh._id].length; instanceIndex++) {
  44. var instance = batch.visibleInstances[subMesh._id][instanceIndex];
  45. this._effect.setMatrix("world", instance.getWorldMatrix());
  46. // Draw
  47. mesh._draw(subMesh, BABYLON.Material.TriangleFillMode);
  48. }
  49. }
  50. }
  51. };
  52. OutlineRenderer.prototype.isReady = function (subMesh, useInstances) {
  53. var defines = [];
  54. var attribs = [BABYLON.VertexBuffer.PositionKind, BABYLON.VertexBuffer.NormalKind];
  55. var mesh = subMesh.getMesh();
  56. var material = subMesh.getMaterial();
  57. // Alpha test
  58. if (material && material.needAlphaTesting()) {
  59. defines.push("#define ALPHATEST");
  60. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  61. attribs.push(BABYLON.VertexBuffer.UVKind);
  62. defines.push("#define UV1");
  63. }
  64. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  65. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  66. defines.push("#define UV2");
  67. }
  68. }
  69. // Bones
  70. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  71. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  72. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  73. defines.push("#define BONES");
  74. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  75. }
  76. // Instances
  77. if (useInstances) {
  78. defines.push("#define INSTANCES");
  79. attribs.push("world0");
  80. attribs.push("world1");
  81. attribs.push("world2");
  82. attribs.push("world3");
  83. }
  84. // Get correct effect
  85. var join = defines.join("\n");
  86. if (this._cachedDefines != join) {
  87. this._cachedDefines = join;
  88. this._effect = this._scene.getEngine().createEffect("outline", attribs, ["world", "mBones", "viewProjection", "diffuseMatrix", "offset", "color"], ["diffuseSampler"], join);
  89. }
  90. return this._effect.isReady();
  91. };
  92. return OutlineRenderer;
  93. })();
  94. BABYLON.OutlineRenderer = OutlineRenderer;
  95. })(BABYLON || (BABYLON = {}));
  96. //# sourceMappingURL=babylon.outlineRenderer.js.map