How can I bend a rectangle in sprite kit
Maybe you should use warpGeometry
I don't know if your runforever create a bad performance.
check the documentation
Curve and stretch a sprite to mouse click/touch
Camera.ScreenToWorldPoint
isn't appropriate here because you don't already know how far away to check from the camera, which is needed for the z position. An incorrect z
component would give the nearest point on the spline to some point that the mouse is aligned with, but not on the sprite, and would modify the spline at an unexpected position:
Instead, draw a ray from the camera and see where it intersects with the plane the sprite lives on, and use that world position.
In Update
:
void Update()
{
if (inseretedPointIndex != INVALLID_INSERTED_POINT_INDEX)
{
spline = spriteShapeController.spline;
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane p = new Plane(Vector3.forward,spriteShapeController.spline.GetPosition(0));
float d;
p.Raycast(r,out d);
spline.SetPosition(inseretedPointIndex, r.GetPoint(d));
spriteShapeController.BakeCollider();
}
}
In OnMouseDown
:
void OnMouseDown()
{
Debug.Log("Mouse Down Position:" + Input.mousePosition);
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane p = new Plane(Vector3.forward, spriteShapeController.spline.GetPosition(0));
float d;
p.Raycast(r,out d);
Vector3 mouseDownPos = r.GetPoint(d);
Debug.Log("World Position: " + mouseDownPos);
How to project a texture to curved surface?
"I know it's about texture projection" - It's about computing UV, based on vertices coordinates.
CylinderGeometry
also may help to achieve the result you described. With less code, and more convenient and predictable way.
body{
overflow: hidden;
margin: 0;
}
<script type="module">
import * as THREE from "https://cdn.skypack.dev/three@0.136.0";
import {OrbitControls} from "https://cdn.skypack.dev/three@0.136.0/examples/jsm/controls/OrbitControls.js";
console.clear();
let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(60, innerWidth / innerHeight, 1, 1000);
camera.position.set(0, 10, 10);
let renderer = new THREE.WebGLRenderer();
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
window.addEventListener("resize", event => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
let controls = new OrbitControls(camera, renderer.domElement);
scene.add(new THREE.AxesHelper(10));
let g = new THREE.CylinderGeometry(5, 5, 5, 100, 20, true, 0, Math.PI * 1.5);
let m = new THREE.MeshBasicMaterial({
side: THREE.DoubleSide,
map: new THREE.TextureLoader().load(
"https://threejs.org/examples/textures/uv_grid_opengl.jpg",
tex => {
tex.wrapS = tex.wrapT = THREE.MirroredRepeatWrapping;
tex.repeat.set(3, 1);
}
)
});
let c = new THREE.Mesh(g, m);
scene.add(c);
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
</script>
bend line 90 degrees at a point where another movieclip intersects with it in actionscript 3
This is an extremely quick and dirty example to get the ball rolling. There are many issues with this code, so I suggest you use it only as a jumping off point. Details are commented within the code.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class MirrorTest extends Sprite
{
private var _mirror:Sprite;
private var _line:Sprite;
public function MirrorTest()
{
super();
// create the line clip
_line = addChild(new Sprite()) as Sprite;
_line.graphics.clear();
_line.graphics.lineStyle(3, 0xFF0000);
_line.graphics.moveTo(0, 0);
_line.graphics.lineTo(500, 0); // starting it off at an arbitrary width
_line.y = 100; // positioning line so that we can see when it bends upwards
// create the mirror clip
_mirror = addChild(new Sprite()) as Sprite;
// draw a square and rotate it so we have a diamond shape
_mirror.graphics.beginFill(0);
_mirror.graphics.drawRect(-20, -20, 40, 40);
_mirror.graphics.endFill();
_mirror.rotation = 45;
_mirror.x = _mirror.y = 200; // position the mirror away from the line
// add even listeners to trigger dragging/dropping
_mirror.addEventListener(MouseEvent.MOUSE_DOWN, dragMirror);
_mirror.addEventListener(MouseEvent.MOUSE_UP, dropMirror);
}
private function dragMirror($event:MouseEvent):void
{
// start dragging the mirror.
_mirror.startDrag(true);
// add the ENTER_FRAME listener so that we can check for colision as the mirror is being moved around
addEventListener(Event.ENTER_FRAME, onTick);
}
private function dropMirror($event:MouseEvent):void
{
// stop dragging the mirror
_mirror.stopDrag();
// remove the ENTER_FRAME listener so we don't waste cycles checking for collision when the mirror is not being moved around
removeEventListener(Event.ENTER_FRAME, onTick);
}
private function onTick($event:Event):void
{
// check to see if the mirror has collided with the line
if (_mirror.hitTestObject(_line))
{
// if so, redraw the line as a right-angle, using the mirror's position as the "collision point"
_line.graphics.clear();
_line.graphics.lineStyle(3, 0xFF0000);
_line.graphics.moveTo(0, 0);
_line.graphics.lineTo(_mirror.x - _mirror.width * .5, 0);
_line.graphics.lineTo(_mirror.x - _mirror.width * .5, -100);
}
else
{
// if not, redraw the original line
_line.graphics.clear();
_line.graphics.lineStyle(3, 0xFF0000);
_line.graphics.moveTo(0, 0);
_line.graphics.lineTo(500, 0);
}
}
}
}
Make A SKSpriteNode Flash White
I figured it out. Here's the important part of the class I wrote to handle this...ignore the name of the class...it's something specific to my app, just call it whatever you need it to be called:
VersusImage.h
#import <SpriteKit/SpriteKit.h>
@interface VersusImage : SKSpriteNode
-(instancetype) initWithImage:(SKSpriteNode*)image;
-(void) flashWhite;
@end
VersusImage.m
#import "VersusImage.h"
@interface VersusImage()
@property SKCropNode *cropNode;
@property SKSpriteNode *image;
@property SKSpriteNode *shape;
@end
@implementation VersusImage
-(instancetype) initWithImage:(SKSpriteNode*)image {
if (self = [super init]) {
_image = image;
[self initImage];
}
return self;
}
-(void) initImage {
_image.zPosition = -1;
_image.alpha = 1;
float width = _image.frame.size.width, height = _image.frame.size.height;
SKTexture *texture = [_image texture];
SKSpriteNode *mask = [SKSpriteNode spriteNodeWithTexture:texture size:CGSizeMake(width, height)];
mask.zPosition = 1;
_shape = [SKSpriteNode spriteNodeWithColor:[NSColor whiteColor] size:CGSizeMake(width, height)];
_shape.zPosition = 2;
_shape.alpha = 0;
_cropNode = [SKCropNode node];
_cropNode.maskNode = mask;
[_cropNode addChild:_shape];
[_cropNode addChild:_image];
[self addChild:_cropNode];
}
-(void) flashWhite {
SKAction *showWhite = [SKAction fadeAlphaTo:1.0 duration:0];
SKAction *hideImage = [SKAction fadeAlphaTo:0 duration:0];
SKAction *fadeOut = [SKAction fadeAlphaTo:0 duration:0.2];
SKAction *fadeIn = [SKAction fadeAlphaTo:1 duration:0.2];
[_shape runAction:[SKAction sequence:@[showWhite, fadeOut]]];
[_image runAction:[SKAction sequence:@[hideImage, fadeIn]]];
}
@end
Related Topics
Create Skspritenode with an Asset Programmatically
How to Set Window Level in Swift
How to Disabled Some Default Functionality in Scene View When Allowscameracontrol = True
Disable Bringing App Window to Front. After Closing Another Window
Need Clarification on Anyobject in Swift
How to Find The Top 3 Maximum Values in a Swift Dictionary
Exc_Badinstruction When Computing a Hash Value
Swiftui Conditional View Transitions Are Not Working
Using a Swiftui List Sidebar in a UIsplitviewcontroller
Allocating Ekeventstore Throws Warnings
Generic Vector with Cardinality Type Safety
Swift 4 Base64 String to Data Not Working Due to String Containing "Incomplete" Emoji
Swift Nstimer Dynamically Changing Interval
Validatemenuitem or Menuwillopen Not Called for Nsmenu
Macos Remote Push Notifications Not Showing Alert Banner
How to Catch an Exception in Swift
Nstimer Scheduledtimerwithtimeinterval and Target Is "Class Level Function"
A Warning "'Init()' Is Deprecated". [Swift, iOS App, Learning Model]