A Scala based ray tracer. 95% pure functional.
A project based on Peter Shirley's raytracing series.
For sample renders from various steps along development, see the ./renders/
directory.
You can launch this through a simple command line call like sbt "run polyCollectionMaterials fastbig"
.
More specifically, the run
command takes two parameters, the scene name (polyCollectionMaterials
in the example, see below)
and the render settings (fastbig
in the example above).
The render settings are based in the /render_settings
folder, and three default settings are provided:
fast
- Renders a small 100x50 render, with very little extra details appliedfastbig
- The same asfast
but with 10x the resolutionprint
- Much slower, but includes enough rays and bounces that this should look pretty realilistc even for complex scenes.
For more details, see Render Settings
below
Scene files define the layout of everything in the render job and are normally stored in the /scenes
directory.
A scene generally describes three things: the camera, the objects, and the background
The camera is configured under the path camera
, and a whole config would look something like:
{
"camera": {
"fov": 5,
"aspect": 2,
"origin": {
"x": -320,
"y": 30.0,
"z": -320
},
"up": {
"x": 0,
"y": 1,
"z": 0
},
"target": {
"x": 0,
"y": 15.0,
"z": 0
},
"aperature": 0.01,
"focus_distance": 452.789
}
...
}
Fields:
fov
- the field of viewaspect
- the expected aspect ratio (width / height)origin
- the coordinates where the camera is placedup
- A unit vector describing which way is "up"target
- The camera is pointed at this locationaperature
- The camera's aperature, larger numbers will increase blur for out of focus elementsfocus_distance
- If an object is this distance away fromorigin
, it will be perfectly in focus
Objects are described as a list under the objects
path. Raytrace4s supports several types of objects:
- Spheres
- Triangles (polygons)
- Platonic Solids
- Imported objects
Additionally, objects may use a common material
definition (see below)
In general, an object will have the following common fields, which we'll ignore in the individual object's documentation for space:
type
- The type of the objectcenter
- The coordinates for the center of the objectrotation
(Optional) - The rotation, defined byyaw
,pitch
androll
(radians)material
- The material of the sphere, see below
Internal Name: sphere
Sample Config:
{
"type": "sphere",
"center": {
"x": 4,
"y": 0.5,
"z": 0
},
"rotation":{
"yaw":1,
"pitch":0,
"roll":0
},
"scale": 1,
"material": {
...
}
}
Parameters:
scale
- The radius
Internal Name: triangle
Sample Config:
{
"type": "triangle",
"center": {
"x": 0,
"y": 0.0,
"z": 0
},
"rotation":{
"yaw":0,
"pitch":0,
"roll":0
},
"v3": {
"x": 0.7,
"y": 0.0,
"z": 0.0
},
"v2": {
"x": 0.0,
"y": 1,
"z": 0.0
},
"v1": {
"x": -0.7,
"y": 0.0,
"z": 0.0
},
"material": {
...
}
}
Parameters:
v1
- A point on the polygonv2
- A point on the polygonv3
- A point on the polygon
Notes: We do not care about culling polygons based on the ordering of the three points
Internal Name: platonic
Sample Config:
{
"type": "platonic",
"subType": "pyramid",
"center": {
"x": 6,
"y": 0.0,
"z": 0
},
"rotation":{
"yaw":0.5,
"pitch":0.5,
"roll":0.5
},
"material": {
...
}
}
Parameters:
subType
- The specific type of platonic solid
Internal Name: imported
Sample Config:
{
"type": "imported",
"subType": "stl_bin",
"fileName": "shapefiles/teapot.stl",
"center": {
"x": 0,
"y": 0.0,
"z": 0
},
"rotation": {
"yaw": 0.0,
"pitch": 0.00,
"roll": 0.0
},
"material": {
...
}
}
Parameters:
subType
- The type of the file being imported, currently we just supportstl_bin
files (binary STL format).fileName
- The name of the file to read
A Material
describes what an object looks like, for example: Is it red? Is it
reflective? Is it transparent? Does it emit light?
We currently support the following materials:
- Color Material
- Light Emitting Material
- Texture Material
- Custom Material
A simple material based on r
, g
, and b
parameters (range 0 to 1)
Generally used for point lights, if needed. Configured the same as a color material, but without a limit on the upper bound of the color parameters.
A fully customizable material
Parameters:
texture
- The texture, see belowdiffuseAmount
- The "fuzziness" of any light that bounces off of this objectlightDampening
- How much light is absorbed by this object, instead of bounced back outreflectionAmount
- How reflective the surface is (0 -> no reflection, 1 -> complete reflection)reflectionFuzziness
- How "fuzzy" the reflection should betransparency
- How transparent the object is (0 -> completely opaque, 1 -> completely transparent)refractionIndex
- The index of refraction, which describes how much the light is bent when going through some transparent object. If 1 or below, it'll pass straight through. See wikipedia for more examples.
A texture
is something painted on top of an object, like an image. We support:
- Color textures
- Checkered Box textures
- Perlin Noise based textures
- Image textures
Like the Color Materials
, a simple config based on r
, g
, and b
parameters (range 0 to 1).
Divides the world into a checkered box of cubes with a size of scale
. This
defines alternating between textureOne
and textureTwo
(recursivly, two
other textures.)
Like a Checkered Box
texture, but the merging of the two textures is based
on the traditional Perlin noise algorithm.
An image is loaded from the image
field and applied to the object.
The sky
is a special object that is infinitly away and emits it's own light.
We support three sky objects:
dark
(default) - A black background with no detailslight
- A simple blue skytexture
- A sky based on a defined texture (see above)
Copyright 2016-2018, 2022 Todd Bodnar
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
See images/licenses.md for more info