初步了解panda3d阴影效果的实现

Posted bcbobo21cn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初步了解panda3d阴影效果的实现相关的知识,希望对你有一定的参考价值。

from panda3d.core import *
import sys,os
import direct.directbase.DirectStart
from direct.interval.IntervalGlobal import *
from direct.showbase.DirectObject import DirectObject
from direct.actor import Actor

class World:
    def __init__(self):
    
        if (base.win.getGsg().getSupportsBasicShaders()==0):
            self.t=addTitle("Shadow Demo: Video driver reports that shaders are not supported.")
            return
        if (base.win.getGsg().getSupportsDepthTexture()==0):
            self.t=addTitle("Shadow Demo: Video driver reports that depth textures are not supported.")
            return       
   
        base.setBackgroundColor(0,0,0.2,1)
    
        base.camLens.setNearFar(1.0,10000)
        base.camLens.setFov(75)
        base.disableMouse()
  
        floorTex=loader.loadTexture('maps/envir-ground.jpg')
        cm=CardMaker('')
        cm.setFrame(-2,2,-2,2)
        floor = render.attachNewNode(PandaNode("floor"))
        for y in range(12):
            for x in range(12):
                nn = floor.attachNewNode(cm.generate())
                nn.setP(-90)
                nn.setPos((x-6)*4, (y-6)*4, 0)
        floor.setTexture(floorTex)
        floor.flattenStrong()    
 
        self.teapot=loader.loadModel('teapot')
        self.teapot.reparentTo(render)
        self.teapot.setPos(0,-20,10)
        self.teapot.setShaderInput("texDisable",1,1,1,1)
        self.teapotMovement = self.teapot.hprInterval(50,Point3(0,360,360))
        self.teapotMovement.loop() 

        self.light = render.attachNewNode(Spotlight("Spot"))
        self.light.node().setScene(render)
        self.light.node().setShadowCaster(True)
        self.light.node().showFrustum()
        self.light.node().getLens().setFov(40)
        self.light.node().getLens().setNearFar(10,100)
        render.setLight(self.light)

        self.alight = render.attachNewNode(AmbientLight("Ambient"))
        self.alight.node().setColor(Vec4(0.2, 0.2, 0.2, 1))
        render.setLight(self.alight)

        # Important! Enable the shader generator.
        render.setShaderAuto()
        
        # default values
        self.cameraSelection = 0
        self.lightSelection = 0
    
        self.incrementCameraPosition(0)
        self.incrementLightPosition(0)
    
# end of __init__

    def toggleInterval(self, ival):
        if (ival.isPlaying()):
            ival.pause()
        else:
            ival.resume()

    def incrementCameraPosition(self,n):
        self.cameraSelection = (self.cameraSelection + n) % 6
        if (self.cameraSelection == 0):
            base.cam.reparentTo(render)
            base.cam.setPos(30,-45,26)
            base.cam.lookAt(0,0,0)
            self.light.node().hideFrustum()
        if (self.cameraSelection == 1):
            base.cam.reparentTo(self.pandaModel)
            base.cam.setPos(7,-3,9)
            base.cam.lookAt(0,0,0)
            self.light.node().hideFrustum()
        if (self.cameraSelection == 2):
            base.cam.reparentTo(self.pandaModel)
            base.cam.setPos(-7,-3,9)
            base.cam.lookAt(0,0,0)
            self.light.node().hideFrustum()
        if (self.cameraSelection == 3):
            base.cam.reparentTo(render)
            base.cam.setPos(7,-23,12)
            base.cam.lookAt(self.teapot)
            self.light.node().hideFrustum()
        if (self.cameraSelection == 4):
            base.cam.reparentTo(render)
            base.cam.setPos(-7,-23,12)
            base.cam.lookAt(self.teapot)
            self.light.node().hideFrustum()
        if (self.cameraSelection == 5):
            base.cam.reparentTo(render)
            base.cam.setPos(1000,0,195)
            base.cam.lookAt(0,0,0)
            self.light.node().showFrustum()
  
    def incrementLightPosition(self,n):
        self.lightSelection = (self.lightSelection + n) % 2
        if (self.lightSelection == 0):
            self.light.setPos(0,-40,25)
            self.light.lookAt(0,-10,0)
            self.light.node().getLens().setNearFar(10,100)
        if (self.lightSelection == 1):
            self.light.setPos(0,-600,200)
            self.light.lookAt(0,-10,0)
            self.light.node().getLens().setNearFar(10,1000)
  
    def shaderSupported(self):
        return base.win.getGsg().getSupportsBasicShaders() and \\
               base.win.getGsg().getSupportsDepthTexture() and \\
               base.win.getGsg().getSupportsShadowFilter()  

World()
run()

   

 

茶壶模型是panda自带的; 

从代码来看,只要添加了灯光,照到模型上,阴影影子就出来了;不需要额外的代码;

class World:
    def __init__(self):
    
        if (base.win.getGsg().getSupportsBasicShaders()==0):
            self.t=addTitle("Shadow Demo: Video driver reports that shaders are not supported.")
            return
        if (base.win.getGsg().getSupportsDepthTexture()==0):
            self.t=addTitle("Shadow Demo: Video driver reports that depth textures are not supported.")
            return       

调用后面的函数,输出是否支持shaders,depth textures;
   
        base.setBackgroundColor(0,0,0.2,1)

设置一个窗口背景色;
    
        base.camLens.setNearFar(1.0,10000)
        base.camLens.setFov(75)
        base.disableMouse()

设置相机参数,禁止鼠标;
  
        floorTex=loader.loadTexture('maps/envir-ground.jpg')
        cm=CardMaker('')
        cm.setFrame(-2,2,-2,2)
        floor = render.attachNewNode(PandaNode("floor"))
        for y in range(12):
            for x in range(12):
                nn = floor.attachNewNode(cm.generate())
                nn.setP(-90)
                nn.setPos((x-6)*4, (y-6)*4, 0)
        floor.setTexture(floorTex)
        floor.flattenStrong()    

使用一个小背景图片,重复画出地板;
 
        self.teapot=loader.loadModel('teapot')
        self.teapot.reparentTo(render)
        self.teapot.setPos(0,-20,10)
        self.teapot.setShaderInput("texDisable",1,1,1,1)
        self.teapotMovement = self.teapot.hprInterval(50,Point3(0,360,360))
        self.teapotMovement.loop() 

载入茶壶,设置茶壶参数,茶壶开始循环;

        self.light = render.attachNewNode(Spotlight("Spot"))
        self.light.node().setScene(render)
        self.light.node().setShadowCaster(True)
        self.light.node().showFrustum()
        self.light.node().getLens().setFov(40)
        self.light.node().getLens().setNearFar(10,100)
        render.setLight(self.light)

        self.alight = render.attachNewNode(AmbientLight("Ambient"))
        self.alight.node().setColor(Vec4(0.2, 0.2, 0.2, 1))
        render.setLight(self.alight)

设置和添加灯光到场景;

        # Important! Enable the shader generator.
        render.setShaderAuto()
        
        # default values
        self.cameraSelection = 0
        self.lightSelection = 0
    
        self.incrementCameraPosition(0)
        self.incrementLightPosition(0)

初始化参数;
    
# end of __init__

    def toggleInterval(self, ival):

    ......
        此函数是循环播放;

def incrementCameraPosition(self,n):
......;

    改变相机位置和lookAt的函数;

  
    def incrementLightPosition(self,n):
    ......

        改变灯光位置的函数;

高性能云服务器 精品线路独享带宽,毫秒延迟,年中盛惠 1 折起

以上是关于初步了解panda3d阴影效果的实现的主要内容,如果未能解决你的问题,请参考以下文章

初步了解Panda3D音频和高级交互组件

初步了解Panda3D音频和高级交互组件

初步了解Panda3d粒子系统

初步了解Panda3d粒子系统

灯光为啥没有阴影效果?

初步了解Panda3D场景管理