Skip to content

Commit

Permalink
Merge pull request #606 from kaliber5/hemi-ground
Browse files Browse the repository at this point in the history
Add missing color properties to lights
  • Loading branch information
simonihmig authored Jan 18, 2022
2 parents 609bf45 + 5d7cd2f commit 7432e9c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/components/light/_light.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Component, ComponentSchema, Types } from 'ecsy';
import { Color3 } from '@babylonjs/core/Maths/math.color';
import { BabylonTypes } from '../../-private/ecsy-types';

export default abstract class Light<C> extends Component<C> {
intensity!: number;
diffuse!: Color3;
specular!: Color3;
}

export const schema: ComponentSchema = {
intensity: { type: Types.Number, default: 1 },
diffuse: { type: BabylonTypes.Color3, default: Color3.White() },
specular: { type: BabylonTypes.Color3, default: Color3.White() },
};
3 changes: 3 additions & 0 deletions src/components/light/hemispheric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { schema as baseSchema } from './_shadow';
import { ComponentSchema } from 'ecsy';
import { BabylonTypes } from '../../-private/ecsy-types';
import Light from './_light';
import { Color3 } from '@babylonjs/core/Maths/math.color';

export default class HemisphericLight extends Light<HemisphericLight> {
direction!: Vector3;
groundColor!: Color3;

static schema: ComponentSchema = {
...baseSchema,
direction: { type: BabylonTypes.Vector3, default: new Vector3(0, -1, 0) },
groundColor: { type: BabylonTypes.Color3, default: Color3.Black() },
};
}
16 changes: 12 additions & 4 deletions test/light.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SpotLight as BabylonSpotLight } from '@babylonjs/core/Lights/spotLight'
import { HemisphericLight as BabylonHemisphericLight } from '@babylonjs/core/Lights/hemisphericLight';
import { Vector3 } from '@babylonjs/core/Maths/math.vector';
import setupWorld from './helpers/setup-world';
import { Color3 } from '@babylonjs/core/Maths/math.color';

describe('light system', function () {
describe('point-light', function () {
Expand All @@ -27,7 +28,9 @@ describe('light system', function () {
const { world, scene } = setupWorld();

const lightEntity = world.createEntity();
lightEntity.addComponent(Parent).addComponent(PointLight, { intensity: 2 });
lightEntity
.addComponent(Parent)
.addComponent(PointLight, { intensity: 2, diffuse: new Color3(1, 0, 0), specular: new Color3(0, 1, 0) });

world.execute(0, 0);

Expand All @@ -36,6 +39,8 @@ describe('light system', function () {
const light = scene.lights[0];
expect(light).toBeInstanceOf(BabylonPointLight);
expect(light.intensity).toBe(2);
expect(light.diffuse.equalsFloats(1, 0, 0)).toBeTrue();
expect(light.specular.equalsFloats(0, 1, 0)).toBeTrue();
});

it('can update point-light', function () {
Expand Down Expand Up @@ -241,9 +246,11 @@ describe('light system', function () {
const { world, scene } = setupWorld();

const lightEntity = world.createEntity();
lightEntity
.addComponent(Parent)
.addComponent(HemisphericLight, { intensity: 2, direction: new Vector3(1, 0, 0) });
lightEntity.addComponent(Parent).addComponent(HemisphericLight, {
intensity: 2,
direction: new Vector3(1, 0, 0),
groundColor: new Color3(1, 0, 0),
});

world.execute(0, 0);

Expand All @@ -253,6 +260,7 @@ describe('light system', function () {
expect(light).toBeInstanceOf(BabylonHemisphericLight);
expect(light.intensity).toBe(2);
expect(light.direction.equalsToFloats(1, 0, 0)).toBeTrue();
expect(light.groundColor.equalsFloats(1, 0, 0)).toBeTrue();
});

it('can update hemispheric-light', function () {
Expand Down

0 comments on commit 7432e9c

Please sign in to comment.