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

Base color for 3D cube #7354

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Base color for 3D cube #7354

wants to merge 4 commits into from

Conversation

NeylMahfouf2608
Copy link
Collaborator

Added support for color in 3DCubeRuntimeObject and renderer,
added prototype of a property to control it;

The color renders only in preview mode, not in editor yet

@NeylMahfouf2608 NeylMahfouf2608 requested a review from 4ian as a code owner January 28, 2025 16:17
];
const materials: THREE.Material[] = [];

for (let i = 0; i < 6; i++) {
Copy link
Collaborator

@AlexandreSi AlexandreSi Jan 28, 2025

Choose a reason for hiding this comment

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

Good idea to use a loop!
In JS, there's a more elegant way to do this, with a map.
So you could write:

const materials = new Array(6).fill(0).map((_, index) => {
  return material;
})

Comment on lines +90 to +96
const basicMaterial: THREE.MeshBasicMaterial = new THREE.MeshBasicMaterial();
basicMaterial.copy(material);
materials.push(
basicMaterial.map
? getFaceMaterial(runtimeObject, materialIndexToFaceIndex[i])
: new THREE.MeshBasicMaterial({ vertexColors: true })
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems unnecessary to me.
I feel like you could just do:

const material = ...
materials.push(material)

since getFaceMaterial already returns a MeshBasicMaterial if no resource

Comment on lines 99 to 111
let colors: number[] = [];
for (let i = 0; i < geometry.attributes.position.count; i++) {
colors.push(
runtimeObject._color.r,
runtimeObject._color.g,
runtimeObject._color.b
);
}

geometry.setAttribute(
'color',
new THREE.BufferAttribute(new Float32Array(colors), 3)
);
Copy link
Collaborator

@AlexandreSi AlexandreSi Jan 28, 2025

Choose a reason for hiding this comment

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

I see MeshBasicMaterial as a color property.
It doesn't work with this property only? So that is wouldn't impact the geometry attribute?

@@ -902,6 +903,12 @@ module.exports = {
.setLabel(_('Depth'))
.setMeasurementUnit(gd.MeasurementUnit.getPixel())
.setGroup(_('Default size'));
objectProperties
.getOrCreate('cubeColor')
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you can name it simply color, we can deduce from the context that it's for the cube

@@ -902,6 +903,12 @@ module.exports = {
.setLabel(_('Depth'))
.setMeasurementUnit(gd.MeasurementUnit.getPixel())
.setGroup(_('Default size'));
objectProperties
.getOrCreate('cubeColor')
.setValue(objectContent.color || (255, 255, 255))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Check for similar attributes (such as fillColor in the text input), how it's done

this._faceResourceNames[faceIndex] = resourceName;
this._renderer.updateFace(faceIndex);
}
setCubeColor(color: string): void {
if (rgbOrHexToRGBColor(color) === this._color) return;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Welcome to JS! Can you enter [1, 2, 3] === [1, 2, 3] in your browser JS console and see the result?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Or you could use rgbOrHexStringToNumber instead. And when you need the 3 components, you can use hexNumberToRGBArray

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that rgbOrHexStringToNumber is better to use: this way, we only store one number, and not an array, which is more efficient memory-wise

this._faceResourceNames[faceIndex] = resourceName;
this._renderer.updateFace(faceIndex);
}
setCubeColor(color: string): void {
if (rgbOrHexToRGBColor(color) === this._color) return;
this._color = rgbOrHexToRGBColor(color);
Copy link
Collaborator

Choose a reason for hiding this comment

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

You do the operation twice, you could store the result in a variable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants