babylon.outlineRenderer.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. var useBones = mesh.skeleton && scene.skeletonsEnabled && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind);
  24. if (useBones) {
  25. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  26. }
  27. mesh._bind(subMesh, this._effect, Material.TriangleFillMode);
  28. // Alpha test
  29. if (material && material.needAlphaTesting()) {
  30. var alphaTexture = material.getAlphaTestTexture();
  31. this._effect.setTexture("diffuseSampler", alphaTexture);
  32. this._effect.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  33. }
  34. if (hardwareInstancedRendering) {
  35. mesh._renderWithInstances(subMesh, Material.TriangleFillMode, batch, this._effect, engine);
  36. } else {
  37. if (batch.renderSelf[subMesh._id]) {
  38. this._effect.setMatrix("world", mesh.getWorldMatrix());
  39. // Draw
  40. mesh._draw(subMesh, 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, Material.TriangleFillMode);
  48. }
  49. }
  50. }
  51. }
  52. public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  53. var defines = [];
  54. var attribs = [VertexBuffer.PositionKind, 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",
  89. attribs,
  90. ["world", "mBones", "viewProjection", "diffuseMatrix", "offset", "color"],
  91. ["diffuseSampler"], join);
  92. }
  93. return this._effect.isReady();
  94. }
  95. }
  96. }