Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ShaderMaterial uniforms typing and autocompletion (+ ShaderPass) #151

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,15 @@
"contributions": [
"doc"
]
},
{
"login": "Lutymane",
"name": "Lutymane",
"avatar_url": "https://avatars.githubusercontent.com/u/22231294?v=4",
"profile": "https://zeokku.com/",
"contributions": [
"code"
]
}
],
"skipCi": true,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
</tr>
<tr>
<td align="center"><a href="https://xk.io/"><img src="https://avatars.githubusercontent.com/u/1046448?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Max Kaye</b></sub></a><br /><a href="https://github.com/three-types/three-ts-types/commits?author=XertroV" title="Documentation">📖</a></td>
<td align="center"><a href="https://zeokku.com/"><img src="https://avatars.githubusercontent.com/u/22231294?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Lutymane</b></sub></a><br /><a href="https://github.com/three-types/three-ts-types/commits?author=Lutymane" title="Code">💻</a></td>
</tr>
</table>

Expand Down
26 changes: 19 additions & 7 deletions types/three/examples/jsm/postprocessing/ShaderPass.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { ShaderMaterial } from '../../../src/Three';
import {
AvoidNullUniforms,
ExtractUniforms,
ShaderMaterial,
ShaderMaterialParameters,
TUniforms,
} from '../../../src/Three';

import { Pass } from './Pass';
import { FullScreenQuad, Pass } from './Pass';

export class ShaderPass extends Pass {
constructor(shader: object, textureID?: string);
export type ShaderLike<Uniforms extends TUniforms> = {
fragmentShader: ShaderMaterial['fragmentShader'];
vertexShader?: ShaderMaterial['vertexShader'];
defines?: ShaderMaterial['defines'];
} & (Exclude<keyof Uniforms, string> extends never ? { uniforms: Uniforms } : { uniforms?: Uniforms });

export class ShaderPass<T extends {} = TUniforms, Uniforms extends TUniforms = ExtractUniforms<T>> extends Pass {
constructor(shader: ShaderLike<Uniforms>, textureID?: string);
textureID: string;
uniforms: { [name: string]: { value: any } };
material: ShaderMaterial;
fsQuad: object;
uniforms: AvoidNullUniforms<Uniforms>;
material: ShaderMaterial<Uniforms>;
fsQuad: FullScreenQuad;
}
14 changes: 11 additions & 3 deletions types/three/src/materials/RawShaderMaterial.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { ShaderMaterialParameters, ShaderMaterial } from './ShaderMaterial';
import { ShaderMaterialParameters, ShaderMaterial, TUniforms, ExtractUniforms } from './ShaderMaterial';

export class RawShaderMaterial extends ShaderMaterial {
constructor(parameters?: ShaderMaterialParameters);
export type RawShaderMaterialParameters<Uniforms extends TUniforms> = ShaderMaterialParameters<Uniforms> & {
vertexShader: string;
fragmentShader: string;
};

export class RawShaderMaterial<
T extends {} = TUniforms,
Uniforms extends TUniforms = ExtractUniforms<T>,
> extends ShaderMaterial<T, Uniforms> {
constructor(parameters?: RawShaderMaterialParameters<Uniforms>);
}
78 changes: 54 additions & 24 deletions types/three/src/materials/ShaderMaterial.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,56 @@ import { IUniform } from '../renderers/shaders/UniformsLib';
import { MaterialParameters, Material } from './Material';
import { GLSLVersion } from '../constants';

export interface ShaderMaterialParameters extends MaterialParameters {
uniforms?: { [uniform: string]: IUniform } | undefined;
vertexShader?: string | undefined;
fragmentShader?: string | undefined;
linewidth?: number | undefined;
wireframe?: boolean | undefined;
wireframeLinewidth?: number | undefined;
lights?: boolean | undefined;
clipping?: boolean | undefined;

extensions?:
| {
derivatives?: boolean | undefined;
fragDepth?: boolean | undefined;
drawBuffers?: boolean | undefined;
shaderTextureLOD?: boolean | undefined;
}
| undefined;
glslVersion?: GLSLVersion | undefined;
export interface TUniforms {
[uniform: string]: IUniform;
}

export class ShaderMaterial extends Material {
constructor(parameters?: ShaderMaterialParameters);
// transform { uniform1: type1, uniform2: type2 } to { uniform1: { value: type1 }, ...}
export type WrapUniforms<T extends { [uniform: string]: any }> = {
[U in keyof T]: T[U] extends IUniform ? T[U] : IUniform<T[U]>;
};

// replace null types by any (typically used by textures)
export type AvoidNullUniforms<T extends TUniforms> = {
[U in keyof T]: IUniform<T[U]['value'] extends null ? any : T[U]['value']>;
};

export type ExtractUniforms<T> = AvoidNullUniforms<
WrapUniforms<T extends ShaderMaterialParametersLike<infer U> ? U : T>
>;

export interface ShaderMaterialParametersLike<Uniforms extends TUniforms> {
uniforms: Uniforms;
}

export type ShaderMaterialParameters<Uniforms extends TUniforms> = MaterialParameters &
// if we have uniforms type defined in var declaration then this paramater is required
// keyof { [string] } will result in string | number, since uniform names can't be numbers we can safely determine the type
// check for extends never instead of extends number because never always extends everything
// uniforms: Type | undefined !== uniforms?: Type
(Exclude<keyof Uniforms, string> extends never ? { uniforms: Uniforms } : { uniforms?: Uniforms }) & {
vertexShader?: string;
fragmentShader?: string;
linewidth?: number;
wireframe?: boolean;
wireframeLinewidth?: number;
lights?: boolean;
clipping?: boolean;

extensions?: {
derivatives?: boolean;
fragDepth?: boolean;
drawBuffers?: boolean;
shaderTextureLOD?: boolean;
};
glslVersion?: GLSLVersion;
};

export class ShaderMaterial<
T extends {} = TUniforms,
Uniforms extends TUniforms = ExtractUniforms<T>,
> extends Material {
constructor(parameters?: ShaderMaterialParameters<Uniforms>);

/**
* @default 'ShaderMaterial'
Expand All @@ -39,7 +66,10 @@ export class ShaderMaterial extends Material {
/**
* @default {}
*/
uniforms: { [uniform: string]: IUniform };
uniforms: AvoidNullUniforms<Uniforms>;
// uniforms: Exclude<keyof Uniforms, string> extends never
// ? AvoidNullUniforms<Uniforms>
// : Partial<AvoidNullUniforms<Uniforms>>;
vertexShader: string;
fragmentShader: string;

Expand Down Expand Up @@ -96,7 +126,7 @@ export class ShaderMaterial extends Material {
/**
* @default undefined
*/
index0AttributeName: string | undefined;
index0AttributeName?: string;

/**
* @default false
Expand All @@ -110,6 +140,6 @@ export class ShaderMaterial extends Material {

isShaderMaterial: boolean;

setValues(parameters: ShaderMaterialParameters): void;
setValues(parameters: ShaderMaterialParameters<Uniforms>): void;
toJSON(meta: any): any;
}