babylon.outlineRenderer.js 4.8 KB

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