Skip to content

Commit

Permalink
Revert "Revert "Merge branch 'playcanvas:main' into zone-component""
Browse files Browse the repository at this point in the history
This reverts commit cc07f78.
  • Loading branch information
MushAsterion committed Apr 13, 2023
1 parent cc07f78 commit fdf1ef9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 71 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2011-2022 PlayCanvas Ltd.
Copyright (c) 2011-2023 PlayCanvas Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 7 additions & 0 deletions src/core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,10 @@ export const TRACEID_RENDERPIPELINE_ALLOC = 'RenderPipelineAlloc';
* @type {string}
*/
export const TRACEID_PIPELINELAYOUT_ALLOC = 'PipelineLayoutAlloc';

/**
* Logs the internal debug information for Elements.
*
* @type {string}
*/
export const TRACE_ID_ELEMENT = "Element";
41 changes: 10 additions & 31 deletions src/framework/components/element/component.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Debug } from '../../../core/debug.js';
import { TRACE_ID_ELEMENT } from '../../../core/constants.js';

import { Mat4 } from '../../../core/math/mat4.js';
import { Vec2 } from '../../../core/math/vec2.js';
Expand All @@ -19,10 +20,6 @@ import { ELEMENTTYPE_GROUP, ELEMENTTYPE_IMAGE, ELEMENTTYPE_TEXT, FITMODE_STRETCH
import { ImageElement } from './image-element.js';
import { TextElement } from './text-element.js';

// #if _DEBUG
const _debugLogging = false;
// #endif

const position = new Vec3();
const invParentWtm = new Mat4();

Expand Down Expand Up @@ -1207,9 +1204,7 @@ class ElementComponent extends Component {
this.system._prerender = [];
this.system.app.once('prerender', this._onPrerender, this);

// #if _DEBUG
if (_debugLogging) console.log('register prerender');
// #endif
Debug.trace(TRACE_ID_ELEMENT, 'register prerender');
}
const i = this.system._prerender.indexOf(this.entity);
if (i >= 0) {
Expand All @@ -1219,9 +1214,7 @@ class ElementComponent extends Component {
if (j < 0) {
this.system._prerender.push(current);
}
// #if _DEBUG
if (_debugLogging) console.log('set prerender root to: ' + current.name);
// #endif
Debug.trace(TRACE_ID_ELEMENT, 'set prerender root to: ' + current.name);
}

current = next;
Expand All @@ -1231,9 +1224,7 @@ class ElementComponent extends Component {
_onPrerender() {
for (let i = 0; i < this.system._prerender.length; i++) {
const mask = this.system._prerender[i];
// #if _DEBUG
if (_debugLogging) console.log('prerender from: ' + mask.name);
// #endif
Debug.trace(TRACE_ID_ELEMENT, 'prerender from: ' + mask.name);

// prevent call if element has been removed since being added
if (mask.element) {
Expand Down Expand Up @@ -1300,9 +1291,7 @@ class ElementComponent extends Component {

if (mask) {
const ref = mask.element._image._maskRef;
// #if _DEBUG
if (_debugLogging) console.log('masking: ' + this.entity.name + ' with ' + ref);
// #endif
Debug.trace(TRACE_ID_ELEMENT, 'masking: ' + this.entity.name + ' with ' + ref);

const sp = new StencilParameters({
ref: ref,
Expand All @@ -1316,9 +1305,7 @@ class ElementComponent extends Component {

this._maskedBy = mask;
} else {
// #if _DEBUG
if (_debugLogging) console.log('no masking on: ' + this.entity.name);
// #endif
Debug.trace(TRACE_ID_ELEMENT, 'no masking on: ' + this.entity.name);

// remove stencil params if this is image or text
if (renderableElement && renderableElement._setStencil) {
Expand Down Expand Up @@ -1348,12 +1335,8 @@ class ElementComponent extends Component {
// increment counter to count mask depth
depth++;

// #if _DEBUG
if (_debugLogging) {
console.log('masking from: ' + this.entity.name + ' with ' + (sp.ref + 1));
console.log('depth++ to: ', depth);
}
// #endif
Debug.trace(TRACE_ID_ELEMENT, 'masking from: ' + this.entity.name + ' with ' + (sp.ref + 1));
Debug.trace(TRACE_ID_ELEMENT, 'depth++ to: ', depth);

currentMask = this.entity;
}
Expand Down Expand Up @@ -1385,12 +1368,8 @@ class ElementComponent extends Component {
// increment mask counter to count depth of masks
depth++;

// #if _DEBUG
if (_debugLogging) {
console.log('masking from: ' + this.entity.name + ' with ' + sp.ref);
console.log('depth++ to: ', depth);
}
// #endif
Debug.trace(TRACE_ID_ELEMENT, 'masking from: ' + this.entity.name + ' with ' + sp.ref);
Debug.trace(TRACE_ID_ELEMENT, 'depth++ to: ', depth);

currentMask = this.entity;
}
Expand Down
18 changes: 7 additions & 11 deletions src/framework/components/element/image-element.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Debug } from '../../../core/debug.js';
import { TRACE_ID_ELEMENT } from '../../../core/constants.js';

import { math } from '../../../core/math/math.js';
import { Color } from '../../../core/math/color.js';
Expand Down Expand Up @@ -32,10 +33,6 @@ import { FITMODE_STRETCH, FITMODE_CONTAIN, FITMODE_COVER } from './constants.js'

import { Asset } from '../../asset/asset.js';

// #if _DEBUG
const _debugLogging = false;
// #endif

const _vertexFormatDeviceCache = new DeviceCache();

class ImageRenderable {
Expand Down Expand Up @@ -190,17 +187,16 @@ class ImageRenderable {
} else {
this.unmaskMeshInstance.drawOrder = this.meshInstance.drawOrder + this._element.getMaskOffset();
}
// #if _DEBUG
if (_debugLogging) console.log('setDrawOrder: ', this.unmaskMeshInstance.name, this.unmaskMeshInstance.drawOrder);
// #endif
Debug.trace(TRACE_ID_ELEMENT, 'setDrawOrder: ', this.unmaskMeshInstance.name, this.unmaskMeshInstance.drawOrder);
}
}

setDrawOrder(drawOrder) {
if (!this.meshInstance) return;
// #if _DEBUG
if (_debugLogging) console.log('setDrawOrder: ', this.meshInstance.name, drawOrder);
// #endif
if (!this.meshInstance)
return;

Debug.trace(TRACE_ID_ELEMENT, 'setDrawOrder: ', this.meshInstance.name, drawOrder);

this.meshInstance.drawOrder = drawOrder;
}

Expand Down
25 changes: 10 additions & 15 deletions src/framework/parsers/texture/img.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { path } from '../../../core/path.js';

import {
PIXELFORMAT_RGB8, PIXELFORMAT_RGBA8, TEXHINT_ASSET
PIXELFORMAT_RGBA8, TEXHINT_ASSET
} from '../../../platform/graphics/constants.js';
import { Texture } from '../../../platform/graphics/texture.js';
import { http } from '../../../platform/net/http.js';
Expand Down Expand Up @@ -30,7 +28,7 @@ class ImgParser {
if (hasContents) {
// ImageBitmap interface can load iage
if (this.device.supportsImageBitmap) {
this._loadImageBitmapFromData(asset.file.contents, callback);
this._loadImageBitmapFromBlob(new Blob([asset.file.contents]), callback);
return;
}
url = {
Expand Down Expand Up @@ -61,16 +59,14 @@ class ImgParser {
}

open(url, data, device) {
const ext = path.getExtension(url).toLowerCase();
const format = (ext === '.jpg' || ext === '.jpeg') ? PIXELFORMAT_RGB8 : PIXELFORMAT_RGBA8;
const texture = new Texture(device, {
name: url,
// #if _PROFILER
profilerHint: TEXHINT_ASSET,
// #endif
width: data.width,
height: data.height,
format: format
format: PIXELFORMAT_RGBA8
});
texture.setSource(data);
return texture;
Expand Down Expand Up @@ -124,21 +120,20 @@ class ImgParser {
retry: this.maxRetries > 0,
maxRetries: this.maxRetries
};
http.get(url, options, function (err, blob) {
http.get(url, options, (err, blob) => {
if (err) {
callback(err);
} else {
createImageBitmap(blob, {
premultiplyAlpha: 'none'
})
.then(imageBitmap => callback(null, imageBitmap))
.catch(e => callback(e));
this._loadImageBitmapFromBlob(blob, callback);
}
});
}

_loadImageBitmapFromData(data, callback) {
createImageBitmap(new Blob([data]), { premultiplyAlpha: 'none' })
_loadImageBitmapFromBlob(blob, callback) {
createImageBitmap(blob, {
premultiplyAlpha: 'none',
colorSpaceConversion: 'none'
})
.then(imageBitmap => callback(null, imageBitmap))
.catch(e => callback(e));
}
Expand Down
31 changes: 19 additions & 12 deletions src/platform/graphics/webgl/webgl-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ class WebglGraphicsDevice extends GraphicsDevice {
* reduce the latency by desynchronizing the canvas paint cycle from the event loop.
* @param {boolean} [options.xrCompatible] - Boolean that hints to the user agent to use a
* compatible graphics adapter for an immersive XR device.
* @param {WebGLRenderingContext | WebGL2RenderingContext} [options.gl] - The rendering context
* to use. If not specified, a new context will be created.
*/
constructor(canvas, options = {}) {
super(canvas);
Expand Down Expand Up @@ -386,35 +388,40 @@ class WebglGraphicsDevice extends GraphicsDevice {
Debug.log("Antialiasing has been turned off due to rendering issues on AppleWebKit 15.4");
}

// Retrieve the WebGL context
const preferWebGl2 = (options.preferWebGl2 !== undefined) ? options.preferWebGl2 : true;

const names = preferWebGl2 ? ["webgl2", "webgl", "experimental-webgl"] : ["webgl", "experimental-webgl"];
let gl = null;
for (let i = 0; i < names.length; i++) {
gl = canvas.getContext(names[i], options);

if (gl) {
this.webgl2 = (names[i] === DEVICETYPE_WEBGL2);
this._deviceType = this.webgl2 ? DEVICETYPE_WEBGL2 : DEVICETYPE_WEBGL1;
break;
// Retrieve the WebGL context
if (options.gl) {
gl = options.gl;
} else {
const preferWebGl2 = (options.preferWebGl2 !== undefined) ? options.preferWebGl2 : true;
const names = preferWebGl2 ? ["webgl2", "webgl", "experimental-webgl"] : ["webgl", "experimental-webgl"];
for (let i = 0; i < names.length; i++) {
gl = canvas.getContext(names[i], options);
if (gl) {
break;
}
}
}
this.gl = gl;

if (!gl) {
throw new Error("WebGL not supported");
}

this.gl = gl;
this.webgl2 = typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext;
this._deviceType = this.webgl2 ? DEVICETYPE_WEBGL2 : DEVICETYPE_WEBGL1;

// pixel format of the framebuffer
const alphaBits = gl.getParameter(gl.ALPHA_BITS);
this.framebufferFormat = alphaBits ? PIXELFORMAT_RGBA8 : PIXELFORMAT_RGB8;

const isChrome = platform.browser && !!window.chrome;
const isSafari = platform.browser && !!window.safari;
const isMac = platform.browser && navigator.appVersion.indexOf("Mac") !== -1;

// enable temporary texture unit workaround on desktop safari
this._tempEnableSafariTextureUnitWorkaround = platform.browser && !!window.safari;
this._tempEnableSafariTextureUnitWorkaround = isSafari;

// enable temporary workaround for glBlitFramebuffer failing on Mac Chrome (#2504)
this._tempMacChromeBlitFramebufferWorkaround = isMac && isChrome && !options.alpha;
Expand Down
4 changes: 3 additions & 1 deletion src/scene/materials/standard-material.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,9 @@ class StandardMaterial extends Material {
this._setParameter('material_sheenGloss', this.sheenGloss);
}

if (this.refractionIndex !== 1.0 / 1.5) {
if (this.refractionIndex === 0.0) {
this._setParameter('material_f0', 1.0);
} else if (this.refractionIndex !== 1.0 / 1.5) {
const oneOverRefractionIndex = 1.0 / this.refractionIndex;
const f0 = (oneOverRefractionIndex - 1) / (oneOverRefractionIndex + 1);
this._setParameter('material_f0', f0 * f0);
Expand Down

0 comments on commit fdf1ef9

Please sign in to comment.