201605151710[blender] 測試群聚效果

https://www.youtube.com/watch?v=p6-8IZn-l8o&feature=youtu.be

 

import bpy
from random import *
from mathutils import *

for o in bpy.data.objects:
    o.select = True
bpy.ops.object.delete()

mat = None
if bpy.data.materials.get("Material") is not None:
    mat = bpy.data.materials["Material"]
else:
    mat = bpy.data.materials.new(name="Material")

speed = 1.2
follow = .01
seperate = .02
dodge = .02

birdCount = 80
birdRange = 100
birdViewRange = 30
birdSeperateRange = 20

blockCount = 30
blockRange = 400
blockSize = 50
blockEffectRange = 20

os = []
bs = []

for i in range( birdCount ):
    bpy.ops.mesh.primitive_cone_add()
    o = bpy.context.object
    o.color = (1,1,0,1)
    o.data.materials.append(mat)
    o.location.x = (random() - .5) * birdRange
    o.location.y = (random() - .5) * birdRange
    o.location.z = (random() - .5) * birdRange
    o['vel'] = ( (random() - .5), (random() - .5), (random() - .5)  )
    os.append( o )
    
for i in range( blockCount ):
    r = random()*blockSize
    bpy.ops.mesh.primitive_ico_sphere_add(size=r)
    bpy.ops.object.shade_smooth()
    o = bpy.context.object
    o.color = (1,0,0,1)
    o.data.materials.append(mat)
    o.location.x = (random() - .5) * blockRange
    o.location.y = (random() - .5) * blockRange
    o.location.z = (random() - .5) * blockRange
    #o.draw_type = 'BOUNDS'
    o['radius'] = r
    bs.append( o )
 
def simulate( id, obj ):
    pAver = Vector()
    vAver = Vector()
    count = 0
    
    navAver = Vector()
    navCount = 0
    
    for j, curr in enumerate(os):
        if id != j:
            d = curr.location - obj.location
            if d.length < birdViewRange:
                pAver += curr.location
                vAver += Vector(curr['vel'])
                count += 1
                if d.length < birdSeperateRange:
                    navAver += -d
                    navCount += 1
            
                
    for b in bs:
        d = b.location - obj.location
        if d.length < b['radius']+blockEffectRange:
            navAver += -d * dodge
            navCount += 1
               
    if count > 0:
        pAver = pAver / count
        v = Vector(obj['vel'])
        d = (pAver - obj.location) * follow
        n = Vector()
        if navCount > 0:
            navAver = navAver / navCount
            n = navAver * seperate
        
        allv = v + d + n
        
        obj['vel'] = allv.normalized() * speed
        
        
    
def move( obj ):
    v = obj['vel']
    obj.location += Vector(v)
    
    up = Vector((0,0,1))
    spin = up.cross(v)
    angle = up.angle(v)
    rm = Matrix.Rotation(angle, 4, spin)
    tm = Matrix.Translation(obj.location)
    
    obj.matrix_local = tm * rm
    
    
def update( scene ):
    f = scene.frame_current 
    for i, o in enumerate(os):
        simulate( i, o )
        move(o )
    
bpy.app.handlers.frame_change_pre.clear()
bpy.app.handlers.frame_change_pre.append( update )
回應

熟悉:

flash, 3ds max

 

玩過的技術有:

網站製作, 遊戲製作, 質子物理學, 粒子特效, 3d建模, 影片後製, 體感控制, 遊戲shader

    沒有新回應!
關鍵字