All files / src/WebGL/materials Material.ts

2.12% Statements 1/47
0% Branches 0/15
0% Functions 0/4
2.17% Lines 1/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                                                                                                                                                                                                        18x  
import {FrontSide}  from "../constants/Sides";
import { EventDispatcher, Color } from "../core";
import type { Texture } from "../core";
import { Vector2, Vector3 } from "../math";
/**
 * Line and Mesh material types
 * @constructor
 */
export class Material extends EventDispatcher {
  id = MaterialIdCount++;
  name = "";
  overdraw: any;
  color?: Color;
  map?: Texture;
  useScreenCoordinates?: boolean;
  alignment?: Vector2;
  screenOffset?: Vector2;
  uvScale?: Vector2;
  uvOffset?: Vector2;
  scaleByViewport?: boolean;
  fog?: unknown;
 
  side = FrontSide;
  opacity = 1;
  transparent = false;
  depthTest = true;
  depthWrite = true;
  stencilTest = true;
  polygonOffset = false;
  polygonOffsetFactor = 0;
  polygonOffsetUnits = 0;
  alphaTest = 0;
  visible = true;
  needsUpdate = true;
  outline = false;
 
  setValues(
    values: Partial<Record<keyof Material, any>> = {} as any
  ) {
    Iif (values === undefined) return;
 
    for (var key in values) {
      var newValue: Material[keyof Material] = values[key as keyof Material];
 
      Iif (newValue === undefined) {
        console.warn("$3Dmol.Material: '" + key + "' parameter is undefined.");
        continue;
      }
 
      Iif (key in this) {
        var currentValue = this[key as keyof Material];
 
        if (currentValue instanceof Color && newValue instanceof Color) {
          currentValue.copy(newValue);
        } else if (currentValue instanceof Color) {
          currentValue.set(newValue as unknown as Color);
        } else if (
          currentValue instanceof Vector3 &&
          newValue instanceof Vector3
        ) {
          currentValue.copy(newValue);
        } else {
          // @ts-ignore checked above
          this[key] = newValue;
        }
      }
    }
  }
 
  //TODO: might want to look into blending equations
  clone<T extends this>(material = new Material() as T): T {
    material.name = this.name;
 
    material.side = this.side;
 
    material.opacity = this.opacity;
    material.transparent = this.transparent;
 
    material.depthTest = this.depthTest;
    material.depthWrite = this.depthWrite;
    material.stencilTest = this.stencilTest;
 
    material.polygonOffset = this.polygonOffset;
    material.polygonOffsetFactor = this.polygonOffsetFactor;
    material.polygonOffsetUnits = this.polygonOffsetUnits;
 
    material.alphaTest = this.alphaTest;
 
    material.overdraw = this.overdraw;
 
    material.visible = this.visible;
 
    return material;
  }
 
  dispose() {
    this.dispatchEvent({ type: "dispose" });
  }
}
 
export let MaterialIdCount = 0;