Skip to content

Commit

Permalink
feat(wasm): add matrix wasm (Orillusion#292)
Browse files Browse the repository at this point in the history
add matrix wasm
add pipeline shader share
add occlusion by octree
new material and material pass
fix depth
fix shader state change bug
fix cluster number light count
fix webgpu version
fix shadowmapping bug
fix frag uv and uv2 uvTransform
fix cluster index error

---------

Co-authored-by: Codeboy-cn <[email protected]>
Co-authored-by: ShuangLiu <[email protected]>
Co-authored-by: hellmor <[email protected]>
  • Loading branch information
4 people authored Sep 6, 2023
1 parent 7a30945 commit 2c8e8ab
Show file tree
Hide file tree
Showing 200 changed files with 4,465 additions and 3,424 deletions.
213 changes: 113 additions & 100 deletions packages/effect/grass/material/GrassMaterial.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { BlendMode, Color, Engine3D, GPUAddressMode, MaterialBase, MaterialPass, RendererType, ShaderLib, Texture, Vector2, Vector3, Vector4 } from "@orillusion/core";
import { BlendMode, Color, GPUAddressMode, Material, RenderShader, RendererType, ShaderLib, Texture, Vector2, Vector3, Vector4 } from "@orillusion/core";
import { GrassShader } from "../shader/GrassShader";
import { GrassVertexAttributeShader } from "../shader/GrassVertexAttributeShader";
import { GrassCastShadowShader } from "../shader/GrassCastShadowShader";

export class GrassMaterial extends MaterialBase {
export class GrassMaterial extends Material {
constructor() {
super();

ShaderLib.register("GrassVertexAttributeShader", GrassVertexAttributeShader);
ShaderLib.register("GrassShader", GrassShader);

let shader = this.setShader(`GrassShader`, `GrassShader`);
shader.setShaderEntry(`VertMain`, `FragMain`)
shader.setDefine("TRANSFORMVERTEX", true);
let shaderState = shader.shaderState;
let colorPass = new RenderShader(`GrassShader`, `GrassShader`);
this.defaultPass = colorPass;
colorPass.setShaderEntry(`VertMain`, `FragMain`)
colorPass.setDefine("TRANSFORMVERTEX", true);
let shaderState = colorPass.shaderState;
shaderState.acceptShadow = true;
shaderState.receiveEnv = true;
shaderState.acceptGI = false;
Expand All @@ -22,191 +23,203 @@ export class GrassMaterial extends MaterialBase {
shaderState.blendMode = BlendMode.NONE;

ShaderLib.register("GrassCastShadowShader", GrassCastShadowShader);
let shadowPass = new MaterialBase();
shadowPass.isPassMaterial = true;
let shadowShader = shadowPass.setShader(`GrassCastShadowShader`, `GrassCastShadowShader`);

let shadowPass = new RenderShader(`GrassCastShadowShader`, `GrassCastShadowShader`);
shadowPass.setDefine("USE_ALPHACUT", true);
shadowPass.setDefine("TRANSFORMVERTEX", true);
shadowShader.setShaderEntry(`VertMain`)
shadowShader.shaderState.blendMode = BlendMode.NONE;
shadowShader.shaderState.receiveEnv = false;
shader.setPassShader(RendererType.SHADOW, shadowPass);
shadowPass.setShaderEntry(`VertMain`)
shadowPass.shaderState.blendMode = BlendMode.NONE;
shadowPass.shaderState.receiveEnv = false;
this.addPass(RendererType.SHADOW, shadowPass);


shader.setUniformColor("baseColor", new Color(0.0, 1.0, 0.0, 1.0));
shader.setUniformColor("grassBottomColor", new Color(3 / 255, 16 / 255, 3 / 255));
shader.setUniformColor("grassTopColor", new Color(45 / 255, 154 / 255, 74 / 255, 1.0));
shader.setUniformColor("materialF0", new Color(0.04, 0.04, 0.04, 1.0 - 0.04));
shader.setUniformVector4("windBound", new Vector4(0, 0, 2000, 2000));
shader.setUniformVector2("windDirection", new Vector2(0.6, 0.8));
shader.setUniformFloat("windPower", 0.8);
shader.setUniformFloat("windSpeed", 12);
shader.setUniformFloat("translucent", 0.35);
shader.setUniformFloat("roughness", 0.35);
shader.setUniformFloat("curvature", 0.4068);
shader.setUniformFloat("grassHeight", 10);
shader.setUniformFloat("soft", 5);
shader.setUniformFloat("specular", 0.15);

shadowShader.setUniformColor("baseColor", new Color(0.0, 1.0, 0.0, 1.0));
shadowShader.setUniformColor("grassBottomColor", new Color(39 / 255, 87 / 255, 36 / 255));
shadowShader.setUniformColor("grassTopColor", new Color(74 / 255, 163 / 255, 93 / 255, 1.0));
shadowShader.setUniformColor("materialF0", new Color(0.04, 0.04, 0.04, 1.0 - 0.04));
shadowShader.setUniformVector4("windBound", new Vector4(0, 0, 2000, 2000));
shadowShader.setUniformVector2("windDirection", new Vector2(0.6, 0.8));
shadowShader.setUniformFloat("windPower", 0.8);
shadowShader.setUniformFloat("windSpeed", 10);
shadowShader.setUniformFloat("translucent", 0.35);
shadowShader.setUniformFloat("roughness", 0.35);
shadowShader.setUniformFloat("curvature", 0.4068);
shadowShader.setUniformFloat("grassHeight", 10);
shadowShader.setUniformFloat("soft", 5);
shadowShader.setUniformFloat("specular", 0.15);
colorPass.setUniformColor("baseColor", new Color(0.0, 1.0, 0.0, 1.0));
colorPass.setUniformColor("grassBottomColor", new Color(3 / 255, 16 / 255, 3 / 255));
colorPass.setUniformColor("grassTopColor", new Color(45 / 255, 154 / 255, 74 / 255, 1.0));
colorPass.setUniformColor("materialF0", new Color(0.04, 0.04, 0.04, 1.0 - 0.04));
colorPass.setUniformVector4("windBound", new Vector4(0, 0, 2000, 2000));
colorPass.setUniformVector2("windDirection", new Vector2(0.6, 0.8));
colorPass.setUniformFloat("windPower", 0.8);
colorPass.setUniformFloat("windSpeed", 12);
colorPass.setUniformFloat("translucent", 0.35);
colorPass.setUniformFloat("roughness", 0.35);
colorPass.setUniformFloat("curvature", 0.4068);
colorPass.setUniformFloat("grassHeight", 10);
colorPass.setUniformFloat("soft", 5);
colorPass.setUniformFloat("specular", 0.15);

shadowPass.setUniformColor("baseColor", new Color(0.0, 1.0, 0.0, 1.0));
shadowPass.setUniformColor("grassBottomColor", new Color(39 / 255, 87 / 255, 36 / 255));
shadowPass.setUniformColor("grassTopColor", new Color(74 / 255, 163 / 255, 93 / 255, 1.0));
shadowPass.setUniformColor("materialF0", new Color(0.04, 0.04, 0.04, 1.0 - 0.04));
shadowPass.setUniformVector4("windBound", new Vector4(0, 0, 2000, 2000));
shadowPass.setUniformVector2("windDirection", new Vector2(0.6, 0.8));
shadowPass.setUniformFloat("windPower", 0.8);
shadowPass.setUniformFloat("windSpeed", 10);
shadowPass.setUniformFloat("translucent", 0.35);
shadowPass.setUniformFloat("roughness", 0.35);
shadowPass.setUniformFloat("curvature", 0.4068);
shadowPass.setUniformFloat("grassHeight", 10);
shadowPass.setUniformFloat("soft", 5);
shadowPass.setUniformFloat("specular", 0.15);

// default value
// this.baseMap = Engine3D.res.whiteTexture;
this.doubleSide = true;
colorPass.doubleSide = true;
shadowPass.doubleSide = true;
}

public set baseMap(texture: Texture) {
texture.visibility = GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT;
super.baseMap = texture;
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.baseMap = texture;
// texture.visibility = GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT;
let shadowPass = this.getPass(RendererType.SHADOW)[0];

this.defaultPass.setTexture(`baseMap`, texture);
shadowPass.setTexture(`baseMap`, texture);
}

public get baseMap(): Texture {
return super.baseMap;
return this.defaultPass.getTexture(`baseMap`);
}

public set windMap(texture: Texture) {
texture.visibility = GPUShaderStage.VERTEX;
// texture.visibility = GPUShaderStage.VERTEX;
texture.addressModeU = GPUAddressMode.repeat;
texture.addressModeV = GPUAddressMode.repeat;
this.renderShader.setTexture("windMap", texture);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setTexture("windMap", texture);
this.defaultPass.setTexture("windMap", texture);
let shadowPass = this.getPass(RendererType.SHADOW)[0];

shadowPass.setTexture("windMap", texture);
}

public set windBound(v: Vector4) {
this.renderShader.setUniformVector4("windBound", v);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setUniformVector4("windBound", v);
this.defaultPass.setUniformVector4("windBound", v);
let shadowPass = this.getPass(RendererType.SHADOW)[0];

this.defaultPass.setUniformVector4("windBound", v);
shadowPass.setUniformVector4("windBound", v);
}

public get windBound(): Vector4 {
return this.renderShader.uniforms["windBound"].vector4;
return this.defaultPass.uniforms["windBound"].vector4;
}

public set grassBaseColor(v: Color) {
this.renderShader.setUniformColor("grassBottomColor", v);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setUniformColor("grassBottomColor", v);
this.defaultPass.setUniformColor("grassBottomColor", v);
let shadowPass = this.getPass(RendererType.SHADOW)[0];
shadowPass.setUniformColor("grassBottomColor", v);
}

public get grassBaseColor(): Color {
return this.renderShader.uniforms["grassBottomColor"].color;
return this.defaultPass.uniforms["grassBottomColor"].color;
}

public set grassTopColor(v: Color) {
this.renderShader.setUniformColor("grassTopColor", v);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setUniformColor("grassTopColor", v);
this.defaultPass.setUniformColor("grassTopColor", v);
let shadowPass = this.getPass(RendererType.SHADOW)[0];

shadowPass.setUniformColor("grassTopColor", v);
}

public get grassTopColor(): Color {
return this.renderShader.uniforms["grassTopColor"].color;
return this.defaultPass.uniforms["grassTopColor"].color;
}

public set windDirection(v: Vector2) {
this.renderShader.setUniformVector2("windDirection", v);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setUniformVector2("windDirection", v);
this.defaultPass.setUniformVector2("windDirection", v);
let shadowPass = this.getPass(RendererType.SHADOW)[0];

shadowPass.setUniformVector2("windDirection", v);
}

public get windDirection(): Vector2 {
return this.renderShader.uniforms["windDirection"].vector2;
return this.defaultPass.uniforms["windDirection"].vector2;
}

public set windPower(v: number) {
this.renderShader.setUniformFloat("windPower", v);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setUniformFloat("windPower", v);
this.defaultPass.setUniformFloat("windPower", v);
let shadowPass = this.getPass(RendererType.SHADOW)[0];

shadowPass.setUniformFloat("windPower", v);
}

public get windPower(): number {
return this.renderShader.uniforms["windPower"].data;
return this.defaultPass.uniforms["windPower"].data;
}

public set windSpeed(v: number) {
this.renderShader.setUniformFloat("windSpeed", v);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setUniformFloat("windSpeed", v);
this.defaultPass.setUniformFloat("windSpeed", v);
let shadowPass = this.getPass(RendererType.SHADOW)[0];

shadowPass.setUniformFloat("windSpeed", v);
}

public get windSpeed(): number {
return this.renderShader.uniforms["windSpeed"].data;
return this.defaultPass.uniforms["windSpeed"].data;
}

public set grassHeight(v: number) {
this.renderShader.setUniformFloat("grassHeight", v);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setUniformFloat("grassHeight", v);
this.defaultPass.setUniformFloat("grassHeight", v);
let shadowPass = this.getPass(RendererType.SHADOW)[0];

shadowPass.setUniformFloat("grassHeight", v);
}

public get grassHeight(): number {
return this.renderShader.uniforms["grassHeight"].data;
return this.defaultPass.uniforms["grassHeight"].data;
}

public set curvature(v: number) {
this.renderShader.setUniformFloat("curvature", v);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setUniformFloat("curvature", v);
this.defaultPass.setUniformFloat("curvature", v);
let shadowPass = this.getPass(RendererType.SHADOW)[0];

shadowPass.setUniformFloat("curvature", v);
}

public get curvature(): number {
return this.renderShader.uniforms["curvature"].data;
return this.defaultPass.uniforms["curvature"].data;
}

public set roughness(v: number) {
this.renderShader.setUniformFloat("roughness", v);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setUniformFloat("roughness", v);
this.defaultPass.setUniformFloat("roughness", v);
let shadowPass = this.getPass(RendererType.SHADOW)[0];

shadowPass.setUniformFloat("roughness", v);
}

public get roughness(): number {
return this.renderShader.uniforms["roughness"].data;
return this.defaultPass.uniforms["roughness"].data;
}

public set translucent(v: number) {
this.renderShader.setUniformFloat("translucent", v);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setUniformFloat("translucent", v);
this.defaultPass.setUniformFloat("translucent", v);
let shadowPass = this.getPass(RendererType.SHADOW)[0];

shadowPass.setUniformFloat("translucent", v);
}

public get translucent(): number {
return this.renderShader.uniforms["translucent"].data;
return this.defaultPass.uniforms["translucent"].data;
}

public set soft(v: number) {
this.renderShader.setUniformFloat("soft", v);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setUniformFloat("soft", v);
this.defaultPass.setUniformFloat("soft", v);
let shadowPass = this.getPass(RendererType.SHADOW)[0];

shadowPass.setUniformFloat("soft", v);
}

public get soft(): number {
return this.renderShader.uniforms["soft"].data;
return this.defaultPass.uniforms["soft"].data;
}

public set specular(v: number) {
this.renderShader.setUniformFloat("specular", v);
let shadowShader = this.renderShader.getPassShader(RendererType.SHADOW);
shadowShader.renderShader.setUniformFloat("specular", v);
this.defaultPass.setUniformFloat("specular", v);
let shadowPass = this.getPass(RendererType.SHADOW)[0];

shadowPass.setUniformFloat("specular", v);
}

public get specular(): number {
return this.renderShader.uniforms["specular"].data;
return this.defaultPass.uniforms["specular"].data;
}
}
46 changes: 34 additions & 12 deletions packages/effect/terrain/geometry/TerrainGeometry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BitmapTexture2D, Plane, PlaneGeometry, Texture, Vector3, VertexAttributeName } from "@orillusion/core"
import { BitmapTexture2D, Plane, PlaneGeometry, Texture, Vector3, VertexAttributeName, lerp } from "@orillusion/core"

export class TerrainGeometry extends PlaneGeometry {

Expand All @@ -22,25 +22,47 @@ export class TerrainGeometry extends PlaneGeometry {

let tw = this.segmentW + 1;
let th = this.segmentH + 1;
for (let ppy = 0; ppy < this.segmentH; ppy++) {
for (let ppx = 0; ppx < this.segmentW; ppx++) {
let px = Math.floor(ppx / tw * texture.width);
let py = Math.floor(ppy / th * texture.height);
for (let ppy = 0; ppy < this.segmentH - 1; ppy++) {
for (let ppx = 0; ppx < this.segmentW - 1; ppx++) {

let index = py * texture.width + px;
let r = pixelData.data[index * 4];
let px0 = Math.floor(ppx / tw * texture.width);
let py0 = Math.floor(ppy / th * texture.height);

// if (r < 200 && g > 50 && b < 200) {
// this._greenList.push(new Vector3(ppx, 0, ppy));
// }
let px1 = Math.floor((ppx + 1) / tw * texture.width);
let py1 = Math.floor((ppy) / th * texture.height);

let px2 = Math.floor((ppx) / tw * texture.width);
let py2 = Math.floor((ppy + 1) / th * texture.height);

let px3 = Math.floor((ppx + 1) / tw * texture.width);
let py3 = Math.floor((ppy + 1) / th * texture.height);

var tt = ppx / tw - Math.floor(ppx / tw);
let t0 = tt;
let t1 = tt;
let t2 = tt * 1.2121;

let index0 = py0 * texture.width + px0;
let index1 = py1 * texture.width + px1;
let index2 = py2 * texture.width + px2;
let index3 = py3 * texture.width + px3;

let h0 = pixelData.data[index0 * 4];
let h1 = pixelData.data[index1 * 4];
let h2 = pixelData.data[index2 * 4];
let h3 = pixelData.data[index3 * 4];

let h = lerp(h0, h1, t0);
h = lerp(h, h2, t1);
h = lerp(h, h3, t2);

let sc = 0.05;
if (r > 45 && r < 150) {
if (h > 45 && h < 150) {
this._greenList.push(new Vector3(ppx, 0, ppy));
}

let posIndex = tw * ppy + ppx;
let hd = r / 256 * height;
let hd = h / 256 * height;
posAttrData.data[posIndex * 3 + 1] = hd;

this._heightData ||= [];
Expand Down
Loading

0 comments on commit 2c8e8ab

Please sign in to comment.