babylon.outlineRenderer.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. module BABYLON {
  2. export class OutlineRenderer {
  3. private _scene: Scene;
  4. private _effect: Effect;
  5. private _cachedDefines: string;
  6. constructor(scene: Scene) {
  7. this._scene = scene;
  8. }
  9. public render(subMesh: SubMesh, batch: _InstancesBatch, useOverlay: boolean = 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, 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, Material.TriangleFillMode, batch, hardwareInstancedRendering,
  34. (isInstance, world) => { this._effect.setMatrix("world", world)});
  35. }
  36. public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  37. var defines = [];
  38. var attribs = [VertexBuffer.PositionKind, VertexBuffer.NormalKind];
  39. var mesh = subMesh.getMesh();
  40. var material = subMesh.getMaterial();
  41. // Alpha test
  42. if (material && material.needAlphaTesting()) {
  43. defines.push("#define ALPHATEST");
  44. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  45. attribs.push(VertexBuffer.UVKind);
  46. defines.push("#define UV1");
  47. }
  48. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  49. attribs.push(VertexBuffer.UV2Kind);
  50. defines.push("#define UV2");
  51. }
  52. }
  53. // Bones
  54. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  55. attribs.push(VertexBuffer.MatricesIndicesKind);
  56. attribs.push(VertexBuffer.MatricesWeightsKind);
  57. if (mesh.numBoneInfluencers > 4) {
  58. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  59. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  60. }
  61. defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
  62. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  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",
  79. attribs,
  80. ["world", "mBones", "viewProjection", "diffuseMatrix", "offset", "color"],
  81. ["diffuseSampler"], join);
  82. }
  83. return this._effect.isReady();
  84. }
  85. }
  86. }