-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Update color instantly #275
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8c32c83
Update color instantly
remcoder e6d5305
add lighting
remcoder e84a81e
Fix typings
remcoder 9aab873
Cleanup
remcoder 4781862
linting
remcoder 86096ee
Remove old lighting
remcoder 8bb05ab
Merge branch 'develop' into poc/instant-color-change
remcoder 43c1164
Cleanup unused imports
remcoder a98ffd3
Support multicolor
remcoder e9a4ca8
Update checks
remcoder 8e6e651
feat: implement y-axis clipping for ShaderMaterial
remcoder 74b2fc2
feat: implement dual clipping system for tubes and travel lines
remcoder 93c03e6
Merge branch 'develop' into poc/instant-color-change
remcoder cd2f5d9
Cleanup and prevent error
remcoder 6e09075
Tweak model colors
remcoder 1bd4ec7
linting
remcoder d2d8af1
Remove gcode files
remcoder 75fd92e
Merge branch 'develop' into poc/instant-color-change
remcoder ff62254
Comment out highlighting as it is not working right now
remcoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { ShaderMaterial } from 'three/src/materials/ShaderMaterial.js'; | ||
import { Color } from 'three/src/math/Color.js'; | ||
|
||
/* | ||
This file contains a custom ShaderMaterial that calculates the lighting of a mesh based on the normal of the mesh and a fixed light direction. | ||
The material also allows for setting the color, ambient light intensity, directional light intensity, and brightness. | ||
*/ | ||
|
||
// Vertex Shader | ||
const vertexShader = ` | ||
uniform float clipMinY; | ||
uniform float clipMaxY; | ||
varying vec3 vNormal; | ||
varying vec3 vPosition; | ||
varying float vWorldY; | ||
|
||
void main() { | ||
vNormal = normalize(normalMatrix * normal); | ||
vPosition = position; | ||
vWorldY = (modelMatrix * vec4(position, 1.0)).y; | ||
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); | ||
} | ||
`; | ||
|
||
// Fragment Shader | ||
const fragmentShader = ` | ||
uniform vec3 uColor; | ||
uniform float ambient; | ||
uniform float directional; | ||
uniform float brightness; | ||
uniform float clipMinY; | ||
uniform float clipMaxY; | ||
varying vec3 vNormal; | ||
varying vec3 vPosition; | ||
varying float vWorldY; | ||
|
||
void main() { | ||
// Apply clipping | ||
if (vWorldY < clipMinY || vWorldY > clipMaxY) { | ||
discard; | ||
} | ||
|
||
// Fixed light direction (pointing from front-left) | ||
vec3 lightDir = normalize(vec3(-0.8, -0.2, -0.8)); | ||
|
||
// Calculate diffuse lighting with increased intensity | ||
float diff = max(dot(vNormal, -lightDir), 0.0) * directional; | ||
|
||
// Combine lighting with color | ||
vec3 finalColor = uColor * (diff + ambient); | ||
|
||
// Add a bit of extra brightness | ||
finalColor = min(finalColor * brightness, 1.0); | ||
|
||
gl_FragColor = vec4(finalColor, 1.0); | ||
} | ||
`; | ||
|
||
// cachedMaterial is used to store the material so that it is only created once for every color | ||
export const cachedMaterials: { [color: number]: ShaderMaterial } = {}; | ||
|
||
// TODO: remove the cache or add a way to clear it | ||
|
||
export function createColorMaterial( | ||
color: number, | ||
ambient: number, | ||
directional: number, | ||
brightness: number | ||
): ShaderMaterial { | ||
// Check if the material for the given color is already cached | ||
if (cachedMaterials[color]) { | ||
return cachedMaterials[color]; | ||
} | ||
console.log('createColorMaterial. not cached'); | ||
|
||
const material = new ShaderMaterial({ | ||
vertexShader, | ||
fragmentShader, | ||
uniforms: { | ||
uColor: { value: new Color(color) }, | ||
ambient: { value: ambient }, | ||
directional: { value: directional }, | ||
brightness: { value: brightness }, | ||
clipMinY: { value: -Infinity }, | ||
clipMaxY: { value: Infinity } | ||
} | ||
}); | ||
|
||
// Cache the material | ||
cachedMaterials[color] = material; | ||
|
||
return material; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be careful with changing
watchedObject
. I had issues with this referencing some old objects when they are replaced.We have many
this.an_object = new ObjectClass()
that made it a challenge to watch on the GUI.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the heads-up. I doublechecked and I don't think this changes breaks anything. The watchedObject is still the same instance but now it is typed to make the compiler happy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you checked with the other places where
watchedObject
is changed too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll check it again! Will try tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked all the dev helpers. Nothing is broken by this PR.
The Job info and _lastRenderTime don't update but I checked that was already the case be for this PR.
Only the types have changed and the namei of the instance but there shouldn't be even a change in the javascript code that is being generated.
btw, did you see you can now change the lighting through the dev helper?