Skip to content

Add default colors to primitives and lights #75

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

Merged
merged 1 commit into from
Jan 18, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Improvements:
- Improve performance of cones by ~6x (#64)
- Improve performance of arrows (#66)
- Improve performance of cylinders (#67)
- Add default colors to primitives and lights (#75)

Documentation:
- Add more examples (#59)
Expand Down
2 changes: 1 addition & 1 deletion docs/lights/ambient.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Properties:
- `color` (type: [color](/mathics-threejs-backend/types/color))
- `color` (type: [color](/mathics-threejs-backend/types/color)) — default: white

Add ambient light to the scene.

Expand Down
2 changes: 1 addition & 1 deletion docs/lights/directional.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Properties:
- `color` (type: [color](/mathics-threejs-backend/types/color))
- `color` (type: [color](/mathics-threejs-backend/types/color)) — default: white
- `coords` (type: [coord](/mathics-threejs-backend/types/coord)) — initial light position in [spherical polar coordinates](https://en.wikipedia.org/wiki/Spherical_coordinate_system)

Add an infinitely far camera-follower light to the scene.
Expand Down
2 changes: 1 addition & 1 deletion docs/lights/point.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Properties:
- `color` (type: [color](/mathics-threejs-backend/types/color))
- `color` (type: [color](/mathics-threejs-backend/types/color)) — default: white
- `coords` (type: [coord](/mathics-threejs-backend/types/coord)) — point coordinates

Add a light that gets emitted from `coords` in all directions to the scene.
Expand Down
2 changes: 1 addition & 1 deletion docs/lights/spot.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Properties:
- `angle` (type: number) — the maximum angle in radians of light dispersion (upper bound is pi/2), default: pi/2
- `color` (type: [color](/mathics-threejs-backend/types/color))
- `color` (type: [color](/mathics-threejs-backend/types/color)) — default: white
- `coords` (type: [coord](/mathics-threejs-backend/types/coord))
- `target` (type: [coord](/mathics-threejs-backend/types/coord))

Expand Down
2 changes: 1 addition & 1 deletion docs/primitives/arrow.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Properties:
- `color` (type: [color](/mathics-threejs-backend/types/color))
- `color` (type: [color](/mathics-threejs-backend/types/color)) — default: black
- `coords` (type: [coord[]](/mathics-threejs-backend/types/coord)) — array of arrow line-segment coordinates
- `opacity` (type: number) — default: `1`

Expand Down
4 changes: 2 additions & 2 deletions docs/primitives/cone.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Properties:
- `color` (type: [color/mathics-threejs-backend/types/color))
- `color` (type: [color/mathics-threejs-backend/types/color)) — default: white
- `coords` (type: [coord[]/mathics-threejs-backend/types/coord)) — array of cylinders' centers
- `edgeForm` (type: object) — default: `{}`, object with the following properties:
- `color` (type: [color/mathics-threejs-backend/types/color)) — edges' color, default: `[0, 0, 0]` (black edges)
- `color` (type: [color/mathics-threejs-backend/types/color)) — edges' color, default: black
- `showEdges` (type: bool) — default: `true`
- `radius` (type: number) — default: `1`
- `opacity` (type: number) — default: `1`
Expand Down
4 changes: 2 additions & 2 deletions docs/primitives/cuboid.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Properties:
- `color` (type: [color](documentation/types/color))
- `color` (type: [color](documentation/types/color)) — default: white
- `coords` (type: [coord[]](documentation/types/coord)) — array of the cuboids' begins and ends
- `edgeForm` (type: object) — default: `{}`, object with the following properties:
- `color` (type: [color](documentation/types/color)) — edges' color, default: `[0, 0, 0]` (black edges)
- `color` (type: [color](documentation/types/color)) — edges' color, default: black
- `showEdges` (type: bool) — default: `true`
- `opacity` (type: number) — default: `1`

Expand Down
4 changes: 2 additions & 2 deletions docs/primitives/cylinder.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Properties:
- `color` (type: [color](/mathics-threejs-backend/types/color))
- `color` (type: [color](/mathics-threejs-backend/types/color)) — default: white
- `coords` (type: [coord[]](/mathics-threejs-backend/types/coord)) — array of cylinders' centers
- `edgeForm` (type: object) — default: `{}`, object with the following properties:
- `color` (type: [color](/mathics-threejs-backend/types/color)) — edges' color, default: `[0, 0, 0]` (black edges)
- `color` (type: [color](/mathics-threejs-backend/types/color)) — edges' color, default: black
- `showEdges` (type: bool) — default: `true`
- `radius` (type: number) — default: `1`
- `opacity` (type: number) — default: `1`
Expand Down
2 changes: 1 addition & 1 deletion docs/primitives/line.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Properties:
- `color` (type: [color](/mathics-threejs-backend/types/color))
- `color` (type: [color](/mathics-threejs-backend/types/color)) — default: black
- `coords` (type: [coord[]](/mathics-threejs-backend/types/coord)) — array of line-segment coordinates
- `dashed` (type: bool) — whether the line is dashed, default: `false`
- `gapSize` (type: number) — the size of the gaps in pixels, default: `10`
Expand Down
2 changes: 1 addition & 1 deletion docs/primitives/point.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Properties:
- `color` (type: [color](/mathics-threejs-backend/types/color))
- `color` (type: [color](/mathics-threejs-backend/types/color)) — default: black
- `coords` (type: [coord[]](/mathics-threejs-backend/types/coord)) — array of each point location
- `opacity` (type: number) — default: `1`
- `pointSize` (type: number)
Expand Down
8 changes: 4 additions & 4 deletions docs/primitives/polygon.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Properties:
- `color` (type: [color](/mathics-threejs-backend/types/color))
- `coords` (type: [coord[]](/mathics-threejs-backend/types/coord)) — array of ordered points tracing the boundary of the polygon
- `color` (type: [color](/mathics-threejs-backend/types/color)) — default: white
- `coords` (type: [coord[]](/mathics-threejs-backend/types/coord)) — array of ordered points tracing the boundary of the polygon, aka. the polygon vertices
- `edgeForm` (type: object) — default: `{}`, object with the following properties:
- `color` (type: [color](/mathics-threejs-backend/types/color)) — edges' color, default: `[0, 0, 0]` (black edges)
- `color` (type: [color](/mathics-threejs-backend/types/color)) — edges' color, default: black
- `showEdges` (type: bool) — default: `true`
- `opacity` (type: number) — default: `1`
- `vertexNormals` (type: number[3][]) — this is not commonly used. This changes the way the polygon reflects the light. If this is smaller than the number of total vertexNormals of the polygon, the other vertexNormals are going to be calculated in the shader. See [vertex normal in Wikipedia](https://en.wikipedia.org/wiki/Vertex_normal). Default: `[]`

Draw a polygon with no holes in it, its vertices are `coords`, it can be non-coplanar and non-triangular.
Draw a polygon with no holes in it, it can be non-coplanar and non-triangular.

## Examples
- ```jsonc
Expand Down
2 changes: 1 addition & 1 deletion docs/primitives/sphere.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Properties:
- `color` (type: [color](/mathics-threejs-backend/types/color))
- `color` (type: [color](/mathics-threejs-backend/types/color)) — default: white
- `coords` (type: [coord[]](/mathics-threejs-backend/types/coord)) — array of sphere centers
- `opacity` (type: number) — default: `1`
- `radius` (type: number) — default: `1`
Expand Down
2 changes: 1 addition & 1 deletion docs/primitives/tube.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Properties:
- `color` (type: [color](/mathics-threejs-backend/types/color))
- `color` (type: [color](/mathics-threejs-backend/types/color)) — default: white
- `coords` (type: [coord[]](/mathics-threejs-backend/types/coord)) — draw a tube passing through these coordinates
- `opacity` (type: number) — default: `1`
- `radius` (type: number) — default: `1`
Expand Down
4 changes: 2 additions & 2 deletions docs/primitives/uniformPolyhedron.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Properties:
- `color` (type: [color](/mathics-threejs-backend/types/color))
- `color` (type: [color](/mathics-threejs-backend/types/color)) — default: white
- `coords` (type: [coord[]](/mathics-threejs-backend/types/coord)) — array of polyhedrons' center
- `edgeForm` (type: object) — default: `{}`, object with the following properties:
- `color` (type: [color](/mathics-threejs-backend/types/color)) — edges' color, default: `[0, 0, 0]` (black edges)
- `color` (type: [color](/mathics-threejs-backend/types/color)) — edges' color, default: black
- `showEdges` (type: bool) — default: `true`
- `edgeLength` (type: number) — default: `1`
- `opacity` (type: number) — default: `1`
Expand Down
8 changes: 4 additions & 4 deletions src/lights.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ export function positionLights(lights, theta, phi, focus) {
}

export default {
ambient: ({ color }) => {
ambient: ({ color = [1, 1, 1] }) => {
return new AmbientLight(new Color(...color).getHex());
},
directional: ({ color }) => {
directional: ({ color = [1, 1, 1] }) => {
return new DirectionalLight(new Color(...color).getHex());
},
spot: ({ angle = 1.57079632679, color, coords, target }, extent) => {
spot: ({ angle = 1.57079632679, color = [1, 1, 1], coords, target }, extent) => {
const light = new SpotLight(new Color(...color).getHex());

light.position.set(
Expand All @@ -75,7 +75,7 @@ export default {

return group;
},
point: ({ color, coords }, extent) => {
point: ({ color = [1, 1, 1], coords }, extent) => {
const light = new PointLight(new Color(...color).getHex());

light.position.set(
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/arrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getBasicMaterial } from '../shader.js';

// See https://reference.wolfram.com/language/ref/Arrow
// for the high-level description of what is being rendered.
export default function ({ color, coords, opacity = 1 }, extent) {
export default function ({ color = [0, 0, 0], coords, opacity = 1 }, extent) {
const material = getBasicMaterial(color, opacity);

const group = new Group();
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/cone.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { get2CoordinatesMaterial } from '../shader.js';

// See https://reference.wolfram.com/language/ref/Cone
// for the high-level description of what is being rendered.
export default function ({ color, coords, edgeForm = {}, opacity = 1, radius = 1 }, extent) {
export default function ({ color = [1, 1, 1], coords, edgeForm = {}, opacity = 1, radius = 1 }, extent) {
const [coneBases, coneTips] = get2PopulatedCoordinateBuffers(coords, extent);

const vertexPosition0 = 0.2588 * radius,
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/cuboid.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { get2PopulatedCoordinateBuffers } from '../bufferUtils.js';

// See https://reference.wolfram.com/language/ref/Cuboid
// for the high-level description of what is being rendered.
export default function ({ color, coords, edgeForm = {}, opacity = 1 }, extent) {
export default function ({ color = [1, 1, 1], coords, edgeForm = {}, opacity = 1 }, extent) {
// The edges of the cuboids are drawn in the fragment shader; doing this is faster than putting the edges in a different object.

const [cuboidsBegin, cuboidsEnd] = get2PopulatedCoordinateBuffers(coords, extent);
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/cylinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { get2CoordinatesMaterial } from '../shader.js';

// See https://reference.wolfram.com/language/ref/Cylinder
// for the high-level description of what is being rendered.
export default function ({ color, coords, edgeForm = {}, opacity = 1, radius = 1 }, extent) {
export default function ({ color = [1, 1, 1], coords, edgeForm = {}, opacity = 1, radius = 1 }, extent) {
const [cylindersBegin, cylindersEnd] = get2PopulatedCoordinateBuffers(coords, extent);

const vertexPosition0 = 0.2588 * radius,
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getBasicMaterial } from '../shader.js';
// for the high-level description of what is being rendered.
// Differently from WL's Line, our lines aren't affected by
// lightning and therefore don't have VertexNormals.
export default function ({ color, coords, dashed = false, gapSize = 10, opacity = 1 }, extent, canvasSize) {
export default function ({ color = [0, 0, 0], coords, dashed = false, gapSize = 10, opacity = 1 }, extent, canvasSize) {
return new Line(
new BufferGeometry().setAttribute(
'position',
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getPopulatedCoordinateBuffer } from '../bufferUtils.js';
// for the high-level description of what is being rendered.
// Differently from WL's Point, our points aren't affected by
// lightning and therefore don't have VertexNormals.
export default function ({ color, coords, opacity = 1, pointSize }, extent, canvasSize) {
export default function ({ color = [0, 0, 0], coords, opacity = 1, pointSize }, extent, canvasSize) {
return new Points(
new BufferGeometry().setAttribute(
'position',
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function getCoplanarityAndNormal(coordinates, extent) {

// See https://reference.wolfram.com/language/ref/Polygon
// for the high-level description of what is being rendered.
export default function ({ color, coords, edgeForm = {}, opacity = 1, vertexNormals = {} }, extent) {
export default function ({ color = [1, 1, 1], coords, edgeForm = {}, opacity = 1, vertexNormals = {} }, extent) {
let geometry;

if (coords.length === 3) { // triangle
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/sphere.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getPopulatedCoordinateBuffer } from '../bufferUtils.js';

// See https://reference.wolfram.com/language/ref/Sphere
// for the high-level description of what is being rendered.
export default function ({ color, coords, opacity = 1, radius = 1 }, extent) {
export default function ({ color = [1, 1, 1], coords, opacity = 1, radius = 1 }, extent) {
const sphereGeometry = new InstancedBufferGeometry().copy(
new SphereGeometry(radius, 48, 48)
);
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/tube.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function mergeBufferGeometries(geometries) {

// See https://reference.wolfram.com/language/ref/Tube.html
// for the high-level description of what is being rendered.
export default function ({ color, coords, opacity = 1, radius = 1 }, extent) {
export default function ({ color = [1, 1, 1], coords, opacity = 1, radius = 1 }, extent) {
// TubeGeometry receives a Curve, but Mathics' Tube recives an array of coordinates, so we use CatmullRomCurve3 to convert the coordinates into a Curve.
// Curve.getPoint receives a flot between 0 and 1, where 0 is the 1st coordinate and 1 is the last.
const curve = new CatmullRomCurve3(
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/uniformPolyhedron.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getPopulatedCoordinateBuffer } from '../bufferUtils.js';

// See https://reference.wolfram.com/language/ref/UniformPolyhedron
// for the high-level description of what is being rendered.
export default function ({ color, coords, edgeForm = {}, edgeLength = 1, opacity = 1, subType }, extent) {
export default function ({ color = [1, 1, 1], coords, edgeForm = {}, edgeLength = 1, opacity = 1, subType }, extent) {
const polyhedronGeometry = new InstancedBufferGeometry();

// The magic numbers below are modified from the position attribute of,
Expand Down