Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

[SDK] Add an emissive color to materials #496

Merged
merged 4 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as MRE from '@microsoft/mixed-reality-extension-sdk';
import { Test } from '../test';

export default class AssetEarlyAssignmentTest extends Test {
public expectedResultDescription = "Colored & textured sphere";
public expectedResultDescription = "Unlit colored & textured sphere";
private assets: MRE.AssetContainer;

public async run(root: MRE.Actor): Promise<boolean> {
Expand All @@ -18,8 +18,9 @@ export default class AssetEarlyAssignmentTest extends Test {
});

const mat = this.assets.createMaterial('blue', {
color: MRE.Color3.Blue(),
mainTextureId: tex.id
color: MRE.Color3.Black(),
emissiveColor: MRE.Color3.Blue(),
emissiveTextureId: tex.id
});

const mesh = this.assets.createSphereMesh('sphere', 0.5);
Expand Down
86 changes: 86 additions & 0 deletions packages/sdk/src/asset/material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
AssetContainer,
AssetLike,
Color3,
Color3Like,
Color4,
Color4Like,
Guid,
Expand Down Expand Up @@ -36,6 +37,14 @@ export interface MaterialLike {
mainTextureOffset: Vector2Like;
/** The main texture's scale from default */
mainTextureScale: Vector2Like;
/** The lighting-independent color of this material. */
emissiveColor: Partial<Color3Like>;
/** The emissive (unlit) texture asset ID */
emissiveTextureId: Guid;
/** The emissive texture's offset from default */
emissiveTextureOffset: Vector2Like;
/** The emissive texture's scale from default */
emissiveTextureScale: Vector2Like;
/** How the color/texture's alpha channel should be handled */
alphaMode: AlphaMode;
/** Visibility threshold in masked alpha mode */
Expand Down Expand Up @@ -67,6 +76,10 @@ export class Material extends Asset implements MaterialLike, Patchable<AssetLike
private _mainTextureId = ZeroGuid;
private _mainTextureOffset = Vector2.Zero();
private _mainTextureScale = Vector2.One();
private _emissiveColor = Color3.Black();
private _emissiveTextureId = ZeroGuid;
private _emissiveTextureOffset = Vector2.Zero();
private _emissiveTextureScale = Vector2.One();
private _alphaMode = AlphaMode.Opaque;
private _alphaCutoff = 0.5;
private _internal = new AssetInternal(this);
Expand Down Expand Up @@ -116,6 +129,48 @@ export class Material extends Asset implements MaterialLike, Patchable<AssetLike
public get mainTextureScale() { return this._mainTextureScale; }
public set mainTextureScale(value) { if (value) { this._mainTextureScale.copy(value); } }

/** @inheritdoc */
public get emissiveColor() { return this._emissiveColor; }
public set emissiveColor(value) { if (value) { this._emissiveColor.copy(value); } }

/** @returns A shared reference to this material's texture asset */
public get emissiveTexture() {
return this.container.context.internal.lookupAsset(this._emissiveTextureId)?.texture;
}
public set emissiveTexture(value) {
this.emissiveTextureId = value?.id ?? ZeroGuid;
}

/** @inheritdoc */
public get emissiveTextureId() { return this._emissiveTextureId; }
public set emissiveTextureId(value) {
if (!value) {
value = ZeroGuid;
}
if (!this.container.context.internal.lookupAsset(value)) {
value = ZeroGuid; // throw?
}

if (value === this._emissiveTextureId) { return; }

if (this.emissiveTexture) {
this.emissiveTexture.clearReference(this);
}
this._emissiveTextureId = value;
if (this.emissiveTexture) {
this.emissiveTexture.addReference(this);
}
this.materialChanged('emissiveTextureId');
}

/** @inheritdoc */
public get emissiveTextureOffset() { return this._emissiveTextureOffset; }
public set emissiveTextureOffset(value) { if (value) { this._emissiveTextureOffset.copy(value); } }

/** @inheritdoc */
public get emissiveTextureScale() { return this._emissiveTextureScale; }
public set emissiveTextureScale(value) { if (value) { this._emissiveTextureScale.copy(value); } }

/** @inheritdoc */
public get alphaMode() { return this._alphaMode; }
public set alphaMode(value) { this._alphaMode = value; this.materialChanged('alphaMode'); }
Expand Down Expand Up @@ -154,6 +209,21 @@ export class Material extends Asset implements MaterialLike, Patchable<AssetLike
targetName: 'mainTextureScale',
notifyChanged: (...path: string[]) => this.materialChanged(...path)
});
observe({
target: this._emissiveColor,
targetName: 'emissiveColor',
notifyChanged: (...path: string[]) => this.materialChanged(...path)
});
observe({
target: this._emissiveTextureOffset,
targetName: 'emissiveTextureOffset',
notifyChanged: (...path: string[]) => this.materialChanged(...path)
});
observe({
target: this._emissiveTextureScale,
targetName: 'emissiveTextureScale',
notifyChanged: (...path: string[]) => this.materialChanged(...path)
});
}

public copy(from: Partial<AssetLike>): this {
Expand All @@ -179,6 +249,18 @@ export class Material extends Asset implements MaterialLike, Patchable<AssetLike
if (from.material.mainTextureId) {
this.mainTextureId = from.material.mainTextureId;
}
if (from.material.emissiveColor) {
this.emissiveColor.copy(from.material.emissiveColor);
}
if (from.material.emissiveTextureOffset) {
this.emissiveTextureOffset.copy(from.material.emissiveTextureOffset);
}
if (from.material.emissiveTextureScale) {
this.emissiveTextureScale.copy(from.material.emissiveTextureScale);
}
if (from.material.emissiveTextureId) {
this.emissiveTextureId = from.material.emissiveTextureId;
}
if (from.material.alphaMode) {
this.alphaMode = from.material.alphaMode;
}
Expand All @@ -200,6 +282,10 @@ export class Material extends Asset implements MaterialLike, Patchable<AssetLike
mainTextureId: this.mainTextureId,
mainTextureOffset: this.mainTextureOffset.toJSON(),
mainTextureScale: this.mainTextureScale.toJSON(),
emissiveColor: this.emissiveColor.toJSON(),
emissiveTextureId: this.emissiveTextureId,
emissiveTextureOffset: this.emissiveTextureOffset.toJSON(),
emissiveTextureScale: this.emissiveTextureScale.toJSON(),
alphaMode: this.alphaMode,
alphaCutoff: this.alphaCutoff
}
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/asset/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export class Texture extends Asset implements TextureLike, Patchable<AssetLike>
if (!(ref instanceof Material)) { return; }
if (ref.mainTexture === this) {
ref.mainTexture = null;
} else if (ref.emissiveTexture === this) {
ref.emissiveTexture = null;
}
}
}