ChromaDepth(tm) 3-D Cube
Return to Home Page

The ChromaDepthTM 3-D Coloring
Algorithm for the Red-Green-Blue on Black
Color Space Model

The following code was originally written for use with Polyray 1.7 as a procedural texture. It assigns color values to the surface of an object based on the distance of that surface point to the camera position. The code is very similar to C, and should be easily adapted to other programs. You may adapt this code to other graphics programs and provided you comply with the following:

1) You send us a working copy of the adapted code, along with any information pertinent to the use of the routine, including the name of the graphics software using the routine, its version number, and any special instructions for using the routine.

2) You do not sell the routine or include it in a package that is sold without obtaining permission from Chromatek Inc. to do so.


//The following pages contain the 3DCOLOR algorithm for Polyray.

//The Scene Definitions which follow the 3DCOLOR algorithm show how it is applied in
//a Polyray file.

//***************************************************************************
// 3-D COLOR ASSIGNMENT DEFINITIONS
include "colors.inc"
include "texture.inc"

start_frame 20
end_frame 59 /*Use 0 to 39 for full cycle*/
outfile "brd5"

//define Camloc
// rotate (<10,(30+frame/3),(-25+(39*frame/25))>, <0,frame*9,0>)

define Camloc <100-frame,15,20> //<80-5*2,15,20>
define Lookat <0, 5,36> /*Lookat point of camera*/

define Near 12 //(3+frame/2)
define Far |(Camloc-Lookat)*1.5| //(7+frame*65/19)
define Depth (Far - Near)

viewpoint {
from Camloc
at <0.0, 0.0, 0.0 >
up <0.0, 1.0, 0.0 >
angle 45
hither 1.0e-3
resolution 133,100
aspect 1.33
// resolution 320,200
// aspect 320/200
yon 1.0e5
max_trace_depth 5
// aperture 0.25
}

//Range is the normalized distance from the camera to the surface point.
//The following code sets the Range to 0 if it is less than 0, to 1 if it
//is more than 1, and to the Range formula if it is in-between. The value
//of Range shoud therefore vary only between 0 and 1, over the normalized
//Depth range. The variable Dist is the distance from the camera location
//<Camloc> and the point on the surface being observed at the moment <W>.
define Dist (|Camloc - W|)
define Aa (Dist<Near ? 0:1)
define Bb (Dist>Far ? 1:0)
define Range (Dist>=Near && Dist<=Far ? ((Dist - Near)/Depth):Aa*Bb)


//Definition of 3d_red component of the color. The value show be between 1
//and 0 over the Range of 0 to 0.75. It should be 0 for all Ranges greater
//than 0.75. From 0 to 0.75 it is calculated by Red_func.
define Red_Range Range/0.9
define Red_func
(-2.13*Red_Range^4-1.07*Red_Range^3+0.133*Red_Range^2+0.0667*Red_Range+1)
define Cc (Red_func <0 || Red_Range>0.75 ? 0:1)
define Dd (Red_func >1 ? 1:0)
define 3d_red (Red_Range<0.75 ? Red_func:Cc*Dd)

//Definition of 3d_green component of the color. The value should be between
//0 and 1 over the Range of 0 to 1, starting from 0, rising to 1, then falling
//to 0 again. It should be 0 at both extremes of Range.
define Green_func1 (1.6*Range^2+1.2*Range)
define Green_func2 (3.2*Range^2-6.8*Range+3.6)
define 3d_green (Range<=0.5 ? Green_func1:Green_func2)

//Definition of 3d_blue component of the color. The value should rise from
//0 at a Range of 0.5 up to 1 at a Range of 1. Below Range 0.5 the value
//must be 0.
define Blue_func (-4.8*Range^2+9.2*Range-3.4)
define 3d_blue (Range>0.5 ? Blue_func:0)

// Specular reflection colors: these need to be slightly closer than the
// surface they glint from. On a red surface the glint must be more red,
// on a blue surface it must be less blue in order to appear closer. This
// is handled by moving the glint color 5 percent closer than the intrinsic
// color; otherwise the color formulas are unchanged.

//Definition of 3d_red_glint
define Grange 0.95*Range /* Grange means glint Range*/
define RGrange Grange/0.9
define Red_glint_func
(-2.13*RGrange^4-1.07*RGrange^3+0.133*RGrange^2+0.0667*RGrange+1)
define Ee (Red_glint_func <0 || RGrange>0.75 ? 0:1)
define Ff (Red_glint_func >1 ? 1:0)
define 3d_red_glint (RGrange<0.75 ? Red_glint_func:Ee*Ff)

//Definition of 3d_green_glint
define Green_glint_func1 (1.6*Grange^2+1.2*Grange)
define Green_glint_func2 (3.2*Grange^2-6.8*Grange+3.6)
define 3d_green_glint (Grange<=0.5 ? Green_glint_func1:Green_glint_func2)

//Defintion of 3d_blue_glint
define Blue_glint_func (-4.8*Grange^2+9.2*Grange-3.4)
define 3d_blue_glint (Grange>0.5 ? Blue_glint_func:0)

//3d_color and 3d_glint can now be defined
define 3d_color <3d_red, 3d_green, 3d_blue> /*<3d_red, 3d_green, 3d_blue>*/
define 3d_glint <3d_red_glint, 3d_green_glint, 3d_blue_glint>

// 3d_color and 3d_glint can now be included in various textures:

define matte_3d
texture {
special surface {
color 3d_color
ambient 0.1
diffuse 0.7
specular 0.0
}
}

define shiny_3d
texture {
special surface {
ambient 0.1
diffuse 0.6
color 3d_color
specular 3d_glint, 0.6
microfacet Phong 3
}
}

define flat_black
texture {
special surface {
color <0,0,0>
ambient 0.0
diffuse 0.0
specular 0.0
}
}
define matte_dark_blue
texture { matte { color <0,0,0.5>}
}
// END OF THE 3DCOLOR ALGORITHM

//****************************************************************************
// SCENE INFORMATION DEFINITIONS

//PROGRAM: CHIP1.PI GENERATED WITH POVCAD 2.0b (c) Alfonso Hermida 1993
//Created on 1/21/95 11:36:14 AM


//background MidnightBlue

// PLANE (Polygon)
//object {
// normal in Y direction
// polygon 4, <-1,0,1>, <-1,0,-1>, <1,0,-1>, <1,0,1>
// scale < 100.0,100.0,100.0>
// matte_grey
// }

define Chip1
object {
object {
// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 8.0,6.0,1.0>
rotate < 90.0,0.0,0.0>
translate < 0.0,1.5,0.0>
matte_3d

}
+
// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 0.5,2.0,0.1>
translate < 0.0,0.0,6.05>
shiny_3d

}
+
// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 0.5,2.0,0.1>
translate < 3.0,0.0,6.05>
shiny_3d

}
+
// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 0.5,2.0,0.1>
translate < 6.0,0.0,6.05>
shiny_3d

}
+
// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 0.5,2.0,0.1>
translate < -3.0,0.0,6.05>
shiny_3d

}
+
// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 0.5,2.0,0.1>
translate < -6.0,0.0,6.05>
shiny_3d

}
+
// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 0.5,2.0,0.1>
translate < 0.0,0.0,-6.05>
shiny_3d

}
+
// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 0.5,2.0,0.1>
translate < 3.0,0.0,-6.05>
shiny_3d

}
+
// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 0.5,2.0,0.1>
translate < 6.0,0.0,-6.05>
shiny_3d

}
+
// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 0.5,2.0,0.1>
translate < -3.0,0.0,-6.05>
shiny_3d

}
+
// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 0.5,2.0,0.1>
translate < -6.0,0.0,-6.05>
shiny_3d
}
}
-
// CYLINDER
object {
cylinder <0.0, -0.5,0.0>, <0.0, 0.5, 0.0>, 1.0
scale < 2.0,2.0,2.0>
translate < 8.0,2.5,0.0>
matte_3d
}
}

// END OF 3DCOLOR ALGORTHM
//***************************************************************************
// SCENE CONSTRUCTION

// LIGHT_SOURCE
light <-10 , 10 , -10 >

// LIGHT_SOURCE
light <10 , 10 , 10 >

// LIGHT SOURCE
light <15,15,-15>

// LIGHT SOURCE
light <-15,-15,15>
/*
define Twochips
object {

object {Chip1}
+
object {
Chip1
rotate <0,90,0>
scale <0.5,1,0.5>
translate <15,0,-15>
}
}

define Fourchips
object {
object {Twochips}
+
object {
Twochips
rotate <0,90,0>
translate <-2,0,-20>
}
}

object {
Fourchips
}

object {
Fourchips
rotate <0,90,0>
translate <0,0,20>
}
*/
//PROGRAM: C:\POLY7\NL\PART1\BOARD1.PI GENERATED WITH POVCAD 2.0b (c) Alfonso Hermida 1993
//Created on 1/21/95 4:11:39 PM

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,3.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,3.0>
translate < 0.0,0.0,10.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,3.0>
translate < 0.0,0.0,21.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,3.0>
translate < 12.0,0.0,21.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,3.0>
translate < 12.0,0.0,0.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,3.0>
translate < 24.0,0.0,21.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,3.0>
translate < 24.0,0.0,10.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,3.0>
translate < 24.0,0.0,0.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
rotate < 0.0,90.0,0.0>
translate < -9.0,0.0,1.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
rotate < 0.0,90.0,0.0>
translate < -14.0,0.0,1.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
rotate < 0.0,90.0,0.0>
translate < -19.0,0.0,1.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
rotate < 0.0,90.0,0.0>
translate < -9.0,0.0,11.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
rotate < 0.0,90.0,0.0>
translate < -14.0,0.0,11.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
rotate < 0.0,90.0,0.0>
translate < -19.0,0.0,11.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
rotate < 0.0,90.0,0.0>
translate < -9.0,0.0,21.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
rotate < 0.0,90.0,0.0>
translate < -14.0,0.0,21.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
rotate < 0.0,90.0,0.0>
translate < -19.0,0.0,21.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
rotate < 0.0,90.0,0.0>
translate < 8.0,0.0,10.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
rotate < 0.0,90.0,0.0>
translate < 12.0,0.0,10.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
rotate < 0.0,90.0,0.0>
translate < 16.0,0.0,10.0>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 0.0,0.0,90.0>
translate < 0.0,0.0,5.25>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 90.0,0.0,0.0>
translate < 6.0,0.0,0.0>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 90.0,0.0,0.0>
translate < 30.0,0.0,10.0>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 90.0,0.0,0.0>
translate < 30.0,0.0,0.0>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 90.0,0.0,0.0>
translate < 30.0,0.0,21.0>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 90.0,0.0,0.0>
translate < 18.0,0.0,21.0>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 90.0,0.0,0.0>
translate < 6.0,0.0,21.0>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 90.0,0.0,0.0>
translate < 18.0,0.0,0.0>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 0.0,0.0,90.0>
translate < 12.0,0.0,5.0>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 0.0,0.0,90.0>
translate < 12.0,0.0,16.0>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 0.0,0.0,90.0>
translate < 24.0,0.0,5.0>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 0.0,0.0,90.0>
translate < 24.0,0.0,16.0>
matte_3d
}

// CYLINDER
object {
cylinder <0.0, -3.0,0.0>, <0.0, 3.0, 0.0>, 1.0
scale < 0.5,0.5,0.5>
rotate < 0.0,0.0,90.0>
translate < 0.0,0.0,16.0>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
translate < 36.0,0.0,-0.5>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
translate < 36.0,0.0,9.5>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
translate < 36.0,0.0,19.5>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
translate < 41.0,0.0,14.75>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
translate < 41.0,0.0,4.75>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
translate < 41.0,0.0,23.75>
matte_3d
}

// BOX
object {
box <-1, -1, -1>, <1, 1, 1>
scale < 4.0,1.0,1.5>
translate < 41.0,0.0,-5.25>
matte_3d
}