Skip to content

Commit

Permalink
NodeMaterial: Use materialReference() for env maps. (#28982)
Browse files Browse the repository at this point in the history
* NodeMaterial: Use `materialReference()` for env maps.

* TSL: Move to TSL approach

* Move to TSL approach *2

* EnvironmentNode: Fix PMREM generation.

---------

Co-authored-by: sunag <[email protected]>
  • Loading branch information
Mugen87 and sunag authored Aug 1, 2024
1 parent 1199339 commit 423f285
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export { default as CubeTextureNode, cubeTexture } from './accessors/CubeTexture
export { default as InstanceNode, instance } from './accessors/InstanceNode.js';
export { default as BatchNode, batch } from './accessors/BatchNode.js';
export { default as MaterialNode, materialAlphaTest, materialColor, materialShininess, materialEmissive, materialOpacity, materialSpecular, materialSpecularStrength, materialReflectivity, materialRoughness, materialMetalness, materialNormal, materialClearcoat, materialClearcoatRoughness, materialClearcoatNormal, materialRotation, materialSheen, materialSheenRoughness, materialIridescence, materialIridescenceIOR, materialIridescenceThickness, materialLineScale, materialLineDashSize, materialLineGapSize, materialLineWidth, materialLineDashOffset, materialPointWidth, materialAnisotropy, materialAnisotropyVector, materialDispersion, materialLightMap, materialAOMap } from './accessors/MaterialNode.js';
export * from './accessors/MaterialProperties.js';
export { default as MaterialReferenceNode, materialReference } from './accessors/MaterialReferenceNode.js';
export { default as RendererReferenceNode, rendererReference } from './accessors/RendererReferenceNode.js';
export { default as MorphNode, morphReference } from './accessors/MorphNode.js';
Expand Down
5 changes: 0 additions & 5 deletions src/nodes/accessors/MaterialNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,6 @@ class MaterialNode extends Node {

node = this.getFloat( scope );

} else if ( scope === MaterialNode.REFRACTION_RATIO ) {

node = this.getFloat( scope );

} else if ( scope === MaterialNode.LIGHT_MAP ) {

node = this.getTexture( scope ).rgb.mul( this.getFloat( 'lightMapIntensity' ) );
Expand Down Expand Up @@ -383,7 +379,6 @@ MaterialNode.POINT_WIDTH = 'pointWidth';
MaterialNode.DISPERSION = 'dispersion';
MaterialNode.LIGHT_MAP = 'light';
MaterialNode.AO_MAP = 'ao';
MaterialNode.REFRACTION_RATIO = 'refractionRatio';

export default MaterialNode;

Expand Down
3 changes: 3 additions & 0 deletions src/nodes/accessors/MaterialProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { uniform } from '../core/UniformNode.js';

export const materialRefractionRatio = /*@__PURE__*/ uniform( 0 ).onReference( ( { material } ) => material ).onRenderUpdate( ( { material } ) => material.refractionRatio );
2 changes: 2 additions & 0 deletions src/nodes/accessors/MaterialReferenceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class MaterialReferenceNode extends ReferenceNode {

//this.updateType = NodeUpdateType.RENDER;

this.isMaterialReferenceNode = true;

}

/*setNodeType( node ) {
Expand Down
5 changes: 5 additions & 0 deletions src/nodes/accessors/ReferenceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Node, { addNodeClass } from '../core/Node.js';
import { NodeUpdateType } from '../core/constants.js';
import { uniform } from '../core/UniformNode.js';
import { texture } from './TextureNode.js';
import { cubeTexture } from './CubeTextureNode.js';
import { buffer } from './BufferNode.js';
import { nodeObject } from '../shadernode/ShaderNode.js';
import { uniformArray } from './UniformArrayNode.js';
Expand Down Expand Up @@ -78,6 +79,10 @@ class ReferenceNode extends Node {

node = texture( null );

} else if ( uniformType === 'cubeTexture' ) {

node = cubeTexture( null );

} else {

node = uniform( null, uniformType );
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/accessors/ReflectVectorNode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cameraViewMatrix } from './CameraNode.js';
import { transformedNormalView } from './NormalNode.js';
import { positionViewDirection } from './PositionNode.js';
import { materialRefractionRatio } from './MaterialNode.js';
import { materialRefractionRatio } from './MaterialProperties.js';

export const reflectView = /*#__PURE__*/ positionViewDirection.negate().reflect( transformedNormalView );
export const refractView = /*#__PURE__*/ positionViewDirection.negate().refract( transformedNormalView, materialRefractionRatio );
Expand Down
14 changes: 8 additions & 6 deletions src/nodes/lighting/EnvironmentNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ class EnvironmentNode extends LightingNode {

setup( builder ) {

const { material } = builder;

let envNode = this.envNode;

if ( envNode.isTextureNode ) {
if ( envNode.isTextureNode || envNode.isMaterialReferenceNode ) {

const value = ( envNode.isTextureNode ) ? envNode.value : material[ envNode.property ];

let cacheEnvNode = _envNodeCache.get( envNode.value );
let cacheEnvNode = _envNodeCache.get( value );

if ( cacheEnvNode === undefined ) {

cacheEnvNode = pmremTexture( envNode.value );
cacheEnvNode = pmremTexture( value );

_envNodeCache.set( envNode.value, cacheEnvNode );
_envNodeCache.set( value, cacheEnvNode );

}

Expand All @@ -45,8 +49,6 @@ class EnvironmentNode extends LightingNode {

//

const { material } = builder;

const envMap = material.envMap;
const intensity = envMap ? reference( 'envMapIntensity', 'float', builder.material ) : reference( 'environmentIntensity', 'float', builder.scene ); // @TODO: Add materialEnvIntensity in MaterialNode

Expand Down
4 changes: 1 addition & 3 deletions src/nodes/materials/NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import { materialReference } from '../accessors/MaterialReferenceNode.js';
import { positionLocal, positionView } from '../accessors/PositionNode.js';
import { skinningReference } from '../accessors/SkinningNode.js';
import { morphReference } from '../accessors/MorphNode.js';
import { texture } from '../accessors/TextureNode.js';
import { cubeTexture } from '../accessors/CubeTextureNode.js';
import { lightsNode } from '../lighting/LightsNode.js';
import { mix } from '../math/MathNode.js';
import { float, vec3, vec4 } from '../shadernode/ShaderNode.js';
Expand Down Expand Up @@ -374,7 +372,7 @@ class NodeMaterial extends Material {

} else if ( this.envMap ) {

node = this.envMap.isCubeTexture ? cubeTexture( this.envMap ) : texture( this.envMap );
node = this.envMap.isCubeTexture ? materialReference( 'envMap', 'cubeTexture' ) : materialReference( 'envMap', 'texture' );

}

Expand Down

0 comments on commit 423f285

Please sign in to comment.