Skip to content

Commit

Permalink
chore: Graphic3D debug draw use engine internal apis (Orillusion#334)
Browse files Browse the repository at this point in the history
chore: Graphic3DLine use engine internal apis
  • Loading branch information
Codeboy-cn authored Dec 5, 2023
1 parent 75c302b commit 2bd1994
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 318 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
export let Graphic3DShader: string = /*wgsl*/ `
#include "WorldMatrixUniform"
#include "GlobalUniform"
struct VertexAttributes {
@location(0) position: vec4<f32>,
@location(1) color: vec4<f32>,
}
struct VertexOutput {
@location(0) varying_WPos: vec4<f32>,
@location(1) varying_Color: vec4<f32>,
@builtin(position) member: vec4<f32>
};
@vertex
fn VertMain( vertex:VertexAttributes ) -> VertexOutput {
var worldMatrix = models.matrix[u32(vertex.position.w)];
var worldPos = (worldMatrix * vec4<f32>(vertex.position.xyz, 1.0));
var viewPosition = ((globalUniform.viewMat) * worldPos);
var clipPosition = globalUniform.projMat * viewPosition;
var ORI_VertexOut: VertexOutput;
ORI_VertexOut.varying_WPos = worldPos;
ORI_VertexOut.varying_Color = vertex.color;
ORI_VertexOut.member = clipPosition;
return ORI_VertexOut;
}
export let Graphic3DShader_fs: string = /*wgsl*/ `
struct FragmentOutput {
@location(0) color: vec4<f32>,
// #if USE_WORLDPOS
Expand All @@ -15,7 +40,7 @@ export let Graphic3DShader_fs: string = /*wgsl*/ `
};
@fragment
fn main(
fn FragMain(
@location(0) vWorldPos: vec4<f32>,
@location(1) varying_Color: vec4<f32>,
) -> FragmentOutput {
Expand All @@ -40,4 +65,4 @@ export let Graphic3DShader_fs: string = /*wgsl*/ `
// result.out_depth = ratio ;
return result;
}
`
`;
29 changes: 0 additions & 29 deletions src/assets/shader/graphic/Graphic3DShader_vs.ts

This file was deleted.

107 changes: 67 additions & 40 deletions src/gfx/renderJob/passRenderer/graphic/Graphic3DBatchRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
import { RenderNode } from "../../../../components/renderer/RenderNode";
import { BoundingBox } from "../../../../core/bound/BoundingBox";
import { View3D } from "../../../../core/View3D";
import { Color } from "../../../../math/Color";
import { Vector3 } from "../../../../math/Vector3";
import { RendererMask } from "../state/RendererMask";
import { RendererPassState } from "../state/RendererPassState";
import { PassType } from "../state/RendererType";
import { ClusterLightingRender } from "../cluster/ClusterLightingRender";
import { Graphic3DFixedRenderPipeline } from "./Graphic3DFixedRenderPipeline";
import { GraphicConfig } from "./GraphicConfig";
import { Graphics3DShape } from "./Graphics3DShape";
import { ClusterLightingBuffer } from "../cluster/ClusterLightingBuffer";
import { GeometryBase, Graphic3DFixedRenderMaterial, VertexAttributeName } from "../../../..";

/**
* @internal
*/
export class Graphic3DBatchRenderer extends RenderNode {
public shapes: Map<string, Graphics3DShape>;
protected mDirtyData: boolean = false;
protected mBatchSize: number;
protected mMinIndexCount: number;
protected mGPUPrimitiveTopology: GPUPrimitiveTopology;
protected mRenderPipeline: Graphic3DFixedRenderPipeline;

constructor(minIndexCount: number, topology: GPUPrimitiveTopology) {
super();
this.alwaysRender = true;
this.mMinIndexCount = minIndexCount;
this.mBatchSize = Math.trunc(65536 / this.mMinIndexCount);
this.mGPUPrimitiveTopology = topology;
this.shapes = new Map<string, Graphics3DShape>();
this.addRendererMask(RendererMask.Particle);
}

public init() {
super.init();
this.castGI = false;
this.castShadow = false;
this.geometry = new GeometryBase();

let indexData = new Uint16Array((Math.trunc(this.mMinIndexCount * this.mBatchSize / 4) + 1) * 4);
for (let i = 0; i < indexData.length; i++) {
indexData[i] = i;
}
this.geometry.setIndices(indexData);

this.geometry.setAttribute(VertexAttributeName.position, new Float32Array(4 * indexData.length));
this.geometry.setAttribute(VertexAttributeName.color, new Float32Array(4 * indexData.length));

this.geometry.addSubGeometry({
indexStart: 0,
indexCount: 0,
vertexStart: 0,
vertexCount: 0,
firstStart: 0,
index: 0,
topology: 0,
});

this.materials = [new Graphic3DFixedRenderMaterial(this.mGPUPrimitiveTopology)];
}

public fillShapeData(uuid: string, type: string, color: Color, points: Vector3[]) {
Expand All @@ -37,37 +61,37 @@ export class Graphic3DBatchRenderer extends RenderNode {

if (this.shapes.has(uuid)) {
data = this.shapes.get(uuid);
if (data.shapeData.length < GraphicConfig.ShapeVertexSize * points.length) {
data.shapeData = new Float32Array(GraphicConfig.ShapeVertexSize * points.length);
if (data.pointData.length < 4 * points.length) {
data.pointData = new Float32Array(4 * points.length);
data.colorData = new Float32Array(4 * points.length);
}
} else {
data = new Graphics3DShape(this.transform._worldMatrix.index);
data.type = type;
data.color = color;
data.shapeData = new Float32Array(GraphicConfig.ShapeVertexSize * points.length);
data.pointData = new Float32Array(4 * points.length);
data.colorData = new Float32Array(4 * points.length);
}

const shapeData = data.shapeData;
const pointData = data.pointData;
const colorData = data.colorData;
const transformIndex = this.transform._worldMatrix.index;
for (let i = 0, index = 0; i < points.length; ++i) {
const point = points[i];
shapeData[index++] = point.x;
shapeData[index++] = point.y;
shapeData[index++] = point.z;
shapeData[index++] = transformIndex;
shapeData[index++] = color.r;
shapeData[index++] = color.g;
shapeData[index++] = color.b;
shapeData[index++] = color.a;
pointData[index] = point.x;
colorData[index++] = color.r;

pointData[index] = point.y;
colorData[index++] = color.g;

pointData[index] = point.z;
colorData[index++] = color.b;

pointData[index] = transformIndex;
colorData[index++] = color.a;
}
this.shapes.set(uuid, data);
}

public init() {
super.init();
this.castGI = false;
this.castShadow = false;
this.mRenderPipeline = new Graphic3DFixedRenderPipeline(this.mMinIndexCount, this.mGPUPrimitiveTopology);
this.shapes.set(uuid, data);
}

public removeShape(uuid: string) {
Expand All @@ -77,26 +101,29 @@ export class Graphic3DBatchRenderer extends RenderNode {
}
}

protected initPipeline() {
this.object3D.bound = new BoundingBox(Vector3.ZERO, Vector3.MAX);
this._readyPipeline = true;
}

public nodeUpdate(view: View3D, passType: PassType, renderPassState: RendererPassState, clusterLightingBuffer?: ClusterLightingBuffer) {
// if(!this.enable || passType != RendererType.COLOR ) return ;
if (this.mDirtyData) {
this.mRenderPipeline.reset();
let offset = 0;
let posAttrData = this.geometry.getAttribute(VertexAttributeName.position);
let colAttrData = this.geometry.getAttribute(VertexAttributeName.color);

this.shapes.forEach((shape, uuid) => {
this.mRenderPipeline.addShapeData(shape);
posAttrData.data.set(shape.pointData, offset);
colAttrData.data.set(shape.colorData, offset);
offset += shape.pointData.length;
});

this.geometry.vertexBuffer.upload(VertexAttributeName.position, posAttrData);
this.geometry.vertexBuffer.upload(VertexAttributeName.color, colAttrData);

let count = offset / 4;
let indexCount = count;
this.geometry.subGeometries[0].lodLevels[0].indexCount = indexCount;


this.mDirtyData = false;
}
return;
}

public renderPass2(view: View3D, passType: PassType, rendererPassState: RendererPassState, clusterLightingBuffer: ClusterLightingBuffer, encoder: GPURenderPassEncoder, useBundle: boolean = false) {
// if(!this.enable || passType != RendererType.COLOR ) return ;
this.mRenderPipeline.render(rendererPassState, encoder);
super.nodeUpdate(view, passType, renderPassState, clusterLightingBuffer);
}

public allocGraphics3DShape(uuid: string, transformIndex: number) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Engine3D, GPUPrimitiveTopology, RenderShaderPass, Shader, ShaderLib, Texture } from "../../../..";
import { Graphic3DShader } from "../../../../assets/shader/graphic/Graphic3DShader";
import { Material } from "../../../../materials/Material";

/**
* @internal
*/
export class Graphic3DFixedRenderMaterial extends Material {
/**
* @constructor
*/
constructor(topology: GPUPrimitiveTopology = GPUPrimitiveTopology.triangle_list) {
super();
ShaderLib.register('Graphic3DShader', Graphic3DShader);

let colorPass = new RenderShaderPass('Graphic3DShader', 'Graphic3DShader');
colorPass.setShaderEntry(`VertMain`, `FragMain`);
colorPass.noticeValueChange();

let shader = new Shader();
shader.addRenderPass(colorPass);
this.shader = shader;

let shaderState = colorPass.shaderState;
shaderState.acceptShadow = false;
shaderState.castShadow = false;
shaderState.receiveEnv = false;
shaderState.acceptGI = false;
shaderState.useLight = false;
shaderState.topology = topology;
}
}
Loading

0 comments on commit 2bd1994

Please sign in to comment.