CSG Union

### 3.3.2 CSG Union

Let's try making a simple union. Create a file called `csgdemo.pov` and edit it as follows:

      #include "colors.inc"
      camera {
        location <0, 1, -10>
        look_at 0
        angle 36
      }
      light_source { <500, 500, -1000> White }
      plane { y, -1.5
        pigment { checker Green White }
      }

Let's add two spheres each translated 0.5 units along the x-axis in each direction. We color one blue and the other red.

      sphere { <0, 0, 0>, 1
        pigment { Blue }
        translate -0.5*x
      }
      sphere { <0, 0, 0>, 1
        pigment { Red }
        translate 0.5*x
      }

We trace this file and note the results. Now we place a union block around the two spheres. This will create a single CSG union out of the two objects.

      union{
        sphere { <0, 0, 0>, 1
          pigment { Blue }
          translate -0.5*x
        }
        sphere { <0, 0, 0>, 1
          pigment { Red }
          translate 0.5*x
        }
      }

We trace the file again. The union will appear no different from what each sphere looked like on its own, but now we can give the entire union a single texture and transform it as a whole. Let's do that now.

      union{
        sphere { <0, 0, 0>, 1
          translate -0.5*x
        }
        sphere { <0, 0, 0>, 1
          translate 0.5*x
        }
        pigment { Red }
        scale <1, .25, 1>
        rotate <30, 0, 45>
      }

We trace the file again. As we can see, the object has changed dramatically. We experiment with different values of scale and rotate and try some different textures.

There are many advantages of assigning only one texture to a CSG object instead of assigning the texture to each individual component. First, it is much easier to use one texture if our CSG object has a lot of components because changing the objects appearance involves changing only one single texture. Second, the file parses faster because the texture has to be parsed only once. This may be a great factor when doing large scenes or animations. Third, using only one texture saves memory because the texture is only stored once and referenced by all components of the CSG object. Assigning the texture to all n components means that it is stored n times.