|
|
|
|
|
|
|
|
import {safeSetProperty} from 'ts-browser-helpers' |
|
|
|
|
|
|
|
|
import {AnyFunction, safeSetProperty} from 'ts-browser-helpers' |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* |
|
|
* |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (targetPrototype: any, propertyKey: string|symbol) => { |
|
|
return (targetPrototype: any, propertyKey: string|symbol) => { |
|
|
const getUniform = (target: any)=>{ |
|
|
const getUniform = (target: any)=>{ |
|
|
const uniforms1 = isThis ? target : cUniforms ? uniforms : target.uniforms || target._uniforms |
|
|
|
|
|
|
|
|
const uniforms1 = isThis ? target : cUniforms ? uniforms : target.uniforms || target._uniforms || target.extraUniforms |
|
|
let propKey1 = cPropKey ? propKey : propertyKey |
|
|
let propKey1 = cPropKey ? propKey : propertyKey |
|
|
if (isThis) propKey1 = '_' + (propKey1 as string) |
|
|
if (isThis) propKey1 = '_' + (propKey1 as string) |
|
|
let a = uniforms1[propKey1!] |
|
|
let a = uniforms1[propKey1!] |
|
|
|
|
|
|
|
|
* @param key - define name |
|
|
* @param key - define name |
|
|
* @param thisMat - access this.defines instead of this.material.defines |
|
|
* @param thisMat - access this.defines instead of this.material.defines |
|
|
*/ |
|
|
*/ |
|
|
export function matDefine(key?: string|symbol, customDefines?: any, thisMat = false, onChange?: (...args: any[]) => any): PropertyDecorator { |
|
|
|
|
|
|
|
|
export function matDefine(key?: string|symbol, customDefines?: any, thisMat = false, onChange?: (...args: any[]) => any, processVal?: (newVal: any)=>any, invProcessVal?: (val:any)=>any): PropertyDecorator { |
|
|
// backing up properties as values are different when called again, no idea why. |
|
|
// backing up properties as values are different when called again, no idea why. |
|
|
const cDefines = !!customDefines |
|
|
const cDefines = !!customDefines |
|
|
const cPropKey = !!key |
|
|
const cPropKey = !!key |
|
|
|
|
|
|
|
|
return (targetPrototype: any, propertyKey: string|symbol) => { |
|
|
return (targetPrototype: any, propertyKey: string|symbol) => { |
|
|
const getTarget = (mat: any)=>{ |
|
|
const getTarget = (mat: any)=>{ |
|
|
const t = cDefines ? customDefines : mat.defines || mat._defines |
|
|
|
|
|
|
|
|
const t = cDefines ? customDefines : mat.defines || mat._defines || mat.extraDefines |
|
|
const p = cPropKey ? key : propertyKey |
|
|
const p = cPropKey ? key : propertyKey |
|
|
return {t, p} |
|
|
return {t, p} |
|
|
} |
|
|
} |
|
|
Object.defineProperty(targetPrototype, propertyKey, { |
|
|
Object.defineProperty(targetPrototype, propertyKey, { |
|
|
get() { |
|
|
get() { |
|
|
const {t, p} = getTarget(thisMat ? this : this.material) |
|
|
const {t, p} = getTarget(thisMat ? this : this.material) |
|
|
return t[p] |
|
|
|
|
|
|
|
|
let res = t[p] |
|
|
|
|
|
if (invProcessVal) res = invProcessVal(res) |
|
|
|
|
|
return res |
|
|
}, |
|
|
}, |
|
|
set(newVal: any) { |
|
|
set(newVal: any) { |
|
|
const {t, p} = getTarget(thisMat ? this : this.material) |
|
|
const {t, p} = getTarget(thisMat ? this : this.material) |
|
|
|
|
|
if (processVal) newVal = processVal(newVal) |
|
|
safeSetProperty(t, p, newVal, true) |
|
|
safeSetProperty(t, p, newVal, true) |
|
|
if (typeof onChange === 'function') { |
|
|
|
|
|
|
|
|
if (newVal === undefined) delete t[p] |
|
|
|
|
|
if (onChange && typeof onChange === 'function') { |
|
|
const params = [p, newVal] |
|
|
const params = [p, newVal] |
|
|
// same logic as onChange in refl.ts |
|
|
|
|
|
|
|
|
// same logic as onChange in ts-browser-helpers. todo: loop through object prototype chain like in onChange? |
|
|
if (onChange.name) { |
|
|
if (onChange.name) { |
|
|
const fn = this[onChange.name] |
|
|
|
|
|
|
|
|
const fn: AnyFunction = this[onChange.name] |
|
|
if (fn === onChange) |
|
|
if (fn === onChange) |
|
|
onChange.call(this, ...params) |
|
|
onChange.call(this, ...params) |
|
|
else if (fn.name === `bound ${onChange.name}`) |
|
|
|
|
|
|
|
|
else if (fn.name.endsWith(`bound ${onChange.name}`)) |
|
|
fn(...params) |
|
|
fn(...params) |
|
|
else onChange(...params) |
|
|
else onChange(...params) |
|
|
} else { |
|
|
} else { |