WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Thread Tools Rate Thread Display Modes
Old 01-14-2013, 05:18 PM
left_leg left_leg is offline
Join Date: Jan 2013
Posts: 6
Question no fog in outer space - trouble getting viz.fog to work

hey guys,

I programmed a starfield environment as action class. since the field size is limited I wanted to use viz.fog to smoothly fade in distant stars while moving through the field. however, the no matter what I do the fog is not appearing in the starfield. checked stuff like color of the fog, different densities, linear or exponential fog and even added a lighting.

my alternative is using a modest gaussian blur postprocessing, but the fog would definitely be the better option...

here is my code, first the starfield action class and then a little demo:

import vizshape
import viz
import random
import viztask

class Starfield(viz.ActionClass):
	action class providing a starfield that can be used as optical flow stimulus
	def begin(self,object):
		constructor: places the stars randomly columns around a column
		center (black cube). columns are stored in a list
		""" 	   =
		self.nStars    =[0]
		self.starSize  =[1]
		self.colDimXYZ =[2]
		self.nColsXYZ  =[3]
		# internal parameters
		self.starsPerColumn = int(self.nStars / (self.nColsXYZ[0] * 
							  self.nColsXYZ[1] * self.nColsXYZ[2]))
		self.xDeltaMax = self.colDimXYZ[0] * self.nColsXYZ[0] / 2
		self.yDeltaMax = self.colDimXYZ[1] * self.nColsXYZ[1] / 2
		self.zDeltaMax = self.colDimXYZ[2] * self.nColsXYZ[2] / 2
		self.countStart = 0
		self.time = 0

		# create a master star
		protoStar = viz.addTexQuad(size = self.starSize)
		diff = viz.add('star.png')
		norm = viz.add('normal1.tif')
		# list with all stars
		self.columns = []
		for idx_x in range(self.nColsXYZ[0]):
			for idx_y in range(self.nColsXYZ[1]):
				for idx_z in range(self.nColsXYZ[2]):
					# create center node as parent for all stars in that column
					columnCenter = vizshape.addCube(size = 0.001)
					columnCenter.setPosition([idx_x * self.colDimXYZ[0],
											  idx_y * self.colDimXYZ[1],
											  idx_z * self.colDimXYZ[2]])
					# randomly create stars within the limits of one column
					for y in range(self.starsPerColumn):
						# positioning
						x = self.colDimXYZ[0] * (random.random() - 0.5)
						y = self.colDimXYZ[1] * (random.random() - 0.5)
						z = self.colDimXYZ[2] * (random.random() - 0.5)
						star = protoStar.clone(columnCenter)
						star.setPosition([x,y,z], mode = viz.ABS_PARENT)

	def update(self,elapsed,object):
		checks every frame if a column center exceeded the volume
		if yes, it reenters the volume on the oppsite side (like in snake)

		# get position of the avatar (center of the field)
		[aX, aY, aZ] = object.getPosition(mode = viz.ABS_GLOBAL)
		# for every star: check if within limits of field, if not, reposition using respawn function
		for column in self.columns[self.countStart::4]:
			# get position of the star
			pos = column.getPosition(mode = viz.ABS_GLOBAL)
			# check every coordinate
			x = pos[0] if abs(pos[0] - aX) <= self.xDeltaMax else self.respawn(aX, pos[0], self.xDeltaMax)
			y = pos[1] if abs(pos[1] - aY) <= self.yDeltaMax else self.respawn(aY, pos[1], self.yDeltaMax)
			z = pos[2] if abs(pos[2] - aZ) <= self.zDeltaMax else self.respawn(aZ, pos[2], self.zDeltaMax)

			self.time = elapsed + self.time
			# eventually reposition
			column.setPosition([x,y,z], mode = viz.ABS_GLOBAL)
		self.countStart = (self.countStart + 1) % 4
		if self.time >= 200.00 and self.time <= 200.1:
			object.setPosition([0,0,0], viz.ABS_GLOBAL)
	def end(self,object):
		print 'starfield ended'		
	def respawn(self, cAvatar, cPoint, deltaMax):
		""" if point exceeded displayed field, this calculates the reentry coordinates """
		# transform into coordinate system with avatar as (0,0)
		delta  = cPoint - cAvatar
		# how far the point exceeded the limit
		rest   = abs(delta) % deltaMax
		# check on which side
		side   = -1 if cPoint < cAvatar else 1
		# calculate new coordinates in global sys after entering on the other side
		new    = cAvatar - side * (deltaMax - rest)
		return new

def addStarfield(nStars = 10000, starSize = 0.18, colDimXYZ = [5, 30, 5], nColsXYZ = [20, 1, 20], blur = True):
	creates a starfield around the object it is assigned to as action
	object is center and the cuboid has the edge lenghts dimX, dimY and dimZ
	every frame every star is checked if it exceeds the cuboid, if yes it reenters
	the cuboid on the other side (like playing good old snkae)
	if blur:
		import vizfx.postprocess
		from vizfx.postprocess.blur import GaussianBlurEffect
		effect = GaussianBlurEffect(blurRadius = 10, blurScale = 0.001, downsample = 0.7)
	bla 			= viz.ActionData() 		= [nStars, starSize, colDimXYZ, nColsXYZ]
	bla.actionclass	= Starfield
	return bla
import iStarfield
import viz

# create an invisible avatar (needed to add the starfield action to it)
avatar = viz.add('wheelbarrow.ive')

# add the main view to it, avatar)

# add starfield action to it's OWN ACTION POOL
avatar.addAction(iStarfield.addStarfield(nStars = 10000, starSize = 0.18, colDimXYZ = [5, 30, 5], 
										 nColsXYZ = [20, 1, 20], blur = False                     ), pool = 1)


# adding the fog
Reply With Quote
Old 01-15-2013, 04:38 PM
Jeff Jeff is offline
WorldViz Team Member
Join Date: Aug 2008
Posts: 2,449
What if you set the background color to the fog color, does that work the way you want?
Reply With Quote
Old 01-16-2013, 02:40 PM
left_leg left_leg is offline
Join Date: Jan 2013
Posts: 6
well, good news is that it does something, namely indeed adjusting the background colour. however I still get absolutely no fog, respawning stars just pop up instead of fading in.
Reply With Quote
Old 01-17-2013, 11:58 AM
Jeff Jeff is offline
WorldViz Team Member
Join Date: Aug 2008
Posts: 2,449
Can you attach both the textures you're using? I tested with just a diffuse texture on the star and it faded in and out.
Reply With Quote
Old 01-17-2013, 03:32 PM
left_leg left_leg is offline
Join Date: Jan 2013
Posts: 6
sure, here are the textures
Attached Files
File Type: zip (190.7 KB, 280 views)
Reply With Quote
Old 01-23-2013, 03:51 PM
left_leg left_leg is offline
Join Date: Jan 2013
Posts: 6
so I played around a little more and it turned out that when I leave out the bumpmap the fog works perfectly fine! no clue why a problem arises when the bumpmap is included, but it doesn't look to bad without it, so I'll just keep it that way. thank's for the help Jeff, your comment led me on the right way!
Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -7. The time now is 10:00 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC