UNPKG

@visactor/vrender-core

Version:

```typescript import { xxx } from '@visactor/vrender-core'; ```

27 lines (22 loc) 1.26 kB
import { isString, max, min, sqrt } from "@visactor/vutils"; import { ColorStore, ColorType } from "../color-string"; import { Factory } from "../factory"; export class DirectionalLight { constructor(dir, color, ambient = .8) { this.dir = dir, this.color = color, this.colorRgb = ColorStore.Get(color, ColorType.Color1), this.ambient = ambient; const length = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]); this.formatedDir = [ dir[0] / length, dir[1] / length, dir[2] / length ]; } computeColor(normal, color) { const lightDir = this.formatedDir, brightness = min(max((normal[0] * lightDir[0] + normal[1] * lightDir[1] + normal[2] * lightDir[2]) * (1 - this.ambient / 2), 0) + this.ambient, 1); let colorArray; colorArray = isString(color) ? ColorStore.Get(color, ColorType.Color1) : color; const lightColorArray = this.colorRgb; return `rgb(${lightColorArray[0] * colorArray[0] * brightness}, ${lightColorArray[1] * colorArray[1] * brightness}, ${lightColorArray[2] * colorArray[2] * brightness})`; } } export const registerDirectionalLight = () => { Factory.registerPlugin("DirectionalLight", DirectionalLight); }; //# sourceMappingURL=light.js.map