Documentation/HowTo/Draw/Make a 3D Star

From The Document Foundation Wiki
Jump to navigation Jump to search

This article describes how to create a three-dimensional star. And along the way you will learn a lot about 3D objects beyond the manuals. Because the UI of LibreOffice is not powerful enough, some steps are done on the file source. So you should know how to manually change it. Find a short description in ODF_Markup

Step 1: Generate a 3D Scene

Start LibreOffice Draw and insert a Cube from section 3D Objects. In fact you have not only inserted a Cube, but you have inserted a 3D scene, which contains a Cube. If it is not already marked, click on it. Now look at the status bar. You should see “3D scene selected”. A 3D Scene is similar to a group. You use the same commands to enter a 3D Scene (F3) or exit it (Ctrl+F3) as you use for a group.

A 3D Scene and its objects have their own dialog 3D Effects for their properties. Its settings are not contained in the Sidebar. You get the dialog in the context menu of a 3D Scene or in menu Format. This dialog can be freely moved and can be docked left or right. The dialog keeps open, if the object is no longer marked.

Unfortunately, the dialog docks very easily. So in case it seems that it has suddenly disappeared, you should look for a small additional bar left or right. Drag the bar a little bit, then you can set the mouse pointer in a free area and drag the dialog to detach it.

We will use the cube to have a frame into which the star will be placed. Why is that useful? The size, which you see in the status bar or in the Position and Size dialog, is of course not the size of the three-dimensional scene. But it is the size of the two-dimensional projection of the 3D Scene. Whenever something is changed in the geometry of the scene, for example rotating an object in the scene, LibreOffice recalculates the size of the two-dimensional projection so that the scene is rendered without distortion.

It is difficult to manually determine the size of the projection for a rotated scene. And that makes it difficult to manually add objects or rotate them directly in the file text because you would have to know the size of the resulting projection. We use the cube as fixed outer frame while working on the star and so keep the size of the resulting projection constant. We delete the cube after the star is finished.

To serve as a frame, set the line style to continuous and set the colour to none.

DefaultCubeInWireframeStyle.png


The next steps are in preparation to be able to determine the size of the resulting projection from the size of the cube.

3D Effects dialog page Geometry.png

Mark the scene and open dialog 3D Effects and go to tab Geometry. Click on Perspective on/off (bottom right) to switch to "parallel", then click on Assign (top right).

CubeInParallelProjection.png

Save the odg-file and close it.


Now we set the cube to a fixed size. For that we edit the file content.xml in the package. Open the odg-archive in 7-Zip (or other suitable packer) and open file content.xml in an editor. Find element <dr3d:cube>. Add attributes dr3d:min-edge="(-5000 -5000 -5000)" and dr3d:max-edge="(5000 5000 5000)".

<dr3d:cube draw:style-name="gr2" draw:layer="layout" dr3d:max-edge="(5000 5000 5000)" dr3d:min-edge="(-5000 -5000 -5000)" />
3DCoordinateSystemIllustration.png

The coordinate system of a 3D Scene is a right-handed, Cartesian coordinate system. The x‑axis points right, the y‑axis points up and the z‑axis points towards the user.

Position and size values inside a 3D Scene are always in 1/100 mm. So the cube has width, height and depth of 10cm and is centered at the origin of the coordinate system.


But the scene is rotated by default, therefore the size of the resulting projection would not be 10cm×10cm. We set the scene upright. Find the element <dr3d:scene>. Remove its attribute dr3d:transform.

<dr3d:scene draw:style-name="gr1" svg:width="7.461cm" svg:height="7.461cm" svg:x="3.81cm" svg:y="4.34cm" dr3d:transform="matrix (1 0 0 0 0.939692620785931 0.342020143325607 0 -0.342020143325607 0.939692620785931 0cm 0cm 0cm)" dr3d:vrp="(0 0 16885.7142857143)" dr3d:vpn="(0 0 14285.7142857143)" dr3d:projection="parallel" dr3d:distance="2.6cm" dr3d:focal-length="10cm" dr3d:shadow-slant="0" dr3d:shade-mode="gouraud" dr3d:ambient-color="#666666" dr3d:lighting-mode="false">

Now we are sure, that the resulting projection has size 10cm×10cm. We write this value into the file. Change the width and height attributes of the <dr3d:scene> element to svg:width="10cm" and svg:height="10cm".

<dr3d:scene draw:style-name="gr1" svg:width="7.46110cm" svg:height="7.46110cm" svg:x="3.81cm" svg:y="4.34cm" dr3d:vrp="(0 0 16885.7142857143)" dr3d:vpn="(0 0 14285.7142857143)" dr3d:projection="parallel" dr3d:distance="2.6cm" dr3d:focal-length="10cm" dr3d:shadow-slant="0" dr3d:shade-mode="gouraud" dr3d:ambient-color="#666666" dr3d:lighting-mode="false">

Save the content.xml file, close your editor and update the zip-container.

CubeWithoutRotation.png

You might want to open the file in LibreOffice. You should see an empty square of 10cm width.

Always before you start with a next step, you should make a backup of the previous step.

Step 2: Create a Pair of Star Tops

There exists four kind of 3D objects: cube, sphere, rotation object and extrusion object. In addition it is possible to group objects by putting them into a 3D Scene. Such 3D Scene can be contained in another 3D Scene. The markup of these 3D objects is specified in part 1 “Open Document Schema” of the ODF v1.2 standard. [1]

PathOnXYPlaneIllustration.png

We use cones as star tops. Instead of using the predefined cone, we write our own cone, in fact a double-cone. For that purpose we use a rotation object. Such is created by a polyline on the xy‑plane. The polyline is then rotated around the y-axis.

We draw a polyline from negative y‑values towards positive y‑values on the right side of the xy‑plane. That way we will later get a correct illumination. For details see section Inside and Outside of a Plane.

Oben file content.xml in an editor (see step 1) and then insert the following element directly after the <dr3d:cube .../> element.

<dr3d:rotate
  draw:style-name="gr2"
  draw:layer="layout"
  svg:viewBox="-5000 -5000 10000 10000"
  svg:d="M0 -4800 l500 4800 -500 4800" />

Use the same style-name as for the cube for the present, here I have assumed it is gr2. It will be changed in the next step.

The values in the svg:viewBox attribute mean from left to right: “minimum x-value”, “minimum y-value”, “width” and “height”. Here the values of our 'frame' cube are used. Because all values are in 1/100 mm and the viewBox is not bound to an outer rectangle, its values are not really relevant. It exists, because it is formally needed if a svg:d attribute is present. When the file is later saved by LibreOffice, it will use the actual coordinates of the polyline.

The generating line for a rotation object is determined by the svg:d attribute. The ODF specification doesn't have its own definition of the commands, but uses the definitions of a path of SVG 1.1 §8.2.[2] Such path description is a sequence of draw actions. An action consists in most cases of a single command character to determine the kind of draw action and a pair of coordinates.

M0 -4800 Move to. The pen is set on point (0|-4800). That is a point on the lower part of the y‑axis.
l500 4800 Line to. A lower case character means, that the numbers are interpreted relative to the current pen position. Here it means to draw a line from (0|-4800) to (0+500|-4800+4800), that is a line to (500|0). Now our current point of the pen is on the right part of the x‑axis.
A upper case character means, that absolute coordinates are given. The same segment could have been achieved with command L500 0. Relative coordinates have the advantage, that you can move the entire object by only changing the first Move command.
-500 4800 If no command character is given, the last command character is used. So here the “relative line to” command is used again. It draws a line from (500|0) to (500-500|0+4800), that is a line to (0|4800). We have got a polyline with two segments.

Save the content.xml file, close editor and update the zip-container.

Step 3: Style the Star Tops

RawDoubleCone.png

Open the odg-file in LibreOffice.

First we need to select the double-cone. Mark the scene and enter it. Remember, that is same as entering a group, e.g. press F3 key. Now click on the double-cone. Look at the status bar. You will likely see “3D cube selected”. The double-cone is inside the cube. The cube has no filling, but it is still there. To get the double-cone you need to press Alt-key while clicking. Alt+Click selects an object, that is behind another one. Verify with the status bar, that you have indeed selected the double-cone. The status bar should show “Rotation object selected”.

Setting fill and line style works the same as for two-dimensional shapes. Set the color of the cone to “Yellow” (or any other color you like). Set the line style to none.

StyledDoubleCone.png

Keep the double-cone selected. Open the 3D Effects dialog and go to tab Geometry. Set Rounded edges to 0%. Set Vertical to 2 in section Segments, in case there is a higher number. Our generating line has only two segments. Introducing intermediate parts has no advantage but costs time when rendering. Assign the changes.

Open Styles pane of the Sidebar and generate a new style from the selected double-cone, name it “StarTop” for example.

Now go to tab Illumination and drag the light to the position you want. Assign the change. If you are not sure, whether it will look good, don't worry. You can use the dialog again, when the star is finished.

Leave the scene with Ctrl+F3 or with command Exit Group in context menu or menu Shape > Group and save the document.

Step 4: Generate all Axis Star Tops

StarTopsOnAxes.png

To create the other star tops we copy the existing one and rotate the copies to the other directions.

Open the odg-archive, then open the file content.xml in an editor. Find the element <dr3d:rotate .../>. You will notice, that attribute svg:viewBox is changed, as mentioned above, and attribute draw:style-name has the style name you have used in Step 3.

Copy the element <dr3d:rotate .../> and paste it after the existing one. Add the attribute dr3d:transform="rotatex(90)". This rotates the object by 90deg around the x‑axis. That makes the tops of the double-cone align with the z‑axis. Because we have made a double-cone, it does not matter, whether the rotation is clockwise or counter-clockwise.

Copy and paste again to create the star tops for the x‑axis, this time with attribute dr3d:transform="rotatez(90)".

Now we have star tops at the three axes. You can save the changes and examine the odg-file in LibreOffice. But do not save it in LibreOffice yet! When LibreOffice saves the file, it converts all transformations into matrices, and rotation matrices are really ugly for manual changes.

Step 5: Generate Diagonal Star Tops

Now we create the diagonal star tops. You can use the same basis double-cone, or you make a new, shorter one, for example

<dr3d:rotate
  draw:style-name="StarTop"
  draw:layer="layout"
  svg:viewBox="-5000 -5000 10000 10000"
  svg:d="M0 -3000 l500 3000 -500 3000"
/>

Notice, that the previously defined style “StarTop” is used and that the path starts at y‑coordinate −3000.

To get the diagonal direction for the octant between positive x‑, y‑ and z‑axis, the double-cone has to be rotated 45deg around x‑axis and then 45deg around the y‑axis. That is again written as dr3d:transform attribute. The transformations are applied from left to right. So you get the following markup for the star top:

<dr3d:rotate
  draw:style-name="StarTop"
  draw:layer="layout"
  dr3d:transform="rotatex(45) rotatey(45)"
  svg:viewBox="-5000 -5000 10000 10000"
  svg:d="M0 -3000 l500 3000 -500 3000" />

In total you need this element four times. They differ only in the transformations:

  dr3d:transform="rotatex(45) rotatey(45)"
  dr3d:transform="rotatex(45) rotatey(-45)"
  dr3d:transform="rotatex(-45) rotatey(45)"
  dr3d:transform="rotatex(-45) rotatey(-45)"
AllStarTops.png

Now the manual writing part is finished. Save the changes to the content.xml file, close editor and update the zip-container.

Step 6: Finishing the Star

StarInRotationMode.png

Open the odg-file in LibreOffice. We now delete the cube, it is no longer needed. Mark the scene, then enter it and mark the cube. Delete it. Do not delete the cube in the editor. You would not know which size to use for the scene.

Leave the scene, but keep it selected. Bring the scene into rotation mode. You have an icon in the Standard toolbar for that. A single click on the selected scene should work as well. First rotate the scene a little bit right or left. Then tilt the scene forward. That makes the star tops more appealing.

StarFinished.png

You can now use the 3D Effects dialog again. You might want to change the shading mode to Phong, for example. That reacts better on the Specular Intensity settings on tab Material. Or you enable a second light source or change the direction of the light.

You can also use a different fill color for the star or make a multi-color star by applying the “Color Stripes”-bitmap. As you have defined a style, you can change it on the Area tab in the style without selecting the 3D-scene. Or you select the 3D Scene and use direct formatting for changing the color. If you do not enter a scene, a style change is applied to all objects in the scene.

Using the 3D Star

Keep the Draw document, and when ever you want to use the star, you can simple copy&paste it to any other document. Or you can drag the star into a Gallery theme. Click on the star and hold mouse down until the mouse pointer changes. Then drag it to the Gallery. When you want to use the star from the Gallery, do not use the item Insert. That would insert a metafile image of the star. But use the Copy item or drag the star to the working area.

If the color of the star is determined by a style, this style is not copied to the Gallery. You need to apply styles again after you have pasted the star.

The archive Star3DSteps contains the result of the steps, so that you can easily experiment with it yourself or simple use the final star. Another version of the star is in File:Star3D.odp.

Naming 3D Objects

Selecting objects inside a scene becomes much more comfortable, if the objects are named. You need to name the scene itself and each object. Mark the object and use menu Format > Name. If scene and objects are named, you will see them in the Navigator and a double-click there will select the object. A scene works in this regard same as a group.

Unfortunately the draw:name attribute is not yet allowed in ODF. So such documents will be invalid in regard to the ODF standard. LibreOffice on the other hand has no problem in using this attribute. The draw:name attribute will be likely in ODF 1.4. But it will certainly take three to five years before the next version of the standard is published.

Inside and Outside of a Plane

RightHandRuleIllustration.png

If you rotate a polyline to make a 3D-object, you generate a plane, which has to sides. One side is the “inside” surface, the other one the “outside” surface. Which one is “inside” and which “outside” depends on the direction of the polyline. For rotation objects use this rule: Hold your right hand so that the thumb points towards the end of the polyline. Your other fingers have to point in rotation direction. Then the outside of you hand indicates the “outside” surface of the rotation object.

If the polyline is orientated as in the illustration, then the technical “inside” surface is really inside the double-cone in common sense of “inside”.

In default settings, the “inside” surface of an object is not drawn at all. That is similar to a feature called “backface culling”.

WrongDirectionNotDrawn.png

Here the polyline had a “wrong” direction. The outside of the cone in common sense is technical the “inside”. You see only the lines at the edge of the segments. The segments themselves are invisible, if you look at them from outside.

WrongDirectionDoubleSided.png
3D Effects Double-Sided.png

You can force LibreOffice to draw “inside” surfaces too. That is setting Double-Sided on tab Geometry in the 3D Effects-dialog. The result is shown left. The face is drawn, but an “inside” surface does not react on light sources, only on ambient light.

CorrectDirection.png

If the polyline was drawn in the opposite direction, then the outside of the cone in common sense is “outside” surface in the technical sense too. And now the segments reflect the light source.

References

  1. https://www.oasis-open.org/standards — Search for "Open Document Format for Office Applications (OpenDocument) Version 1.2" on that page. Then open the tree. You will find several ways for download or you can use the HTML-version to read it in your browser. The version 1.3 is nearly finished, but not yet released. It will be available with the same link. The draft of version 1.3 is available at http://docs.oasis-open.org/office/OpenDocument/v1.3/OpenDocument-v1.3-part3-schema.html
  2. https://www.w3.org/TR/SVG11/paths.html