mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
gizmos adaptation (#3401)
* raycast adapt * gizmos: first working version
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
{
|
||||
"techniques": [
|
||||
{
|
||||
"stages": ["transparent"],
|
||||
"params": [
|
||||
{
|
||||
"name": "color",
|
||||
"type": "color4",
|
||||
"value": [1, 1, 1, 1]
|
||||
},
|
||||
{
|
||||
"name": "mainTiling",
|
||||
"type": "float2",
|
||||
"value": [1, 1]
|
||||
},
|
||||
{
|
||||
"name": "mainOffset",
|
||||
"type": "float2",
|
||||
"value": [0, 0]
|
||||
},
|
||||
{
|
||||
"name": "mainTexture",
|
||||
"type": "texture2d",
|
||||
"value": null
|
||||
}
|
||||
],
|
||||
"passes": [
|
||||
{
|
||||
"program": "unlit",
|
||||
"cullMode": "none",
|
||||
"depthTest": false,
|
||||
"depthWrite": false,
|
||||
"blend": true,
|
||||
"blendEq": "add",
|
||||
"blendSrc": "one",
|
||||
"blendDst": "oneMinusSrcAlpha",
|
||||
"blendAlphaEq": "add",
|
||||
"blendSrcAlpha": "one",
|
||||
"blendDstAlpha": "oneMinusSrcAlpha"
|
||||
}
|
||||
],
|
||||
"layer": 0
|
||||
}
|
||||
],
|
||||
|
||||
"properties": {},
|
||||
|
||||
"defines": [
|
||||
{
|
||||
"name": "USE_TEXTURE",
|
||||
"value": false
|
||||
},
|
||||
{
|
||||
"name": "USE_COLOR",
|
||||
"value": false
|
||||
},
|
||||
{
|
||||
"name": "USE_SKINNING",
|
||||
"value": false
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -29,7 +29,7 @@
|
||||
"program": "unlit",
|
||||
"cullMode": "back",
|
||||
"depthTest": true,
|
||||
"depthWrite": true,
|
||||
"depthWrite": false,
|
||||
"blend": true,
|
||||
"blendEq": "add",
|
||||
"blendSrc": "srcAlpha",
|
||||
|
||||
@@ -40,6 +40,7 @@ let line_plane = (function () {
|
||||
|
||||
/**
|
||||
* ray-triangle intersect
|
||||
* based on http://fileadmin.cs.lth.se/cs/Personal/Tomas_Akenine-Moller/raytri/
|
||||
*
|
||||
* @param {ray} ray
|
||||
* @param {triangle} triangle
|
||||
@@ -52,32 +53,26 @@ let ray_triangle = (function () {
|
||||
let tvec = vec3.create(0, 0, 0);
|
||||
let qvec = vec3.create(0, 0, 0);
|
||||
|
||||
return function (ray, triangle) {
|
||||
return function (ray, triangle, doubleSided) {
|
||||
vec3.sub(ab, triangle.b, triangle.a);
|
||||
vec3.sub(ac, triangle.c, triangle.a);
|
||||
|
||||
vec3.cross(pvec, ray.d, ac);
|
||||
let det = vec3.dot(ab, pvec);
|
||||
if (det < Number.EPSILON && (!doubleSided || det > -Number.EPSILON)) return 0;
|
||||
|
||||
if (det <= 0) {
|
||||
return 0;
|
||||
}
|
||||
let inv_det = 1 / det;
|
||||
|
||||
vec3.sub(tvec, ray.o, triangle.a);
|
||||
let u = vec3.dot(tvec, pvec);
|
||||
if (u < 0 || u > det) {
|
||||
return 0;
|
||||
}
|
||||
let u = vec3.dot(tvec, pvec) * inv_det;
|
||||
if (u < 0 || u > 1) return 0;
|
||||
|
||||
vec3.cross(qvec, tvec, ab);
|
||||
let v = vec3.dot(ray.d, qvec);
|
||||
if (v < 0 || u + v > det) {
|
||||
return 0;
|
||||
}
|
||||
let v = vec3.dot(ray.d, qvec) * inv_det;
|
||||
if (v < 0 || u + v > 1) return 0;
|
||||
|
||||
let t = vec3.dot(ac, qvec) / det;
|
||||
if (t < 0) return 0;
|
||||
return t;
|
||||
let t = vec3.dot(ac, qvec) * inv_det;
|
||||
return t < 0 ? 0 : t;
|
||||
};
|
||||
})();
|
||||
|
||||
|
||||
@@ -212,7 +212,7 @@ CallbacksHandler.prototype = {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* !#en The callbacks invoker to handle and invoke callbacks by key.
|
||||
@@ -273,4 +273,4 @@ if (CC_TEST) {
|
||||
export {
|
||||
CallbacksHandler,
|
||||
CallbacksInvoker
|
||||
}
|
||||
};
|
||||
|
||||
@@ -543,6 +543,15 @@ export default class Color extends ValueType {
|
||||
this.a = color.a;
|
||||
}
|
||||
}
|
||||
|
||||
to01(out) {
|
||||
out = out || cc.vmath.color4.create();
|
||||
out.r = this.r / 255;
|
||||
out.g = this.g / 255;
|
||||
out.b = this.b / 255;
|
||||
out.a = this.a / 255;
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
CCClass.fastDefine('cc.Color', Color, {r: 0, g: 0, b: 0, a: 255});
|
||||
|
||||
@@ -128,6 +128,14 @@ export default class Quat extends ValueType {
|
||||
var cosy = 1.0 - 2.0 * (this.y * this.y + this.z * this.z);
|
||||
return 180 * Math.atan2(siny, cosy) / Math.PI;
|
||||
}
|
||||
|
||||
getEulerAngles (out) {
|
||||
out = out || cc.v3();
|
||||
out.x = this.getRoll();
|
||||
out.y = this.getPitch();
|
||||
out.z = this.getYaw();
|
||||
return out;
|
||||
}
|
||||
|
||||
lerp (to, ratio, out) {
|
||||
out = out || new cc.Quat();
|
||||
|
||||
@@ -355,6 +355,40 @@ export default class Camera {
|
||||
out._frustum.update(_matViewProj, _matInvViewProj);
|
||||
}
|
||||
|
||||
/**
|
||||
* transform a screen position to a world space ray
|
||||
* @param {number} x the screen x position to be transformed
|
||||
* @param {number} y the screen y position to be transformed
|
||||
* @param {number} width framebuffer width
|
||||
* @param {number} height framebuffer height
|
||||
* @param {ray} out the resulting ray
|
||||
* @returns {Ray} the resulting ray
|
||||
*/
|
||||
screenPointToRay(x, y, width, height, out) {
|
||||
out = out || ray.create();
|
||||
this._calcMatrices(width, height);
|
||||
|
||||
let cx = this._rect.x * width;
|
||||
let cy = this._rect.y * height;
|
||||
let cw = this._rect.w * width;
|
||||
let ch = this._rect.h * height;
|
||||
|
||||
// far plane intersection
|
||||
vec3.set(_tmp2_v3, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, 1);
|
||||
vec3.transformMat4(_tmp2_v3, _tmp2_v3, _matInvViewProj);
|
||||
|
||||
if (this._projection === enums.PROJ_PERSPECTIVE) {
|
||||
// camera origin
|
||||
this._node.getWorldPosition(_tmp_v3);
|
||||
} else {
|
||||
// near plane intersection
|
||||
vec3.set(_tmp_v3, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, -1);
|
||||
vec3.transformMat4(_tmp_v3, _tmp_v3, _matInvViewProj);
|
||||
}
|
||||
|
||||
return ray.fromPoints(out, _tmp_v3, _tmp2_v3);
|
||||
}
|
||||
|
||||
/**
|
||||
* transform a screen position to world space
|
||||
* @param {vec3} out the resulting vector
|
||||
@@ -400,40 +434,6 @@ export default class Camera {
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* transform a screen position to a world space ray
|
||||
* @param {number} x the screen x position to be transformed
|
||||
* @param {number} y the screen y position to be transformed
|
||||
* @param {number} width framebuffer width
|
||||
* @param {number} height framebuffer height
|
||||
* @param {ray} out the resulting ray
|
||||
* @returns {Ray} the resulting ray
|
||||
*/
|
||||
screenPointToRay(x, y, width, height, out) {
|
||||
out = out || ray.create();
|
||||
this._calcMatrices(width, height);
|
||||
|
||||
let cx = this._rect.x * width;
|
||||
let cy = this._rect.y * height;
|
||||
let cw = this._rect.w * width;
|
||||
let ch = this._rect.h * height;
|
||||
|
||||
// far plane intersection
|
||||
vec3.set(_tmp2_v3, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, 1);
|
||||
vec3.transformMat4(_tmp2_v3, _tmp2_v3, _matInvViewProj);
|
||||
|
||||
if (this._projection === enums.PROJ_PERSPECTIVE) {
|
||||
// camera origin
|
||||
this._node.getWorldPosition(_tmp_v3);
|
||||
} else {
|
||||
// near plane intersection
|
||||
vec3.set(_tmp_v3, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, -1);
|
||||
vec3.transformMat4(_tmp_v3, _tmp_v3, _matInvViewProj);
|
||||
}
|
||||
|
||||
return ray.fromPoints(out, _tmp_v3, _tmp2_v3);
|
||||
}
|
||||
|
||||
/**
|
||||
* transform a world space position to screen space
|
||||
* @param {vec3} out the resulting vector
|
||||
@@ -450,17 +450,10 @@ export default class Camera {
|
||||
let cw = this._rect.w * width;
|
||||
let ch = this._rect.h * height;
|
||||
|
||||
// calculate w
|
||||
let w =
|
||||
worldPos.x * _matViewProj.m03 +
|
||||
worldPos.y * _matViewProj.m07 +
|
||||
worldPos.z * _matViewProj.m11 +
|
||||
_matViewProj.m15;
|
||||
|
||||
vec3.transformMat4(out, worldPos, _matViewProj);
|
||||
out.x = cx + (out.x / w + 1) * 0.5 * cw;
|
||||
out.y = cy + (out.y / w + 1) * 0.5 * ch;
|
||||
out.z = out.z / w * 0.5 + 0.5;
|
||||
out.x = cx + (out.x + 1) * 0.5 * cw;
|
||||
out.y = cy + (out.y + 1) * 0.5 * ch;
|
||||
out.z = out.z * 0.5 + 0.5;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -194,60 +194,59 @@ class Scene {
|
||||
}
|
||||
|
||||
if (CC_EDITOR) {
|
||||
/**
|
||||
* create a raycast result array
|
||||
* @return {RecyclePool}
|
||||
*/
|
||||
Scene.createRaycastResult = function() {
|
||||
return new RecyclePool(() => {
|
||||
return { node: null, distance: Infinity };
|
||||
}, 16);
|
||||
};
|
||||
|
||||
/**
|
||||
* Cast a ray into the scene, record all the intersected models in the result array
|
||||
* @param {Ray} worldRay the testing ray
|
||||
* @param {RecyclePool} results the result array
|
||||
* @param {Object[]} results the results array
|
||||
* @param {number} mask the layer mask to filter the models
|
||||
* @return {boolean} does the ray hit any models?
|
||||
* @return {Object[]} the results array
|
||||
*/
|
||||
Scene.prototype.raycast = (function() {
|
||||
let modelRay = ray.create();
|
||||
let v3 = vec3.create();
|
||||
let m4 = mat4.create();
|
||||
let distance = Infinity;
|
||||
let tri = triangle.create();
|
||||
return function(worldRay, results, mask = Layers.RaycastMask) {
|
||||
results.reset();
|
||||
let pool = new RecyclePool(() => {
|
||||
return { node: null, distance: Infinity };
|
||||
}, 8);
|
||||
let results = [];
|
||||
return function(worldRay, mask = Layers.RaycastMask) {
|
||||
pool.reset();
|
||||
for (let i = 0; i < this._models.length; i++) {
|
||||
let m = this._models.data[i];
|
||||
if ((m._node._layer & mask) === 0 ||
|
||||
m._inputAssembler._primitiveType !== gfx.PT_TRIANGLES) continue;
|
||||
let m = this._models.data[i], node = m._node;
|
||||
if (!cc.Layers.check(node._layer, mask) || !m._bsModelSpace) continue;
|
||||
// transform ray back to model space
|
||||
mat4.invert(m4, m._node.getWorldMatrix(m4));
|
||||
mat4.invert(m4, node.getWorldMatrix(m4));
|
||||
vec3.transformMat4(modelRay.o, worldRay.o, m4);
|
||||
vec3.normalize(modelRay.d, vec3.transformMat4Normal(modelRay.d, worldRay.d, m4));
|
||||
// broadphase
|
||||
if (intersect.ray_aabb(modelRay, m._bsModelSpace) <= 0) continue;
|
||||
// narrowphase
|
||||
distance = Infinity;
|
||||
let vb = m._inputAssembler._vertexBuffer[gfx.ATTR_POSITION];
|
||||
let ib = m._inputAssembler._indexBuffer._data;
|
||||
for (let j = 0; j < ib.length; j += 3) {
|
||||
let i0 = ib[j] * 3, i1 = ib[j + 1] * 3, i2 = ib[j + 2] * 3;
|
||||
vec3.set(tri.a, vb[i0], vb[i0 + 1], vb[i0 + 2]);
|
||||
vec3.set(tri.b, vb[i1], vb[i1 + 1], vb[i1 + 2]);
|
||||
vec3.set(tri.c, vb[i2], vb[i2 + 1], vb[i2 + 2]);
|
||||
let dist = intersect.ray_triangle(modelRay, tri);
|
||||
if (dist <= 0 || dist > distance) continue;
|
||||
distance = dist;
|
||||
if ((distance = intersect.ray_aabb(modelRay, m._bsModelSpace)) <= 0) continue;
|
||||
let ia = m._inputAssembler;
|
||||
if (ia._primitiveType == gfx.PT_TRIANGLES) {
|
||||
// narrowphase
|
||||
distance = Infinity;
|
||||
let vb = ia._vertexBuffer[gfx.ATTR_POSITION];
|
||||
let ib = ia._indexBuffer._data, sides = ia.doubleSided;
|
||||
for (let j = 0; j < ib.length; j += 3) {
|
||||
let i0 = ib[j] * 3, i1 = ib[j + 1] * 3, i2 = ib[j + 2] * 3;
|
||||
vec3.set(tri.a, vb[i0], vb[i0 + 1], vb[i0 + 2]);
|
||||
vec3.set(tri.b, vb[i1], vb[i1 + 1], vb[i1 + 2]);
|
||||
vec3.set(tri.c, vb[i2], vb[i2 + 1], vb[i2 + 2]);
|
||||
let dist = intersect.ray_triangle(modelRay, tri, sides);
|
||||
if (dist <= 0 || dist > distance) continue;
|
||||
distance = dist;
|
||||
}
|
||||
}
|
||||
if (distance < Infinity) {
|
||||
let res = results.add();
|
||||
res.node = m._node;
|
||||
res.distance = distance;
|
||||
let r = pool.add();
|
||||
r.node = node;
|
||||
r.distance = distance * vec3.magnitude(vec3.mul(v3, modelRay.d, node._scale));
|
||||
results[pool.length - 1] = r;
|
||||
}
|
||||
}
|
||||
return results.length;
|
||||
results.length = pool.length;
|
||||
return results;
|
||||
};
|
||||
})();
|
||||
}
|
||||
|
||||
@@ -52,9 +52,11 @@ Layers._nextAvailable = 8;
|
||||
// built-in layers, reserved up to (1 << 7)
|
||||
Layers.Default = (1 << 0);
|
||||
Layers.IgnoreRaycast = (1 << 1);
|
||||
Layers.Gizmos = (1 << 2);
|
||||
|
||||
// masks
|
||||
Layers.All = ~0;
|
||||
Layers.RaycastMask = Layers.makeExclusiveMask([Layers.IgnoreRaycast]);
|
||||
Layers.All = Layers.makeExclusiveMask([Layers.Gizmos]);
|
||||
Layers.RaycastMask = Layers.makeExclusiveMask([Layers.Gizmos, Layers.IgnoreRaycast]);
|
||||
|
||||
export default Layers;
|
||||
export default Layers;
|
||||
cc.Layers = Layers;
|
||||
|
||||
Reference in New Issue
Block a user