babylon.outlineRenderer.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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) {
  24. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  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) {
  55. attribs.push(VertexBuffer.MatricesIndicesKind);
  56. attribs.push(VertexBuffer.MatricesWeightsKind);
  57. defines.push("#define BONES");
  58. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  59. }
  60. // Instances
  61. if (useInstances) {
  62. defines.push("#define INSTANCES");
  63. attribs.push("world0");
  64. attribs.push("world1");
  65. attribs.push("world2");
  66. attribs.push("world3");
  67. }
  68. // Get correct effect
  69. var join = defines.join("\n");
  70. if (this._cachedDefines !== join) {
  71. this._cachedDefines = join;
  72. this._effect = this._scene.getEngine().createEffect("outline",
  73. attribs,
  74. ["world", "mBones", "viewProjection", "diffuseMatrix", "offset", "color"],
  75. ["diffuseSampler"], join);
  76. }
  77. return this._effect.isReady();
  78. }
  79. }
  80. }