babylon.outlineRenderer.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 (typeof useOverlay === "undefined") { 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. } else {
  36. if (batch.renderSelf[subMesh._id]) {
  37. this._effect.setMatrix("world", mesh.getWorldMatrix());
  38. // Draw
  39. mesh._draw(subMesh, BABYLON.Material.TriangleFillMode);
  40. }
  41. if (batch.visibleInstances[subMesh._id]) {
  42. for (var instanceIndex = 0; instanceIndex < batch.visibleInstances[subMesh._id].length; instanceIndex++) {
  43. var instance = batch.visibleInstances[subMesh._id][instanceIndex];
  44. this._effect.setMatrix("world", instance.getWorldMatrix());
  45. // Draw
  46. mesh._draw(subMesh, BABYLON.Material.TriangleFillMode);
  47. }
  48. }
  49. }
  50. };
  51. OutlineRenderer.prototype.isReady = function (subMesh, useInstances) {
  52. var defines = [];
  53. var attribs = [BABYLON.VertexBuffer.PositionKind, BABYLON.VertexBuffer.NormalKind];
  54. var mesh = subMesh.getMesh();
  55. var material = subMesh.getMaterial();
  56. // Alpha test
  57. if (material && material.needAlphaTesting()) {
  58. defines.push("#define ALPHATEST");
  59. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  60. attribs.push(BABYLON.VertexBuffer.UVKind);
  61. defines.push("#define UV1");
  62. }
  63. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  64. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  65. defines.push("#define UV2");
  66. }
  67. }
  68. // Bones
  69. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  70. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  71. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  72. defines.push("#define BONES");
  73. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  74. }
  75. // Instances
  76. if (useInstances) {
  77. defines.push("#define INSTANCES");
  78. attribs.push("world0");
  79. attribs.push("world1");
  80. attribs.push("world2");
  81. attribs.push("world3");
  82. }
  83. // Get correct effect
  84. var join = defines.join("\n");
  85. if (this._cachedDefines != join) {
  86. this._cachedDefines = join;
  87. this._effect = this._scene.getEngine().createEffect("outline", attribs, ["world", "mBones", "viewProjection", "diffuseMatrix", "offset", "color"], ["diffuseSampler"], join);
  88. }
  89. return this._effect.isReady();
  90. };
  91. return OutlineRenderer;
  92. })();
  93. BABYLON.OutlineRenderer = OutlineRenderer;
  94. })(BABYLON || (BABYLON = {}));
  95. //# sourceMappingURL=babylon.outlineRenderer.js.map