Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a verbose parameter to disable the warning messages in the console #38

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ dist

# TernJS port file
.tern-port

# Webstorm project config folder
.idea
2 changes: 1 addition & 1 deletion examples/debug/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function App() {
<Canvas shadows>
<OrbitControls makeDefault />
<PerspectiveCamera position={[-5, 5, 5]} makeDefault />
<Environment preset="warehouse" />
<Environment files="https://dl.polyhaven.org/file/ph-assets/HDRIs/hdr/1k/empty_warehouse_01_1k.hdr" />
<Thing />
{/* <CacheTest /> */}

Expand Down
2 changes: 1 addition & 1 deletion examples/instances/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function App() {
}}
>
<Suspense>
<Environment preset="sunset" />
<Environment files="https://dl.polyhaven.org/file/ph-assets/HDRIs/hdr/1k/venice_sunset_1k.hdr" />
<Thing />
</Suspense>

Expand Down
2 changes: 1 addition & 1 deletion examples/points/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function App() {
}}
>
<Suspense>
<Environment preset="sunset" />
<Environment files="https://dl.polyhaven.org/file/ph-assets/HDRIs/hdr/1k/venice_sunset_1k.hdr" />
<Thing />
</Suspense>

Expand Down
2 changes: 1 addition & 1 deletion examples/waves/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default function App() {
<color attach="background" args={['#ebebeb']} />
<Suspense fallback={null}>
{['MeshPhysicalMaterial', 'MeshStanderedMaterial'].includes(Base.name) ? (
<Environment preset="sunset" />
<Environment files="https://dl.polyhaven.org/file/ph-assets/HDRIs/hdr/1k/venice_sunset_1k.hdr" />
) : (
<Lights />
)}
Expand Down
3 changes: 3 additions & 0 deletions package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function Box() {
baseMaterial: THREE.MeshPhysicalMaterial,
vertexShader: /* glsl */ ` ... `,
fragmentShader: /* glsl */ ` ... `,
silent: true, // Disables the default warning if true
uniforms: {
uTime: {
value: 0,
Expand Down Expand Up @@ -83,6 +84,8 @@ function Cube() {
baseMaterial={THREE.MeshPhysicalMaterial}
vertexShader={/* glsl */ ` ... `}
fragmentShader={/* glsl */ ` ... `}
{/*silent parameter to true disables the default warning if needed*/}
silent
uniforms={{
uTime: {
value: 0,
Expand Down
2 changes: 1 addition & 1 deletion package/src/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export default {
metalness: 'csm_Metalness', // Metalness
emissive: 'csm_Emissive', // Emissive
ao: 'csm_AO', // AO
bump: 'csm_Bump' // Bump
bump: 'csm_Bump', // Bump
}
2 changes: 2 additions & 0 deletions package/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type iCSMParams<T extends MaterialConstructor> = {
fragmentShader?: string
cacheKey?: () => string
patchMap?: iCSMPatchMap
silent?: boolean
uniforms?: { [key: string]: THREE.IUniform<any> }
} & (MaterialParams<T> extends undefined ? {} : MaterialParams<T>)

Expand All @@ -30,6 +31,7 @@ export interface iCSMInternals<T extends MaterialConstructor> {
type: string
isAlreadyExtended: boolean
cacheHash: string
silent?: boolean
}

export type Uniform = { [key: string]: THREE.IUniform<any> }
Expand Down
9 changes: 6 additions & 3 deletions package/src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function isConstructor<T extends MaterialConstructor>(f: T | InstanceType<T>): f
return true
}

function copyObject(target: any, source: any) {
function copyObject(target: any, source: any, silent = false) {
Object.assign(target, source)

const proto = Object.getPrototypeOf(source)
Expand All @@ -63,7 +63,7 @@ function copyObject(target: any, source: any) {
.forEach((val) => {
// If function exists on target, rename it with "base_" prefix
if (typeof target[val[0]] === 'function') {
console.warn(`Function ${val[0]} already exists on CSM, renaming to base_${val[0]}`)
if (!silent) console.warn(`Function ${val[0]} already exists on CSM, renaming to base_${val[0]}`)
const baseName = `base_${val[0]}`
target[baseName] = val[1].value.bind(target)
return
Expand Down Expand Up @@ -105,6 +105,7 @@ export default class CustomShaderMaterial<
uniforms,
patchMap,
cacheKey,
silent,
...opts
}: iCSMParams<T>) {
let base: THREE.Material
Expand All @@ -127,7 +128,7 @@ export default class CustomShaderMaterial<
// Copy all properties from base material onto this material
// Rename any functions that already exist on this material with "base_" prefix
super()
copyObject(this, base)
copyObject(this, base, silent)

// Set up private internals
this.__csm = {
Expand All @@ -140,6 +141,7 @@ export default class CustomShaderMaterial<
type: base.type,
isAlreadyExtended: !isFunctionEmpty(base.onBeforeCompile),
cacheHash: ``,
silent: silent,
}

this.uniforms = {
Expand Down Expand Up @@ -196,6 +198,7 @@ export default class CustomShaderMaterial<
fragmentShader: this.__csm.fragmentShader,
vertexShader: this.__csm.vertexShader,
uniforms: this.uniforms,
silent: this.__csm.silent,
patchMap: this.__csm.patchMap,
cacheKey: this.__csm.cacheKey,
}
Expand Down