Skip to content

Commit

Permalink
Merge pull request #2331 from Azagwen/next
Browse files Browse the repository at this point in the history
Added "solid with marker color" view mode from #2316
  • Loading branch information
JannisX11 authored May 31, 2024
2 parents a581305 + a565e72 commit 475f6ff
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 83 deletions.
5 changes: 4 additions & 1 deletion js/outliner/cube.js
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,10 @@ new NodePreviewController(Cube, {
mesh.geometry.setIndex(indices)

if (Project.view_mode === 'solid') {
mesh.material = Canvas.solidMaterial
mesh.material = Canvas.monochromaticSolidMaterial

} else if (Project.view_mode === 'colored_solid') {
mesh.material = Canvas.coloredSolidMaterials[element.color % Canvas.emptyMaterials.length]

} else if (Project.view_mode === 'wireframe') {
mesh.material = Canvas.wireframeMaterial
Expand Down
5 changes: 4 additions & 1 deletion js/outliner/mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,10 @@ new NodePreviewController(Mesh, {
let {mesh} = element;

if (Project.view_mode === 'solid') {
mesh.material = Canvas.solidMaterial
mesh.material = Canvas.monochromaticSolidMaterial

} else if (Project.view_mode === 'colored_solid') {
mesh.material = Canvas.coloredSolidMaterials[element.color]

} else if (Project.view_mode === 'wireframe') {
mesh.material = Canvas.wireframeMaterial
Expand Down
5 changes: 4 additions & 1 deletion js/outliner/texture_mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,11 @@ new NodePreviewController(TextureMesh, {
let {mesh} = element;

if (Project.view_mode === 'solid') {
mesh.material = Canvas.solidMaterial
mesh.material = Canvas.monochromaticSolidMaterial

} else if (Project.view_mode === 'colored_solid') {
mesh.material = Canvas.coloredSolidMaterials[0]

} else if (Project.view_mode === 'wireframe') {
mesh.material = Canvas.wireframeMaterial

Expand Down
180 changes: 104 additions & 76 deletions js/preview/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,78 @@ const Reusable = {
euler2: new THREE.Euler(),
}

// Aza note:
// ---------------------------------------
// Not sure about the pertinence of doing this, but my reasoning is that it saves us
// from copying the exact same shaders twice for both solid view mode variants (monochromatic & colored).
const SolidMaterialShaders = {
vertShader: `
attribute float highlight;
uniform bool SHADE;
varying float light;
varying float lift;
float AMBIENT = 0.1;
float XFAC = -0.05;
float ZFAC = 0.05;
void main()
{
if (SHADE) {
vec3 N = normalize( vec3( modelViewMatrix * vec4(normal, 0.0) ) );
light = (0.2 + abs(N.z) * 0.8) * (1.0-AMBIENT) + N.x*N.x * XFAC + N.y*N.y * ZFAC + AMBIENT;
} else {
light = 1.0;
}
if (highlight == 2.0) {
lift = 0.3;
} else if (highlight == 1.0) {
lift = 0.12;
} else {
lift = 0.0;
}
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
gl_Position = projectionMatrix * mvPosition;
}`,
fragShader: `
#ifdef GL_ES
precision ${isApp ? 'highp' : 'mediump'} float;
#endif
uniform bool SHADE;
uniform float BRIGHTNESS;
uniform vec3 base;
varying float light;
varying float lift;
void main(void)
{
gl_FragColor = vec4(lift + base * light * BRIGHTNESS, 1.0);
if (lift > 0.1) {
gl_FragColor.b = gl_FragColor.b * 1.16;
gl_FragColor.g = gl_FragColor.g * 1.04;
}
if (lift > 0.2) {
gl_FragColor.r = gl_FragColor.r * 0.6;
gl_FragColor.g = gl_FragColor.g * 0.7;
}
}`
}

const Canvas = {
// Stores various colors for the 3D scene
gizmo_colors,
Expand All @@ -58,81 +130,15 @@ const Canvas = {
wireframeMaterial: new THREE.MeshBasicMaterial({
wireframe: true
}),
solidMaterial: (function() {
var vertShader = `
attribute float highlight;
uniform bool SHADE;
varying float light;
varying float lift;
float AMBIENT = 0.1;
float XFAC = -0.05;
float ZFAC = 0.05;
void main()
{
if (SHADE) {
vec3 N = normalize( vec3( modelViewMatrix * vec4(normal, 0.0) ) );
light = (0.2 + abs(N.z) * 0.8) * (1.0-AMBIENT) + N.x*N.x * XFAC + N.y*N.y * ZFAC + AMBIENT;
} else {
light = 1.0;
}
if (highlight == 2.0) {
lift = 0.3;
} else if (highlight == 1.0) {
lift = 0.12;
} else {
lift = 0.0;
}
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
gl_Position = projectionMatrix * mvPosition;
}`
var fragShader = `
#ifdef GL_ES
precision ${isApp ? 'highp' : 'mediump'} float;
#endif
uniform bool SHADE;
uniform float BRIGHTNESS;
uniform vec3 base;
varying float light;
varying float lift;
void main(void)
{
gl_FragColor = vec4(lift + base * light * BRIGHTNESS, 1.0);
if (lift > 0.1) {
gl_FragColor.b = gl_FragColor.b * 1.16;
gl_FragColor.g = gl_FragColor.g * 1.04;
}
if (lift > 0.2) {
gl_FragColor.r = gl_FragColor.r * 0.6;
gl_FragColor.g = gl_FragColor.g * 0.7;
}
}`

monochromaticSolidMaterial: (function() {
return new THREE.ShaderMaterial({
uniforms: {
SHADE: {type: 'bool', value: settings.shading.value},
BRIGHTNESS: {type: 'bool', value: settings.brightness.value / 50},
base: {value: gizmo_colors.solid}
},
vertexShader: vertShader,
fragmentShader: fragShader,
vertexShader: SolidMaterialShaders.vertShader,
fragmentShader: SolidMaterialShaders.fragShader,
side: THREE.DoubleSide
});
})(),
Expand Down Expand Up @@ -314,6 +320,7 @@ const Canvas = {
})
})(),
emptyMaterials: [],
coloredSolidMaterials:[],
updateMarkerColorMaterials() {
var img = new Image()
img.src = 'assets/missing.png'
Expand Down Expand Up @@ -397,17 +404,32 @@ const Canvas = {

markerColors.forEach(function(color, i) {
if (Canvas.emptyMaterials[i]) return;

// Define uniforms that all marker colored shaders share
let commonUniforms = {
SHADE: {type: 'bool', value: settings.shading.value},
BRIGHTNESS: {type: 'bool', value: settings.brightness.value / 50},
base: {value: new THREE.Color().set(color.pastel)}
}

// Empty texture materials
Canvas.emptyMaterials[i] = new THREE.ShaderMaterial({
uniforms: {
map: {type: 't', value: tex},
SHADE: {type: 'bool', value: settings.shading.value},
BRIGHTNESS: {type: 'bool', value: settings.brightness.value / 50},
base: {value: new THREE.Color().set(color.pastel)}
...commonUniforms
},
vertexShader: vertShader,
fragmentShader: fragShader,
side: THREE.DoubleSide,
})

// Colored solid materials
Canvas.coloredSolidMaterials[i] = new THREE.ShaderMaterial({
uniforms: commonUniforms,
vertexShader: SolidMaterialShaders.vertShader,
fragmentShader: SolidMaterialShaders.fragShader,
side: THREE.DoubleSide
});
})
},
transparentMaterial: new THREE.MeshBasicMaterial({visible: false, name: 'invisible'}),
Expand Down Expand Up @@ -935,9 +957,12 @@ const Canvas = {
if (Canvas.layered_material) {
Canvas.layered_material.side = side;
}
if (Canvas.solidMaterial) {
Canvas.solidMaterial.side = side;
if (Canvas.monochromaticSolidMaterial) {
Canvas.monochromaticSolidMaterial.side = side;
}
Canvas.coloredSolidMaterials.forEach(function(mat) {
mat.side = side
})
Canvas.emptyMaterials.forEach(function(mat) {
mat.side = side
})
Expand Down Expand Up @@ -1219,8 +1244,11 @@ const Canvas = {
Canvas.adaptObjectFaceGeo(cube);

if (Project.view_mode === 'solid') {
mesh.material = Canvas.solidMaterial

mesh.material = Canvas.monochromaticSolidMaterial

} else if (Project.view_mode === 'colored_solid') {
mesh.material = Canvas.coloredSolidMaterials[cube.color]

} else if (Project.view_mode === 'wireframe') {
mesh.material = Canvas.wireframeMaterial

Expand Down
9 changes: 7 additions & 2 deletions js/preview/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -2070,8 +2070,12 @@ function updateShading() {
material.uniforms.SHADE.value = settings.shading.value;
material.uniforms.BRIGHTNESS.value = settings.brightness.value / 50;
})
Canvas.solidMaterial.uniforms.SHADE.value = settings.shading.value;
Canvas.solidMaterial.uniforms.BRIGHTNESS.value = settings.brightness.value / 50;
Canvas.coloredSolidMaterials.forEach(material => {
material.uniforms.SHADE.value = settings.shading.value;
material.uniforms.BRIGHTNESS.value = settings.brightness.value / 50;
})
Canvas.monochromaticSolidMaterial.uniforms.SHADE.value = settings.shading.value;
Canvas.monochromaticSolidMaterial.uniforms.BRIGHTNESS.value = settings.brightness.value / 50;
Canvas.uvHelperMaterial.uniforms.SHADE.value = settings.shading.value;
Canvas.normalHelperMaterial.uniforms.SHADE.value = settings.shading.value;
Blockbench.dispatchEvent('update_scene_shading');
Expand All @@ -2094,6 +2098,7 @@ BARS.defineActions(function() {
options: {
textured: {name: true, icon: 'image', condition: () => (!Toolbox.selected.allowed_view_modes || Toolbox.selected.allowed_view_modes.includes('textured'))},
solid: {name: true, icon: 'fas.fa-square', condition: () => (!Toolbox.selected.allowed_view_modes || Toolbox.selected.allowed_view_modes.includes('solid'))},
colored_solid: {name: true, icon: 'fas.fa-square-plus', condition: () => (!Toolbox.selected.allowed_view_modes || Toolbox.selected.allowed_view_modes.includes('colored_solid'))},
wireframe: {name: true, icon: 'far.fa-square', condition: () => (!Toolbox.selected.allowed_view_modes || Toolbox.selected.allowed_view_modes.includes('wireframe'))},
uv: {name: true, icon: 'grid_guides', condition: () => (!Toolbox.selected.allowed_view_modes || Toolbox.selected.allowed_view_modes.includes('uv'))},
normal: {name: true, icon: 'fa-square-caret-up', condition: () => ((!Toolbox.selected.allowed_view_modes || Toolbox.selected.allowed_view_modes.includes('normal')) && Mesh.all.length)},
Expand Down
1 change: 1 addition & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@
"action.view_mode.desc": "Change the model view mode",
"action.view_mode.textured": "Textured",
"action.view_mode.solid": "Solid",
"action.view_mode.colored_solid": "Solid with Marker Colors",
"action.view_mode.wireframe": "Wireframe",
"action.view_mode.uv": "UV Preview",
"action.view_mode.normal": "Face Orientation",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 475f6ff

Please sign in to comment.