Fancy3D引擎sample学习(L)

1、lighting

通过按键设置_SkyLight和_PointLight

_SkyLight的direction是一个_Vector3,从原点指向的。_PointLight可以设置属性mat,作为变换矩阵,可以从中取出位移分量_Vector然后赋值给position,这个position是点光源的位置,mat值可以渲染小球出现在点光源的位置。

PS:所有颜色值是从 0xff000000~0xffffffff

 1 _sys:addPath('res')
 2 _dofile('cameracontrol.lua')
 3 
 4 slNum = 0
 5 plNum = 0
 6 LQ = false
 7 
 8 _rd:useLight(_AmbientLight.new())
 9 _rd.camera.eye = _Vector3.new(60, 60, 60)
10 
11 blender = _Blender.new()
12 
13 pointSphere, teapot, cube = _mf:createSphere(), _mf:createTeapot(), _mf:createCube()
14 
15 cube:setTexture(_Image.new('stone_color.jpg')):setBumpmap(_Image.new('stone_normal.jpg'))
16 cube.transform:setScaling(10, 10, 10)
17 
18 pointLights, skyLights, mats = {}, {}, {}
19 
20 function newsl(i)
21     skyLights[i] = _SkyLight.new()
22     skyLights[i].color, skyLights[i].direction = _random(0xff000000, 0xffffffff), _Vector3.new(_random(-1, 1), _random(-1, 1), _random(-1, 1))
23 end
24 
25 function newpl(i)
26     pointLights[i] = _PointLight.new()
27     pointLights[i].range, pointLights[i].color = 200, _random(0xff000000, 0xffffffff)
28     pointLights[i].mat = _Matrix3D.new():setTranslation( 25 * _random(-1, 1), 25 * _random(-1, 1), 25 * _random(-1, 1))
29     pointLights[i].position = pointLights[i].mat:getTranslation()
30 end
31 
32 for i = 1, 4 do mats[i] = _Matrix3D.new():setScaling(6, 6, 6) end
33 newpl(1)
34 newsl(1)
35 mats[1]:mulTranslationRight(50, 0, 0)
36 mats[2]:mulTranslationRight(-50, 0, 0)
37 mats[3]:mulTranslationRight(0, 50, 0)
38 mats[4]:mulTranslationRight(0, -50, 0)
39 
40 _app:onKeyDown(function(key)
41     if key == _System.KeyQ then
42         if(slNum < 10) then slNum = slNum + 1 newsl(slNum) end
43     elseif key == _System.KeyA then
44         if(slNum > 0) then slNum = slNum - 1 newsl(slNum) end
45     elseif key == _System.KeyW then
46         if(plNum < 10) then plNum = plNum + 1 newpl(plNum) end
47     elseif key == _System.KeyS then
48         if(plNum > 0) then plNum = plNum - 1 newpl(plNum) end
49     elseif key == _System.Key1 then
50         if LQ then LQ = false else LQ = true end
51         _rd.lightQualit = LQ and _RenderDevice.PixelLighting or _RenderDevice.VertexLighting
52     end
53 end)
54 
55 noticefont = _Font.new('Arial', 10)
56 noticefont.textColor = _Color.Yellow
57 
58 _app:onIdle(function(e)
59     noticefont:drawText(0, 0, 'Press 1 to switch lightQualit(No significant effect)')
60     noticefont:drawText(0, 14, 'Press QA to adjust skyLights Number')
61     noticefont:drawText(0, 28, 'Press WS to adjust pointLights Number')
62 
63     for i = 1, plNum do
64         _rd:useBlender(blender:blend(pointLights[i].color))
65         _rd:pushMatrix3D(pointLights[i].mat)
66             pointSphere:drawMesh()
67         _rd:popMatrix3D()
68         _rd:popBlender()
69     end
70     for i = 1, slNum do _rd:useLight(skyLights[i]) end
71     for i = 1, plNum do _rd:useLight(pointLights[i]) end
72     for i = 1, 4 do
73         _rd:pushMatrix3D(mats[i])
74             teapot:drawMesh()
75         _rd:popMatrix3D()
76     end
77     cube:drawMesh()
78     for i = 1, slNum + plNum do _rd:popLight() end
79     _rd:drawAxis(50)
80 end)
View Code

 2、lighting-backlight

skylight.backLight = true     -- 设置成逆光灯,就是把direction反向取反

 1 _sys:addPath('res')
 2 _dofile('cameracontrol.lua')
 3 --_rd.camera.eye = _Vector3.new(60, 60, 60)
 4 
 5 teapot = _mf:createTeapot()
 6 _mf:transformShape(teapot, _Matrix3D.new():setScaling(10, 10, 10))
 7 
 8 lit = _SkyLight.new()
 9 lit.color = _Color.Green
10 lit.backLight = true
11 
12 _rd:useLight(_AmbientLight.new())
13 
14 _app:onIdle(function(e)
15     lit.direction = _Vector3.sub(_rd.camera.look, _rd.camera.eye)
16 
17     _rd:useLight(lit)
18         teapot:drawMesh()
19     _rd:popLight()
20 
21     _rd:drawAxis(50)
22 end)
View Code

 3、lighting-foglight

skylight.fogLight = true       --  设置成雾光灯

 1 _sys:addPath('res')
 2 _dofile('cameracontrol.lua')
 3 --_rd.camera.eye = _Vector3.new(60, 60, 60)
 4 
 5 teapot = _mf:createTeapot()
 6 _mf:transformShape(teapot, _Matrix3D.new():setScaling(10, 10, 10))
 7 
 8 lit = _SkyLight.new()
 9 lit.color = _Color.Green
10 lit.backLight = true
11 lit.fogLight = true
12 
13 fog = _Fog.new()
14 fog.near = 40
15 fog.far = 100
16 fog.color = _Color.Red
17 
18 _rd:useLight(_AmbientLight.new())
19 
20 _app:onIdle(function(e)
21     lit.direction = _Vector3.sub(_rd.camera.look, _rd.camera.eye)
22 
23     _rd:useLight(lit)
24     _rd:useFog(fog)
25         teapot:drawMesh()
26     _rd:popLight()
27     _rd:popFog()
28 
29     _rd:drawAxis(50)
30 end)
View Code

 

posted @ 2013-11-27 11:22  丿聪丶  阅读(476)  评论(0)    收藏  举报