VRML : Chemistry in VRML
Source code for example 12 : An CH2CH2 (ethene) molecule, with pi bond shown
This is the most complex of all the examples, so don't expect to understand it all if you're new to VRML. You should read the other examples before reading this one, as they explain the basics, whilst this concentrates more on slightly more complex bits.

#VRML V1.0 ascii

Separator {

#Setup Lights, Camera, and Material as usuall

	DirectionalLight { direction 0 0 -1 }
	DirectionalLight { direction 0 -1 0 }
	PerspectiveCamera { 
		position 0 4 8
		orientation -1 0 0 0.4
	}
	Material {
		diffuseColor 1 0 0
		ambientColor 0.2 0  0
	}

#Here we create the two carbon atoms and the sigma bond
#between them.  Note that we DEFine several of the 
#components for use later in the world

	Separator {
		Translation { translation -2 0 0 }
		DEF carbon Sphere { radius 0.6 }
		Translation { translation 4 0 0 }
		USE carbon
		Translation { translation -2 0 0 }
		Rotation { rotation 0 0 1 1.570796 }
		DEF bondmat Material {
			diffuseColor 0 1 0
			ambientColor 0 0.2 0
		}
		Cylinder {
			height 4
			radius 0.1
			parts  SIDES
		}
	}

#Now we add two hyrdogens and bonds to the carbon
#atom at (-2,0,0).  This is done in a way that allows
#us to copy this segment, changing only two values, to
#do the same for the other carbon atom.

	Separator {
		Translation { translation -2 0 0 }
		Rotation { rotation 0 0 1 1.570796 }
		DEF r120 Rotation { rotation 1 0 0 2.094395 }
		DEF halfchbond Translation { translation 0 -1.2 0 }
		USE bondmat
		DEF chbond Cylinder {
			height 2.4
			radius 0.1
			parts SIDES
		}
		USE halfchbond
		DEF hmat Material {
			diffuseColor 1 1 1
			ambientColor 0.2 0.2 0.2
		}
		DEF hydrogen Sphere { radius 0.3 }
		Translation { translation 0 2.4 0 }
		USE r120
		USE halfchbond
		USE bondmat
		USE chbond
		USE halfchbond
		USE hmat
		USE hydrogen
	}

#This segment adds the two hydrogens and bonds to the
#carbon atom at (2,0,0).  It is copied from the above
#segment, changing only the sign of a couple of numbers

	Separator {
		Translation { translation 2 0 0 }
		Rotation { rotation 0 0 1 1.570796 }
		USE r120
		DEF halfchbond Translation { translation 0 1.2 0 }
		USE bondmat
		USE chbond
		USE halfchbond
		USE hmat
		USE hydrogen
		Translation { translation 0 -2.4 0 }
		USE r120
		USE halfchbond
		USE bondmat
		USE chbond
		USE halfchbond
		USE hmat
		USE hydrogen
	}

#Now we create a new material for the pi bond

	Material {
		diffuseColor 0 0 1
		ambientColor 0 0 0.2
	}

#Now we add the pi orbital to the carbon atom at
#(-2,0,0), using the same code as in the pi orbital
#example in example 4.

	Separator {
		Translation { translation -2 -1.2 0 }
		DEF mycone Cone {
			parts SIDES
			height 2
			bottomRadius 1
		}
		DEF halfpio Translation { translation 0 -1.2 0 }
		DEF ball Sphere { radius 1 }
		Rotation { rotation 1 0 0 3.151692 }
		USE halfpio
		USE halfpio
		USE halfpio
		USE mycone
		USE halfpio
		USE ball
	}

#Now we add the pi orbital to the carbon atom at
#(2,0,0), this code being a "copy and paste" of the
#segment above, with only a different initall translation

	Separator {
		Translation { translation 2 -1.2 0 }
		USE mycone
		USE halfpio
		USE ball
		Rotation { rotation 1 0 0 3.151692 }
		USE halfpio
		USE halfpio
		USE halfpio
		USE mycone
		USE halfpio
		USE ball
	}

#Now we create a semi-transparent material for use
#in the pi bond

	Material {
		diffuseColor 0 0 1
		ambientColor 0 0 0.2
		transparency 0.7
	}

#Now we add to semi-transparent cylinders between
#the pi orbitals on the two carbon atoms, representing
#a pi bond, and completeing the world

	Translation { translation 0 2.4 0 }
	Rotation { rotation 0 0 1 1.570796 }
	DEF pibond Cylinder {
		radius 0.8
		height 4
		parts SIDES
	}
	Translation { translation -4.8 0 0 }
	USE pibond
}


#End of File



Previous Topic (Chemistry in VRML)
Back to the VRML Reference
Back to the Main Page

©Tom Thurston, 1997
Please feel free to use and redistribute this NONcommercially!