@awayjs/scene
Version:
AwayJS scene classes
128 lines (127 loc) • 5.9 kB
JavaScript
import { __extends } from "tslib";
import { TriangleElements } from '@awayjs/renderer';
/**
* @class away.base.TriangleElements
*/
var SkyboxElements = /** @class */ (function (_super) {
__extends(SkyboxElements, _super);
function SkyboxElements() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(SkyboxElements.prototype, "assetType", {
get: function () {
return SkyboxElements.assetType;
},
enumerable: false,
configurable: true
});
SkyboxElements.assetType = '[asset SkyboxElements]';
return SkyboxElements;
}(TriangleElements));
export { SkyboxElements };
import { Stage, ContextGLDrawMode, ContextGLProgramType } from '@awayjs/stage';
import { RenderGroup } from '@awayjs/renderer';
import { Matrix3D, Vector3D } from '@awayjs/core';
import { _Render_ElementsBase, _Stage_TriangleElements } from '@awayjs/renderer';
/**
* @class away.pool.LineMaterialPool
*/
var _Render_SkyboxElements = /** @class */ (function (_super) {
__extends(_Render_SkyboxElements, _super);
function _Render_SkyboxElements() {
return _super !== null && _super.apply(this, arguments) || this;
}
_Render_SkyboxElements.prototype._includeDependencies = function (shader) {
};
/**
* @inheritDoc
*/
_Render_SkyboxElements.prototype._getVertexCode = function (shader, registerCache, sharedRegisters) {
var code = '';
//get the projection coordinates
var position = (shader.globalPosDependencies > 0) ?
sharedRegisters.globalPositionVertex :
sharedRegisters.animatedPosition;
//reserving vertex constants for projection matrix
var viewMatrixReg = registerCache.getFreeVertexConstant();
registerCache.getFreeVertexConstant();
registerCache.getFreeVertexConstant();
registerCache.getFreeVertexConstant();
shader.viewMatrixIndex = viewMatrixReg.index * 4;
var scenePosition = registerCache.getFreeVertexConstant();
shader.scenePositionIndex = scenePosition.index * 4;
var skyboxScale = registerCache.getFreeVertexConstant();
var temp = registerCache.getFreeVertexVectorTemp();
code += 'mul ' + temp + ', ' + position + ', ' + skyboxScale + '\n' +
'add ' + temp + ', ' + temp + ', ' + scenePosition + '\n';
if (shader.projectionDependencies > 0) {
sharedRegisters.projectionFragment = registerCache.getFreeVarying();
code += 'm44 ' + temp + ', ' + temp + ', ' + viewMatrixReg + '\n' +
'mov ' + sharedRegisters.projectionFragment + ', ' + temp + '\n' +
'mov op, ' + temp + '\n';
}
else {
code += 'm44 op, ' + temp + ', ' + viewMatrixReg + '\n';
}
return code;
};
_Render_SkyboxElements.prototype._getFragmentCode = function (shader, registerCache, sharedRegisters) {
return '';
};
return _Render_SkyboxElements;
}(_Render_ElementsBase));
export { _Render_SkyboxElements };
/**
*
* @class away.pool._Stage_SkyboxElements
*/
var _Stage_SkyboxElements = /** @class */ (function (_super) {
__extends(_Stage_SkyboxElements, _super);
function _Stage_SkyboxElements() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._skyboxProjection = new Matrix3D();
return _this;
}
_Stage_SkyboxElements.prototype.draw = function (renderable, shader, count, offset) {
var index = shader.scenePositionIndex;
var camPos = shader.view.projection.transform.matrix3D.position;
shader.vertexConstantData[index++] = 2 * camPos.x;
shader.vertexConstantData[index++] = 2 * camPos.y;
shader.vertexConstantData[index++] = 2 * camPos.z;
shader.vertexConstantData[index++] = 1;
shader.vertexConstantData[index++] = shader.vertexConstantData[index++] =
shader.vertexConstantData[index++] = shader.view.projection.far / Math.sqrt(3);
shader.vertexConstantData[index] = 1;
var near = new Vector3D();
this._skyboxProjection.copyFrom(shader.view.viewMatrix3D);
this._skyboxProjection.copyRowTo(2, near);
var cx = near.x;
var cy = near.y;
var cz = near.z;
var cw = -(near.x * camPos.x + near.y * camPos.y + near.z * camPos.z + Math.sqrt(cx * cx + cy * cy + cz * cz));
var signX = cx >= 0 ? 1 : -1;
var signY = cy >= 0 ? 1 : -1;
var p = new Vector3D(signX, signY, 1, 1);
var inverse = this._skyboxProjection.clone();
inverse.invert();
var q = inverse.transformVector(p);
this._skyboxProjection.copyRowTo(3, p);
var a = (q.x * p.x + q.y * p.y + q.z * p.z + q.w * p.w) / (cx * q.x + cy * q.y + cz * q.z + cw * q.w);
this._skyboxProjection.copyRowFrom(2, new Vector3D(cx * a, cy * a, cz * a, cw * a));
//set constants
if (shader.sceneMatrixIndex >= 0)
shader.sceneMatrix.copyFrom(renderable.entity.renderSceneTransform, true);
shader.viewMatrix.copyFrom(this._skyboxProjection, true);
var context = this._pool.context;
context.setProgramConstantsFromArray(ContextGLProgramType.VERTEX, shader.vertexConstantData);
context.setProgramConstantsFromArray(ContextGLProgramType.FRAGMENT, shader.fragmentConstantData);
if (this._indices)
this.getIndexBufferGL().draw(ContextGLDrawMode.TRIANGLES, 0, this.numIndices);
else
this._pool.context.drawVertices(ContextGLDrawMode.TRIANGLES, offset, count || this.numVertices);
};
return _Stage_SkyboxElements;
}(_Stage_TriangleElements));
export { _Stage_SkyboxElements };
RenderGroup.registerElements(_Render_SkyboxElements, SkyboxElements);
Stage.registerAbstraction(_Stage_SkyboxElements, SkyboxElements);