This commit is contained in:
JannisX11 2024-06-01 12:01:17 +02:00
commit 70483fa933
7 changed files with 126 additions and 83 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,
@ -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
});
})(),
@ -314,6 +320,7 @@ const Canvas = {
})
})(),
emptyMaterials: [],
coloredSolidMaterials:[],
updateMarkerColorMaterials() {
var img = new Image()
img.src = 'assets/missing.png'
@ -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'}),
@ -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
})
@ -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

View File

@ -2074,8 +2074,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');
@ -2098,6 +2102,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)},

View File

@ -1621,6 +1621,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",

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "Blockbench",
"version": "4.10.0-beta.1",
"version": "4.10.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "Blockbench",
"version": "4.10.0-beta.1",
"version": "4.10.0",
"license": "GPL-3.0-or-later",
"dependencies": {
"@electron/remote": "^2.1.2",