Skip to content

Commit 921cdd0

Browse files
authored
Updated reprojectTexture (playcanvas#3167)
* update reproject * add backwards compatibilty * remove device option * use instanceOf * remove unused param doc
1 parent 95257b6 commit 921cdd0

File tree

2 files changed

+61
-27
lines changed

2 files changed

+61
-27
lines changed

examples/graphics/render-to-cubemap.html

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,22 +252,30 @@
252252
var srcCube = shinyBall.script.cubemapRenderer.cubeMap;
253253

254254
// cube -> equi1
255-
pc.reprojectTexture(device, srcCube, textureEqui, undefined, 1);
255+
pc.reprojectTexture(srcCube, textureEqui, {
256+
numSamples: 1
257+
});
256258
app.renderTexture(-0.6, 0.7, 0.6, 0.3, textureEqui);
257259

258260
// cube -> octa1
259-
pc.reprojectTexture(device, srcCube, textureOcta, undefined, 1);
261+
pc.reprojectTexture(srcCube, textureOcta, {
262+
numSamples: 1
263+
});
260264
app.renderTexture(0.7, 0.7, 0.4, 0.4, textureOcta);
261265

262266
// equi1 -> octa2
263-
pc.reprojectTexture(device, textureEqui, textureOcta2, 32, 1024);
267+
pc.reprojectTexture(textureEqui, textureOcta2, {
268+
specularPower: 32,
269+
numSamples: 1024
270+
});
264271
app.renderTexture(-0.7, -0.7, 0.4, 0.4, textureOcta2);
265272

266273
// octa1 -> equi2
267-
pc.reprojectTexture(device, textureOcta, textureEqui2, 16, 512);
274+
pc.reprojectTexture(textureOcta, textureEqui2, {
275+
specularPower: 16,
276+
numSamples: 512
277+
});
268278
app.renderTexture(0.6, -0.7, 0.6, 0.3, textureEqui2);
269-
270-
271279
});
272280
</script>
273281
</body>

src/graphics/reproject-texture.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createShaderFromCode } from './program-lib/utils.js';
77
import { drawQuadWithShader } from './simple-post-effect.js';
88
import { shaderChunks } from './program-lib/chunks/chunks.js';
99
import { RenderTarget } from './render-target.js';
10+
import { GraphicsDevice } from './graphics-device.js';
1011

1112
// get a coding string for texture based on its type and pixel format.
1213
function getCoding(texture) {
@@ -48,15 +49,36 @@ function getProjectionName(projection) {
4849
* function can read and write textures with pixel data in RGBE, RGBM, linear and sRGB formats. When
4950
* specularPower is specified it will perform a phong-weighted convolution of the source (for generating
5051
* a gloss maps).
51-
* @param {GraphicsDevice} device - The graphics device.
5252
* @param {Texture} source - The source texture.
5353
* @param {Texture} target - The target texture.
54-
* @param {number} [specularPower] - Optional specular power. When specular power is specified,
54+
* @param {object} [options] - The options object.
55+
* @param {number} [options.specularPower] - Optional specular power. When specular power is specified,
5556
* the source is convolved by a phong-weighted kernel raised to the specified power. Otherwise
5657
* the function performs a standard resample.
57-
* @param {number} [numSamples] - Optional number of samples (default is 1024).
58+
* @param {number} [options.numSamples] - Optional number of samples (default is 1024).
59+
* @param {number} [options.face] - Optional cubemap face to update (default is update all faces).
5860
*/
59-
function reprojectTexture(device, source, target, specularPower = 1, numSamples = 1024) {
61+
function reprojectTexture(source, target, options = {}) {
62+
// maintain backwards compatibility with previous function signature
63+
// reprojectTexture(device, source, target, specularPower = 1, numSamples = 1024)
64+
if (source instanceof GraphicsDevice) {
65+
source = arguments[1];
66+
target = arguments[2];
67+
options = {
68+
specularPower: arguments[3] === undefined ? 1 : arguments[3],
69+
numSamples: arguments[4] === undefined ? 1024 : arguments[4]
70+
};
71+
// #if _DEBUG
72+
console.warn('DEPRECATED: please use the updated pc.reprojectTexture API.');
73+
// #endif
74+
}
75+
76+
// extract options
77+
const device = source.device;
78+
const specularPower = options.hasOwnProperty('specularPower') ? options.specularPower : 1;
79+
const numSamples = options.hasOwnProperty('numSamples') ? options.numSamples : 1024;
80+
const face = options.hasOwnProperty('face') ? options.face : null;
81+
6082
const processFunc = (specularPower === 1) ? 'reproject' : 'prefilter';
6183
const decodeFunc = "decode" + getCoding(source);
6284
const encodeFunc = "encode" + getCoding(target);
@@ -91,23 +113,27 @@ function reprojectTexture(device, source, target, specularPower = 1, numSamples
91113
constantSource.setValue(source);
92114

93115
const constantParams = device.scope.resolve("params");
94-
const params = new Float32Array(4);
95-
params[1] = specularPower;
96-
params[2] = 1.0 - (source.fixCubemapSeams ? 1.0 / source.width : 0.0); // source seam scale
97-
params[3] = 1.0 - (target.fixCubemapSeams ? 1.0 / target.width : 0.0); // target seam scale
98-
99-
for (let face = 0; face < (target.cubemap ? 6 : 1); face++) {
100-
const renderTarget = new RenderTarget({
101-
colorBuffer: target,
102-
face: face,
103-
depth: false
104-
});
105-
params[0] = face;
106-
constantParams.setValue(params);
107-
108-
drawQuadWithShader(device, renderTarget, shader);
109-
110-
renderTarget.destroy();
116+
const params = [
117+
0,
118+
specularPower,
119+
1.0 - (source.fixCubemapSeams ? 1.0 / source.width : 0.0), // source seam scale
120+
1.0 - (target.fixCubemapSeams ? 1.0 / target.width : 0.0) // target seam scale
121+
];
122+
123+
for (let f = 0; f < (target.cubemap ? 6 : 1); f++) {
124+
if (face === null || f === face) {
125+
const renderTarget = new RenderTarget({
126+
colorBuffer: target,
127+
face: f,
128+
depth: false
129+
});
130+
params[0] = f;
131+
constantParams.setValue(params);
132+
133+
drawQuadWithShader(device, renderTarget, shader);
134+
135+
renderTarget.destroy();
136+
}
111137
}
112138

113139
// #if _DEBUG

0 commit comments

Comments
 (0)