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

New Material Type For Threejs #28543

Closed
wants to merge 5 commits into from
Closed
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
561 changes: 296 additions & 265 deletions build/three.cjs

Large diffs are not rendered by default.

562 changes: 296 additions & 266 deletions build/three.module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/three.module.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions examples/jsm/curves/NURBSUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,16 @@ function calcVolumePoint( p, q, r, U, V, W, P, u, v, w, target ) {
}

}

const Sw = new Vector4( 0, 0, 0, 0 );
for ( let m = 0; m <= r; ++ m ) {

for ( let l = 0; l <= q; ++ l ) {

Sw.add( temp[ m ][ l ].multiplyScalar( Nw[ m ] ).multiplyScalar( Nv[ l ] ) );

}

}

Sw.divideScalar( Sw.w );
Expand Down
8 changes: 4 additions & 4 deletions examples/jsm/objects/GroundedSkybox.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Mesh, MeshBasicMaterial, SphereGeometry, Vector3 } from 'three';

/**
* A ground-projected skybox. The height is how far the camera that took the photo was above the ground -
* a larger value will magnify the downward part of the image. By default the object is centered at the camera,
* so it is often helpful to set skybox.position.y = height to put the ground at the origin. Set the radius
* A ground-projected skybox. The height is how far the camera that took the photo was above the ground -
* a larger value will magnify the downward part of the image. By default the object is centered at the camera,
* so it is often helpful to set skybox.position.y = height to put the ground at the origin. Set the radius
* large enough to ensure your user's camera stays inside.
*/

Expand All @@ -18,7 +18,7 @@ class GroundedSkybox extends Mesh {
}

const geometry = new SphereGeometry( radius, 2 * resolution, resolution );
geometry.scale( 1, 1, -1 );
geometry.scale( 1, 1, - 1 );

const pos = geometry.getAttribute( 'position' );
const tmp = new Vector3();
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/renderers/CSS2DRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class CSS2DRenderer {
return;

}

if ( object.isCSS2DObject ) {

_vector.setFromMatrixPosition( object.matrixWorld );
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/renderers/CSS3DRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class CSS3DRenderer {
function hideObject( object ) {

if ( object.isCSS3DObject ) object.element.style.display = 'none';

for ( let i = 0, l = object.children.length; i < l; i ++ ) {

hideObject( object.children[ i ] );
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ ${ flowData.code }

snippets.push( snippet );

snippets.push( `\nvar<private> output : ${ name };\n\n`);
snippets.push( `\nvar<private> output : ${ name };\n\n` );

}

Expand Down
2 changes: 1 addition & 1 deletion examples/webgl_camera_logarithmicdepthbuffer.html
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@

materialargs.color = new THREE.Color().setHSL( Math.random(), 0.5, 0.5 );

const material = new THREE.MeshPhongMaterial( materialargs );
const material = new THREE.MeshSphereLambertMaterial( materialargs );

const group = new THREE.Group();
group.position.z = - labeldata[ i ].size * scale;
Expand Down
2 changes: 2 additions & 0 deletions src/loaders/MaterialLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
MeshToonMaterial,
MeshNormalMaterial,
MeshLambertMaterial,
MeshSphereLambertMaterial,
MeshDepthMaterial,
MeshDistanceMaterial,
MeshBasicMaterial,
Expand Down Expand Up @@ -356,6 +357,7 @@ class MaterialLoader extends Loader {
MeshToonMaterial,
MeshNormalMaterial,
MeshLambertMaterial,
MeshSphereLambertMaterial,
MeshDepthMaterial,
MeshDistanceMaterial,
MeshBasicMaterial,
Expand Down
2 changes: 2 additions & 0 deletions src/materials/Materials.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { MeshPhongMaterial } from './MeshPhongMaterial.js';
import { MeshToonMaterial } from './MeshToonMaterial.js';
import { MeshNormalMaterial } from './MeshNormalMaterial.js';
import { MeshLambertMaterial } from './MeshLambertMaterial.js';
import { MeshSphereLambertMaterial } from './MeshSphereLambertMatertial.js';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- import { MeshSphereLambertMaterial } from './MeshSphereLambertMatertial.js';
+ import { MeshSphereLambertMaterial } from './MeshSphereLambertMaterial.js';

import { MeshDepthMaterial } from './MeshDepthMaterial.js';
import { MeshDistanceMaterial } from './MeshDistanceMaterial.js';
import { MeshBasicMaterial } from './MeshBasicMaterial.js';
Expand All @@ -30,6 +31,7 @@ export {
MeshNormalMaterial,
MeshLambertMaterial,
MeshDepthMaterial,
MeshSphereLambertMaterial,
MeshDistanceMaterial,
MeshBasicMaterial,
MeshMatcapMaterial,
Expand Down
16 changes: 16 additions & 0 deletions src/materials/MeshSphereLambertMatertial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MeshLambertMaterial } from './MeshLambertMaterial';

class MeshSphereLambertMaterial extends MeshLambertMaterial {

constructor( parameters ) {

super( parameters );
this.type = 'MeshSphereLambertMaterial';
this.isMeshLambertMaterial = false;
this.isMeshSphereLambertMaterial = true;

}

}

export { MeshSphereLambertMaterial };
5 changes: 3 additions & 2 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1994,7 +1994,8 @@ class WebGLRenderer {
if ( material.isMeshPhongMaterial ||
material.isMeshToonMaterial ||
material.isMeshLambertMaterial ||
material.isMeshBasicMaterial ||
material.isMeshSphereLambertMaterial ||
material.isMeshBasicMaterial ||
material.isMeshStandardMaterial ||
material.isShaderMaterial ) {

Expand Down Expand Up @@ -2179,7 +2180,7 @@ class WebGLRenderer {

function materialNeedsLights( material ) {

return material.isMeshLambertMaterial || material.isMeshToonMaterial || material.isMeshPhongMaterial ||
return material.isMeshLambertMaterial || material.isMeshSphereLambertMaterial || material.isMeshToonMaterial || material.isMeshPhongMaterial ||
material.isMeshStandardMaterial || material.isShadowMaterial ||
( material.isShaderMaterial && material.lights === true );

Expand Down
7 changes: 7 additions & 0 deletions src/renderers/shaders/ShaderChunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import gradientmap_pars_fragment from './ShaderChunk/gradientmap_pars_fragment.g
import lightmap_pars_fragment from './ShaderChunk/lightmap_pars_fragment.glsl.js';
import lights_lambert_fragment from './ShaderChunk/lights_lambert_fragment.glsl.js';
import lights_lambert_pars_fragment from './ShaderChunk/lights_lambert_pars_fragment.glsl.js';
import lights_spherelambert_fragment from './ShaderChunk/lights_spherelambert_fragment.glsl.js';
import lights_spherelambert_pars_fragment from './ShaderChunk/lights_spherelambert_pars_fragment.glsl.js';
import lights_pars_begin from './ShaderChunk/lights_pars_begin.glsl.js';
import envmap_physical_pars_fragment from './ShaderChunk/envmap_physical_pars_fragment.glsl.js';
import lights_toon_fragment from './ShaderChunk/lights_toon_fragment.glsl.js';
Expand Down Expand Up @@ -115,6 +117,7 @@ import * as equirect from './ShaderLib/equirect.glsl.js';
import * as linedashed from './ShaderLib/linedashed.glsl.js';
import * as meshbasic from './ShaderLib/meshbasic.glsl.js';
import * as meshlambert from './ShaderLib/meshlambert.glsl.js';
import * as meshspherelambert from './ShaderLib/meshspherelambert.glsl.js';
import * as meshmatcap from './ShaderLib/meshmatcap.glsl.js';
import * as meshnormal from './ShaderLib/meshnormal.glsl.js';
import * as meshphong from './ShaderLib/meshphong.glsl.js';
Expand Down Expand Up @@ -171,6 +174,8 @@ export const ShaderChunk = {
lightmap_pars_fragment: lightmap_pars_fragment,
lights_lambert_fragment: lights_lambert_fragment,
lights_lambert_pars_fragment: lights_lambert_pars_fragment,
lights_spherelambert_fragment: lights_spherelambert_fragment,
lights_spherelambert_pars_fragment: lights_spherelambert_pars_fragment,
lights_pars_begin: lights_pars_begin,
lights_toon_fragment: lights_toon_fragment,
lights_toon_pars_fragment: lights_toon_pars_fragment,
Expand Down Expand Up @@ -251,6 +256,8 @@ export const ShaderChunk = {
meshbasic_frag: meshbasic.fragment,
meshlambert_vert: meshlambert.vertex,
meshlambert_frag: meshlambert.fragment,
meshspherelambert_vert: meshspherelambert.vertex,
meshspherelambert_frag: meshspherelambert.fragment,
meshmatcap_vert: meshmatcap.vertex,
meshmatcap_frag: meshmatcap.fragment,
meshnormal_vert: meshnormal.vertex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default /* glsl */`

uniform float reflectivity;

#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( LAMBERT )
#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( LAMBERT ) || defined( SPHERELAMBERT )

#define ENV_WORLDPOS

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default /* glsl */`
#ifdef USE_ENVMAP

#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( LAMBERT )
#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( LAMBERT ) || defined(SPHERELAMBERT)

#define ENV_WORLDPOS

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default /* glsl */`
LambertMaterial material;
material.diffuseColor = diffuseColor.rgb;
material.specularStrength = specularStrength;
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
export default /* glsl */`
varying vec3 vViewPosition;

struct LambertMaterial {

vec3 diffuseColor;
float specularStrength;

};
float safeacos(const float x) {
return acos(clamp(x, -1.0, 1.0));
}

float phase(float u) {
return (2.0*(sqrt(1.0 - u*u) - u*acos(u)))/(3.0*PI*PI);
}
vec3 shadeLambertianSphereBRDF(vec3 wi, vec3 wo, vec3 norm,vec3 kd) {

float ui = dot(wi, norm);
float uo = dot(wo, norm);
if (ui < 0.0 || uo < 0.0) return vec3(0.0);

float uIuO = ui*uo;
vec3 c = (1.0 - pow(1.0 - kd, vec3(2.73556))) / (1.0 - 0.184096*pow(1.0 - kd, vec3(2.48423)));
float minusiodot = -dot(wi, wo);
float ui2 = ui*ui;
float uo2 = uo*uo;
float S = sqrt((1.0-ui2)*(1.0-uo2));
float cp = -((minusiodot + uIuO)/S);
float phi = safeacos(cp);


// Single-Scattering component, corresponds to "f_1" in the paper.
vec3 SS = c*(phase(minusiodot) / (ui + uo));

// The block is a literal coding of Equation 48 from the paper.

vec3 fr = max(
vec3(0.0),
SS + 0.234459*pow(kd, vec3(1.85432)) \
+ (0.0151829*(c-0.24998)*(abs(phi)+sqrt(uIuO))) / (0.113706 + (safeacos(S)/S))
);
return PI * uo * fr;

}
/* struct IncidentLight {
vec3 direction;
};

struct ReflectedLight {
vec3 directDiffuse;
vec3 directSpecular;
vec3 indirectDiffuse;
vec3 indirectSpecular;
}; */
void RE_Direct_Lambert( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in LambertMaterial material, inout ReflectedLight reflectedLight ) {


vec3 kd = material.diffuseColor;
vec3 wo = directLight.direction;
vec3 wi = geometryViewDir;
vec3 color = shadeLambertianSphereBRDF(wi,wo,geometryNormal,kd);
reflectedLight.directDiffuse += color; // no need to multiply with BRDF_Lambert( material.diffuseColor );

}

void RE_IndirectDiffuse_Lambert( const in vec3 irradiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in LambertMaterial material, inout ReflectedLight reflectedLight ) {



reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );

}

#define RE_Direct RE_Direct_Lambert
#define RE_IndirectDiffuse RE_IndirectDiffuse_Lambert
`;
23 changes: 23 additions & 0 deletions src/renderers/shaders/ShaderLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ const ShaderLib = {

},

spherelambert: {

uniforms: /*@__PURE__*/ mergeUniforms( [
UniformsLib.common,
UniformsLib.specularmap,
UniformsLib.envmap,
UniformsLib.aomap,
UniformsLib.lightmap,
UniformsLib.emissivemap,
UniformsLib.bumpmap,
UniformsLib.normalmap,
UniformsLib.displacementmap,
UniformsLib.fog,
UniformsLib.lights,
{
emissive: { value: /*@__PURE__*/ new Color( 0x000000 ) }
}
] ),

vertexShader: ShaderChunk.meshspherelambert_vert,
fragmentShader: ShaderChunk.meshspherelambert_frag
},

phong: {

uniforms: /*@__PURE__*/ mergeUniforms( [
Expand Down
Loading
Loading