babylon.outlineRenderer.js 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. var _this = this;
  9. if (useOverlay === void 0) { useOverlay = false; }
  10. var scene = this._scene;
  11. var engine = this._scene.getEngine();
  12. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null) && (batch.visibleInstances[subMesh._id] !== undefined);
  13. if (!this.isReady(subMesh, hardwareInstancedRendering)) {
  14. return;
  15. }
  16. var mesh = subMesh.getRenderingMesh();
  17. var material = subMesh.getMaterial();
  18. engine.enableEffect(this._effect);
  19. this._effect.setFloat("offset", useOverlay ? 0 : mesh.outlineWidth);
  20. this._effect.setColor4("color", useOverlay ? mesh.overlayColor : mesh.outlineColor, useOverlay ? mesh.overlayAlpha : 1.0);
  21. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  22. // Bones
  23. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  24. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  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. mesh._processRendering(subMesh, this._effect, BABYLON.Material.TriangleFillMode, batch, hardwareInstancedRendering, function (isInstance, world) { _this._effect.setMatrix("world", world); });
  34. };
  35. OutlineRenderer.prototype.isReady = function (subMesh, useInstances) {
  36. var defines = [];
  37. var attribs = [BABYLON.VertexBuffer.PositionKind, BABYLON.VertexBuffer.NormalKind];
  38. var mesh = subMesh.getMesh();
  39. var material = subMesh.getMaterial();
  40. // Alpha test
  41. if (material && material.needAlphaTesting()) {
  42. defines.push("#define ALPHATEST");
  43. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  44. attribs.push(BABYLON.VertexBuffer.UVKind);
  45. defines.push("#define UV1");
  46. }
  47. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  48. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  49. defines.push("#define UV2");
  50. }
  51. }
  52. // Bones
  53. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  54. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  55. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  56. if (mesh.numBoneInfluencers > 4) {
  57. attribs.push(BABYLON.VertexBuffer.MatricesIndicesExtraKind);
  58. attribs.push(BABYLON.VertexBuffer.MatricesWeightsExtraKind);
  59. }
  60. defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
  61. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  62. }
  63. else {
  64. defines.push("#define NUM_BONE_INFLUENCERS 0");
  65. }
  66. // Instances
  67. if (useInstances) {
  68. defines.push("#define INSTANCES");
  69. attribs.push("world0");
  70. attribs.push("world1");
  71. attribs.push("world2");
  72. attribs.push("world3");
  73. }
  74. // Get correct effect
  75. var join = defines.join("\n");
  76. if (this._cachedDefines !== join) {
  77. this._cachedDefines = join;
  78. this._effect = this._scene.getEngine().createEffect("outline", attribs, ["world", "mBones", "viewProjection", "diffuseMatrix", "offset", "color"], ["diffuseSampler"], join);
  79. }
  80. return this._effect.isReady();
  81. };
  82. return OutlineRenderer;
  83. })();
  84. BABYLON.OutlineRenderer = OutlineRenderer;
  85. })(BABYLON || (BABYLON = {}));