SceneKit - 照明和投射阴影
SceneKit - Lighting and casting shadows
我正在尝试创建照亮立方体的聚光灯。下面的表面应该会显示阴影。不幸的是我无法做到这一点。灯光忽略立方体并将光投射到表面和立方体上,无论立方体是否挡路。
看起来像:
代码,我只是在每个节点上将 castsShadow 设置为 YES 以进行测试,但到目前为止没有任何帮助:
BOOL shadows = YES;
_baseNode.castsShadow = shadows;
_scene.rootNode.castsShadow = shadows;
SCNBox *box = [SCNBox boxWithWidth: 50 height: 50 length: 50 chamferRadius: 10];
box.firstMaterial.diffuse.contents = [UIColor colorWithRed: 0.0 green:0.0 blue: 0.8 alpha: 1.0];
box.firstMaterial.doubleSided = YES;
SCNNode *boxNode = [SCNNode nodeWithGeometry: box];
boxNode.position = SCNVector3Make(100, 100, 100);
boxNode.castsShadow = shadows;
[_baseNode addChildNode: boxNode];
SCNBox *plane = [SCNBox boxWithWidth: 1000 height:1000 length: 40 chamferRadius: 10];
plane.firstMaterial.diffuse.contents = [UIColor colorWithWhite: 0.2 alpha: 1.0];
SCNNode *planeNode = [SCNNode nodeWithGeometry: plane];
planeNode.position = SCNVector3Make(100, 100, 0);
planeNode.castsShadow = shadows;
[_baseNode addChildNode: planeNode];
SCNNode *lightNode = [SCNNode node];
lightNode.light = [SCNLight light];
lightNode.light.type = SCNLightTypeSpot;
lightNode.light.spotInnerAngle = 0;
lightNode.light.spotOuterAngle = 45;
lightNode.light.shadowRadius = 10.0;
lightNode.light.zFar = 10000;
lightNode.light.shadowColor = [UIColor colorWithRed: 0.0 green: 1.0 blue:0.0 alpha: 1.0];
lightNode.castsShadow = shadows;
lightNode.position = SCNVector3Make(100, 100, 170);
[_baseNode addChildNode:lightNode];
SCNNode *lightNode2 = [SCNNode node];
lightNode2.light = [SCNLight light];
lightNode2.light.type = SCNLightTypeAmbient;
lightNode2.light.color = [UIColor colorWithRed: 0.3 green: 0.3 blue:0.3 alpha: 1.0];
[_baseNode addChildNode: lightNode2];
[_scene.rootNode addChildNode: _baseNode];
}
您必须在 SCNLight
上设置 castsShadow
(而不是在保持灯的 SCNNode
上)。
我正在尝试创建照亮立方体的聚光灯。下面的表面应该会显示阴影。不幸的是我无法做到这一点。灯光忽略立方体并将光投射到表面和立方体上,无论立方体是否挡路。
看起来像:
代码,我只是在每个节点上将 castsShadow 设置为 YES 以进行测试,但到目前为止没有任何帮助:
BOOL shadows = YES;
_baseNode.castsShadow = shadows;
_scene.rootNode.castsShadow = shadows;
SCNBox *box = [SCNBox boxWithWidth: 50 height: 50 length: 50 chamferRadius: 10];
box.firstMaterial.diffuse.contents = [UIColor colorWithRed: 0.0 green:0.0 blue: 0.8 alpha: 1.0];
box.firstMaterial.doubleSided = YES;
SCNNode *boxNode = [SCNNode nodeWithGeometry: box];
boxNode.position = SCNVector3Make(100, 100, 100);
boxNode.castsShadow = shadows;
[_baseNode addChildNode: boxNode];
SCNBox *plane = [SCNBox boxWithWidth: 1000 height:1000 length: 40 chamferRadius: 10];
plane.firstMaterial.diffuse.contents = [UIColor colorWithWhite: 0.2 alpha: 1.0];
SCNNode *planeNode = [SCNNode nodeWithGeometry: plane];
planeNode.position = SCNVector3Make(100, 100, 0);
planeNode.castsShadow = shadows;
[_baseNode addChildNode: planeNode];
SCNNode *lightNode = [SCNNode node];
lightNode.light = [SCNLight light];
lightNode.light.type = SCNLightTypeSpot;
lightNode.light.spotInnerAngle = 0;
lightNode.light.spotOuterAngle = 45;
lightNode.light.shadowRadius = 10.0;
lightNode.light.zFar = 10000;
lightNode.light.shadowColor = [UIColor colorWithRed: 0.0 green: 1.0 blue:0.0 alpha: 1.0];
lightNode.castsShadow = shadows;
lightNode.position = SCNVector3Make(100, 100, 170);
[_baseNode addChildNode:lightNode];
SCNNode *lightNode2 = [SCNNode node];
lightNode2.light = [SCNLight light];
lightNode2.light.type = SCNLightTypeAmbient;
lightNode2.light.color = [UIColor colorWithRed: 0.3 green: 0.3 blue:0.3 alpha: 1.0];
[_baseNode addChildNode: lightNode2];
[_scene.rootNode addChildNode: _baseNode];
}
您必须在 SCNLight
上设置 castsShadow
(而不是在保持灯的 SCNNode
上)。