-
-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor skybox using infinity matrix (#1048)
* feat: refactor skybox using infinity matrix * fix: camera orth sky render error
- Loading branch information
1 parent
6e127ce
commit 6b8b696
Showing
4 changed files
with
70 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
#include <common_vert> | ||
|
||
uniform mat4 u_mvpNoscale; | ||
uniform mat4 u_VPMat; | ||
|
||
varying vec3 v_cubeUV; | ||
|
||
void main() { | ||
|
||
v_cubeUV = vec3( -POSITION.x, POSITION.yz );// TextureCube is left-hand,so x need inverse | ||
gl_Position = u_mvpNoscale * vec4( POSITION, 1.0 ); | ||
gl_Position.z = gl_Position.w; | ||
gl_Position = u_VPMat * vec4( POSITION, 1.0 ); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,73 @@ | ||
import { Matrix } from "@oasis-engine/math"; | ||
import { Mesh } from "../graphic"; | ||
import { MathUtil, Matrix } from "@oasis-engine/math"; | ||
import { Mesh } from "../graphic/Mesh"; | ||
import { Material } from "../material"; | ||
import { Camera } from "../Camera"; | ||
import { Logger } from "../base/Logger"; | ||
import { Shader } from "../shader/Shader"; | ||
import { ShaderMacroCollection } from "../shader/ShaderMacroCollection"; | ||
|
||
/** | ||
* Sky. | ||
*/ | ||
export class Sky { | ||
private static _epsilon: number = 1e-6; | ||
private static _viewProjMatrix: Matrix = new Matrix(); | ||
private static _projectionMatrix: Matrix = new Matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, Sky._epsilon - 1, -1, 0, 0, 0, 0); | ||
|
||
/** Material of the sky. */ | ||
material: Material; | ||
/** Mesh of the sky. */ | ||
mesh: Mesh; | ||
/** @internal */ | ||
_matrix: Matrix = new Matrix(); | ||
|
||
/** | ||
* @internal | ||
*/ | ||
_render(camera: Camera): void { | ||
const { material, mesh } = this; | ||
if (!material) { | ||
Logger.warn("The material of sky is not defined."); | ||
return; | ||
} | ||
if (!mesh) { | ||
Logger.warn("The mesh of sky is not defined."); | ||
return; | ||
} | ||
|
||
const { engine, aspectRatio, fieldOfView, viewMatrix, shaderData: cameraShaderData } = camera; | ||
const { _viewProjMatrix: viewProjMatrix, _projectionMatrix: projectionMatrix } = Sky; | ||
const rhi = engine._hardwareRenderer; | ||
const { shaderData: materialShaderData, shader, renderState } = material; | ||
|
||
// no-scale view matrix | ||
viewProjMatrix.copyFrom(viewMatrix); | ||
const e = viewProjMatrix.elements; | ||
e[12] = e[13] = e[14] = 0; | ||
|
||
// epsilon-infinity projection matrix http://terathon.com/gdc07_lengyel.pdf | ||
const f = 1.0 / Math.tan(MathUtil.degreeToRadian(fieldOfView) / 2); | ||
projectionMatrix.elements[0] = f / aspectRatio; | ||
projectionMatrix.elements[5] = f; | ||
|
||
// view-proj matrix | ||
Matrix.multiply(projectionMatrix, viewProjMatrix, viewProjMatrix); | ||
const originViewProjMatrix = cameraShaderData.getMatrix(Camera._vpMatrixProperty); | ||
cameraShaderData.setMatrix(Camera._vpMatrixProperty, viewProjMatrix); | ||
|
||
const compileMacros = Shader._compileMacros; | ||
ShaderMacroCollection.unionCollection( | ||
camera._globalShaderMacro, | ||
materialShaderData._macroCollection, | ||
compileMacros | ||
); | ||
const program = shader.passes[0]._getShaderProgram(engine, compileMacros); | ||
program.bind(); | ||
program.groupingOtherUniformBlock(); | ||
program.uploadAll(program.cameraUniformBlock, cameraShaderData); | ||
program.uploadAll(program.materialUniformBlock, materialShaderData); | ||
program.uploadUnGroupTextures(); | ||
|
||
renderState._apply(engine, false); | ||
rhi.drawPrimitive(mesh, mesh.subMesh, program); | ||
cameraShaderData.setMatrix(Camera._vpMatrixProperty, originViewProjMatrix); | ||
} | ||
} |